generated from hmcts/expressjs-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfrom-api-format.ts
195 lines (186 loc) · 9.05 KB
/
from-api-format.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { invert } from 'lodash';
import { Case, Checkbox, LanguagePreference, formFieldsToCaseMapping, formatCase } from './case';
import { CaseData, ContactDetailsType, HowToRespondApplication, MarriageFormation, YesOrNo } from './definition';
import { fromApi as formatAddress } from './formatter/address';
import {
fromApiApplicant1 as uploadedFilesFromApiApplicant1,
fromApiApplicant2 as uploadedFilesFromApiApplicant2,
} from './formatter/uploaded-files';
type FromApiConverters = Partial<Record<keyof CaseData, string | ((data: Partial<CaseData>) => Partial<Case>)>>;
const checkboxConverter = (value: string | undefined) => {
if (!value) {
return undefined;
}
return value === YesOrNo.YES ? Checkbox.Checked : Checkbox.Unchecked;
};
const prayerConverter = (applicant: 'applicant1' | 'applicant2') => {
return data => ({
[`${applicant}IConfirmPrayer`]:
data[`${applicant}PrayerDissolveDivorce`]?.length > 0 ||
data[`${applicant}PrayerEndCivilPartnership`]?.length > 0 ||
data[`${applicant}PrayerFinancialOrdersThemselves`]?.length > 0 ||
data[`${applicant}PrayerFinancialOrdersChild`]?.length > 0
? Checkbox.Checked
: Checkbox.Unchecked,
});
};
const fields: FromApiConverters = {
...invert(formFieldsToCaseMapping),
marriageFormationType: ({ marriageFormationType }) => ({
sameSex: marriageFormationType === MarriageFormation.SAME_SEX_COUPLE ? Checkbox.Checked : Checkbox.Unchecked,
}),
marriageDate: data => ({
relationshipDate: fromApiDate(data.marriageDate),
}),
doesApplicant1WantToApplyForFinalOrder: data => ({
doesApplicant1WantToApplyForFinalOrder: checkboxConverter(data.doesApplicant1WantToApplyForFinalOrder),
}),
doesApplicant2WantToApplyForFinalOrder: data => ({
doesApplicant2WantToApplyForFinalOrder: checkboxConverter(data.doesApplicant2WantToApplyForFinalOrder),
}),
applicant1LanguagePreferenceWelsh: data => ({
applicant1EnglishOrWelsh:
data.applicant1LanguagePreferenceWelsh === YesOrNo.YES ? LanguagePreference.Welsh : LanguagePreference.English,
}),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
applicant2LanguagePreferenceWelsh: data => ({
applicant2EnglishOrWelsh:
data.applicant2LanguagePreferenceWelsh === YesOrNo.YES
? LanguagePreference.Welsh
: data.applicant2LanguagePreferenceWelsh === null
? data.applicant2LanguagePreferenceWelsh
: LanguagePreference.English,
}),
applicant1Address: data => formatAddress(data, 'applicant1'),
applicant1AddressOverseas: ({ applicant1AddressOverseas }) => ({
applicant1AddressOverseas: applicant1AddressOverseas ?? YesOrNo.NO,
}),
applicant1AgreedToReceiveEmails: data => ({
applicant1AgreeToReceiveEmails: checkboxConverter(data.applicant1AgreedToReceiveEmails),
}),
applicant2AgreedToReceiveEmails: data => ({
applicant2AgreeToReceiveEmails: checkboxConverter(data.applicant2AgreedToReceiveEmails),
}),
applicant1KnowsApplicant2EmailAddress: data => ({
applicant1DoesNotKnowApplicant2EmailAddress:
data.applicant1KnowsApplicant2EmailAddress === YesOrNo.YES ? Checkbox.Unchecked : Checkbox.Checked,
}),
applicant1FinalOrderStatementOfTruth: data => ({
applicant1FinalOrderStatementOfTruth: checkboxConverter(data.applicant1FinalOrderStatementOfTruth),
}),
applicant2FinalOrderStatementOfTruth: data => ({
applicant2FinalOrderStatementOfTruth: checkboxConverter(data.applicant2FinalOrderStatementOfTruth),
}),
applicant1ContactDetailsType: ({ applicant1ContactDetailsType }) => ({
applicant1AddressPrivate: applicant1ContactDetailsType === ContactDetailsType.PRIVATE ? YesOrNo.YES : YesOrNo.NO,
}),
applicant1InRefuge: ({ applicant1InRefuge }) => ({
applicant1InRefuge: applicant1InRefuge ?? YesOrNo.NO,
}),
applicant1WantsToHavePapersServedAnotherWay: data => ({
iWantToHavePapersServedAnotherWay: checkboxConverter(data.applicant1WantsToHavePapersServedAnotherWay),
}),
applicant2ContactDetailsType: ({ applicant2ContactDetailsType }) => ({
applicant2AddressPrivate: applicant2ContactDetailsType === ContactDetailsType.PRIVATE ? YesOrNo.YES : YesOrNo.NO,
}),
applicant2InRefuge: ({ applicant2InRefuge }) => ({
applicant2InRefuge: applicant2InRefuge ?? YesOrNo.NO,
}),
applicant2Address: data => formatAddress(data, 'applicant2'),
applicant2AddressOverseas: ({ applicant2AddressOverseas }) => ({
applicant2AddressOverseas: applicant2AddressOverseas ?? YesOrNo.NO,
}),
applicant1DocumentsUploaded: uploadedFilesFromApiApplicant1,
applicant2DocumentsUploaded: uploadedFilesFromApiApplicant2,
applicant1CannotUploadSupportingDocument: uploadedFilesFromApiApplicant1,
applicant2CannotUploadSupportingDocument: uploadedFilesFromApiApplicant2,
applicant1PrayerDissolveDivorce: prayerConverter('applicant1'),
applicant1PrayerEndCivilPartnership: prayerConverter('applicant1'),
applicant1PrayerFinancialOrdersThemselves: prayerConverter('applicant1'),
applicant1PrayerFinancialOrdersChild: prayerConverter('applicant1'),
applicant2PrayerDissolveDivorce: prayerConverter('applicant2'),
applicant2PrayerEndCivilPartnership: prayerConverter('applicant2'),
applicant2PrayerFinancialOrdersThemselves: prayerConverter('applicant2'),
applicant2PrayerFinancialOrdersChild: prayerConverter('applicant2'),
applicant1StatementOfTruth: data => ({
applicant1StatementOfTruth: checkboxConverter(data.applicant1StatementOfTruth),
}),
applicant2StatementOfTruth: data => ({
applicant2StatementOfTruth: checkboxConverter(data.applicant2StatementOfTruth),
}),
statementOfTruth: data => ({
aosStatementOfTruth: checkboxConverter(data.statementOfTruth),
}),
confirmReadPetition: data => ({
confirmReadPetition: checkboxConverter(data.confirmReadPetition),
}),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
howToRespondApplication: ({ howToRespondApplication }) => ({
disputeApplication:
howToRespondApplication === HowToRespondApplication.DISPUTE_DIVORCE
? YesOrNo.YES
: howToRespondApplication === null
? howToRespondApplication
: YesOrNo.NO,
}),
coApplicant1StatementOfTruth: data => ({
coApplicant1StatementOfTruth: checkboxConverter(data.coApplicant1StatementOfTruth),
}),
coApplicant2StatementOfTruth: data => ({
coApplicant2StatementOfTruth: checkboxConverter(data.coApplicant2StatementOfTruth),
}),
coCannotUploadClarificationDocuments: data => ({
coCannotUploadClarificationDocuments: checkboxConverter(data.coCannotUploadClarificationDocuments),
}),
coClarificationResponses: data => ({
coClarificationResponses: data.coClarificationResponses?.length ? data.coClarificationResponses?.[0].value : '',
}),
applicant2SolicitorAddress: data => {
const address = data.applicant2SolicitorAddress ? data.applicant2SolicitorAddress?.split('\n') : Array(7).fill('');
return {
applicant2SolicitorAddress: data.applicant2SolicitorAddress,
applicant2SolicitorAddress1: address?.[0],
applicant2SolicitorAddress2: address?.[1],
applicant2SolicitorAddress3: address?.[2],
applicant2SolicitorAddressTown: address?.[3],
applicant2SolicitorAddressCounty: address?.[4],
applicant2SolicitorAddressPostcode: address?.[5],
applicant2SolicitorAddressCountry: address?.[6],
};
},
applicant2SolicitorAddressOverseas: ({ applicant2SolicitorAddressOverseas }) => ({
applicant2SolicitorAddressOverseas: applicant2SolicitorAddressOverseas ?? YesOrNo.NO,
}),
previousState: 'previousState',
applicant1SolicitorRepresented: 'applicant1SolicitorRepresented',
applicant2Offline: 'applicant2Offline',
switchedToSoleCo: 'switchedToSoleCo',
applicant1AppliedForFinalOrderFirst: 'applicant1AppliedForFinalOrderFirst',
applicant2AppliedForFinalOrderFirst: 'applicant2AppliedForFinalOrderFirst',
coIsAdminClarificationSubmitted: 'coIsAdminClarificationSubmitted',
doesApplicant1IntendToSwitchToSole: 'doesApplicant1IntendToSwitchToSole',
dateApplicant1DeclaredIntentionToSwitchToSoleFo: 'dateApplicant1DeclaredIntentionToSwitchToSoleFo',
doesApplicant2IntendToSwitchToSole: 'doesApplicant2IntendToSwitchToSole',
dateApplicant2DeclaredIntentionToSwitchToSoleFo: 'dateApplicant2DeclaredIntentionToSwitchToSoleFo',
app1RfiDraftResponseDocs: uploadedFilesFromApiApplicant1,
app1RfiDraftResponseDetails: 'app1RfiDraftResponseDetails',
app1RfiDraftResponseCannotUploadDocs: data => ({
app1RfiDraftResponseCannotUploadDocs: checkboxConverter(data.app1RfiDraftResponseCannotUploadDocs),
}),
app2RfiDraftResponseDocs: uploadedFilesFromApiApplicant2,
app2RfiDraftResponseDetails: 'app2RfiDraftResponseDetails',
app2RfiDraftResponseCannotUploadDocs: data => ({
app2RfiDraftResponseCannotUploadDocs: checkboxConverter(data.app2RfiDraftResponseCannotUploadDocs),
}),
requestsForInformation: 'requestsForInformation',
};
const fromApiDate = date => {
if (!date) {
return;
}
const [y, m, d] = date.split('-');
return { year: `${+y}`, month: `${+m}`, day: `${+d}` };
};
export const fromApiFormat = (data: CaseData): Case => formatCase(fields, data);