Skip to content

Commit

Permalink
MHR API account reg list add/remove registrations. (#1264)
Browse files Browse the repository at this point in the history
* MHR API account reg list add/remove registrations.

Signed-off-by: Doug Lovett <doug@diamante.ca>

* MHR API account reg list add/remove registrations. Update current status.

Signed-off-by: Doug Lovett <doug@diamante.ca>

---------

Signed-off-by: Doug Lovett <doug@diamante.ca>
doug-lovett authored Apr 6, 2023
1 parent 35a5e95 commit fda6fc7
Showing 4 changed files with 76 additions and 8 deletions.
15 changes: 13 additions & 2 deletions mhr_api/src/mhr_api/models/db2/utils.py
Original file line number Diff line number Diff line change
@@ -82,6 +82,11 @@
FROM mhr_registrations mr
WHERE account_id = :query_value
AND mr.registration_type = 'MHREG'
AND NOT EXISTS (SELECT mer.mhr_number
FROM mhr_extra_registrations mer
WHERE mer.account_id = mr.account_id
AND mer.mhr_number = mr.mhr_number
AND mer.removed_ind = 'Y')
)
"""
QUERY_ACCOUNT_REGISTRATIONS_SUMMARY = """
@@ -110,11 +115,17 @@
FROM mhr_registrations mr
WHERE mr.account_id = :query_value
AND mr.mhr_number = :query_value2
AND mr.registration_type IN ('MHREG')) AS reg_count,
AND mr.registration_type IN ('MHREG')
AND NOT EXISTS (SELECT mer.mhr_number
FROM mhr_extra_registrations mer
WHERE mer.mhr_number = mr.mhr_number
AND mer.account_id = mr.account_id
AND mer.removed_ind = 'Y')) AS reg_count,
(SELECT COUNT(mer.id)
FROM mhr_extra_registrations mer
WHERE mer.account_id = :query_value
AND mer.mhr_number = :query_value2) as extra_reg_count,
AND mer.mhr_number = :query_value2
AND mer.removed_ind != 'Y') as extra_reg_count,
(SELECT mr.account_id
FROM mhr_registrations mr
WHERE mr.account_id = :query_value
33 changes: 28 additions & 5 deletions mhr_api/src/mhr_api/resources/v1/other_registrations.py
Original file line number Diff line number Diff line change
@@ -93,16 +93,28 @@ def post_other_registration(mhr_number: str):
if not authorized(account_id, jwt):
return resource_utils.unauthorized_error_response(account_id)
# Try to fetch summary registration by mhr number
registration = MhrRegistration.find_summary_by_mhr_number(account_id, mhr_number)
registration = MhrRegistration.find_summary_by_mhr_number(account_id, mhr_number, is_staff(jwt))
if not registration:
return resource_utils.not_found_error_response('Manufactured Home registration', mhr_number)

extra_existing: MhrExtraRegistration = MhrExtraRegistration.find_by_mhr_number(mhr_number, account_id)
# Check if registration was created by the account and removed. If so, restore it.
if extra_existing and extra_existing.removed_ind == MhrExtraRegistration.REMOVE_IND:
current_app.logger.info(f'Restoring MHR# {mhr_number} for account {account_id}')
MhrExtraRegistration.delete(mhr_number, account_id)
if 'inUserList' in registration:
del registration['inUserList']
return registration, HTTPStatus.CREATED

# Check if duplicate.
extra_existing = MhrExtraRegistration.find_by_mhr_number(mhr_number, account_id)
if extra_existing:
message = resource_utils.DUPLICATE_REGISTRATION_ERROR.format(mhr_number)
return resource_utils.duplicate_error_response(message)
if not extra_existing and registration.get('inUserList'):
message = resource_utils.DUPLICATE_REGISTRATION_ERROR.format(mhr_number)
return resource_utils.duplicate_error_response(message)

current_app.logger.info(f'Adding another account MHR# {mhr_number} for account {account_id}')
extra_registration = MhrExtraRegistration(account_id=account_id, mhr_number=mhr_number)
extra_registration.save()

@@ -121,7 +133,7 @@ def post_other_registration(mhr_number: str):
@cross_origin(origin='*')
@jwt.requires_auth
def delete_other_registration(mhr_number: str):
"""Remove a registration created by another account from the current account list."""
"""Remove a registration from the current account registrations list."""
try:
if mhr_number is None:
return resource_utils.path_param_error_response('MHR Number')
@@ -135,13 +147,24 @@ def delete_other_registration(mhr_number: str):
# Verify request JWT and account ID
if not authorized(account_id, jwt):
return resource_utils.unauthorized_error_response(account_id)
# Try to find user registration
registration = MhrRegistration.find_summary_by_mhr_number(account_id, mhr_number, is_staff(jwt))
# Try to find extra registration.
extra_registration = MhrExtraRegistration.find_by_mhr_number(mhr_number, account_id)
if not extra_registration:
if not registration and not extra_registration:
return resource_utils.not_found_error_response('user account registration', mhr_number)
if registration and not registration.get('inUserList') and not extra_registration:
return resource_utils.not_found_error_response('user account registration', mhr_number)
# Remove another account's financing statement registration.
# Remove another account's registration.
if extra_registration and not extra_registration.removed_ind:
current_app.logger.info(f'Removing another account MHR# {mhr_number} for account {account_id}')
MhrExtraRegistration.delete(mhr_number, account_id)
# Or mark user account's registration as removed.
elif not extra_registration:
current_app.logger.info(f'Marking MHR# {mhr_number} as removed for account {account_id}')
extra_registration = MhrExtraRegistration(account_id=account_id, mhr_number=mhr_number)
extra_registration.removed_ind = MhrExtraRegistration.REMOVE_IND
extra_registration.save()
return '', HTTPStatus.NO_CONTENT

except DatabaseException as db_exception:
13 changes: 13 additions & 0 deletions mhr_api/src/mhr_api/utils/registration_validator.py
Original file line number Diff line number Diff line change
@@ -108,6 +108,7 @@ def validate_registration(json_data, staff: bool = False):
owner_count: int = len(json_data.get('ownerGroups')) if json_data.get('ownerGroups') else 0
error_msg += validate_owner_groups(json_data.get('ownerGroups'), True, None, None, owner_count)
error_msg += validate_location(json_data)
error_msg += validate_description(json_data)
except Exception as validation_exception: # noqa: B902; eat all errors
current_app.logger.error('validate_registration exception: ' + str(validation_exception))
error_msg += VALIDATOR_ERROR
@@ -651,6 +652,18 @@ def validate_location(json_data):
return error_msg


def validate_description(json_data):
"""Verify description values are valid."""
error_msg = ''
if not json_data.get('description'):
return error_msg
description = json_data.get('description')
desc: str = 'description'
error_msg += validate_text(description.get('rebuiltRemarks'), desc + ' rebuilt remarks')
error_msg += validate_text(description.get('otherRemarks'), desc + ' other remarks')
return error_msg


def validate_individual_name(name_json, desc: str = ''):
"""Verify individual name is valid."""
error_msg = validate_text(name_json.get('first'), desc + ' first')
23 changes: 22 additions & 1 deletion mhr_api/tests/unit/utils/test_registration_validator.py
Original file line number Diff line number Diff line change
@@ -357,6 +357,11 @@
('Invalid exception plan', None, None, None, INVALID_TEXT_CHARSET, None, INVALID_CHARSET_MESSAGE),
('Invalid band name', None, None, None, None, INVALID_TEXT_CHARSET, INVALID_CHARSET_MESSAGE)
]
# testdata pattern is ({description}, {rebuilt}, {other}, {message content})
TEST_DESCRIPTION_DATA = [
('Invalid rebuilt remarks', INVALID_TEXT_CHARSET, None, INVALID_CHARSET_MESSAGE),
('Invalid other remarks', None, INVALID_TEXT_CHARSET, INVALID_CHARSET_MESSAGE)
]
# testdata pattern is ({description}, {valid}, {staff}, {doc_id}, {message content}, {status})
TEST_EXEMPTION_DATA = [
(DESC_VALID, True, True, None, None, MhrRegistrationStatusTypes.ACTIVE),
@@ -551,7 +556,7 @@ def test_validate_owner(session, desc, bus_name, first, middle, last, message_co
elif desc == 'Reg invalid streetAdditional':
party['address']['streetAdditional'] = INVALID_TEXT_CHARSET
elif desc == 'Reg invalid city':
party['address']['city'] = INVALID_TEXT_CHARSET
party['address']['city'] = 'Montréal' # INVALID_TEXT_CHARSET
error_msg = ''
if desc.startswith('Reg'):
error_msg = validator.validate_registration(json_data, False)
@@ -584,6 +589,22 @@ def test_validate_reg_location(session, desc, park_name, dealer, additional, exc
assert error_msg.find(message_content) != -1


@pytest.mark.parametrize('desc,rebuilt,other,message_content', TEST_DESCRIPTION_DATA)
def test_validate_reg_description(session, desc, rebuilt, other, message_content):
"""Assert that description validation works as expected."""
# setup
json_data = get_valid_registration(MhrTenancyTypes.SOLE)
description = json_data.get('description')
if rebuilt:
description['rebuiltRemarks'] = rebuilt
elif other:
description['otherRemarks'] = other
error_msg = validator.validate_registration(json_data, False)
assert error_msg != ''
if message_content:
assert error_msg.find(message_content) != -1


@pytest.mark.parametrize('desc,valid,street,city,message_content', TEST_LEGACY_REG_DATA)
def test_validate_registration_legacy(session, desc, valid, street, city, message_content):
"""Assert that new MH registration legacy validation works as expected."""

0 comments on commit fda6fc7

Please sign in to comment.