Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.

## [7.10.1](https://github.com/opengovsg/formsg/compare/v7.10.0...v7.10.1) (2026-04-21)


### Bug Fixes

* **ci:** ignore sdk bump commit ([b622dee](https://github.com/opengovsg/formsg/commit/b622deeb09b43278df31571ceddfee8e72b10238))


### Miscellaneous

* Merge pull request #9318 from opengovsg/feat/single-sub-mrf ([#9318](https://github.com/opengovsg/formsg/commit/b1c598270f21d43398988f947ba19911c02b667a))
* Merge pull request #9322 from opengovsg/fix/ci/ignore-sdk-bump-commit ([#9322](https://github.com/opengovsg/formsg/commit/aeb4b78e6b3c3c2e3325223b99851d51dbe0f9bf))

## [7.10.0](https://github.com/opengovsg/formsg/compare/v7.9.2...v7.10.0) (2026-04-20)


Expand Down
2 changes: 1 addition & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "formsg-backend",
"version": "7.10.0",
"version": "7.10.1",
"private": true,
"homepage": ".",
"scripts": {
Expand Down
55 changes: 55 additions & 0 deletions apps/backend/src/app/models/submission.server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
WebhookView,
} from '../../types'
import { getPaymentWebhookEventObject } from '../modules/payments/payment.service.utils'
import { MultirespondentSubmissionContent } from '../modules/submission/multirespondent-submission/multirespondent-submission.types'
import { buildMrfMetadata } from '../modules/submission/submission.utils'
import { createQueryWithDateParam } from '../utils/date'

Expand Down Expand Up @@ -614,6 +615,60 @@ MultirespondentSubmissionSchema.methods.getWebhookView = async function (
}
}

MultirespondentSubmissionSchema.statics.saveIfSubmitterIdIsUnique =
async function (
formId: string,
submitterId: string,
zeroIndexedStepNumber: number,
submissionContent: MultirespondentSubmissionContent,
): Promise<
(IMultirespondentSubmissionSchema & { _id: mongoose.Types.ObjectId }) | null
> {
const submitterIdKey = `submittedSteps.${zeroIndexedStepNumber}.submitterId`
const session = await this.startSession()
session.startTransaction()
const beforeCreateRes = await this.exists({
form: formId,
submissionType: SubmissionType.Multirespondent,
[submitterIdKey]: submitterId,
})
.setOptions({ readPreference: 'primary' })
.session(session)
.exec()

if (beforeCreateRes) {
await session.abortTransaction()
await session.endSession()
return null
}

await this.create([submissionContent], { session })

const afterCreateRes = await this.find(
{
form: formId,
submissionType: SubmissionType.Multirespondent,
[submitterIdKey]: submitterId,
},
null,
{
limit: 2,
readPreference: 'primary',
},
)
.session(session)
.exec()
if (afterCreateRes.length != 1) {
await session.abortTransaction()
await session.endSession()
return null
}

await session.commitTransaction()
await session.endSession()
return afterCreateRes[0]
}

MultirespondentSubmissionSchema.statics.findSingleMetadata = function (
formId: string,
submissionId: string,
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/app/modules/core/core.errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export enum ErrorCodes {
SUBMISSION_GD_PARSE_VIRUS_SCANNER_LAMBDA_PAYLOAD = 100231,
SUBMISSION_GD_MALICIOUS_FILE_DETECTED = 100232,
SUBMISSION_MRF_WORKFLOW_OVERFLOW_ERROR = 100233,
SUBMISSION_MISSING_SUBMITTER_ID = 100234,
// [100300 - 100399] Receiver Errors (/modules/submission/receiver)
RECEIVER_INITIALISE_MULTIPART_RECEIVER = 100301,
RECEIVER_MULTIPART_CONTENT_LIMIT = 100302,
Expand Down
106 changes: 106 additions & 0 deletions apps/backend/src/app/modules/form/__tests__/form.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
BasicField,
FormResponseMode,
FormStatus,
PublicFormDto,
SubmissionType,
WorkflowType,
} from 'formsg-shared/types'
Expand All @@ -17,6 +18,7 @@ import { IFormSchema, IPopulatedForm } from 'src/types'

import * as SmsService from '../../../services/sms/sms.service'
import { ApplicationError, DatabaseError } from '../../core/core.errors'
import { MissingSubmitterIdError } from '../../submission/submission.errors'
import {
FormDeletedError,
FormNotFoundError,
Expand Down Expand Up @@ -80,6 +82,110 @@ describe('FormService', () => {
await dbHandler.closeDatabase()
})

describe('checkHasSingleSubmissionValidationFailure', () => {
const singleCheckFormId = new ObjectId().toHexString()
const singleCheckSubmitterId = 'mock-hashed-submitter-id'

it('should return false if isSingleSubmission setting is disabled', async () => {
const existsSpy = jest.spyOn(Submission, 'exists')

const form = {
_id: singleCheckFormId,
responseMode: FormResponseMode.Encrypt,
isSingleSubmission: false,
} as unknown as PublicFormDto

const result =
await FormService.checkHasSingleSubmissionValidationFailure(
form,
singleCheckSubmitterId,
)

expect(existsSpy).not.toHaveBeenCalled()
expect(result.isOk()).toBe(true)
expect(result._unsafeUnwrap()).toBe(false)

existsSpy.mockRestore()
})

it('should return MissingSubmitterIdError if submitterId is not set and isSingleSubmission setting is enabled', async () => {
const existsSpy = jest.spyOn(Submission, 'exists')

const form = {
_id: singleCheckFormId,
responseMode: FormResponseMode.Multirespondent,
isSingleSubmission: true,
} as unknown as PublicFormDto

const result =
await FormService.checkHasSingleSubmissionValidationFailure(form, '')

expect(existsSpy).not.toHaveBeenCalled()
expect(result.isErr()).toBe(true)
expect(result._unsafeUnwrapErr()).toBeInstanceOf(MissingSubmitterIdError)
expect(result._unsafeUnwrapErr().message).toEqual(
'Cannot find submitterId which is required for single submission per submitterId form',
)

existsSpy.mockRestore()
})

it('should send the mrf query based if form is mrf and isSingleSubmission setting is enabled', async () => {
const existsSpy = jest.spyOn(Submission, 'exists').mockResolvedValue(null)

const form = {
_id: singleCheckFormId,
responseMode: FormResponseMode.Multirespondent,
isSingleSubmission: true,
} as unknown as PublicFormDto

const result =
await FormService.checkHasSingleSubmissionValidationFailure(
form,
singleCheckSubmitterId,
)

expect(existsSpy).toHaveBeenCalledWith(
expect.objectContaining({
form: singleCheckFormId,
submissionType: SubmissionType.Multirespondent,
'submittedSteps.0.submitterId': singleCheckSubmitterId,
}),
)
expect(result.isOk()).toBe(true)
expect(result._unsafeUnwrap()).toBe(false)

existsSpy.mockRestore()
})

it('should send encrypt mode query if form is encrypt mode and isSingleSubmission setting is enabled', async () => {
const existsSpy = jest.spyOn(Submission, 'exists').mockResolvedValue(null)

const form = {
_id: singleCheckFormId,
responseMode: FormResponseMode.Encrypt,
isSingleSubmission: true,
} as unknown as PublicFormDto

const result =
await FormService.checkHasSingleSubmissionValidationFailure(
form,
singleCheckSubmitterId,
)

expect(existsSpy).toHaveBeenCalledWith(
expect.objectContaining({
form: singleCheckFormId,
submitterId: singleCheckSubmitterId,
}),
)
expect(result.isOk()).toBe(true)
expect(result._unsafeUnwrap()).toBe(false)

existsSpy.mockRestore()
})
})

describe('deactivateForm', () => {
it('should call Form.deactivateById', async () => {
const mock = jest.spyOn(Form, 'deactivateById')
Expand Down
45 changes: 30 additions & 15 deletions apps/backend/src/app/modules/form/form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
FormStatus,
Language,
PublicFormDto,
SubmissionType,
} from 'formsg-shared/types'
import { encryptString } from 'formsg-shared/utils/crypto'
import mongoose from 'mongoose'
Expand Down Expand Up @@ -43,6 +44,7 @@ import {
} from '../core/core.errors'
import { IntranetService } from '../intranet/intranet.service'
import { getMyInfoFieldOptions } from '../myinfo/myinfo.util'
import { MissingSubmitterIdError } from '../submission/submission.errors'
import * as SubmissionService from '../submission/submission.service'

import {
Expand Down Expand Up @@ -387,25 +389,38 @@ export const checkHasSingleSubmissionValidationFailure = (

if (!submitterId) {
return errAsync(
new ApplicationError(
new MissingSubmitterIdError(
'Cannot find submitterId which is required for single submission per submitterId form',
),
)
}
return ResultAsync.fromPromise(
SubmissionModel.exists({
form: form._id,
submitterId,
}),
(error) => {
logger.error({
message: 'Error while counting submissions for form',
meta: logMeta,
error,
})
return transformMongoError(error)
},
).andThen((result) => okAsync(result !== null))

const mrfStepOneIndex = 0
// NOTE: Hardcoded to step one since single submission is only supported for step one for now.
const submitterIdKey = `submittedSteps.${mrfStepOneIndex}.submitterId`
const baseQuery = {
form: form._id,
}
const query =
form.responseMode === FormResponseMode.Multirespondent
? {
...baseQuery,
submissionType: SubmissionType.Multirespondent,
[submitterIdKey]: submitterId,
}
: {
...baseQuery,
submitterId,
}

return ResultAsync.fromPromise(SubmissionModel.exists(query), (error) => {
logger.error({
message: 'Error while counting submissions for form',
meta: logMeta,
error,
})
return transformMongoError(error)
}).andThen((result) => okAsync(result !== null))
}

export const getFormModelByResponseMode = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ describe('public-form.controller', () => {
).toEqual(
generateHashedSubmitterId(
MOCK_SPCP_SESSION.userName.toUpperCase(),
MOCK_SP_FORM.id,
MOCK_SP_FORM._id,
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export const handleGetPublicForm: ControllerHandler<
const hasSingleSubmissionValidationFailureResult =
await FormService.checkHasSingleSubmissionValidationFailure(
publicForm,
generateHashedSubmitterId(submitterId, form.id),
generateHashedSubmitterId(submitterId, formId),
)

if (hasSingleSubmissionValidationFailureResult.isErr()) {
Expand Down
Loading
Loading