@@ -3,21 +3,24 @@ import mongoose from 'mongoose'
33import { err , ok , okAsync , Result , ResultAsync } from 'neverthrow'
44import Mail from 'nodemailer/lib/mailer'
55
6- import { AutoReplyMailData } from 'src/app/services/mail/mail.types'
6+ import { AutoReplyMailData , AutoreplySummaryRenderData } from 'src/app/services/mail/mail.types'
77import MailService from '../../../services/mail/mail.service'
88import * as EmailSubmissionService from '../email-submission/email-submission.service'
99
1010import { featureFlags } from '../../../../../shared/constants'
1111import {
1212 DateString ,
1313 FormResponseMode ,
14+ PaymentChannel ,
1415 SubmissionType ,
1516} from '../../../../../shared/types'
1617import {
18+ EmailAdminDataField ,
1719 FieldResponse , IEncryptedSubmissionSchema ,
1820 IPopulatedEncryptedForm ,
1921 IPopulatedForm
2022} from '../../../../types'
23+ import config from '../../../config/config'
2124import { createLoggerWithLabel } from '../../../config/logger'
2225import { getEncryptSubmissionModel } from '../../../models/submission.server.model'
2326import { createQueryWithDateParam } from '../../../utils/date'
@@ -42,7 +45,9 @@ import {
4245import { sendEmailConfirmations } from '../submission.service'
4346import { extractEmailConfirmationData } from '../submission.utils'
4447
48+ import moment from 'moment'
4549import { AutoreplyPdfGenerationError } from 'src/app/services/mail/mail.errors'
50+ import { generateAutoreplyPdf } from 'src/app/services/mail/mail.utils'
4651import { MYINFO_PREFIX } from '../email-submission/email-submission.constants'
4752import { ProcessedFieldResponse } from '../submission.types'
4853import { CHARTS_MAX_SUBMISSION_RESULTS } from './encrypt-submission.constants'
@@ -136,23 +141,71 @@ export const createEncryptSubmissionWithoutSave = ({
136141}
137142
138143const checkIfAdminPdfIsRequired = ( ) : boolean => {
139- return false
144+ return true
140145}
141146
142- const checkIfRespondentFormSummaryIsRequired = ( ) : boolean => {
143- return false
147+ const checkIfRespondentFormSummaryIsRequired = ( {
148+ autoReplyMailDatas,
149+ isPaymentEnabled,
150+ } : {
151+ autoReplyMailDatas : AutoReplyMailData [ ]
152+ isPaymentEnabled : boolean
153+ } ) : boolean => {
154+ return ! isPaymentEnabled && autoReplyMailDatas . some ( ( data ) => data . includeFormSummary )
144155}
145156
146- const checkIfPdfGenerationIsRequired = ( ) : boolean => {
147- return checkIfAdminPdfIsRequired ( ) || checkIfRespondentFormSummaryIsRequired ( )
157+ const checkIfPdfGenerationIsRequired = ( {
158+ isPaymentEnabled,
159+ autoReplyMailDatas,
160+ } : {
161+ isPaymentEnabled : boolean
162+ autoReplyMailDatas : AutoReplyMailData [ ]
163+ } ) : boolean => {
164+ return checkIfAdminPdfIsRequired ( ) || checkIfRespondentFormSummaryIsRequired ( {
165+ isPaymentEnabled,
166+ autoReplyMailDatas,
167+ } )
148168}
149169
150- const generatePdfAttachmentIfRequired = ( ) : ResultAsync < Mail . Attachment | undefined , AutoreplyPdfGenerationError > => {
151- if ( ! checkIfPdfGenerationIsRequired ( ) ) {
170+ const generatePdfAttachmentIfRequired = ( {
171+ isPaymentEnabled,
172+ autoReplyMailDatas,
173+ submission,
174+ form,
175+ responsesData,
176+ } : {
177+ isPaymentEnabled : boolean
178+ autoReplyMailDatas : AutoReplyMailData [ ]
179+ submission : IEncryptedSubmissionSchema
180+ form : IPopulatedEncryptedForm
181+ responsesData : ( Pick < EmailAdminDataField , 'question' | 'answerTemplate' > & {
182+ answer ?: EmailAdminDataField [ 'answer' ]
183+ } ) [ ]
184+ } ) : ResultAsync < Mail . Attachment | undefined , AutoreplyPdfGenerationError > => {
185+ if ( ! checkIfPdfGenerationIsRequired ( {
186+ isPaymentEnabled,
187+ autoReplyMailDatas,
188+ } ) ) {
152189 return okAsync ( undefined )
153190 }
154-
155- return okAsync ( undefined )
191+
192+ const renderData : AutoreplySummaryRenderData = {
193+ refNo : submission . id ,
194+ formTitle : form . title ,
195+ submissionTime : moment ( submission . created )
196+ . tz ( 'Asia/Singapore' )
197+ . format ( 'ddd, DD MMM YYYY hh:mm:ss A' ) ,
198+ formData : responsesData ,
199+ formUrl : `${ config . app . appUrl } /${ form . _id } ` ,
200+ }
201+
202+ return generateAutoreplyPdf (
203+ renderData ,
204+ true ,
205+ ) . map ( ( pdfBuffer ) => ( {
206+ filename : 'response.pdf' ,
207+ content : Buffer . copyBytesFrom ( pdfBuffer ) ,
208+ } ) )
156209}
157210
158211/**
@@ -269,13 +322,25 @@ export const performEncryptPostSubmissionActions = ({
269322 answer : item . answer ,
270323 } ) )
271324
272- const pdfAttachmentResult = generatePdfAttachmentIfRequired ( )
325+ const recipientEmailDatas = [
326+ ...extractEmailConfirmationData ( responses , form . form_fields ) ,
327+ ...respondentCopyEmailData ,
328+ ]
273329
274- return pdfAttachmentResult . andThen ( ( pdfAttachment ) => {
275- if ( pdfAttachment ) {
276- attachments = [ ...( attachments ?? [ ] ) , pdfAttachment ]
277- }
330+ const isPaymentEnabled =
331+ form . responseMode === FormResponseMode . Encrypt &&
332+ form . payments_channel . channel !== PaymentChannel . Unconnected &&
333+ form . payments_field . enabled === true
334+
335+ const pdfAttachmentResult = generatePdfAttachmentIfRequired ( {
336+ isPaymentEnabled,
337+ autoReplyMailDatas : recipientEmailDatas ,
338+ submission,
339+ form,
340+ responsesData : emailData . formData ,
341+ } )
278342
343+ return pdfAttachmentResult . andThen ( ( pdfAttachment ) => {
279344 void MailService . sendSubmissionToAdmin ( {
280345 replyToEmails : EmailSubmissionService . extractEmailAnswers ( emailFields ) ,
281346 form,
@@ -293,11 +358,8 @@ export const performEncryptPostSubmissionActions = ({
293358 submission,
294359 attachments,
295360 responsesData : emailData ?. autoReplyData ,
296- recipientData : [
297- ...extractEmailConfirmationData ( responses , form . form_fields ) ,
298- ...respondentCopyEmailData ,
299- ] ,
300- isUseLambdaOutput,
361+ recipientData : recipientEmailDatas ,
362+ pdfAttachment,
301363 } ) . mapErr ( ( error ) => {
302364 logger . error ( {
303365 message : 'Error while sending email confirmations' ,
0 commit comments