Skip to content

Commit 81c04c6

Browse files
committed
fix: duplicate pdf due to mutation
1 parent 980e227 commit 81c04c6

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

src/app/modules/submission/encrypt-submission/encrypt-submission.service.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import { err, ok, okAsync, Result, ResultAsync } from 'neverthrow'
33
import Mail from 'nodemailer/lib/mailer'
44

55
import {
6-
BasicField,
76
DateString,
87
FormResponseMode,
98
PaymentChannel,
10-
SubmissionType,
9+
SubmissionType
1110
} from '../../../../../shared/types'
1211
import {
1312
EmailAdminDataField,
@@ -22,8 +21,7 @@ import { getEncryptSubmissionModel } from '../../../models/submission.server.mod
2221
import { AutoreplyPdfGenerationError } from '../../../services/mail/mail.errors'
2322
import MailService from '../../../services/mail/mail.service'
2423
import {
25-
AutoReplyMailData,
26-
AutoreplySummaryRenderData,
24+
AutoReplyMailData
2725
} from '../../../services/mail/mail.types'
2826
import { generateAutoreplyPdf, generatePdfRenderData } from '../../../services/mail/mail.utils'
2927
import { createQueryWithDateParam } from '../../../utils/date'
@@ -173,15 +171,6 @@ const checkIfPdfGenerationIsRequired = ({
173171
)
174172
}
175173

176-
const getPdfResponsesData = (responsesData: EmailAdminDataField[]): AutoreplySummaryRenderData['formData'] => {
177-
return responsesData.map(({ question, answerTemplate, answer, fieldType }) => ({
178-
question,
179-
// Only signature field types
180-
answer: fieldType === BasicField.Signature ? answer : undefined,
181-
answerTemplate
182-
}))
183-
}
184-
185174
const generatePdfAttachmentIfRequired = ({
186175
isPaymentEnabled,
187176
autoReplyMailDatas,

src/app/services/mail/mail.service.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -711,10 +711,10 @@ export class MailService {
711711
return okAsync(true)
712712
}
713713

714-
const attachments = submissionAttachments
715-
if (pdfAttachment) {
716-
attachments.push(pdfAttachment)
717-
}
714+
const attachmentsToInclude = [
715+
...submissionAttachments,
716+
...(pdfAttachment ? [pdfAttachment] : []),
717+
]
718718

719719
logger.info({
720720
message: 'Sending admin notification mail',
@@ -755,13 +755,12 @@ export class MailService {
755755
const adminEmails: string[] = getAdminEmails(form)
756756

757757
return generateSubmissionToAdminHtml(htmlData).andThen((mailHtml) => {
758-
759758
const mail: MailOptions = {
760759
to: adminEmails,
761760
from: this.#senderFromString,
762761
subject: `formsg-auto: ${formTitle} (#${refNo})`,
763762
html: mailHtml,
764-
attachments,
763+
attachments: attachmentsToInclude,
765764
headers: {
766765
[EMAIL_HEADERS.formId]: String(form._id),
767766
[EMAIL_HEADERS.submissionId]: refNo,
@@ -836,22 +835,33 @@ export class MailService {
836835
formUrl: `${this.#appUrl}/${form._id}`,
837836
}
838837

839-
// Create a copy of attachments for attaching of autoreply pdf if needed.
840-
const attachmentsWithAutoreplyPdf = [
841-
...submissionAttachments,
842-
...(pdfAttachment ? [pdfAttachment] : []),
843-
]
838+
const getAttachmentsToInclude = (mailData: AutoReplyMailData) => {
839+
const shouldPdfAttachmentBeIncluded =
840+
!isPaymentEnabled && mailData.includeFormSummary
841+
842+
if (shouldPdfAttachmentBeIncluded && !pdfAttachment) {
843+
logger.error({
844+
message: 'Could not find PDF attachment required for autoReply email. Continuing to send without PDF attachment.',
845+
meta: {
846+
action: 'sendAutoReplyEmails',
847+
formId: String(form._id),
848+
submissionId: String(submission.id),
849+
},
850+
})
851+
}
852+
853+
return pdfAttachment && shouldPdfAttachmentBeIncluded
854+
? [...submissionAttachments, pdfAttachment]
855+
: [...submissionAttachments]
856+
}
844857

845858
// Prepare mail sending for each autoreply mail.
846859
return Promise.allSettled(
847860
autoReplyMailDatas.map((mailData, index) => {
848861
return this.#sendSingleAutoreplyMail({
849862
form,
850863
submission,
851-
attachments:
852-
mailData.includeFormSummary && !isPaymentEnabled
853-
? attachmentsWithAutoreplyPdf
854-
: submissionAttachments,
864+
attachments: getAttachmentsToInclude(mailData),
855865
autoReplyMailData: mailData,
856866
formSummaryRenderData: strippedRenderData,
857867
index,

0 commit comments

Comments
 (0)