def test_diff_of_stored_completed_filings(session): """Assert that the filing diff works correctly.""" identifier = 'CP1234567' business = factory_business(identifier, founding_date=(datetime.utcnow() - datedelta.YEAR)) factory_business_mailing_address(business) json1 = copy.deepcopy(MINIMAL_FILING_JSON) original_filing = factory_completed_filing(business, json1) json2 = copy.deepcopy(CORRECTION_FILING_JSON) json2['filing']['correction']['correctedFilingId'] = str( original_filing.id) correction_filing = factory_completed_filing(business, json2) filing = Filing.find_by_id(correction_filing.id) filing_json = filing.json assert filing_json assert filing_json['filing']['correction']['diff'] == [{ 'newValue': 'Be it resolved, and now it is.', 'oldValue': 'Be it resolved, that it is resolved to be resolved.', 'path': RESOLUTION_PATH }]
async def test_technical_correction_ar(app, session): """Assert we can create a business based on transition filing.""" filing_data = copy.deepcopy(ANNUAL_REPORT) business = create_business(filing_data['filing']['business']['identifier']) business_identifier = business.identifier payment_id = str(random.SystemRandom().getrandbits(0x58)) filing = (create_filing(payment_id, filing_data, business.id)) filing_id = filing.id filing_msg = {'filing': {'id': filing.id}} # sanity check # that it is an AR, and it is based on the ANNUAL_REPORT template assert filing.json['filing']['annualReport'] assert filing.json['filing']['annualReport']['annualGeneralMeetingDate'] \ == ANNUAL_REPORT['filing']['annualReport']['annualGeneralMeetingDate'] # and the businesses last AR date is null assert not business.last_ar_date # subvert the filing technical_correction_filing = copy.deepcopy(FILING_HEADER) technical_correction_filing['specialResolution'] = copy.deepcopy( SPECIAL_RESOLUTION) filing.tech_correction_json = technical_correction_filing # over ride the state and skip state setting listeners for this test filing.skip_status_listener = True filing._status = 'PENDING' filing.save() # Test await process_filing(filing_msg, app) # Check outcome business = Business.find_by_identifier(business_identifier) filing = FilingCore.find_by_id(filing_id) assert not business.last_ar_date assert filing.filing_type == 'annualReport'
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.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_core_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_core_submission.json, filing_submission) elif filing.get('conversion'): business, filing_submission = conversion.process( business, filing_core_submission.json, 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') 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 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')