From 99fa925b389feabfd28c3a9416520947854bc9f1 Mon Sep 17 00:00:00 2001 From: g-tejas Date: Thu, 20 Jun 2024 14:02:31 +0800 Subject: [PATCH] fix: fix issue where email notifs for storage forms have wrong submission id --- .../encrypt-submission.controller.ts | 75 ++++++++++--------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/src/app/modules/submission/encrypt-submission/encrypt-submission.controller.ts b/src/app/modules/submission/encrypt-submission/encrypt-submission.controller.ts index 5cfbfe215d..cf18910d54 100644 --- a/src/app/modules/submission/encrypt-submission/encrypt-submission.controller.ts +++ b/src/app/modules/submission/encrypt-submission/encrypt-submission.controller.ts @@ -280,41 +280,6 @@ const submitEncryptModeForm = async ( } } - const logMetaWithSubmission = { - ...logMeta, - submissionId: form._id, - responseMetadata, - } - - logger.info({ - message: 'Sending admin notification mail', - meta: logMetaWithSubmission, - }) - - const emailData = new SubmissionEmailObj( - req.body.responses, - new Set(), // the MyInfo prefixes are already inserted in middleware - form.authType, - ) - - // We don't await for email submission, as the submission gets saved for encrypt - // submissions regardless, the email is more of a notification and shouldn't - // stop the storage of the data in the db - if (((form as IEncryptedForm)?.emails || []).length > 0) { - void MailService.sendSubmissionToAdmin({ - replyToEmails: EmailSubmissionService.extractEmailAnswers( - req.body.responses, - ), - form, - submission: { - created: form.created, - id: form._id, - }, - attachments: undefined, // Don't send attachments in the email notifications - formData: emailData.formData, - }) - } - // Save Responses to Database let attachmentMetadata = new Map() @@ -370,6 +335,7 @@ const submitEncryptModeForm = async ( res, logMeta, formId, + form, responses: req.formsg.filteredResponses, responseMetadata, submissionContent, @@ -628,6 +594,7 @@ const _createSubmission = async ({ submissionContent, logMeta, formId, + form, responseMetadata, responses, }: { @@ -636,6 +603,7 @@ const _createSubmission = async ({ responseMetadata: EncryptSubmissionDto['responseMetadata'] responses: ParsedClearFormFieldResponse[] formId: string + form: IPopulatedEncryptedForm submissionContent: EncryptSubmissionContent logMeta: CustomLoggerParams['meta'] }) => { @@ -670,6 +638,41 @@ const _createSubmission = async ({ }, }) + const createdTime = submission.created || new Date() + + const logMetaWithSubmission = { + ...logMeta, + submissionId, + responseMetadata, + } + + logger.info({ + message: 'Sending admin notification mail', + meta: logMetaWithSubmission, + }) + + const emailData = new SubmissionEmailObj( + responses, + new Set(), // the MyInfo prefixes are already inserted in middleware + form.authType, + ) + + // We don't await for email submission, as the submission gets saved for encrypt + // submissions regardless, the email is more of a notification and shouldn't + // stop the storage of the data in the db + if (((form as IEncryptedForm)?.emails || []).length > 0) { + void MailService.sendSubmissionToAdmin({ + replyToEmails: EmailSubmissionService.extractEmailAnswers(responses), + form, + submission: { + created: createdTime, + id: submission.id, + }, + attachments: undefined, // Don't send attachments in the email notifications + formData: emailData.formData, + }) + } + // TODO 6395 make responseMetadata mandatory if (responseMetadata) { reportSubmissionResponseTime(responseMetadata, { @@ -682,7 +685,7 @@ const _createSubmission = async ({ res.json({ message: 'Form submission successful.', submissionId, - timestamp: (submission.created || new Date()).getTime(), + timestamp: createdTime.getTime(), }) return await performEncryptPostSubmissionActions(submission, responses)