Skip to content

Commit

Permalink
Move the state check above applicants. Also put in an error when ther… (
Browse files Browse the repository at this point in the history
#2531)

* Move the state check above applicants. Also put in an error when there are no applicants.

* Fix linting rules.

* Fix lint
  • Loading branch information
seeker25 authored Sep 11, 2023
1 parent d48cf77 commit 920a751
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 55 deletions.
3 changes: 3 additions & 0 deletions auth-api/src/auth_api/exceptions/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class Error(Enum):
NR_INVALID_CONTACT = 'Invalid email or phone number.', http_status.HTTP_400_BAD_REQUEST
NR_INVALID_CORP_TYPE = 'The business type associated with this name request is not yet supported.', \
http_status.HTTP_400_BAD_REQUEST
NR_INVALID_APPLICANTS = 'The specified name request must have at least one applicant. Please contact staff ' + \
'to fix this name request.', \
http_status.HTTP_400_BAD_REQUEST

ENTITY_DELETE_FAILED = 'Cannot delete entity due to related records.', http_status.HTTP_400_BAD_REQUEST

Expand Down
112 changes: 57 additions & 55 deletions auth-api/src/auth_api/services/affiliation.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,63 +220,65 @@ def create_new_business_affiliation(org_id, # pylint: disable=too-many-argument
entity = EntityService.find_by_business_identifier(business_identifier, skip_auth=True)

# Call the legal-api to verify the NR details
nr_json = Affiliation._get_nr_details(business_identifier, bearer_token)

if nr_json:
status = nr_json.get('state')
nr_phone = nr_json.get('applicants').get('phoneNumber')
nr_email = nr_json.get('applicants').get('emailAddress')

if status not in (NRStatus.APPROVED.value, NRStatus.CONDITIONAL.value, NRStatus.DRAFT.value):
raise BusinessException(Error.NR_INVALID_STATUS, None)

if status == NRStatus.DRAFT.value:
invoices = Affiliation.get_nr_payment_details(business_identifier)

# Ideally there should be only one or two (priority fees) payment request for the NR.
if not (invoices and invoices['invoices'] and invoices['invoices'][0].get('statusCode') == 'COMPLETED'):
raise BusinessException(Error.NR_NOT_PAID, None)

# If consentFlag is not R, N or Null for a CONDITIONAL NR throw error
if status == NRStatus.CONDITIONAL.value and nr_json.get('consentFlag', None) not in (None, 'R', 'N'):
raise BusinessException(Error.NR_NOT_APPROVED, None)

if not user_is_staff and ((phone and phone != nr_phone) or
(email and email.casefold() != nr_email.casefold())):
raise BusinessException(Error.NR_INVALID_CONTACT, None)

# Create an entity with the Name from NR if entity doesn't exist
if not entity:
# Filter the names from NR response and get the name which has status APPROVED as the name.
# Filter the names from NR response and get the name which has status CONDITION as the name.
nr_name_state = NRNameStatus.APPROVED.value if status == NRStatus.APPROVED.value \
else NRNameStatus.CONDITION.value
name = next((name.get('name') for name in nr_json.get('names') if
name.get('state', None) == nr_name_state), None)

entity = EntityService.save_entity({
'businessIdentifier': business_identifier,
'name': name or business_identifier,
'corpTypeCode': CorpType.NR.value,
'passCodeClaimed': True
})

# Affiliation may already already exist.
if not (affiliation_model :=
AffiliationModel.find_affiliation_by_org_and_entity_ids(org_id, entity.identifier)):
# Create an affiliation with org
affiliation_model = AffiliationModel(
org_id=org_id, entity_id=entity.identifier, certified_by_name=certified_by_name)

if entity.corp_type not in [CorpType.RTMP.value, CorpType.TMP.value]:
ActivityLogPublisher.publish_activity(Activity(org_id, ActivityAction.CREATE_AFFILIATION.value,
name=entity.name, id=entity.business_identifier))
affiliation_model.certified_by_name = certified_by_name
affiliation_model.save()
entity.set_pass_code_claimed(True)
else:
if not (nr_json := Affiliation._get_nr_details(business_identifier, bearer_token)):
raise BusinessException(Error.NR_NOT_FOUND, None)

status = nr_json.get('state')

if status not in (NRStatus.APPROVED.value, NRStatus.CONDITIONAL.value, NRStatus.DRAFT.value):
raise BusinessException(Error.NR_INVALID_STATUS, None)

if not nr_json.get('applicants'):
raise BusinessException(Error.NR_INVALID_APPLICANTS, None)

nr_phone = nr_json.get('applicants').get('phoneNumber')
nr_email = nr_json.get('applicants').get('emailAddress')

if status == NRStatus.DRAFT.value:
invoices = Affiliation.get_nr_payment_details(business_identifier)

# Ideally there should be only one or two (priority fees) payment request for the NR.
if not (invoices and invoices['invoices'] and invoices['invoices'][0].get('statusCode') == 'COMPLETED'):
raise BusinessException(Error.NR_NOT_PAID, None)

# If consentFlag is not R, N or Null for a CONDITIONAL NR throw error
if status == NRStatus.CONDITIONAL.value and nr_json.get('consentFlag', None) not in (None, 'R', 'N'):
raise BusinessException(Error.NR_NOT_APPROVED, None)

if not user_is_staff and ((phone and phone != nr_phone) or
(email and email.casefold() != nr_email.casefold())):
raise BusinessException(Error.NR_INVALID_CONTACT, None)

# Create an entity with the Name from NR if entity doesn't exist
if not entity:
# Filter the names from NR response and get the name which has status APPROVED as the name.
# Filter the names from NR response and get the name which has status CONDITION as the name.
nr_name_state = NRNameStatus.APPROVED.value if status == NRStatus.APPROVED.value \
else NRNameStatus.CONDITION.value
name = next((name.get('name') for name in nr_json.get('names') if
name.get('state', None) == nr_name_state), None)

entity = EntityService.save_entity({
'businessIdentifier': business_identifier,
'name': name or business_identifier,
'corpTypeCode': CorpType.NR.value,
'passCodeClaimed': True
})

# Affiliation may already already exist.
if not (affiliation_model :=
AffiliationModel.find_affiliation_by_org_and_entity_ids(org_id, entity.identifier)):
# Create an affiliation with org
affiliation_model = AffiliationModel(
org_id=org_id, entity_id=entity.identifier, certified_by_name=certified_by_name)

if entity.corp_type not in [CorpType.RTMP.value, CorpType.TMP.value]:
ActivityLogPublisher.publish_activity(Activity(org_id, ActivityAction.CREATE_AFFILIATION.value,
name=entity.name, id=entity.business_identifier))
affiliation_model.certified_by_name = certified_by_name
affiliation_model.save()
entity.set_pass_code_claimed(True)

return Affiliation(affiliation_model)

@staticmethod
Expand Down

0 comments on commit 920a751

Please sign in to comment.