From 050f390855e174b8018ba18063e1565bffdd1ca9 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Wed, 31 Jul 2024 14:25:21 -0700 Subject: [PATCH 1/2] 22431 update to use v2 of legal api --- .../email_processors/__init__.py | 24 ++++++ .../agm_extension_notification.py | 15 ++-- .../agm_location_change_notification.py | 15 ++-- .../amalgamation_notification.py | 36 +++------ .../change_of_registration_notification.py | 28 +++---- .../consent_continuation_out_notification.py | 15 ++-- .../continuation_in_notification.py | 36 +++------ .../correction_notification.py | 38 +++------- .../dissolution_notification.py | 66 ++++++----------- .../email_processors/filing_notification.py | 72 +++++------------- .../registration_notification.py | 15 ++-- .../restoration_notification.py | 41 +++------- .../special_resolution_helper.py | 74 ++++++------------- .../test_correction_notification.py | 9 +-- ...test_cp_special_resolution_notification.py | 9 ++- .../test_restoration_notification.py | 7 +- 16 files changed, 171 insertions(+), 329 deletions(-) diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/__init__.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/__init__.py index 985f11e860..1cea7f90a9 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/__init__.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/__init__.py @@ -22,6 +22,7 @@ from pathlib import Path from typing import Tuple +import base64 import requests from entity_queue_common.service_utils import logger from flask import current_app @@ -214,3 +215,26 @@ def get_jurisdictions(identifier: str, token: str) -> dict: except Exception: # noqa B902; pylint: disable=W0703; current_app.logger.error('Failed to get MRAS response') return None + + +def get_filing_document(business_identifier, filing_id, document_type, token): + """Get the filing documents.""" + headers = { + 'Accept': 'application/pdf', + 'Authorization': f'Bearer {token}' + } + + document = requests.get( + f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business_identifier}/filings/{filing_id}' + f'/documents/{document_type}', headers=headers + ) + + if document.status_code != HTTPStatus.OK: + current_app.logger.error('Failed to get %s pdf for filing: %s', document_type, filing_id) + return None + try: + filing_pdf_encoded = base64.b64encode(document.content) + return filing_pdf_encoded + except Exception: # noqa B902; pylint: disable=W0703; + current_app.logger.error('Failed to get document response') + return None diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_extension_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_extension_notification.py index 6610cdc87d..1881480e39 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_extension_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_extension_notification.py @@ -25,7 +25,9 @@ from jinja2 import Template from legal_api.models import Business, Filing -from entity_emailer.email_processors import get_filing_info, get_recipient_from_auth, substitute_template_parts +from entity_emailer.email_processors import ( + get_filing_document, get_filing_info, get_recipient_from_auth, substitute_template_parts +) def _get_pdfs( @@ -44,14 +46,9 @@ def _get_pdfs( } # add filing pdf - filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=letterOfAgmExtension', headers=headers - ) - if filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - filing_pdf_encoded = base64.b64encode(filing_pdf.content) + filing_pdf_type = 'letterOfAgmExtension' + filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, filing_pdf_type, token) + if filing_pdf_encoded: pdfs.append( { 'fileName': 'Letter of AGM Extension Approval.pdf', diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_location_change_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_location_change_notification.py index 831d3cbcb5..b5a13d96ec 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_location_change_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_location_change_notification.py @@ -25,7 +25,9 @@ from jinja2 import Template from legal_api.models import Business, Filing -from entity_emailer.email_processors import get_filing_info, get_recipient_from_auth, substitute_template_parts +from entity_emailer.email_processors import ( + get_filing_document, get_filing_info, get_recipient_from_auth, substitute_template_parts +) def _get_pdfs( @@ -44,14 +46,9 @@ def _get_pdfs( } # add filing pdf - filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=letterOfAgmLocationChange', headers=headers - ) - if filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - filing_pdf_encoded = base64.b64encode(filing_pdf.content) + filing_pdf_type = 'letterOfAgmLocationChange' + filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, filing_pdf_type, token) + if filing_pdf_encoded: pdfs.append( { 'fileName': 'Letter of AGM Location Change Approval.pdf', diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/amalgamation_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/amalgamation_notification.py index 7d40cb9629..068ce3d296 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/amalgamation_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/amalgamation_notification.py @@ -27,6 +27,7 @@ from entity_emailer.email_processors import ( get_entity_dashboard_url, + get_filing_document, get_filing_info, get_recipients, substitute_template_parts, @@ -52,14 +53,9 @@ def _get_pdfs( if status == Filing.Status.PAID.value: # add filing pdf - filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}', - headers=headers - ) - if filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - filing_pdf_encoded = base64.b64encode(filing_pdf.content) + filing_pdf_type = 'amalgamationApplication' + filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, filing_pdf_type, token) + if filing_pdf_encoded: pdfs.append( { 'fileName': f'{amalgamation_application_name}.pdf', @@ -97,15 +93,9 @@ def _get_pdfs( attach_order += 1 elif status == Filing.Status.COMPLETED.value: # add certificate of amalgamation - certificate = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=certificateOfAmalgamation', - headers=headers - ) - if certificate.status_code != HTTPStatus.OK: - logger.error('Failed to get corrected registration statement pdf for filing: %s', filing.id) - else: - certificate_encoded = base64.b64encode(certificate.content) + certificate_pdf_type = 'certificateOfAmalgamation' + certificate_encoded = get_filing_document(business['identifier'], filing.id, certificate_pdf_type, token) + if certificate_encoded: pdfs.append( { 'fileName': 'Certificate Of Amalgamation.pdf', @@ -116,15 +106,9 @@ def _get_pdfs( ) attach_order += 1 # add notice of articles - noa = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=noticeOfArticles', - headers=headers - ) - if noa.status_code != HTTPStatus.OK: - logger.error('Failed to get noa pdf for filing: %s', filing.id) - else: - noa_encoded = base64.b64encode(noa.content) + noa_pdf_type = 'noticeOfArticles' + noa_encoded = get_filing_document(business['identifier'], filing.id, noa_pdf_type, token) + if noa_encoded: pdfs.append( { 'fileName': 'Notice of Articles.pdf', diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/change_of_registration_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/change_of_registration_notification.py index 66b3f53bfd..ac603d1c11 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/change_of_registration_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/change_of_registration_notification.py @@ -25,7 +25,9 @@ from jinja2 import Template from legal_api.models import Business, Filing, UserRoles -from entity_emailer.email_processors import get_filing_info, get_user_email_from_auth, substitute_template_parts +from entity_emailer.email_processors import ( + get_filing_document, get_filing_info, get_user_email_from_auth, substitute_template_parts +) def _get_pdfs( @@ -46,15 +48,9 @@ def _get_pdfs( if status == Filing.Status.PAID.value: # add filing pdf - filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - f'?type=changeOfRegistration', - headers=headers - ) - if filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - filing_pdf_encoded = base64.b64encode(filing_pdf.content) + filing_pdf_type = 'changeOfRegistration' + filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, filing_pdf_type, token) + if filing_pdf_encoded: pdfs.append( { 'fileName': 'Change of Registration.pdf', @@ -93,15 +89,9 @@ def _get_pdfs( attach_order += 1 elif status == Filing.Status.COMPLETED.value: # add amended registration statement - certificate = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=amendedRegistrationStatement', - headers=headers - ) - if certificate.status_code != HTTPStatus.OK: - logger.error('Failed to get amended registration statement pdf for filing: %s', filing.id) - else: - certificate_encoded = base64.b64encode(certificate.content) + certificate_pdf_type = 'amendedRegistrationStatement' + certificate_encoded = get_filing_document(business['identifier'], filing.id, certificate_pdf_type, token) + if certificate_encoded: pdfs.append( { 'fileName': 'AmendedRegistrationStatement.pdf', diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/consent_continuation_out_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/consent_continuation_out_notification.py index 9338cb7ddb..d8d3c4116c 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/consent_continuation_out_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/consent_continuation_out_notification.py @@ -25,7 +25,9 @@ from jinja2 import Template from legal_api.models import Business, Filing, UserRoles -from entity_emailer.email_processors import get_filing_info, get_recipient_from_auth, substitute_template_parts +from entity_emailer.email_processors import ( + get_filing_document, get_filing_info, get_recipient_from_auth, substitute_template_parts +) def _get_pdfs( @@ -44,14 +46,9 @@ def _get_pdfs( } # add filing pdf - filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=letterOfConsent', headers=headers - ) - if filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - filing_pdf_encoded = base64.b64encode(filing_pdf.content) + filing_pdf_type = 'letterOfConsent' + filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, filing_pdf_type, token) + if filing_pdf_encoded: pdfs.append( { 'fileName': 'Letter of Consent.pdf', diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/continuation_in_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/continuation_in_notification.py index 19c3b785df..5a023ba793 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/continuation_in_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/continuation_in_notification.py @@ -27,6 +27,7 @@ from entity_emailer.email_processors import ( get_entity_dashboard_url, + get_filing_document, get_filing_info, get_recipients, substitute_template_parts, @@ -51,14 +52,9 @@ def _get_pdfs( if status == Filing.Status.PAID.value: # add filing pdf - filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}', - headers=headers - ) - if filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - filing_pdf_encoded = base64.b64encode(filing_pdf.content) + filing_pdf_type = 'continuationIn' + filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, filing_pdf_type, token) + if filing_pdf_encoded: pdfs.append( { 'fileName': 'Continuation Application.pdf', @@ -96,15 +92,9 @@ def _get_pdfs( attach_order += 1 elif status == Filing.Status.COMPLETED.value: # add certificate of continuation - certificate = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=certificateOfContinuation', - headers=headers - ) - if certificate.status_code != HTTPStatus.OK: - logger.error('Failed to get certificate of continuation for filing: %s', filing.id) - else: - certificate_encoded = base64.b64encode(certificate.content) + certificate_pdf_type = 'certificateOfContinuation' + certificate_encoded = get_filing_document(business['identifier'], filing.id, certificate_pdf_type, token) + if certificate_encoded: pdfs.append( { 'fileName': 'Certificate of Continuation.pdf', @@ -115,15 +105,9 @@ def _get_pdfs( ) attach_order += 1 # add notice of articles - noa = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=noticeOfArticles', - headers=headers - ) - if noa.status_code != HTTPStatus.OK: - logger.error('Failed to get noa pdf for filing: %s', filing.id) - else: - noa_encoded = base64.b64encode(noa.content) + noa_pdf_type = 'noticeOfArticles' + noa_encoded = get_filing_document(business['identifier'], filing.id, noa_pdf_type, token) + if noa_encoded: pdfs.append( { 'fileName': 'Notice of Articles.pdf', diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/correction_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/correction_notification.py index 24c426cbf7..502217c122 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/correction_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/correction_notification.py @@ -27,7 +27,7 @@ from legal_api.core.filing_helper import is_special_resolution_correction_by_filing_json from legal_api.models import Filing -from entity_emailer.email_processors import get_filing_info, substitute_template_parts +from entity_emailer.email_processors import get_filing_document, get_filing_info, substitute_template_parts from entity_emailer.email_processors.special_resolution_helper import get_completed_pdfs @@ -54,15 +54,9 @@ def _get_pdfs( if status == Filing.Status.PAID.value: # add filing pdf - filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - f'?type=correction', - headers=headers - ) - if filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - filing_pdf_encoded = base64.b64encode(filing_pdf.content) + filing_pdf_type = 'correction' + filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, filing_pdf_type, token) + if filing_pdf_encoded: pdfs.append( { 'fileName': 'Register Correction Application.pdf', @@ -101,15 +95,9 @@ def _get_pdfs( elif status == Filing.Status.COMPLETED.value: if legal_type in ('SP', 'GP'): # add corrected registration statement - certificate = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=correctedRegistrationStatement', - headers=headers - ) - if certificate.status_code != HTTPStatus.OK: - logger.error('Failed to get corrected registration statement pdf for filing: %s', filing.id) - else: - certificate_encoded = base64.b64encode(certificate.content) + certificate_pdf_type = 'correctedRegistrationStatement' + certificate_encoded = get_filing_document(business['identifier'], filing.id, certificate_pdf_type, token) + if certificate_encoded: pdfs.append( { 'fileName': 'Corrected - Registration Statement.pdf', @@ -121,15 +109,9 @@ def _get_pdfs( attach_order += 1 elif legal_type in ('BC', 'BEN', 'CC', 'ULC'): # add notice of articles - noa = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=noticeOfArticles', - headers=headers - ) - if noa.status_code != HTTPStatus.OK: - logger.error('Failed to get noa pdf for filing: %s', filing.id) - else: - noa_encoded = base64.b64encode(noa.content) + noa_pdf_type = 'noticeOfArticles' + noa_encoded = get_filing_document(business['identifier'], filing.id, noa_pdf_type, token) + if noa_encoded: pdfs.append( { 'fileName': 'Notice of Articles.pdf', diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/dissolution_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/dissolution_notification.py index 085c1d1f23..4e8a549cff 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/dissolution_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/dissolution_notification.py @@ -26,6 +26,7 @@ from legal_api.models import Business, Filing, UserRoles from entity_emailer.email_processors import ( + get_filing_document, get_filing_info, get_recipient_from_auth, get_user_email_from_auth, @@ -53,14 +54,9 @@ def _get_pdfs( if status == Filing.Status.PAID.value: # add filing pdf if legal_type not in ['SP', 'GP']: - filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}', - headers=headers - ) - if filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - filing_pdf_encoded = base64.b64encode(filing_pdf.content) + filing_pdf_type = 'voluntaryDissolution' + filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, filing_pdf_type, token) + if filing_pdf_encoded: pdfs.append( { 'fileName': 'Voluntary Dissolution Application.pdf', @@ -99,14 +95,9 @@ def _get_pdfs( attach_order += 1 elif status == Filing.Status.COMPLETED.value: if legal_type in ['SP', 'GP']: - filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}', - headers=headers - ) - if filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - filing_pdf_encoded = base64.b64encode(filing_pdf.content) + filing_pdf_type = 'dissolution' + filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, filing_pdf_type, token) + if filing_pdf_encoded: pdfs.append( { 'fileName': 'Statement of Dissolution.pdf', @@ -119,15 +110,10 @@ def _get_pdfs( else: if filing.filing_sub_type != 'administrative': # add certificateOfDissolution, suppress certificate of dissolution for admin dissolution - certificate = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=certificateOfDissolution', - headers=headers - ) - if certificate.status_code != HTTPStatus.OK: - logger.error('Failed to get certificateOfDissolution pdf for filing: %s', filing.id) - else: - certificate_encoded = base64.b64encode(certificate.content) + certificate_pdf_type = 'certificateOfDissolution' + certificate_encoded = get_filing_document(business['identifier'], filing.id, + certificate_pdf_type, token) + if certificate_encoded: pdfs.append( { 'fileName': 'Certificate of Dissolution.pdf', @@ -140,19 +126,14 @@ def _get_pdfs( if legal_type == Business.LegalTypes.COOP.value: # certifiedAffidavit - certified_affidavit = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=affidavit', - headers=headers - ) - if certified_affidavit.status_code != HTTPStatus.OK: - logger.error('Failed to get affidavit pdf for filing: %s', filing.id) - else: - certificate_encoded = base64.b64encode(certified_affidavit.content) + certified_affidavit_pdf_type = 'affidavit' + certified_affidavit_encoded = get_filing_document(business['identifier'], filing.id, + certified_affidavit_pdf_type, token) + if certified_affidavit_encoded: pdfs.append( { 'fileName': 'Certified Affidavit.pdf', - 'fileBytes': certificate_encoded.decode('utf-8'), + 'fileBytes': certified_affidavit_encoded.decode('utf-8'), 'fileUrl': '', 'attachOrder': str(attach_order) } @@ -160,19 +141,14 @@ def _get_pdfs( attach_order += 1 # specialResolution - special_resolution = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=specialResolution', - headers=headers - ) - if special_resolution.status_code != HTTPStatus.OK: - logger.error('Failed to get specialResolution pdf for filing: %s', filing.id) - else: - certificate_encoded = base64.b64encode(special_resolution.content) + special_resolution_pdf_type = 'specialResolution' + special_resolution_encoded = get_filing_document(business['identifier'], filing.id, + special_resolution_pdf_type, token) + if special_resolution_encoded: pdfs.append( { 'fileName': 'Certified Special Resolution.pdf', - 'fileBytes': certificate_encoded.decode('utf-8'), + 'fileBytes': special_resolution_encoded.decode('utf-8'), 'fileUrl': '', 'attachOrder': str(attach_order) } diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/filing_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/filing_notification.py index deaba54d7c..6ee69c244c 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/filing_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/filing_notification.py @@ -27,6 +27,7 @@ from entity_emailer.email_processors import ( get_entity_dashboard_url, + get_filing_document, get_filing_info, get_recipients, get_user_email_from_auth, @@ -62,14 +63,8 @@ def _get_pdfs( if status == Filing.Status.PAID.value: # add filing pdf - filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}', - headers=headers - ) - if filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - filing_pdf_encoded = base64.b64encode(filing_pdf.content) + filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, filing.filing_type, token) + if filing_pdf_encoded: file_name = filing.filing_type[0].upper() + \ ' '.join(re.findall('[a-zA-Z][^A-Z]*', filing.filing_type[1:])) if ar_date := filing.filing_json['filing'].get('annualReport', {}).get('annualReportDate'): @@ -118,15 +113,9 @@ def _get_pdfs( if status == Filing.Status.COMPLETED.value: if legal_type != Business.LegalTypes.COOP.value: # add notice of articles - noa = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=noticeOfArticles', - headers=headers - ) - if noa.status_code != HTTPStatus.OK: - logger.error('Failed to get noa pdf for filing: %s', filing.id) - else: - noa_encoded = base64.b64encode(noa.content) + noa_pdf_type = 'noticeOfArticles' + noa_encoded = get_filing_document(business['identifier'], filing.id, noa_pdf_type, token) + if noa_encoded: pdfs.append( { 'fileName': 'Notice of Articles.pdf', @@ -139,15 +128,9 @@ def _get_pdfs( if filing.filing_type == 'incorporationApplication': # add certificate - certificate = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=certificate', - headers=headers - ) - if certificate.status_code != HTTPStatus.OK: - logger.error('Failed to get certificate pdf for filing: %s', filing.id) - else: - certificate_encoded = base64.b64encode(certificate.content) + certificate_pdf_type = 'certificate' + certificate_encoded = get_filing_document(business['identifier'], filing.id, certificate_pdf_type, token) + if certificate_encoded: file_name = 'Incorporation Certificate.pdf' pdfs.append( { @@ -161,15 +144,9 @@ def _get_pdfs( if legal_type == Business.LegalTypes.COOP.value: # Add rules - rules = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=certifiedRules', - headers=headers - ) - if rules.status_code != HTTPStatus.OK: - logger.error('Failed to get certifiedRules pdf for filing: %s', filing.id) - else: - certified_rules_encoded = base64.b64encode(rules.content) + rules_pdf_type = 'certifiedRules' + certified_rules_encoded = get_filing_document(business['identifier'], filing.id, rules_pdf_type, token) + if certified_rules_encoded: pdfs.append( { 'fileName': 'Certified Rules.pdf', @@ -181,15 +158,10 @@ def _get_pdfs( attach_order += 1 # Add memorandum - memorandum = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=certifiedMemorandum', - headers=headers - ) - if memorandum.status_code != HTTPStatus.OK: - logger.error('Failed to get certifiedMemorandum pdf for filing: %s', filing.id) - else: - certified_memorandum_encoded = base64.b64encode(memorandum.content) + memorandum_pdf_type = 'memorandum' + certified_memorandum_encoded = get_filing_document(business['identifier'], filing.id, + memorandum_pdf_type, token) + if certified_memorandum_encoded: pdfs.append( { 'fileName': 'Certified Memorandum.pdf', @@ -202,15 +174,9 @@ def _get_pdfs( if filing.filing_type == 'alteration' and get_additional_info(filing).get('nameChange', False): # add certificate of name change - certificate = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=certificateOfNameChange', - headers=headers - ) - if certificate.status_code != HTTPStatus.OK: - logger.error('Failed to get certificateOfNameChange pdf for filing: %s', filing.id) - else: - certificate_encoded = base64.b64encode(certificate.content) + certificate_pdf_type = 'certificateOfNameChange' + certificate_encoded = get_filing_document(business['identifier'], filing.id, certificate_pdf_type, token) + if certificate_encoded: file_name = 'Certificate of Name Change.pdf' pdfs.append( { diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/registration_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/registration_notification.py index d193735b18..b4bd780667 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/registration_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/registration_notification.py @@ -25,7 +25,9 @@ from jinja2 import Template from legal_api.models import Business, CorpType, Filing -from entity_emailer.email_processors import get_entity_dashboard_url, get_filing_info, substitute_template_parts +from entity_emailer.email_processors import ( + get_entity_dashboard_url, get_filing_document, get_filing_info, substitute_template_parts +) def _get_pdfs( @@ -73,14 +75,9 @@ def _get_pdfs( ) attach_order += 1 elif status == Filing.Status.COMPLETED.value: - filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}', - headers=headers - ) - if filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - filing_pdf_encoded = base64.b64encode(filing_pdf.content) + filing_pdf_type = 'registration' + filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, filing_pdf_type, token) + if filing_pdf_encoded: pdfs.append( { 'fileName': 'Statement of Registration.pdf', diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/restoration_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/restoration_notification.py index 2f53d8b23c..cc76757a86 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/restoration_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/restoration_notification.py @@ -24,7 +24,7 @@ from jinja2 import Environment, FileSystemLoader from legal_api.models import Business, CorpType, Filing -from entity_emailer.email_processors import get_filing_info +from entity_emailer.email_processors import get_filing_document, get_filing_info def _get_completed_pdfs( @@ -35,21 +35,11 @@ def _get_completed_pdfs( """Get the pdfs for the restoration output.""" pdfs = [] attach_order = 1 - headers = { - 'Accept': 'application/pdf', - 'Authorization': f'Bearer {token}' - } # add notice of articles - noa = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}', - params={'type': 'noticeOfArticles'}, - headers=headers - ) - if noa.status_code != HTTPStatus.OK: - logger.error('Failed to get noa pdf for filing: %s', filing.id) - else: - noa_encoded = base64.b64encode(noa.content) + noa_pdf_type = 'noticeOfArticles' + noa_encoded = get_filing_document(business['identifier'], filing.id, noa_pdf_type, token) + if noa_encoded: pdfs.append( { 'fileName': 'Notice of Articles.pdf', @@ -60,15 +50,9 @@ def _get_completed_pdfs( ) attach_order += 1 # add certificate of restoration - certificate = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=certificateOfRestoration', - headers=headers - ) - if certificate.status_code != HTTPStatus.OK: - logger.error('Failed to get certificate pdf for filing: %s', filing.id) - else: - certificate_encoded = base64.b64encode(certificate.content) + certificate_pdf_type = 'certificateOfRestoration' + certificate_encoded = get_filing_document(business['identifier'], filing.id, certificate_pdf_type, token) + if certificate_encoded: pdfs.append( { 'fileName': 'Certificate of Restoration.pdf', @@ -97,14 +81,9 @@ def _get_paid_pdfs( 'Authorization': f'Bearer {token}' } - filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}', - headers=headers - ) - if filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - filing_pdf_encoded = base64.b64encode(filing_pdf.content) + filing_pdf_type = 'restoration' + filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, filing_pdf_type, token) + if filing_pdf_encoded: pdfs.append( { 'fileName': 'Restoration Application.pdf', diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/special_resolution_helper.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/special_resolution_helper.py index 4a07e427ca..969fe98213 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/special_resolution_helper.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/special_resolution_helper.py @@ -20,6 +20,8 @@ from flask import current_app from legal_api.models import Business, Filing +from entity_emailer.email_processors import get_filing_document + def get_completed_pdfs( token: str, @@ -32,44 +34,29 @@ def get_completed_pdfs( """Get the completed pdfs for the special resolution output.""" pdfs = [] attach_order = 1 - headers = { - 'Accept': 'application/pdf', - 'Authorization': f'Bearer {token}' - } # specialResolution - special_resolution = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}' - f'/businesses/{business["identifier"]}' - f'/filings/{filing.id}' - '?type=specialResolution', - headers=headers - ) - if special_resolution.status_code == HTTPStatus.OK: - certificate_encoded = base64.b64encode(special_resolution.content) + special_resolution_pdf_type = 'specialResolution' + special_resolution_encoded = get_filing_document(business['identifier'], filing.id, + special_resolution_pdf_type, token) + if special_resolution_encoded: pdfs.append( { 'fileName': 'Special Resolution.pdf', - 'fileBytes': certificate_encoded.decode('utf-8'), + 'fileBytes': special_resolution_encoded.decode('utf-8'), 'fileUrl': '', 'attachOrder': str(attach_order) } ) attach_order += 1 - else: - logger.error('Failed to get specialResolution pdf for filing: %s, status code: %s', - filing.id, special_resolution.status_code) # Change of Name if name_changed: - name_change = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=certificateOfNameChange', - headers=headers - ) + certified_name_change_pdf_type = 'certificateOfNameChange' + certified_name_change_encoded = get_filing_document(business['identifier'], filing.id, + certified_name_change_pdf_type, token) - if name_change.status_code == HTTPStatus.OK: - certified_name_change_encoded = base64.b64encode(name_change.content) + if certified_name_change_encoded: pdfs.append( { 'fileName': 'Certificate of Name Change.pdf', @@ -79,19 +66,12 @@ def get_completed_pdfs( } ) attach_order += 1 - else: - logger.error('Failed to get certificateOfNameChange pdf for filing: %s, status code: %s', - filing.id, name_change.status_code) # Certified Rules if rules_changed: - rules = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=certifiedRules', - headers=headers - ) - if rules.status_code == HTTPStatus.OK: - certified_rules_encoded = base64.b64encode(rules.content) + rules_pdf_type = 'certifiedRules' + certified_rules_encoded = get_filing_document(business['identifier'], filing.id, rules_pdf_type, token) + if certified_rules_encoded: pdfs.append( { 'fileName': 'Certified Rules.pdf', @@ -104,13 +84,10 @@ def get_completed_pdfs( # Certified Memorandum if memorandum_changed: - memorandum = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}/businesses/{business["identifier"]}/filings/{filing.id}' - '?type=certifiedMemorandum', - headers=headers - ) - if memorandum.status_code == HTTPStatus.OK: - certified_memorandum_encoded = base64.b64encode(memorandum.content) + certified_memorandum_pdf_type = 'certifiedMemorandum' + certified_memorandum_encoded = get_filing_document(business['identifier'], filing.id, + certified_memorandum_pdf_type, token) + if certified_memorandum_encoded: pdfs.append( { 'fileName': 'Certified Memorandum.pdf', @@ -140,18 +117,9 @@ def get_paid_pdfs( } # add filing pdf - sr_filing_pdf = requests.get( - f'{current_app.config.get("LEGAL_API_URL")}' - f'/businesses/{business["identifier"]}' - f'/filings/{filing.id}' - '?type=specialResolutionApplication', - headers=headers - ) - - if sr_filing_pdf.status_code != HTTPStatus.OK: - logger.error('Failed to get pdf for filing: %s', filing.id) - else: - sr_filing_pdf_encoded = base64.b64encode(sr_filing_pdf.content) + sr_filing_pdf_type = 'specialResolutionApplication' + sr_filing_pdf_encoded = get_filing_document(business['identifier'], filing.id, sr_filing_pdf_type, token) + if sr_filing_pdf_encoded: pdfs.append( { 'fileName': 'Special Resolution Application.pdf', diff --git a/queue_services/entity-emailer/tests/unit/email_processors/test_correction_notification.py b/queue_services/entity-emailer/tests/unit/email_processors/test_correction_notification.py index 5b2b0f6a82..acc398fd95 100644 --- a/queue_services/entity-emailer/tests/unit/email_processors/test_correction_notification.py +++ b/queue_services/entity-emailer/tests/unit/email_processors/test_correction_notification.py @@ -165,19 +165,19 @@ def test_complete_special_resolution_correction_attachments(session, config): f'{config.get("LEGAL_API_URL")}' f'/businesses/{CP_IDENTIFIER}' f'/filings/{filing.id}' - f'?type=specialResolution' + '/documents/specialResolution' ), content=b'pdf_content_1', status_code=200 ) m.get( f'{config.get("LEGAL_API_URL")}/businesses/{CP_IDENTIFIER}/filings/{filing.id}' - '?type=certificateOfNameChange', + '/documents/certificateOfNameChange', content=b'pdf_content_2', status_code=200 ) m.get( - f'{config.get("LEGAL_API_URL")}/businesses/{CP_IDENTIFIER}/filings/{filing.id}?type=certifiedRules', + f'{config.get("LEGAL_API_URL")}/businesses/{CP_IDENTIFIER}/filings/{filing.id}/documents/certifiedRules', content=b'pdf_content_3', status_code=200 ) @@ -211,8 +211,7 @@ def test_paid_special_resolution_correction_attachments(session, config): '1', status, SPECIAL_RESOLUTION_FILING_TYPE) with requests_mock.Mocker() as m: m.get( - f'{config.get("LEGAL_API_URL")}/businesses/{CP_IDENTIFIER}/filings/{filing.id}' - f'?type=correction', + f'{config.get("LEGAL_API_URL")}/businesses/{CP_IDENTIFIER}/filings/{filing.id}/documents/correction', content=b'pdf_content_1', status_code=200 ) diff --git a/queue_services/entity-emailer/tests/unit/email_processors/test_cp_special_resolution_notification.py b/queue_services/entity-emailer/tests/unit/email_processors/test_cp_special_resolution_notification.py index f8568e2d20..d7a5ea1c22 100644 --- a/queue_services/entity-emailer/tests/unit/email_processors/test_cp_special_resolution_notification.py +++ b/queue_services/entity-emailer/tests/unit/email_processors/test_cp_special_resolution_notification.py @@ -79,19 +79,20 @@ def test_complete_special_resolution_attachments(session, config): f'{config.get("LEGAL_API_URL")}' f'/businesses/{IDENTIFIER}' f'/filings/{filing.id}' - f'?type=specialResolution' + f'/documents/specialResolution' ), content=b'pdf_content_1', status_code=200 ) m.get( f'{config.get("LEGAL_API_URL")}/businesses/{IDENTIFIER}/filings/{filing.id}' - '?type=certificateOfNameChange', + '/documents/certificateOfNameChange', content=b'pdf_content_2', status_code=200 ) m.get( - f'{config.get("LEGAL_API_URL")}/businesses/{IDENTIFIER}/filings/{filing.id}?type=certifiedRules', + f'{config.get("LEGAL_API_URL")}/businesses/{IDENTIFIER}/filings/{filing.id}' + '/documents/certifiedRules', content=b'pdf_content_3', status_code=200 ) @@ -130,7 +131,7 @@ def test_paid_special_resolution_attachments(session, config): f'{config.get("LEGAL_API_URL")}' f'/businesses/{IDENTIFIER}' f'/filings/{filing.id}' - f'?type=specialResolutionApplication' + f'/documents/specialResolutionApplication' ), content=b'pdf_content_1', status_code=200 diff --git a/queue_services/entity-emailer/tests/unit/email_processors/test_restoration_notification.py b/queue_services/entity-emailer/tests/unit/email_processors/test_restoration_notification.py index 49f1c7c5ac..32010399a5 100644 --- a/queue_services/entity-emailer/tests/unit/email_processors/test_restoration_notification.py +++ b/queue_services/entity-emailer/tests/unit/email_processors/test_restoration_notification.py @@ -35,9 +35,10 @@ def test_complete_full_restoration_notification_includes_notice_of_articles_and_ status = 'COMPLETED' filing = prep_restoration_filing(BUS_ID, '1', 'BC', LEGAL_NAME) with requests_mock.Mocker() as m: - m.get(f'{config.get("LEGAL_API_URL")}/businesses/{BUS_ID}/filings/{filing.id}?type=noticeOfArticles', + m.get(f'{config.get("LEGAL_API_URL")}/businesses/{BUS_ID}/filings/{filing.id}/documents/noticeOfArticles', content=b'pdf_content_1', status_code=200) - m.get(f'{config.get("LEGAL_API_URL")}/businesses/{BUS_ID}/filings/{filing.id}?type=certificateOfRestoration', + m.get(f'{config.get("LEGAL_API_URL")}/businesses/{BUS_ID}/filings/{filing.id}' + '/documents/certificateOfRestoration', content=b'pdf_content_2') output = restoration_notification.process({ 'filingId': filing.id, @@ -61,7 +62,7 @@ def test_paid_restoration_notification_includes_receipt_and_restoration_applicat with requests_mock.Mocker() as m: m.post(f'{config.get("PAY_API_URL")}/{filing.payment_token}/receipts', content=b'pdf_content_1', status_code=201) - m.get(f'{config.get("LEGAL_API_URL")}/businesses/{BUS_ID}/filings/{filing.id}', + m.get(f'{config.get("LEGAL_API_URL")}/businesses/{BUS_ID}/filings/{filing.id}/documents/restoration', content=b'pdf_content_2', status_code=200) output = restoration_notification.process({ 'filingId': filing.id, From 54401b0828ff983df6fdc8c14ca609ffe019223e Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Wed, 31 Jul 2024 15:08:29 -0700 Subject: [PATCH 2/2] fix lint issue --- .../src/entity_emailer/email_processors/__init__.py | 2 +- .../email_processors/agm_extension_notification.py | 5 ++++- .../email_processors/agm_location_change_notification.py | 5 ++++- .../email_processors/change_of_registration_notification.py | 5 ++++- .../consent_continuation_out_notification.py | 5 ++++- .../email_processors/registration_notification.py | 5 ++++- 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/__init__.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/__init__.py index 1cea7f90a9..fa75ab7b5b 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/__init__.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/__init__.py @@ -17,12 +17,12 @@ """ from __future__ import annotations +import base64 from datetime import datetime from http import HTTPStatus from pathlib import Path from typing import Tuple -import base64 import requests from entity_queue_common.service_utils import logger from flask import current_app diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_extension_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_extension_notification.py index 1881480e39..95ead6ffcf 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_extension_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_extension_notification.py @@ -26,7 +26,10 @@ from legal_api.models import Business, Filing from entity_emailer.email_processors import ( - get_filing_document, get_filing_info, get_recipient_from_auth, substitute_template_parts + get_filing_document, + get_filing_info, + get_recipient_from_auth, + substitute_template_parts, ) diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_location_change_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_location_change_notification.py index b5a13d96ec..b8f63cfb84 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_location_change_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/agm_location_change_notification.py @@ -26,7 +26,10 @@ from legal_api.models import Business, Filing from entity_emailer.email_processors import ( - get_filing_document, get_filing_info, get_recipient_from_auth, substitute_template_parts + get_filing_document, + get_filing_info, + get_recipient_from_auth, + substitute_template_parts, ) diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/change_of_registration_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/change_of_registration_notification.py index ac603d1c11..808ab5643d 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/change_of_registration_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/change_of_registration_notification.py @@ -26,7 +26,10 @@ from legal_api.models import Business, Filing, UserRoles from entity_emailer.email_processors import ( - get_filing_document, get_filing_info, get_user_email_from_auth, substitute_template_parts + get_filing_document, + get_filing_info, + get_user_email_from_auth, + substitute_template_parts, ) diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/consent_continuation_out_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/consent_continuation_out_notification.py index d8d3c4116c..b11e12a220 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/consent_continuation_out_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/consent_continuation_out_notification.py @@ -26,7 +26,10 @@ from legal_api.models import Business, Filing, UserRoles from entity_emailer.email_processors import ( - get_filing_document, get_filing_info, get_recipient_from_auth, substitute_template_parts + get_filing_document, + get_filing_info, + get_recipient_from_auth, + substitute_template_parts, ) diff --git a/queue_services/entity-emailer/src/entity_emailer/email_processors/registration_notification.py b/queue_services/entity-emailer/src/entity_emailer/email_processors/registration_notification.py index b4bd780667..711195eb8c 100644 --- a/queue_services/entity-emailer/src/entity_emailer/email_processors/registration_notification.py +++ b/queue_services/entity-emailer/src/entity_emailer/email_processors/registration_notification.py @@ -26,7 +26,10 @@ from legal_api.models import Business, CorpType, Filing from entity_emailer.email_processors import ( - get_entity_dashboard_url, get_filing_document, get_filing_info, substitute_template_parts + get_entity_dashboard_url, + get_filing_document, + get_filing_info, + substitute_template_parts, )