def test_process_ar_filing(app, session): """Assert that an AR filling can be applied to the model correctly.""" from entity_filer.worker import APP_CONFIG from entity_filer.filing_processors import annual_report # vars payment_id = str(random.SystemRandom().getrandbits(0x58)) identifier = 'CP1234567' # setup business = create_business(identifier, 'CP') business_id = business.id now = datetime.date(2020, 9, 17) ar_date = datetime.date(2020, 8, 5) agm_date = datetime.date(2020, 7, 1) ar = copy.deepcopy(ANNUAL_REPORT) ar['filing']['business']['identifier'] = identifier ar['filing']['annualReport']['annualReportDate'] = ar_date.isoformat() ar['filing']['annualReport'][ 'annualGeneralMeetingDate'] = agm_date.isoformat() filing_meta = FilingMeta() # TEST with freeze_time(now): filing = create_filing(payment_id, ar, business.id) filing_id = filing.id filing_msg = {'filing': {'id': filing_id}} annual_report.process(business, filing.filing_json['filing'], filing_meta=filing_meta) # check it out # NOTE: until we save or convert the dates, they are FakeDate objects, so casting to str() assert str(business.last_agm_date) == str(agm_date) assert str(business.last_ar_date) == str(agm_date)
def process_filing(payment_token, flask_app): """Render the filings contained in the submission.""" if not flask_app: raise QueueException('Flask App not available.') with flask_app.app_context(): # try to find the filing 5 times before putting back on the queue - in case payment token ends up on the queue # before it is assigned to filing. counter = 1 filing_submission = None while not filing_submission and counter <= 5: filing_submission = get_filing_by_payment_id(payment_token['paymentToken'].get('id')) counter += 1 sleep(0.2) if not filing_submission: raise FilingException if filing_submission.status == Filing.Status.COMPLETED.value: logger.warning('Queue: Attempting to reprocess business.id=%s, filing.id=%s payment=%s', filing_submission.business_id, filing_submission.id, payment_token) return if payment_token['paymentToken'].get('statusCode') == 'TRANSACTION_FAILED': # TODO - need to surface TRANSACTION_FAILED, but Filings manages its own status # filing_submission.status = Filing.Status.ERROR filing_submission.payment_completion_date = datetime.datetime.utcnow() db.session.add(filing_submission) db.session.commit() return legal_filings = filing_submission.legal_filings() # TODO: handle case where there are no legal_filings uow = versioning_manager.unit_of_work(db.session) transaction = uow.create_transaction(db.session) if not payment_token['paymentToken'].get('statusCode') == 'TRANSACTION_FAILED': if not payment_token['paymentToken'].get('statusCode') == Filing.Status.COMPLETED.value: logger.error('Unknown payment status given: %s', payment_token['paymentToken'].get('statusCode')) raise QueueException business = Business.find_by_internal_id(filing_submission.business_id) for filing in legal_filings: if filing.get('annualReport'): annual_report.process(business, filing) if filing.get('changeOfAddress'): change_of_address.process(business, filing) if filing.get('changeOfDirectors'): change_of_directors.process(business, filing) filing_submission.transaction_id = transaction.id db.session.add(business) filing_submission.payment_completion_date = datetime.datetime.utcnow() db.session.add(filing_submission) db.session.commit() return
def process_filing(filing_msg: Dict, flask_app: Flask): """Render the filings contained in the submission.""" if not flask_app: raise QueueException('Flask App not available.') with flask_app.app_context(): filing_submission = Filing.find_by_id(filing_msg['filing']['id']) if not filing_submission: raise QueueException if filing_submission.status == Filing.Status.COMPLETED.value: logger.warning('QueueFiler: Attempting to reprocess business.id=%s, filing.id=%s filing=%s', filing_submission.business_id, filing_submission.id, filing_msg) return legal_filings = filing_submission.legal_filings() if legal_filings: uow = versioning_manager.unit_of_work(db.session) transaction = uow.create_transaction(db.session) business = Business.find_by_internal_id(filing_submission.business_id) for filing in legal_filings: if filing.get('annualReport'): annual_report.process(business, filing) elif filing.get('changeOfAddress'): change_of_address.process(business, filing) elif filing.get('changeOfDirectors'): filing['colinIds'] = filing_submission.colin_event_ids change_of_directors.process(business, filing) elif filing.get('changeOfName'): change_of_name.process(business, filing) elif filing.get('specialResolution'): pass # nothing to do here elif filing.get('voluntaryDissolution'): voluntary_dissolution.process(business, filing) elif filing.get('incorporationApplication'): incorporation_filing.process(business, filing, flask_app) filing_submission.transaction_id = transaction.id filing_submission.set_processed() db.session.add(business) db.session.add(filing_submission) db.session.commit() publish_event(business, filing_submission) return
async def process_filing(filing_msg: Dict, flask_app: Flask): # pylint: disable=too-many-branches,too-many-statements """Render the filings contained in the submission.""" if not flask_app: raise QueueException('Flask App not available.') with flask_app.app_context(): filing_submission = Filing.find_by_id(filing_msg['filing']['id']) if not filing_submission: raise QueueException if filing_submission.status == Filing.Status.COMPLETED.value: logger.warning( 'QueueFiler: Attempting to reprocess business.id=%s, filing.id=%s filing=%s', filing_submission.business_id, filing_submission.id, filing_msg) return None, None if legal_filings := filing_submission.legal_filings(): uow = versioning_manager.unit_of_work(db.session) transaction = uow.create_transaction(db.session) business = Business.find_by_internal_id( filing_submission.business_id) for filing in legal_filings: if filing.get('alteration'): alteration.process(business, filing_submission, filing) if filing.get('annualReport'): annual_report.process(business, filing) elif filing.get('changeOfAddress'): change_of_address.process(business, filing) elif filing.get('changeOfDirectors'): filing['colinIds'] = filing_submission.colin_event_ids change_of_directors.process(business, filing) elif filing.get('changeOfName'): change_of_name.process(business, filing) elif filing.get('voluntaryDissolution'): voluntary_dissolution.process(business, filing) elif filing.get('incorporationApplication'): business, filing_submission = incorporation_filing.process( business, filing, filing_submission) if filing.get('correction'): filing_submission = correction.process( filing_submission, filing) if filing.get('transition'): filing_submission = transition.process( business, filing_submission, filing) filing_submission.transaction_id = transaction.id filing_submission.set_processed() db.session.add(business) db.session.add(filing_submission) db.session.commit() # post filing changes to other services if any('alteration' in x for x in legal_filings): if name_request.has_new_nr_for_alteration( business, filing_submission.filing_json): name_request.consume_nr( business, filing_submission, '/filing/alteration/nameRequest/nrNumber') alteration.post_process(business, filing_submission) db.session.add(business) db.session.commit() AccountService.update_entity( business_registration=business.identifier, business_name=business.legal_name, corp_type_code=business.legal_type) if any('incorporationApplication' in x for x in legal_filings): if any('correction' in x for x in legal_filings): if name_request.has_new_nr_for_correction( filing_submission.filing_json): name_request.consume_nr(business, filing_submission) else: filing_submission.business_id = business.id db.session.add(filing_submission) db.session.commit() incorporation_filing.update_affiliation( business, filing_submission) name_request.consume_nr(business, filing_submission) incorporation_filing.post_process(business, filing_submission) try: await publish_email_message( qsm, APP_CONFIG.EMAIL_PUBLISH_OPTIONS['subject'], filing_submission, 'mras') except Exception as err: # pylint: disable=broad-except, unused-variable # noqa F841; # mark any failure for human review capture_message( f'Queue Error: Failed to place email for filing:{filing_submission.id}' f'on Queue with error:{err}', level='error') try: await publish_email_message( qsm, APP_CONFIG.EMAIL_PUBLISH_OPTIONS['subject'], filing_submission, filing_submission.status) except Exception as err: # pylint: disable=broad-except, unused-variable # noqa F841; # mark any failure for human review capture_message( f'Queue Error: Failed to place email for filing:{filing_submission.id}' f'on Queue with error:{err}', level='error') try: await publish_event(business, filing_submission) except Exception as err: # pylint: disable=broad-except, unused-variable # noqa F841; # mark any failure for human review capture_message( f'Queue Error: Failed to publish event for filing:{filing_submission.id}' f'on Queue with error:{err}', level='error')
async def process_filing(filing_msg: Dict, flask_app: Flask): # pylint: disable=too-many-branches,too-many-statements """Render the filings contained in the submission. Start the migration to using core/Filing """ if not flask_app: raise QueueException('Flask App not available.') with flask_app.app_context(): # filing_submission = Filing.find_by_id(filing_msg['filing']['id']) filing_core_submission = FilingCore.find_by_id( filing_msg['filing']['id']) if not filing_core_submission: raise QueueException filing_submission = filing_core_submission.storage if filing_core_submission.status == Filing.Status.COMPLETED: logger.warning( 'QueueFiler: Attempting to reprocess business.id=%s, filing.id=%s filing=%s', filing_submission.business_id, filing_submission.id, filing_msg) return None, None # convenience flag to set that the envelope is a correction is_correction = (filing_core_submission.filing_type == FilingCore.FilingTypes.CORRECTION) if legal_filings := filing_core_submission.legal_filings( with_diff=False): uow = versioning_manager.unit_of_work(db.session) transaction = uow.create_transaction(db.session) business = Business.find_by_internal_id( filing_submission.business_id) filing_meta = FilingMeta( application_date=filing_submission.effective_date, legal_filings=[ item for sublist in [list(x.keys()) for x in legal_filings] for item in sublist ]) for filing in legal_filings: if filing.get('alteration'): alteration.process(business, filing_submission, filing, filing_meta, is_correction) elif filing.get('annualReport'): annual_report.process(business, filing, filing_meta) elif filing.get('changeOfAddress'): change_of_address.process(business, filing, filing_meta) elif filing.get('changeOfDirectors'): filing['colinIds'] = filing_submission.colin_event_ids change_of_directors.process(business, filing, filing_meta) elif filing.get('changeOfName'): change_of_name.process(business, filing, filing_meta) elif filing.get('dissolution'): dissolution.process(business, filing, filing_meta) elif filing.get('incorporationApplication'): business, filing_submission, filing_meta = incorporation_filing.process( business, filing_core_submission.json, filing_submission, filing_meta) elif filing.get('conversion'): business, filing_submission = conversion.process( business, filing_core_submission.json, filing_submission, filing_meta) elif filing.get('courtOrder'): court_order.process(filing_submission, filing, filing_meta) elif filing.get('registrarsNotation'): registrars_notation.process(filing_submission, filing, filing_meta) elif filing.get('registrarsOrder'): registrars_order.process(filing_submission, filing, filing_meta) elif filing.get('correction'): filing_submission = correction.process( filing_submission, filing, filing_meta) elif filing.get('transition'): filing_submission = transition.process( business, filing_submission, filing, filing_meta) filing_submission.transaction_id = transaction.id filing_submission.set_processed() filing_submission._meta_data = json.loads( # pylint: disable=W0212 json.dumps(filing_meta.asjson, default=json_serial)) db.session.add(business) db.session.add(filing_submission) db.session.commit() # post filing changes to other services if any('alteration' in x for x in legal_filings): alteration.post_process(business, filing_submission, correction) db.session.add(business) db.session.commit() AccountService.update_entity( business_registration=business.identifier, business_name=business.legal_name, corp_type_code=business.legal_type) if any('incorporationApplication' in x for x in legal_filings): if any('correction' in x for x in legal_filings): if name_request.has_new_nr_for_correction( filing_submission.filing_json): name_request.consume_nr(business, filing_submission) else: filing_submission.business_id = business.id db.session.add(filing_submission) db.session.commit() incorporation_filing.update_affiliation( business, filing_submission) name_request.consume_nr(business, filing_submission) incorporation_filing.post_process(business, filing_submission) try: await publish_email_message( qsm, APP_CONFIG.EMAIL_PUBLISH_OPTIONS['subject'], filing_submission, 'mras') except Exception as err: # pylint: disable=broad-except, unused-variable # noqa F841; # mark any failure for human review capture_message( f'Queue Error: Failed to place email for filing:{filing_submission.id}' f'on Queue with error:{err}', level='error') if any('conversion' in x for x in legal_filings): filing_submission.business_id = business.id db.session.add(filing_submission) db.session.commit() conversion.post_process(business, filing_submission) try: await publish_email_message( qsm, APP_CONFIG.EMAIL_PUBLISH_OPTIONS['subject'], filing_submission, filing_submission.status) except Exception as err: # pylint: disable=broad-except, unused-variable # noqa F841; # mark any failure for human review capture_message( f'Queue Error: Failed to place email for filing:{filing_submission.id}' f'on Queue with error:{err}', level='error') try: await publish_event(business, filing_submission) except Exception as err: # pylint: disable=broad-except, unused-variable # noqa F841; # mark any failure for human review print(err) capture_message( f'Queue Error: Failed to publish event for filing:{filing_submission.id}' f'on Queue with error:{err}', level='error')