diff --git a/src/app/models/__tests__/submission.server.model.spec.ts b/src/app/models/__tests__/submission.server.model.spec.ts index 766a4cb86b..d99438fecf 100644 --- a/src/app/models/__tests__/submission.server.model.spec.ts +++ b/src/app/models/__tests__/submission.server.model.spec.ts @@ -3,6 +3,7 @@ import { times } from 'lodash' import mongoose from 'mongoose' import getSubmissionModel, { + getEmailSubmissionModel, getEncryptSubmissionModel, } from 'src/app/models/submission.server.model' @@ -17,6 +18,7 @@ import { const Submission = getSubmissionModel(mongoose) const EncryptedSubmission = getEncryptSubmissionModel(mongoose) +const EmailSubmission = getEmailSubmissionModel(mongoose) // TODO: Add more tests for the rest of the submission schema. describe('Submission Model', () => { @@ -107,7 +109,7 @@ describe('Submission Model', () => { // Arrange const formId = new ObjectID() - const submission = await Submission.create({ + const submission = await EncryptedSubmission.create({ submissionType: SubmissionType.Encrypt, form: formId, encryptedContent: MOCK_ENCRYPTED_CONTENT, @@ -136,7 +138,7 @@ describe('Submission Model', () => { it('should return null view with non-encryptSubmission type', async () => { // Arrange const formId = new ObjectID() - const submission = await Submission.create({ + const submission = await EmailSubmission.create({ submissionType: SubmissionType.Email, form: formId, encryptedContent: MOCK_ENCRYPTED_CONTENT, @@ -182,7 +184,6 @@ describe('Submission Model', () => { response: { data: '{"result":"test-result"}', status: 200, - statusText: 'success', headers: '{}', }, }) as IWebhookResponse @@ -223,6 +224,11 @@ describe('Submission Model', () => { created: submission.created, signature: 'some signature', webhookUrl: 'https://form.gov.sg/endpoint', + response: { + status: 200, + headers: '', + data: '', + }, } as IWebhookResponse const invalidSubmissionId = new ObjectID().toHexString() diff --git a/src/app/models/submission.server.model.ts b/src/app/models/submission.server.model.ts index b8314c5d51..4d0e434d4f 100644 --- a/src/app/models/submission.server.model.ts +++ b/src/app/models/submission.server.model.ts @@ -126,12 +126,19 @@ EmailSubmissionSchema.methods.getWebhookView = function (): null { const webhookResponseSchema = new Schema( { - webhookUrl: String, - signature: String, - errorMessage: String, + webhookUrl: { + type: String, + required: true, + }, + signature: { + type: String, + required: true, + }, response: { - status: Number, - statusText: String, + status: { + type: Number, + required: true, + }, headers: String, data: String, }, diff --git a/src/app/modules/webhook/__tests__/webhook.service.spec.ts b/src/app/modules/webhook/__tests__/webhook.service.spec.ts index 03be2ea88d..e0e5485dda 100644 --- a/src/app/modules/webhook/__tests__/webhook.service.spec.ts +++ b/src/app/modules/webhook/__tests__/webhook.service.spec.ts @@ -9,7 +9,6 @@ import { getEncryptSubmissionModel } from 'src/app/models/submission.server.mode import { WebhookValidationError } from 'src/app/modules/webhook/webhook.errors' import * as WebhookValidationModule from 'src/app/modules/webhook/webhook.validation' import { transformMongoError } from 'src/app/utils/handle-mongo-error' -import * as HasPropModule from 'src/app/utils/has-prop' import { IEncryptedSubmissionSchema, IWebhookResponse, @@ -59,7 +58,6 @@ const MOCK_WEBHOOK_SUCCESS_RESPONSE: Pick = { response: { data: '{"result":"test-result"}', status: 200, - statusText: 'success', headers: '{}', }, } @@ -67,7 +65,6 @@ const MOCK_WEBHOOK_FAILURE_RESPONSE: Pick = { response: { data: '{"result":"test-result"}', status: 400, - statusText: 'failed', headers: '{}', }, } @@ -78,7 +75,6 @@ const MOCK_WEBHOOK_DEFAULT_FORMAT_RESPONSE: Pick< response: { data: '', status: 0, - statusText: '', headers: '', }, } @@ -305,7 +301,6 @@ describe('webhook.service', () => { // Assert const expectedResult = { - errorMessage: AXIOS_ERROR_MSG, ...MOCK_WEBHOOK_FAILURE_RESPONSE, signature: testSignature, webhookUrl: MOCK_WEBHOOK_URL, @@ -334,7 +329,6 @@ describe('webhook.service', () => { // Assert const expectedResult = { - errorMessage: DEFAULT_ERROR_MSG, ...MOCK_WEBHOOK_DEFAULT_FORMAT_RESPONSE, signature: testSignature, webhookUrl: MOCK_WEBHOOK_URL, @@ -359,9 +353,6 @@ describe('webhook.service', () => { MockAxios.post.mockRejectedValue(mockOriginalError) MockAxios.isAxiosError.mockReturnValue(false) - const hasPropSpy = jest - .spyOn(HasPropModule, 'hasProp') - .mockReturnValueOnce(false) // Act const actual = await sendWebhook( @@ -371,7 +362,6 @@ describe('webhook.service', () => { // Assert const expectedResult = { - errorMessage: '', ...MOCK_WEBHOOK_DEFAULT_FORMAT_RESPONSE, signature: testSignature, webhookUrl: MOCK_WEBHOOK_URL, @@ -380,7 +370,6 @@ describe('webhook.service', () => { expect( MockWebhookValidationModule.validateWebhookUrl, ).toHaveBeenCalledWith(MOCK_WEBHOOK_URL) - expect(hasPropSpy).toHaveBeenCalledWith(mockOriginalError, 'message') expect(MockAxios.post).toHaveBeenCalledWith( MOCK_WEBHOOK_URL, testSubmissionWebhookView, diff --git a/src/app/modules/webhook/webhook.service.ts b/src/app/modules/webhook/webhook.service.ts index 5632573728..ce4e6bf55c 100644 --- a/src/app/modules/webhook/webhook.service.ts +++ b/src/app/modules/webhook/webhook.service.ts @@ -12,7 +12,6 @@ import formsgSdk from '../../config/formsg-sdk' import { createLoggerWithLabel } from '../../config/logger' import { getEncryptSubmissionModel } from '../../models/submission.server.model' import { transformMongoError } from '../../utils/handle-mongo-error' -import { hasProp } from '../../utils/has-prop' import { PossibleDatabaseError } from '../core/core.errors' import { SubmissionNotFoundError } from '../submission/submission.errors' @@ -159,14 +158,9 @@ export const sendWebhook = ( // Webhook was posted but failed if (error instanceof WebhookFailedWithUnknownError) { - const originalError = error.meta.originalError - const errorMessage = hasProp(originalError, 'message') - ? originalError.message - : '' return okAsync({ signature, webhookUrl, - errorMessage, // Not Axios error so no guarantee of having response. // Hence allow formatting function to return default shape. response: formatWebhookResponse(), @@ -177,7 +171,6 @@ export const sendWebhook = ( return okAsync({ signature, webhookUrl, - errorMessage: axiosError.message, response: formatWebhookResponse(axiosError.response), }) }) diff --git a/src/app/modules/webhook/webhook.utils.ts b/src/app/modules/webhook/webhook.utils.ts index fef9a3a545..e3331465b4 100644 --- a/src/app/modules/webhook/webhook.utils.ts +++ b/src/app/modules/webhook/webhook.utils.ts @@ -11,7 +11,6 @@ export const formatWebhookResponse = ( response?: AxiosResponse, ): IWebhookResponse['response'] => ({ status: response?.status ?? 0, - statusText: response?.statusText ?? '', headers: stringifySafe(response?.headers) ?? '', data: stringifySafe(response?.data) ?? '', }) diff --git a/src/types/submission.ts b/src/types/submission.ts index 0edf9d46d0..150a833b36 100644 --- a/src/types/submission.ts +++ b/src/types/submission.ts @@ -1,4 +1,3 @@ -import { AxiosResponse } from 'axios' import { Document, Model, QueryCursor } from 'mongoose' import { MyInfoAttribute } from './field' @@ -105,9 +104,10 @@ export type IEncryptedSubmissionSchema = IEncryptedSubmission & export interface IWebhookResponse { webhookUrl: string signature: string - errorMessage?: string - response?: Omit, 'config' | 'request' | 'headers'> & { + response: { + status: number headers: string + data: string } }