Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,42 @@ def get_filing_info(filing_id: str) -> tuple[Filing, dict, dict, str, str]:
return filing, business_json, leg_tmz_filing_date, leg_tmz_effective_date


def get_party_emails(parties: list[dict], roles: list[str]) -> list[str]:
"""Return the emails for the specified party types."""
recipients = []
for party in parties:
for role in party["roles"]:
if role["roleType"] in roles and (email := party["officer"].get("email")):
recipients.append(email)
break

return recipients


def get_recipients(option: str, filing_json: dict, token: str | None = None, filing_type: str | None = None) -> str:
"""Get the recipients for the email output."""
recipients = ""
filing_type = filing_type if filing_type else "incorporationApplication"
if filing_json["filing"].get(filing_type):
recipients = filing_json["filing"][filing_type].get("contactPoint", {}).get("email", "")
if option in [Filing.Status.PAID.value, "bn"] and \
filing_json["filing"]["header"]["name"] == filing_type:
parties = filing_json["filing"][filing_type].get("parties")
comp_party_email = None
for party in parties:
for role in party["roles"]:
if role["roleType"] == "Completing Party" and \
(comp_party_email := party["officer"].get("email")):
recipients = f"{recipients}, {comp_party_email}"
break
else:
identifier = filing_json["filing"]["business"]["identifier"]
if identifier[:2] != "CP":
# only add recipients if not coop
recipients = get_recipient_from_auth(identifier, token)
identifier = filing_json["filing"]["business"]["identifier"]

@mruff-aeq mruff-aeq Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_recipients reads filing_json["filing"]["business"]["identifier"] directly (line 66), but new business filings like incorporation and registration don't have a business block yet (your own prep_registration_filing deletes it for PAID), so this'll throw a KeyError on something like a future-effective incorporation

can we guard it with .get("business", {})?

@kialj876 kialj876 Jun 30, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually required by the schema so it will always be there. In the case of a bootstrap filing (incorp/amalg/cont in/registration) this would be a temporary identifier (regardless if its future effective or not). I will update the tests so that its realistic / more clear

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I adjusted a couple things to make it more clear about the temp reg and made the tests setup more realistic

is_coop = identifier.startswith("CP")

if filing_type and (filing_data := filing_json["filing"].get(filing_type)):
# add filing contact point email
recipients = filing_data.get("contactPoint", {}).get("email", "") or ""

# add relevant party emails
# FUTURE: after amalg and continuation have completing party removed 'temp_logic' can be removed
temp_logic = filing_type in ["amalgamationApplication", "continuationIn"] and option in ["PAID", "bn"]
is_coop_incorp_paid = is_coop and filing_type == "incorporationApplication" and option == "PAID"
is_valid_filing = filing_type in ["changeOfRegistration", "registration", "correction", "dissolution"]
if ((temp_logic or is_coop_incorp_paid or is_valid_filing)
and (parties := filing_data.get("parties"))
and (party_emails := get_party_emails(parties, ["Completing Party", "Custodian", "Partner", "Proprietor"]))
):
recipients = f"{recipients}, {', '.join(party_emails)}"

elif not is_coop:
# only add business email recipient for non-coop
recipients = get_recipient_from_auth(identifier, token)

return recipients

Expand Down Expand Up @@ -298,6 +313,8 @@ def _add_filing_document_pdf( # noqa: PLR0913
file_name = "Address Change"
elif document_type == "changeOfDirectors":
file_name = "Director Change"
elif document_type == "registration":
file_name = "Statement of Registration"

# Get pdf and add it to the list
filing_pdf_encoded = get_filing_document(business["identifier"], filing.id, document_type, token)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
OFFICE_NAME,
get_legal_type_key,
)
from business_model.models import Business, Filing, UserRoles
from business_model.models import Business, CorpType, Filing, UserRoles


def _get_additional_info(filing: Filing) -> dict:
Expand Down Expand Up @@ -78,7 +78,7 @@
return attachments, extra_pdf_types


def process(email_info: dict, token: str) -> dict:
def process(email_info: dict, token: str) -> dict | None:

Check failure on line 81 in queue_services/business-emailer/src/business_emailer/email_processors/filing_notification.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 21 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=bcgov_lear&issues=AZ8Z0_vvvAm_7qDFO_Zo&open=AZ8Z0_vvvAm_7qDFO_Zo&pullRequest=4542
"""Build the email the filing notification."""
current_app.logger.debug("filing_notification: %s", email_info)
filing_type, status = email_info["type"], email_info["option"]
Expand All @@ -92,7 +92,8 @@
new_business_filings = ["amalgamationApplication", "continuationIn", "incorporationApplication", "registration"]
filing_data = filing.json.get("filing", {}).get(filing_type, {})
if filing_type in new_business_filings and not business:
# For new business filings, the business record is not created yet. So we get the business info from the nameRequest.
# For new business filings, the nameRequest contains relevant business details.
# We overwrite the business info from the nameRequest and then set the identifier back to the temp reg id.
name_request = filing_data.get("nameRequest")
business = name_request
business["identifier"] = filing.temp_reg
Expand All @@ -103,6 +104,7 @@
if not legal_type or not filing_name or not business_identifier:
# Should never happen - log and return. It will be skipped.
current_app.logger.error("Missing legal_type, identifier and/or filing_name. Email: %s", email_info)
return

skipped_coop_filing_types = ["changeOfDirectors", "changeOfAddress"]
if legal_type == Business.LegalTypes.COOP.value and filing_type in skipped_coop_filing_types:
Expand All @@ -118,6 +120,9 @@
business_name = business.get("legalName") or NOT_AVAILABLE
filing_name_short = FILING_TITLE_SHORT.get(filing_type)
legal_type_key = get_legal_type_key(legal_type)
business_description = "Business"
if corp_type := CorpType.find_by_id(legal_type):
business_description: str = corp_type.full_desc.replace("BC ", "")

business_number = None
if len(business.get("taxId", "")) > 9: # noqa: PLR2004
Expand Down Expand Up @@ -145,18 +150,25 @@
entity_dashboard_url=dashboard_url,
filing_type=filing_type,
attachments_list=attachments_list,
business_description=business_description,
business_name=business_name,
business_identifier=business_identifier,
business_number=business_number,
filing_name=filing_name,
filing_name_short=filing_name_short,
future_attachments_list=future_attachments,
office_name=OFFICE_NAME.get(legal_type_key),
number_description="Registration" if legal_type_key == "FIRM" else "Incorporation",
show_effective_date=show_effective_date,
)

Comment thread
kialj876 marked this conversation as resolved.
# get recipients
recipients = get_recipients(status, filing.filing_json, token)
recipient_filing_type = None
if filing_type in ["incorporationApplication", "registration", "changeOfRegistration"]:
recipient_filing_type = filing_type

recipients = get_recipients(status, filing.filing_json, token, recipient_filing_type)

if additional_recipients := _get_additional_recipients(filing, token):
recipients = f"{recipients}, {additional_recipients}"

Expand Down
Loading
Loading