-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsubmission.ts
More file actions
110 lines (100 loc) · 2.81 KB
/
submission.ts
File metadata and controls
110 lines (100 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { type SubmitPayload } from '@defra/forms-model'
import { PaymentField } from '~/src/server/plugins/engine/components/PaymentField.js'
import { getAnswer } from '~/src/server/plugins/engine/components/helpers/components.js'
import {
type DetailItem,
type DetailItemField
} from '~/src/server/plugins/engine/models/types.js'
import {
formatCurrency,
formatPaymentDate
} from '~/src/server/plugins/payment/helper.js'
export interface SubmitRecord {
name: string
title: string
value: string
}
/**
* Builds the main submission records from field items.
* Regular fields are converted to single records, while PaymentField
* components are expanded into four separate records.
*/
export function buildMainRecords(items: DetailItem[]): SubmitRecord[] {
const fieldItems = items.filter(
(item): item is DetailItemField => 'field' in item
)
const records: SubmitRecord[] = []
for (const item of fieldItems) {
if (item.field instanceof PaymentField) {
records.push(...buildPaymentRecords(item))
} else {
records.push({
name: item.name,
title: item.label,
value: getAnswer(item.field, item.state, { format: 'data' })
})
}
}
return records
}
/**
* Expands a PaymentField into four submission records:
* - Payment description
* - Payment amount (formatted with currency symbol)
* - Payment reference
* - Payment date (formatted date/time)
*
* Returns an empty array if no payment state exists.
*/
export function buildPaymentRecords(item: DetailItemField): SubmitRecord[] {
const paymentState = (item.field as PaymentField).getPaymentStateFromState(
item.state
)
if (!paymentState) {
return []
}
return [
{
name: `${item.name}_paymentDescription`,
title: 'Payment description',
value: paymentState.description
},
{
name: `${item.name}_paymentAmount`,
title: 'Payment amount',
value: formatCurrency(paymentState.amount)
},
{
name: `${item.name}_paymentReference`,
title: 'Payment reference',
value: paymentState.reference
},
{
name: `${item.name}_paymentDate`,
title: 'Payment date',
value: paymentState.preAuth?.createdAt
? formatPaymentDate(paymentState.preAuth.createdAt)
: ''
}
]
}
/**
* Builds the repeater submission records from repeater items.
*/
export function buildRepeaterRecords(
items: DetailItem[]
): SubmitPayload['repeaters'] {
return items
.filter((item) => 'subItems' in item)
.map((item) => ({
name: item.name,
title: item.label,
value: item.subItems.map((detailItems) =>
detailItems.map((subItem) => ({
name: subItem.name,
title: subItem.label,
value: getAnswer(subItem.field, subItem.state, { format: 'data' })
}))
)
}))
}