Skip to content

Commit

Permalink
refactor(email-submission): encapsulate parsedResponses (#2206)
Browse files Browse the repository at this point in the history
  • Loading branch information
yong-jie authored Jun 22, 2021
1 parent 77933c6 commit 6512a84
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 468 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import {
} from 'tests/unit/backend/helpers/generate-form-data'
import expressHandler from 'tests/unit/backend/helpers/jest-express'

import ParsedResponsesObject from '../../../submission/email-submission/ParsedResponsesObject.class'
import * as UserService from '../../../user/user.service'
import {
ForbiddenFormError,
Expand Down Expand Up @@ -127,6 +128,8 @@ jest.mock('src/app/modules/submission/submission.utils')
const MockSubmissionUtils = mocked(SubmissionUtils)
jest.mock('../admin-form.service')
const MockAdminFormService = mocked(AdminFormService)
jest.mock('../../../submission/email-submission/ParsedResponsesObject.class')
const MockParsedResponsesObject = mocked(ParsedResponsesObject)
jest.mock('../../form.service')
const MockFormService = mocked(FormService)
jest.mock('../../../user/user.service')
Expand Down Expand Up @@ -5130,8 +5133,12 @@ describe('admin-form.controller', () => {
MockEmailSubmissionService.validateAttachments.mockReturnValue(
okAsync(true),
)
MockSubmissionService.getProcessedResponses.mockReturnValue(
ok(MOCK_PARSED_RESPONSES),
MockParsedResponsesObject.mockClear()
MockParsedResponsesObject.parseResponses.mockReturnValue(
ok(new ParsedResponsesObject(MOCK_PARSED_RESPONSES)),
)
MockParsedResponsesObject.mock.instances[0].getAllResponses.mockReturnValue(
MOCK_PARSED_RESPONSES,
)
MockEmailSubmissionService.createEmailSubmissionWithoutSave.mockReturnValue(
MOCK_SUBMISSION,
Expand Down Expand Up @@ -5186,7 +5193,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockSubmissionService.getProcessedResponses).toHaveBeenCalledWith(
expect(MockParsedResponsesObject.parseResponses).toHaveBeenCalledWith(
MOCK_FORM,
MOCK_RESPONSES,
)
Expand Down Expand Up @@ -5253,7 +5260,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5302,7 +5309,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5355,7 +5362,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5408,7 +5415,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5461,7 +5468,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5514,7 +5521,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5567,7 +5574,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5620,7 +5627,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5673,7 +5680,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand All @@ -5692,7 +5699,7 @@ describe('admin-form.controller', () => {
})

it('should return 400 when responses cannot be processed', async () => {
MockSubmissionService.getProcessedResponses.mockReturnValueOnce(
MockParsedResponsesObject.parseResponses.mockReturnValueOnce(
err(new ProcessingError()),
)
const mockReq = expressHandler.mockRequest({
Expand Down Expand Up @@ -5726,7 +5733,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockSubmissionService.getProcessedResponses).toHaveBeenCalledWith(
expect(MockParsedResponsesObject.parseResponses).toHaveBeenCalledWith(
MOCK_FORM,
MOCK_RESPONSES,
)
Expand All @@ -5748,7 +5755,7 @@ describe('admin-form.controller', () => {
})

it('should return 409 when the submitted field IDs do not match the form field IDs', async () => {
MockSubmissionService.getProcessedResponses.mockReturnValueOnce(
MockParsedResponsesObject.parseResponses.mockReturnValueOnce(
err(new ConflictError('')),
)
const mockReq = expressHandler.mockRequest({
Expand Down Expand Up @@ -5782,7 +5789,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockSubmissionService.getProcessedResponses).toHaveBeenCalledWith(
expect(MockParsedResponsesObject.parseResponses).toHaveBeenCalledWith(
MOCK_FORM,
MOCK_RESPONSES,
)
Expand All @@ -5804,7 +5811,7 @@ describe('admin-form.controller', () => {
})

it('should return 400 when any answer is invalid', async () => {
MockSubmissionService.getProcessedResponses.mockReturnValueOnce(
MockParsedResponsesObject.parseResponses.mockReturnValueOnce(
err(new ValidateFieldError()),
)
const mockReq = expressHandler.mockRequest({
Expand Down Expand Up @@ -5838,7 +5845,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockSubmissionService.getProcessedResponses).toHaveBeenCalledWith(
expect(MockParsedResponsesObject.parseResponses).toHaveBeenCalledWith(
MOCK_FORM,
MOCK_RESPONSES,
)
Expand Down Expand Up @@ -5894,7 +5901,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockSubmissionService.getProcessedResponses).toHaveBeenCalledWith(
expect(MockParsedResponsesObject.parseResponses).toHaveBeenCalledWith(
MOCK_FORM,
MOCK_RESPONSES,
)
Expand Down Expand Up @@ -5959,7 +5966,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockSubmissionService.getProcessedResponses).toHaveBeenCalledWith(
expect(MockParsedResponsesObject.parseResponses).toHaveBeenCalledWith(
MOCK_FORM,
MOCK_RESPONSES,
)
Expand Down Expand Up @@ -6024,7 +6031,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockSubmissionService.getProcessedResponses).toHaveBeenCalledWith(
expect(MockParsedResponsesObject.parseResponses).toHaveBeenCalledWith(
MOCK_FORM,
MOCK_RESPONSES,
)
Expand Down
38 changes: 19 additions & 19 deletions src/app/modules/form/admin-form/admin-form.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,14 @@ import {
} from '../../core/core.errors'
import { ControllerHandler } from '../../core/core.types'
import * as FeedbackService from '../../feedback/feedback.service'
import {
createCorppassParsedResponses,
createSingpassParsedResponses,
} from '../../spcp/spcp.util'
import * as EmailSubmissionMiddleware from '../../submission/email-submission/email-submission.middleware'
import * as EmailSubmissionService from '../../submission/email-submission/email-submission.service'
import {
mapAttachmentsFromResponses,
mapRouteError as mapEmailSubmissionError,
SubmissionEmailObj,
} from '../../submission/email-submission/email-submission.util'
import ParsedResponsesObject from '../../submission/email-submission/ParsedResponsesObject.class'
import * as EncryptSubmissionMiddleware from '../../submission/encrypt-submission/encrypt-submission.middleware'
import * as EncryptSubmissionService from '../../submission/encrypt-submission/encrypt-submission.service'
import { mapRouteError as mapEncryptSubmissionError } from '../../submission/encrypt-submission/encrypt-submission.utils'
Expand Down Expand Up @@ -1512,7 +1509,7 @@ export const submitEmailPreview: ControllerHandler<

const parsedResponsesResult =
await EmailSubmissionService.validateAttachments(responses).andThen(() =>
SubmissionService.getProcessedResponses(form, responses),
ParsedResponsesObject.parseResponses(form, responses),
)
if (parsedResponsesResult.isErr()) {
logger.error({
Expand All @@ -1529,21 +1526,22 @@ export const submitEmailPreview: ControllerHandler<
const attachments = mapAttachmentsFromResponses(req.body.responses)

// Handle SingPass, CorpPass and MyInfo authentication and validation
if (form.authType === AuthType.SP || form.authType === AuthType.MyInfo) {
parsedResponses.push(
...createSingpassParsedResponses(PREVIEW_SINGPASS_UINFIN),
)
} else if (form.authType === AuthType.CP) {
parsedResponses.push(
...createCorppassParsedResponses(
PREVIEW_CORPPASS_UINFIN,
PREVIEW_CORPPASS_UID,
),
)
const { authType } = form
if (authType === AuthType.SP || authType === AuthType.MyInfo) {
parsedResponses.addNdiResponses({
authType,
uinFin: PREVIEW_SINGPASS_UINFIN,
})
} else if (authType === AuthType.CP) {
parsedResponses.addNdiResponses({
authType,
uinFin: PREVIEW_CORPPASS_UINFIN,
userInfo: PREVIEW_CORPPASS_UID,
})
}

const emailData = new SubmissionEmailObj(
parsedResponses,
parsedResponses.getAllResponses(),
// All MyInfo fields are verified in preview
new Set(AdminFormService.extractMyInfoFieldIds(form.form_fields)),
form.authType,
Expand All @@ -1556,7 +1554,9 @@ export const submitEmailPreview: ControllerHandler<
)

const sendAdminEmailResult = await MailService.sendSubmissionToAdmin({
replyToEmails: EmailSubmissionService.extractEmailAnswers(parsedResponses),
replyToEmails: EmailSubmissionService.extractEmailAnswers(
parsedResponses.getAllResponses(),
),
form,
submission,
attachments,
Expand Down Expand Up @@ -1585,7 +1585,7 @@ export const submitEmailPreview: ControllerHandler<
attachments,
responsesData: emailData.autoReplyData,
recipientData: extractEmailConfirmationData(
parsedResponses,
parsedResponses.getAllResponses(),
form.form_fields,
),
}).mapErr((error) => {
Expand Down
Loading

0 comments on commit 6512a84

Please sign in to comment.