Skip to content

Commit

Permalink
19058 initial draft creation fix (bcgov#2452)
Browse files Browse the repository at this point in the history
  • Loading branch information
vysakh-menon-aot authored Feb 9, 2024
1 parent ad532ff commit ef9ef0d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
32 changes: 20 additions & 12 deletions legal-api/src/legal_api/resources/v2/business/business.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,17 @@ def post_businesses():
except (TypeError, KeyError):
return {'error': babel('Requires a valid filing.')}, HTTPStatus.BAD_REQUEST

filing_title = filing_type
with suppress(KeyError):
if filing_sub_type := Filing.get_filings_sub_type(filing_type, json_input):
filing_title = Filing.FILINGS[filing_type][filing_sub_type]['title']
else:
filing_title = Filing.FILINGS[filing_type]['title']

# @TODO rollback bootstrap if there is A failure, awaiting changes in the affiliation service
bootstrap = RegistrationBootstrapService.create_bootstrap(filing_account_id)
if not isinstance(bootstrap, RegistrationBootstrap):
if filing_sub_type := Filing.get_filings_sub_type(filing_type, json_input):
title = Filing.FILINGS[filing_type][filing_sub_type]['title']
else:
title = Filing.FILINGS[filing_type]['title']
return {'error': babel('Unable to create {0} Filing.'.format(title))}, \
return {'error': babel('Unable to create {0} Filing.'.format(filing_title))}, \
HTTPStatus.SERVICE_UNAVAILABLE

try:
Expand All @@ -143,7 +146,7 @@ def post_businesses():
if not isinstance(rv, HTTPStatus):
with suppress(Exception):
bootstrap.delete()
return {'error': babel('Unable to create {0} Filing.'.format(Filing.FILINGS[filing_type]['title']))}, \
return {'error': babel('Unable to create {0} Filing.'.format(filing_title))}, \
HTTPStatus.SERVICE_UNAVAILABLE

return saving_filings(identifier=bootstrap.identifier) # pylint: disable=no-value-for-parameter
Expand Down Expand Up @@ -172,18 +175,23 @@ def search_businesses():
draft_results = []
for draft_dao in draft_query.all():
draft = {
'identifier': draft_dao.temp_reg,
'identifier': draft_dao.temp_reg,
'legalType': draft_dao.json_legal_type
}
if draft_dao.json_nr:
draft['nrNumber'] = draft_dao.json_nr
draft['legalName'] = (draft_dao.filing_json.get('filing', {})
.get(draft_dao.filing_type, {})
.get('nameRequest', {})
.get('legalName'))
.get(draft_dao.filing_type, {})
.get('nameRequest', {})
.get('legalName'))
draft['draftType'] = Filing.FILINGS.get(draft_dao.filing_type, {}).get('temporaryCorpTypeCode')
if draft['draftType'] == 'ATMP' and draft['legalName'] is None:
draft['legalName'] = 'Numbered Amalgamated Company'
if draft['legalName'] is None:
if draft['draftType'] == 'TMP':
draft['legalName'] = (Business.BUSINESSES
.get(draft_dao.json_legal_type, {})
.get('numberedDescription'))
elif draft['draftType'] == 'ATMP':
draft['legalName'] = 'Numbered Amalgamated Company'
draft_results.append(draft)

return jsonify({'businessEntities': bus_results, 'draftEntities': draft_results}), HTTPStatus.OK
Expand Down
14 changes: 12 additions & 2 deletions legal-api/src/legal_api/services/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ def create_bootstrap(account: int) -> Union[Dict, RegistrationBootstrap]:
return {'error': babel('An account number must be provided.')}

bootstrap = RegistrationBootstrap(account=account)
allowed_encoded = string.ascii_letters + string.digits

# try to create a bootstrap registration with a unique ID
for _ in range(5):
bootstrap.identifier = 'T' + ''.join(secrets.choice(allowed_encoded) for _ in range(9))
try:
bootstrap.identifier = RegistrationBootstrapService._generate_temp_identifier()
bootstrap.save()
return bootstrap
except FlushError:
Expand All @@ -53,6 +52,17 @@ def create_bootstrap(account: int) -> Union[Dict, RegistrationBootstrap]:

return {'error': babel('Unable to create bootstrap registration.')}

@staticmethod
def _generate_temp_identifier():
"""Generate a 10 character string which starts with `T` and have at least 1 digit."""
allowed_encoded = string.ascii_letters + string.digits
identifier = None
while True:
identifier = 'T' + ''.join(secrets.choice(allowed_encoded) for _ in range(9))
if any(c.isdigit() for c in identifier): # identifier requires at least 1 digit (as per auth-api)
break
return identifier

@staticmethod
def delete_bootstrap(bootstrap: RegistrationBootstrap):
"""Delete the bootstrap registration."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def test_create_bootstrap_registrations(session):
"""Assert the service creates registrations."""
r = RegistrationBootstrapService.create_bootstrap(account=28)
assert r.identifier
assert any(c.isdigit() for c in r.identifier)


@integration_affiliation
Expand Down

0 comments on commit ef9ef0d

Please sign in to comment.