Skip to content

Commit 0ff82f1

Browse files
committed
chore: address PR comments, fix tests
1 parent b623722 commit 0ff82f1

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

packages/backend/src/apps/postman/__tests__/actions/send-transactional-email.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ describe('send transactional email', () => {
626626
})
627627
})
628628

629-
it('should only send one email if there are blacklisted recipients and invalid attachments', async () => {
629+
it('should send two emails if there are blacklisted recipients and invalid attachments', async () => {
630630
const recipients = ['[email protected]', '[email protected]']
631631
$.step.parameters.destinationEmail = recipients.join(',')
632632
$.step.parameters.attachments = [
@@ -687,5 +687,15 @@ describe('send transactional email', () => {
687687
executionId: $.execution.id,
688688
blacklistedRecipients: [recipients[1]],
689689
})
690+
expect(mocks.sendInvalidAttachmentsEmail).toHaveBeenCalledWith({
691+
flowName: $.flow.name,
692+
flowId: $.flow.id,
693+
userEmail: $.user.email,
694+
executionId: $.execution.id,
695+
submissionId: 'abc',
696+
formAdminLink: 'https://form.gov.sg/admin/form/123/results',
697+
invalidAttachments: ['file-2.svg'],
698+
hasInvalidAttachments: true,
699+
})
690700
})
691701
})

packages/backend/src/apps/postman/actions/send-transactional-email/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { sendBlacklistEmail } from '../../common/send-blacklist-email'
2121
import { sendInvalidAttachmentsEmail } from '../../common/send-invalid-attachments-email'
2222
import { throwPostmanStepError } from '../../common/throw-errors'
2323

24+
const BASE_FORM_URL = 'https://form.gov.sg'
25+
2426
const action: IRawAction = {
2527
name: 'Send email',
2628
key: 'sendTransactionalEmail',
@@ -165,7 +167,9 @@ const action: IRawAction = {
165167
executionId: $.execution.id,
166168
}
167169
const hasInvalidAttachments = invalidAttachments.length > 0
168-
const formAdminLink = `https://form.gov.sg/admin/form/${formId}/results`
170+
const formAdminLink = formId
171+
? `${BASE_FORM_URL}/admin/form/${formId}/results`
172+
: BASE_FORM_URL
169173
const invalidAttachmentParams = {
170174
hasInvalidAttachments,
171175
submissionId,

packages/backend/src/apps/postman/common/parameters-helper.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ export async function getDefaultReplyTo(flowId: string): Promise<string> {
1414
return flow.user.email
1515
}
1616

17-
async function getFormId(executionId: string) {
17+
export async function getFormId(executionId: string): Promise<string | null> {
1818
const formData = await ExecutionStep.query()
1919
.where('execution_id', executionId)
2020
.where('app_key', 'formsg')
2121
.first()
2222
.select('data_out')
2323

24-
return String(formData?.dataOut?.formId)
24+
const formId = formData?.dataOut?.formId
25+
return formId ? String(formId) : null
2526
}
2627

2728
export async function filterAttachments(
@@ -39,8 +40,8 @@ export async function filterAttachments(
3940
// maliciously/ manually injected by another user who does not have access to this attachment
4041
const obj = await getObjectFromS3Id(attachment, { flowId: $.flow.id }, $)
4142
const fileName = obj.name
42-
const fileType = obj.name.split('.').pop()
43-
if (!POSTMAN_ACCEPTED_EXTENSIONS.includes(fileType)) {
43+
const fileType = obj.name.split('.').pop()?.toLowerCase()
44+
if (!fileType || !POSTMAN_ACCEPTED_EXTENSIONS.includes(fileType)) {
4445
invalidAttachments.push(fileName)
4546

4647
if (formId === null) {

packages/backend/src/apps/postman/common/send-invalid-attachments-email.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { truncateFlowName } from '@/helpers/generate-error-email'
22
import { sendEmail } from '@/helpers/send-email'
3-
import ExecutionStep from '@/models/execution-step'
43

54
interface SendInvalidAttachmentsEmailProps {
65
flowName: string
@@ -19,16 +18,6 @@ interface CreateMessageProps {
1918
formAdminLink: string
2019
}
2120

22-
export async function getFormId(executionId: string) {
23-
const formData = await ExecutionStep.query()
24-
.where('execution_id', executionId)
25-
.where('app_key', 'formsg')
26-
.first()
27-
.select('data_out')
28-
29-
return String(formData?.dataOut?.formId)
30-
}
31-
3221
export function createInvalidAttachmentsMessage(props: CreateMessageProps) {
3322
const {
3423
flowName,
@@ -62,7 +51,7 @@ export async function sendInvalidAttachmentsEmail(
6251
) {
6352
const { flowName, userEmail, formAdminLink } = props
6453
const truncatedFlowName = truncateFlowName(flowName)
65-
const bodyContent = await createInvalidAttachmentsMessage({
54+
const bodyContent = createInvalidAttachmentsMessage({
6655
...props,
6756
formAdminLink,
6857
})

0 commit comments

Comments
 (0)