@@ -191,6 +191,65 @@ def _get_out_filing_details(filing: Filing) -> dict | None:
191191 }
192192
193193
194+ def _get_temp_business (filing : Filing , filing_data : dict , withdrawn_filing : Filing | None ) -> dict | None :
195+ """Return the business details for a filing that has no business record yet (new business filings)."""
196+ if filing .filing_type in Filing .TempCorpFilingType :
197+ # For new business filings, the nameRequest contains relevant business details.
198+ # We overwrite the business info from the nameRequest and then set the identifier back to the temp reg id.
199+ business = filing_data .get ("nameRequest" )
200+ business ["identifier" ] = filing .temp_reg
201+ return business
202+
203+ if withdrawn_filing :
204+ # For a withdrawn new business filing (IA, Amalgamation, Continuation In), the nameRequest on the
205+ # withdrawn filing contains the business details (NR name, or the primary/holding business name).
206+ business = withdrawn_filing .filing_json ["filing" ][withdrawn_filing .filing_type ]["nameRequest" ]
207+ business ["identifier" ] = withdrawn_filing .temp_reg
208+ if not business .get ("legalName" ):
209+ business ["legalName" ] = Business .BUSINESSES .get (business .get ("legalType" ), {}).get ("numberedDescription" )
210+ return business
211+
212+ return None
213+
214+
215+ def _get_filing_recipients (
216+ status : str , filing : Filing , withdrawn_filing : Filing | None , business_identifier : str , token : str
217+ ) -> str :
218+ """Return the recipients for the filing notification."""
219+ filing_type = filing .filing_type
220+ recipient_filing_type = None
221+ recipient_filing_json = filing .filing_json
222+ if filing_type in Filing .TempCorpFilingType or filing_type in ["changeOfRegistration" , "correction" , "dissolution" ]:
223+ recipient_filing_type = filing_type
224+ elif withdrawn_filing and not filing .business_id :
225+ # withdrawn new business filing: notify the contacts on the withdrawn filing
226+ recipient_filing_type = withdrawn_filing .filing_type
227+ recipient_filing_json = withdrawn_filing .filing_json
228+
229+ recipients = get_recipients (status , recipient_filing_json , token , recipient_filing_type )
230+
231+ if filing_type == "dissolution" and (business_email := get_recipient_from_auth (business_identifier , token )):
232+ # dissolution also notifies the business contact email
233+ recipients = f"{ recipients } , { business_email } " if recipients else business_email
234+
235+ if additional_recipients := _get_additional_recipients (filing , token ):
236+ recipients = f"{ recipients } , { additional_recipients } "
237+
238+ return recipients
239+
240+
241+ def _get_withdrawal_details (filing : Filing , withdrawn_filing : Filing | None ) -> dict :
242+ """Return the template details for a notice of withdrawal filing."""
243+ if not withdrawn_filing :
244+ return {}
245+ withdrawn_filing_name , _ = _get_filing_display_names (withdrawn_filing .filing_type , withdrawn_filing .filing_sub_type )
246+ return {
247+ # the withdrawn filing id is shown (instead of the incorporation number) for new business filings
248+ "filing_id" : withdrawn_filing .id if not filing .business_id else None ,
249+ "withdrawn_filing_name" : withdrawn_filing_name
250+ }
251+
252+
194253def _get_filing_display_names (filing_type : str , filing_subtype : str | None ):
195254 """Return the filing display names for the given type and subtype."""
196255 filing_name , filing_name_short = FILING_TITLE .get (filing_type ), FILING_TITLE_SHORT .get (filing_type )
@@ -227,12 +286,9 @@ def process(email_info: dict, token: str) -> dict | None:
227286 filing , business , leg_tmz_filing_date , leg_tmz_effective_date = get_filing_info (email_info ["filingId" ])
228287
229288 filing_data = filing .json .get ("filing" , {}).get (filing_type , {})
230- if filing_type in Filing .TempCorpFilingType and not business :
231- # For new business filings, the nameRequest contains relevant business details.
232- # We overwrite the business info from the nameRequest and then set the identifier back to the temp reg id.
233- name_request = filing_data .get ("nameRequest" )
234- business = name_request
235- business ["identifier" ] = filing .temp_reg
289+ withdrawn_filing = Filing .find_by_id (filing .withdrawn_filing_id ) if filing_type == "noticeOfWithdrawal" else None
290+ if not business :
291+ business = _get_temp_business (filing , filing_data , withdrawn_filing )
236292
237293 legal_type = business .get ("legalType" )
238294 filing_name , filing_name_short = _get_filing_display_names (filing_type , filing .filing_sub_type )
@@ -291,23 +347,13 @@ def process(email_info: dict, token: str) -> dict | None:
291347 show_effective_date = show_effective_date ,
292348 what_happens_next_name = what_happens_next_name ,
293349 # consent/continuation/amalgamation out values
294- ** out_filing_details
350+ ** out_filing_details ,
351+ # notice of withdrawal values
352+ ** _get_withdrawal_details (filing , withdrawn_filing )
295353 )
296354
297355 # get recipients
298- recipient_filing_type = None
299- if filing_type in Filing .TempCorpFilingType or filing_type in ["changeOfRegistration" , "correction" , "dissolution" ]:
300- recipient_filing_type = filing_type
301-
302- recipients = get_recipients (status , filing .filing_json , token , recipient_filing_type )
303-
304- if filing_type == "dissolution" and (business_email := get_recipient_from_auth (business_identifier , token )):
305- # dissolution also notifies the business contact email
306- recipients = f"{ recipients } , { business_email } " if recipients else business_email
307-
308- if additional_recipients := _get_additional_recipients (filing , token ):
309- recipients = f"{ recipients } , { additional_recipients } "
310-
356+ recipients = _get_filing_recipients (status , filing , withdrawn_filing , business_identifier , token )
311357 if not recipients :
312358 current_app .logger .error ("No recipients found for filing notification email: %s" , email_info )
313359 return
0 commit comments