Skip to content

Commit 19951d9

Browse files
authored
PLU-344: chore: add mock data improvements (#800)
## Problem Some fields are either missing or not user-friendly for mock data ## Improvements - Standardise email field types to the user email so it could be used in postman step - Add corppass missing info - Reference: https://github.com/opengovsg/FormSG/blob/82c5ba6fff7e9628b6c32449148e89c0224e9ff5/src/app/modules/spcp/spcp.util.ts#L92 - Can refer to this pipe in production for example: 9777d71f-dbac-47c4-a70a-c18f02e3a7bc - Hide NRIC collection if disabled - Add mock payment data if the form has payment enabled - For `Products` payment_type: take the first payment product available in the form setup (`Product or service`) - For `Variable` payment_type: mock a default product name and amount to be returned (`Respondents choose what to pay`) ## Tests (for mock data) - [x] Old and standard forms still work (no payment, singpass or corppass) - [x] Payment forms work (test using staging form, can add payments without a stripe account) - [x] Singpass forms still work with or without verified NRIC - [x] Corppass forms will return the verified NRIC and UEN
1 parent 9bc1bf4 commit 19951d9

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

packages/backend/src/apps/formsg/triggers/new-submission/get-mock-data.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@ type FormField = {
1818
othersRadioButton?: boolean
1919
}
2020

21+
// Adapted from https://github.com/opengovsg/FormSG/blob/82c5ba6fff7e9628b6c32449148e89c0224e9ff5/shared/types/form/form.ts#L96
22+
type PaymentProduct = {
23+
_id: string
24+
name: string
25+
description: string
26+
multi_qty: boolean
27+
min_qty: number
28+
max_qty: number
29+
amount_cents: number
30+
}
31+
2132
export const MOCK_ATTACHMENT_FILE_PATH = `${COMMON_S3_MOCK_FOLDER_PREFIX}plumber-logo.jpg`
2233
const MOCK_NRIC = 'S1234568B'
34+
const MOCK_UEN = '201612345A'
2335

2436
function generateVerifiedSubmitterInfoData(
2537
authType: string,
@@ -41,10 +53,43 @@ function generateVerifiedSubmitterInfoData(
4153
uinFin: filteredNric,
4254
},
4355
}
56+
case 'CP':
57+
return {
58+
verifiedSubmitterInfo: {
59+
cpUid: filteredNric,
60+
cpUen: MOCK_UEN,
61+
},
62+
}
4463
}
4564
return {}
4665
}
4766

67+
function generateMockPaymentData(products: Partial<PaymentProduct>[]) {
68+
// if there are no payment products, default to a mocked one
69+
const firstProduct: Partial<PaymentProduct> =
70+
products.length > 0
71+
? products[0]
72+
: {
73+
name: 'Test Product',
74+
amount_cents: 123,
75+
}
76+
77+
// Only the amount and product service is ideally obtainable based on their form data
78+
return {
79+
paymentContent: {
80+
type: 'payment_charge',
81+
status: 'succeeded',
82+
83+
url: 'https://form.gov.sg/api/v3/payments/abcde/12345/invoice/download',
84+
paymentIntent: 'pi_12345',
85+
amount: (firstProduct.amount_cents / 100).toFixed(2),
86+
productService: firstProduct.name,
87+
dateTime: new Date().toISOString(),
88+
transactionFee: '0.05',
89+
},
90+
}
91+
}
92+
4893
async function getMockData($: IGlobalVariable) {
4994
try {
5095
const { formId } = getFormDetailsFromGlobalVariable($)
@@ -80,6 +125,10 @@ async function getMockData($: IGlobalVariable) {
80125
)
81126
}
82127

128+
if (data.responses[formFields[i]._id].fieldType === 'email') {
129+
data.responses[formFields[i]._id].answer = $.user.email
130+
}
131+
83132
data.responses[formFields[i]._id].order = i + 1
84133
data.responses[formFields[i]._id].id = undefined
85134
}
@@ -95,7 +144,10 @@ async function getMockData($: IGlobalVariable) {
95144
submissionId: await generateIdAsync(),
96145
submissionTime: DateTime.now().toISO(),
97146
formId,
98-
...generateVerifiedSubmitterInfoData(formDetails.form.authType, $),
147+
...(formDetails.form.isSubmitterIdCollectionEnabled &&
148+
generateVerifiedSubmitterInfoData(formDetails.form.authType, $)),
149+
...(formDetails.form.payments_field.enabled &&
150+
generateMockPaymentData(formDetails.form.payments_field.products)),
99151
}
100152
} catch (e) {
101153
throw new Error(

0 commit comments

Comments
 (0)