Skip to content

Commit

Permalink
refactor: revert "refactor(email-submission): encapsulate parsedRespo…
Browse files Browse the repository at this point in the history
…nses (#2206)"

This reverts commit 6512a84.
  • Loading branch information
mantariksh committed Jun 24, 2021
1 parent e41b690 commit ff86725
Show file tree
Hide file tree
Showing 8 changed files with 468 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ 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 @@ -128,8 +127,6 @@ 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 @@ -5133,12 +5130,8 @@ describe('admin-form.controller', () => {
MockEmailSubmissionService.validateAttachments.mockReturnValue(
okAsync(true),
)
MockParsedResponsesObject.mockClear()
MockParsedResponsesObject.parseResponses.mockReturnValue(
ok(new ParsedResponsesObject(MOCK_PARSED_RESPONSES)),
)
MockParsedResponsesObject.mock.instances[0].getAllResponses.mockReturnValue(
MOCK_PARSED_RESPONSES,
MockSubmissionService.getProcessedResponses.mockReturnValue(
ok(MOCK_PARSED_RESPONSES),
)
MockEmailSubmissionService.createEmailSubmissionWithoutSave.mockReturnValue(
MOCK_SUBMISSION,
Expand Down Expand Up @@ -5193,7 +5186,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockParsedResponsesObject.parseResponses).toHaveBeenCalledWith(
expect(MockSubmissionService.getProcessedResponses).toHaveBeenCalledWith(
MOCK_FORM,
MOCK_RESPONSES,
)
Expand Down Expand Up @@ -5260,7 +5253,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5309,7 +5302,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5362,7 +5355,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5415,7 +5408,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5468,7 +5461,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5521,7 +5514,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5574,7 +5567,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).not.toHaveBeenCalled()
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5627,7 +5620,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand Down Expand Up @@ -5680,7 +5673,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockParsedResponsesObject.parseResponses).not.toHaveBeenCalled()
expect(MockSubmissionService.getProcessedResponses).not.toHaveBeenCalled()
expect(MockAdminFormService.extractMyInfoFieldIds).not.toHaveBeenCalled()
expect(
MockEmailSubmissionService.createEmailSubmissionWithoutSave,
Expand All @@ -5699,7 +5692,7 @@ describe('admin-form.controller', () => {
})

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

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

it('should return 400 when any answer is invalid', async () => {
MockParsedResponsesObject.parseResponses.mockReturnValueOnce(
MockSubmissionService.getProcessedResponses.mockReturnValueOnce(
err(new ValidateFieldError()),
)
const mockReq = expressHandler.mockRequest({
Expand Down Expand Up @@ -5845,7 +5838,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockParsedResponsesObject.parseResponses).toHaveBeenCalledWith(
expect(MockSubmissionService.getProcessedResponses).toHaveBeenCalledWith(
MOCK_FORM,
MOCK_RESPONSES,
)
Expand Down Expand Up @@ -5901,7 +5894,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockParsedResponsesObject.parseResponses).toHaveBeenCalledWith(
expect(MockSubmissionService.getProcessedResponses).toHaveBeenCalledWith(
MOCK_FORM,
MOCK_RESPONSES,
)
Expand Down Expand Up @@ -5966,7 +5959,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockParsedResponsesObject.parseResponses).toHaveBeenCalledWith(
expect(MockSubmissionService.getProcessedResponses).toHaveBeenCalledWith(
MOCK_FORM,
MOCK_RESPONSES,
)
Expand Down Expand Up @@ -6031,7 +6024,7 @@ describe('admin-form.controller', () => {
expect(
MockEmailSubmissionService.validateAttachments,
).toHaveBeenCalledWith(MOCK_RESPONSES)
expect(MockParsedResponsesObject.parseResponses).toHaveBeenCalledWith(
expect(MockSubmissionService.getProcessedResponses).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,14 +50,17 @@ 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 @@ -1509,7 +1512,7 @@ export const submitEmailPreview: ControllerHandler<

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

// Handle SingPass, CorpPass and MyInfo authentication and validation
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,
})
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 emailData = new SubmissionEmailObj(
parsedResponses.getAllResponses(),
parsedResponses,
// All MyInfo fields are verified in preview
new Set(AdminFormService.extractMyInfoFieldIds(form.form_fields)),
form.authType,
Expand All @@ -1554,9 +1556,7 @@ export const submitEmailPreview: ControllerHandler<
)

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

0 comments on commit ff86725

Please sign in to comment.