def update_affiliation(business: Business, filing: Filing): """Create an affiliation for the business and remove the bootstrap.""" try: bootstrap = RegistrationBootstrap.find_by_identifier(filing.temp_reg) rv = AccountService.create_affiliation( account=bootstrap.account, business_registration=business.identifier, business_name=business.legal_name, corp_type_code=business.legal_type) if rv not in (HTTPStatus.OK, HTTPStatus.CREATED): deaffiliation = AccountService.delete_affiliation( bootstrap.account, business.identifier) sentry_sdk.capture_message( f'Queue Error: Unable to affiliate business:{business.identifier} for filing:{filing.id}', level='error') else: # flip the registration # recreate the bootstrap, but point to the new business in the name old_bs_affiliation = AccountService.delete_affiliation( bootstrap.account, bootstrap.identifier) new_bs_affiliation = AccountService.create_affiliation( account=bootstrap.account, business_registration=bootstrap.identifier, business_name=business.identifier, corp_type_code='TMP') reaffiliate = bool( new_bs_affiliation in (HTTPStatus.OK, HTTPStatus.CREATED) and old_bs_affiliation == HTTPStatus.OK) if rv not in (HTTPStatus.OK, HTTPStatus.CREATED) \ or ('deaffiliation' in locals() and deaffiliation != HTTPStatus.OK)\ or ('reaffiliate' in locals() and not reaffiliate): raise QueueException except Exception as err: # pylint: disable=broad-except; note out any exception, but don't fail the call sentry_sdk.capture_message( f'Queue Error: Affiliation error for filing:{filing.id}, with err:{err}', level='error')
def test_account_affiliation_integration(account, app_ctx): """Assert that the affiliation can be created.""" business_registration = ( f'T{random.SystemRandom().getrandbits(0x58)}')[:10] r = AccountService.create_affiliation( account=account, business_registration=business_registration, business_name='') assert r == HTTPStatus.OK r = AccountService.update_entity( business_registration=business_registration, business_name=business_registration, corp_type_code='BEN') assert r == HTTPStatus.OK r = AccountService.delete_affiliation( account=account, business_registration=business_registration) # @TODO change this next sprint when affiliation service is updated. assert r == HTTPStatus.OK