@@ -4,6 +4,8 @@ import { err, ok, okAsync, Result, ResultAsync } from 'neverthrow'
44import Mail from 'nodemailer/lib/mailer'
55
66import { AutoReplyMailData } from 'src/app/services/mail/mail.types'
7+ import MailService from '../../../services/mail/mail.service'
8+ import * as EmailSubmissionService from '../email-submission/email-submission.service'
79
810import { featureFlags } from '../../../../../shared/constants'
911import {
@@ -12,10 +14,9 @@ import {
1214 SubmissionType ,
1315} from '../../../../../shared/types'
1416import {
15- FieldResponse ,
16- IEncryptedSubmissionSchema ,
17+ FieldResponse , IEncryptedSubmissionSchema ,
1718 IPopulatedEncryptedForm ,
18- IPopulatedForm ,
19+ IPopulatedForm
1920} from '../../../../types'
2021import { createLoggerWithLabel } from '../../../config/logger'
2122import { getEncryptSubmissionModel } from '../../../models/submission.server.model'
@@ -41,6 +42,9 @@ import {
4142import { sendEmailConfirmations } from '../submission.service'
4243import { extractEmailConfirmationData } from '../submission.utils'
4344
45+ import { AutoreplyPdfGenerationError } from 'src/app/services/mail/mail.errors'
46+ import { MYINFO_PREFIX } from '../email-submission/email-submission.constants'
47+ import { ProcessedFieldResponse } from '../submission.types'
4448import { CHARTS_MAX_SUBMISSION_RESULTS } from './encrypt-submission.constants'
4549import { SaveEncryptSubmissionParams } from './encrypt-submission.types'
4650
@@ -98,10 +102,10 @@ export const assertFormIsSingleSubmissionDisabled = (
98102 return ! form . isSingleSubmission
99103 ? ok ( form )
100104 : err (
101- new UnsupportedSettingsError (
102- 'isSingleSubmission cannot be enabled for payment forms as it is not currently supported' ,
103- ) ,
104- )
105+ new UnsupportedSettingsError (
106+ 'isSingleSubmission cannot be enabled for payment forms as it is not currently supported' ,
107+ ) ,
108+ )
105109}
106110
107111/**
@@ -131,6 +135,26 @@ export const createEncryptSubmissionWithoutSave = ({
131135 } )
132136}
133137
138+ const checkIfAdminPdfIsRequired = ( ) : boolean => {
139+ return false
140+ }
141+
142+ const checkIfRespondentFormSummaryIsRequired = ( ) : boolean => {
143+ return false
144+ }
145+
146+ const checkIfPdfGenerationIsRequired = ( ) : boolean => {
147+ return checkIfAdminPdfIsRequired ( ) || checkIfRespondentFormSummaryIsRequired ( )
148+ }
149+
150+ const generatePdfAttachmentIfRequired = ( ) : ResultAsync < Mail . Attachment | undefined , AutoreplyPdfGenerationError > => {
151+ if ( ! checkIfPdfGenerationIsRequired ( ) ) {
152+ return okAsync ( undefined )
153+ }
154+
155+ return okAsync ( undefined )
156+ }
157+
134158/**
135159 * Performs the post-submission actions for encrypt submissions. This is to be
136160 * called when the submission is completed
@@ -149,14 +173,14 @@ export const performEncryptPostSubmissionActions = ({
149173 submission,
150174 responses,
151175 growthbook,
152- emailData ,
176+ emailFields ,
153177 attachments,
154178 respondentEmails,
155179} : {
156180 submission : IEncryptedSubmissionSchema
157181 responses : FieldResponse [ ]
158182 growthbook ?: GrowthBook
159- emailData ?: SubmissionEmailObj
183+ emailFields : ProcessedFieldResponse [ ]
160184 attachments ?: Mail . Attachment [ ]
161185 respondentEmails ?: string [ ]
162186} ) : ResultAsync <
@@ -207,11 +231,11 @@ export const performEncryptPostSubmissionActions = ({
207231 . andThen ( ( form ) => {
208232 const respondentCopyEmailData : AutoReplyMailData [ ] = respondentEmails
209233 ? respondentEmails ?. map ( ( val ) => {
210- return {
211- email : val ,
212- includeFormSummary : true ,
213- }
214- } )
234+ return {
235+ email : val ,
236+ includeFormSummary : true ,
237+ }
238+ } )
215239 : [ ]
216240
217241 // TODO [PDF-LAMBDA-GENERATION]: Remove setting of Growthbook targetting once pdf generation rollout is complete
@@ -230,25 +254,60 @@ export const performEncryptPostSubmissionActions = ({
230254 } ,
231255 } )
232256
233- return sendEmailConfirmations ( {
234- form,
235- submission,
236- attachments,
237- responsesData : emailData ?. autoReplyData ,
238- recipientData : [
239- ...extractEmailConfirmationData ( responses , form . form_fields ) ,
240- ...respondentCopyEmailData ,
241- ] ,
242- isUseLambdaOutput,
243- } ) . mapErr ( ( error ) => {
244- logger . error ( {
245- message : 'Error while sending email confirmations' ,
246- meta : {
247- action : 'sendEmailAutoReplies' ,
257+ const emailData = new SubmissionEmailObj (
258+ emailFields ,
259+ new Set ( ) , // the MyInfo prefixes are already inserted in middleware
260+ form . authType ,
261+ )
262+
263+ // Since we insert the [MyInfo] prefix in `encrypt-submission.middleware.ts`:L434
264+ // we want to remove it for the dataCollationData
265+ const dataCollationData = emailData . dataCollationData . map ( ( item ) => ( {
266+ question : item . question . startsWith ( MYINFO_PREFIX )
267+ ? item . question . slice ( MYINFO_PREFIX . length )
268+ : item . question ,
269+ answer : item . answer ,
270+ } ) )
271+
272+ const pdfAttachmentResult = generatePdfAttachmentIfRequired ( )
273+
274+ return pdfAttachmentResult . andThen ( ( pdfAttachment ) => {
275+ if ( pdfAttachment ) {
276+ attachments = [ ...( attachments ?? [ ] ) , pdfAttachment ]
277+ }
278+
279+ void MailService . sendSubmissionToAdmin ( {
280+ replyToEmails : EmailSubmissionService . extractEmailAnswers ( emailFields ) ,
281+ form,
282+ submission : {
283+ created : submission . created ,
284+ id : submission . id ,
248285 } ,
249- error,
286+ attachments,
287+ formData : emailData . formData ,
288+ dataCollationData,
289+ } )
290+
291+ return sendEmailConfirmations ( {
292+ form,
293+ submission,
294+ attachments,
295+ responsesData : emailData ?. autoReplyData ,
296+ recipientData : [
297+ ...extractEmailConfirmationData ( responses , form . form_fields ) ,
298+ ...respondentCopyEmailData ,
299+ ] ,
300+ isUseLambdaOutput,
301+ } ) . mapErr ( ( error ) => {
302+ logger . error ( {
303+ message : 'Error while sending email confirmations' ,
304+ meta : {
305+ action : 'sendEmailAutoReplies' ,
306+ } ,
307+ error,
308+ } )
309+ return error
250310 } )
251- return error
252311 } )
253312 } )
254313 )
0 commit comments