@defra/fcp-sfd-frontend-engine 0.2.12 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -64,15 +64,66 @@ var businessSbiSchema = Joi.object({
64
64
  })
65
65
  });
66
66
 
67
+ // src/schemas/address-schema.js
68
+ import Joi2 from "joi";
69
+
70
+ // src/constants/validation-fields.js
71
+ var FIRST_NAME_MAX = 100;
72
+ var LAST_NAME_MAX = 100;
73
+ var MIDDLE_NAMES_MAX = 100;
74
+ var EMAIL_MAX = 254;
75
+ var PHONE_NUMBER_MIN = 10;
76
+ var PHONE_NUMBER_MAX = 50;
77
+ var MAX_AGE_YEARS = 120;
78
+ var ADDRESS_LINE_MAX = 100;
79
+ var TOWN_CITY_MAX = 60;
80
+ var COUNTY_MAX = 60;
81
+ var COUNTRY_MAX = 60;
82
+ var POSTCODE_MAX = 8;
83
+
84
+ // src/schemas/address-schema.js
85
+ var addressSchema = Joi2.object({
86
+ address1: Joi2.string().required().max(ADDRESS_LINE_MAX).messages({
87
+ "string.empty": "Enter address line 1, typically the building and street",
88
+ "string.max": `Address line 1 must be ${ADDRESS_LINE_MAX} characters or less`,
89
+ "any.required": "Enter address line 1, typically the building and street"
90
+ }),
91
+ address2: Joi2.string().allow("").max(ADDRESS_LINE_MAX).messages({
92
+ "string.max": `Address line 2 must be ${ADDRESS_LINE_MAX} characters or less`
93
+ }),
94
+ address3: Joi2.string().allow("").max(ADDRESS_LINE_MAX).messages({
95
+ "string.max": `Address line 3 must be ${ADDRESS_LINE_MAX} characters or less`
96
+ }),
97
+ city: Joi2.string().required().max(TOWN_CITY_MAX).messages({
98
+ "string.empty": "Enter town or city",
99
+ "string.max": `Town or city must be ${TOWN_CITY_MAX} characters or less`,
100
+ "any.required": "Enter town or city"
101
+ }),
102
+ county: Joi2.string().allow("").max(COUNTY_MAX).messages({
103
+ "string.max": `County must be ${COUNTY_MAX} characters or less`
104
+ }),
105
+ postcode: Joi2.string().required().max(POSTCODE_MAX).messages({
106
+ "any.required": "Enter a postal code or zip code",
107
+ "string.empty": "Enter a postal code or zip code",
108
+ "string.max": `Postal code or zip code must be ${POSTCODE_MAX} characters or less`
109
+ }),
110
+ country: Joi2.string().required().max(COUNTRY_MAX).messages({
111
+ "string.empty": "Enter a country",
112
+ "string.max": `Country must be ${COUNTRY_MAX} characters or less`,
113
+ "any.required": "Enter a country"
114
+ })
115
+ });
116
+
67
117
  // src/schemas/business/business-schemas.js
68
118
  var businessSchemas = {
69
- sbi: businessSbiSchema
119
+ sbi: businessSbiSchema,
120
+ address: addressSchema
70
121
  };
71
122
 
72
123
  // src/schemas/customer/customer-crn-schema.js
73
- import Joi2 from "joi";
74
- var customerCrnSchema = Joi2.object({
75
- crn: Joi2.string().pattern(/^\d{10}$/).allow("").optional().messages({
124
+ import Joi3 from "joi";
125
+ var customerCrnSchema = Joi3.object({
126
+ crn: Joi3.string().pattern(/^\d{10}$/).allow("").optional().messages({
76
127
  "string.pattern.base": "Enter the full CRN"
77
128
  })
78
129
  });
@@ -82,10 +133,219 @@ var customerSchemas = {
82
133
  crn: customerCrnSchema
83
134
  };
84
135
 
136
+ // src/schemas/personal/personal-name-schema.js
137
+ import Joi4 from "joi";
138
+ var personalNameSchema = Joi4.object({
139
+ first: Joi4.string().required().max(FIRST_NAME_MAX).messages({
140
+ "string.empty": "Enter first name",
141
+ "string.max": `First name must be ${FIRST_NAME_MAX} characters or less`,
142
+ "any.required": "Enter first name"
143
+ }),
144
+ last: Joi4.string().required().max(LAST_NAME_MAX).messages({
145
+ "string.empty": "Enter last name",
146
+ "string.max": `Last name must be ${LAST_NAME_MAX} characters or less`,
147
+ "any.required": "Enter last name"
148
+ }),
149
+ middle: Joi4.string().allow("").max(MIDDLE_NAMES_MAX).messages({
150
+ "string.max": `Middle names must be ${MIDDLE_NAMES_MAX} characters or less`
151
+ })
152
+ });
153
+
154
+ // src/schemas/personal/personal-dob-schema.js
155
+ import Joi5 from "joi";
156
+
157
+ // src/constants/patterns.js
158
+ var PHONE_NUMBER_PATTERN = /^\+?[0-9 ()]+$/;
159
+
160
+ // src/constants/month-map.js
161
+ var MONTH_MAP = {
162
+ january: 1,
163
+ jan: 1,
164
+ february: 2,
165
+ feb: 2,
166
+ march: 3,
167
+ mar: 3,
168
+ april: 4,
169
+ apr: 4,
170
+ may: 5,
171
+ june: 6,
172
+ jun: 6,
173
+ july: 7,
174
+ jul: 7,
175
+ august: 8,
176
+ aug: 8,
177
+ september: 9,
178
+ sep: 9,
179
+ sept: 9,
180
+ october: 10,
181
+ oct: 10,
182
+ november: 11,
183
+ nov: 11,
184
+ december: 12,
185
+ dec: 12
186
+ };
187
+
188
+ // src/schemas/personal/personal-dob-schema.js
189
+ var personalDobSchema = Joi5.object({
190
+ day: Joi5.string().allow(""),
191
+ month: Joi5.string().allow(""),
192
+ year: Joi5.string().allow("")
193
+ }).custom((value, helpers) => {
194
+ const { day, month, year } = value;
195
+ const missingFieldsError = checkMissingFields(day, month, year, helpers);
196
+ if (missingFieldsError) {
197
+ return missingFieldsError;
198
+ }
199
+ if (year && year.length !== 4) {
200
+ return makeError(helpers, "dob.yearLength", ["year"]);
201
+ }
202
+ const monthValue = getMonthNumber(month, helpers);
203
+ if (monthValue.isJoiError) {
204
+ return monthValue;
205
+ }
206
+ const fullDate = getFullDate(day, monthValue, year, helpers);
207
+ if (fullDate.isJoiError) {
208
+ return fullDate;
209
+ }
210
+ if (fullDate > /* @__PURE__ */ new Date()) {
211
+ return makeError(helpers, "dob.future", ["day", "month", "year"]);
212
+ }
213
+ const tooOldError = checkNotTooOld(fullDate, helpers);
214
+ if (tooOldError) {
215
+ return tooOldError;
216
+ }
217
+ return value;
218
+ }).messages({
219
+ "dob.missingAll": "Enter your date of birth",
220
+ "dob.missingDay": "Date of birth must include a day",
221
+ "dob.missingMonth": "Date of birth must include a month",
222
+ "dob.missingYear": "Date of birth must include a year",
223
+ "dob.missingDayMonth": "Date of birth must include a day and month",
224
+ "dob.missingDayYear": "Date of birth must include a day and year",
225
+ "dob.missingMonthYear": "Date of birth must include a month and year",
226
+ "dob.yearLength": "Enter a year with 4 numbers, like 1975",
227
+ "dob.invalid": "Date of birth must be a real date",
228
+ "dob.future": "Date of birth must be in the past",
229
+ "dob.tooOld": "Date of birth must be on or after {{#oldest}}"
230
+ });
231
+ var checkNotTooOld = (fullDate, helpers) => {
232
+ const oldestDateAllowed = getOldestAllowedDate();
233
+ if (fullDate < oldestDateAllowed) {
234
+ const oldestDateString = oldestDateAllowed.toLocaleDateString("en-GB", {
235
+ day: "numeric",
236
+ month: "long",
237
+ year: "numeric"
238
+ });
239
+ const error = helpers.error("dob.tooOld", { oldest: oldestDateString });
240
+ error.path = ["day", "month", "year"];
241
+ return error;
242
+ }
243
+ return null;
244
+ };
245
+ var getOldestAllowedDate = () => {
246
+ const date = /* @__PURE__ */ new Date();
247
+ date.setUTCHours(0, 0, 0, 0);
248
+ date.setUTCFullYear(date.getUTCFullYear() - MAX_AGE_YEARS);
249
+ return date;
250
+ };
251
+ var getFullDate = (day, monthValue, year, helpers) => {
252
+ const dayValue = Number.parseInt(day, 10);
253
+ const yearValue = Number.parseInt(year, 10);
254
+ const date = new Date(Date.UTC(yearValue, monthValue - 1, dayValue));
255
+ if (date.getUTCFullYear() !== yearValue || date.getUTCMonth() + 1 !== monthValue || date.getUTCDate() !== dayValue) {
256
+ return makeError(helpers, "dob.invalid", ["day", "month", "year"]);
257
+ }
258
+ return date;
259
+ };
260
+ var getMonthNumber = (month, helpers) => {
261
+ if (Number.isNaN(Number(month))) {
262
+ const lower = month.toLowerCase();
263
+ const mapped = MONTH_MAP[lower];
264
+ if (!mapped) {
265
+ return makeError(helpers, "dob.invalid", ["month"]);
266
+ }
267
+ return mapped;
268
+ }
269
+ return Number.parseInt(month, 10);
270
+ };
271
+ var checkMissingFields = (day, month, year, helpers) => {
272
+ if (!day && !month && !year) {
273
+ return makeError(helpers, "dob.missingAll", ["day", "month", "year"]);
274
+ }
275
+ if (!day && month && year) {
276
+ return makeError(helpers, "dob.missingDay", ["day"]);
277
+ }
278
+ if (day && !month && year) {
279
+ return makeError(helpers, "dob.missingMonth", ["month"]);
280
+ }
281
+ if (day && month && !year) {
282
+ return makeError(helpers, "dob.missingYear", ["year"]);
283
+ }
284
+ if (!day && !month && year) {
285
+ return makeError(helpers, "dob.missingDayMonth", ["day", "month"]);
286
+ }
287
+ if (!day && month && !year) {
288
+ return makeError(helpers, "dob.missingDayYear", ["day", "year"]);
289
+ }
290
+ if (day && !month && !year) {
291
+ return makeError(helpers, "dob.missingMonthYear", ["month", "year"]);
292
+ }
293
+ return null;
294
+ };
295
+ var makeError = (helpers, code, fields) => {
296
+ const error = helpers.error(code);
297
+ error.path = fields;
298
+ error.isJoiError = true;
299
+ return error;
300
+ };
301
+
302
+ // src/schemas/personal/personal-email-schema.js
303
+ import Joi6 from "joi";
304
+ var personalEmailSchema = Joi6.object({
305
+ personalEmail: Joi6.string().required().max(EMAIL_MAX).email({
306
+ minDomainSegments: 2,
307
+ tlds: {
308
+ allow: true,
309
+ min: 2
310
+ }
311
+ }).messages({
312
+ "string.max": `Email address must be ${EMAIL_MAX} characters or less`,
313
+ "string.empty": "Enter a personal email address",
314
+ "string.email": "Enter an email address, like name@example.com"
315
+ })
316
+ });
317
+
318
+ // src/schemas/personal/personal-phone-schema.js
319
+ import Joi7 from "joi";
320
+ var personalPhoneSchema = Joi7.object({
321
+ personalTelephone: Joi7.string().empty("").min(PHONE_NUMBER_MIN).max(PHONE_NUMBER_MAX).pattern(PHONE_NUMBER_PATTERN).messages({
322
+ "string.min": `Personal telephone number must be ${PHONE_NUMBER_MIN} characters or more`,
323
+ "string.max": `Personal telephone number must be ${PHONE_NUMBER_MAX} characters or less`,
324
+ "string.pattern.base": "Personal telephone number must only include numbers 0 to 9 and special characters such as spaces, brackets and +"
325
+ }),
326
+ personalMobile: Joi7.string().empty("").min(PHONE_NUMBER_MIN).max(PHONE_NUMBER_MAX).pattern(PHONE_NUMBER_PATTERN).messages({
327
+ "string.min": `Personal mobile phone number must be ${PHONE_NUMBER_MIN} characters or more`,
328
+ "string.max": `Personal mobile phone number must be ${PHONE_NUMBER_MAX} characters or less`,
329
+ "string.pattern.base": "Personal mobile phone number must only include numbers 0 to 9 and special characters such as spaces, brackets and +"
330
+ })
331
+ }).or("personalTelephone", "personalMobile").messages({
332
+ "object.missing": "Enter at least one phone number"
333
+ });
334
+
335
+ // src/schemas/personal/personal-schemas.js
336
+ var personalSchemas = {
337
+ name: personalNameSchema,
338
+ dob: personalDobSchema,
339
+ address: addressSchema,
340
+ phone: personalPhoneSchema,
341
+ email: personalEmailSchema
342
+ };
343
+
85
344
  // src/schemas/schemas.js
86
345
  var schemas = {
87
346
  business: businessSchemas,
88
- customer: customerSchemas
347
+ customer: customerSchemas,
348
+ personal: personalSchemas
89
349
  };
90
350
 
91
351
  // src/utils/format-validation-errors.js
@@ -118,8 +378,158 @@ var formatValidationErrors = (errors) => {
118
378
  var utils = {
119
379
  formatValidationErrors
120
380
  };
381
+
382
+ // src/presenters/base-presenter.js
383
+ var BACK_LINK_DISPLAY_MAX = 50;
384
+ var formatBackLink = (businessName) => {
385
+ if (businessName.length > BACK_LINK_DISPLAY_MAX) {
386
+ return `Back to ${businessName.slice(0, BACK_LINK_DISPLAY_MAX)}\u2026`;
387
+ }
388
+ return `Back to ${businessName}`;
389
+ };
390
+ var formatNumber = (payloadNumber, changedNumber, originalNumber) => {
391
+ if (payloadNumber !== void 0) {
392
+ return payloadNumber;
393
+ }
394
+ if (changedNumber !== void 0) {
395
+ return changedNumber;
396
+ }
397
+ return originalNumber;
398
+ };
399
+ var formatDisplayAddress = (address) => {
400
+ const { lookup, manual, postcode, country, city } = address;
401
+ let addressLines = [];
402
+ if (lookup.uprn) {
403
+ const buildingAndStreet = [
404
+ lookup.buildingNumberRange,
405
+ lookup.street
406
+ ].filter(Boolean).join(" ");
407
+ addressLines = [
408
+ lookup.pafOrganisationName,
409
+ lookup.flatName,
410
+ lookup.buildingName,
411
+ buildingAndStreet,
412
+ lookup.doubleDependentLocality,
413
+ lookup.dependentLocality,
414
+ city,
415
+ lookup.county
416
+ ];
417
+ } else {
418
+ addressLines = [
419
+ manual.line1,
420
+ manual.line2,
421
+ manual.line3,
422
+ city,
423
+ manual.line4,
424
+ // County
425
+ manual.line5
426
+ ];
427
+ }
428
+ return [
429
+ ...addressLines.filter(Boolean),
430
+ postcode,
431
+ country
432
+ ];
433
+ };
434
+ var buildAddressLine = (parts) => {
435
+ return parts.filter(Boolean).join(", ") || null;
436
+ };
437
+ var buildStreetLine = (buildingRange, street) => {
438
+ return [buildingRange, street].filter(Boolean).join(" ") || null;
439
+ };
440
+ var formatLookupAddress = (lookup, city, country, postcode) => ({
441
+ address1: buildAddressLine([lookup.pafOrganisationName, lookup.flatName, lookup.buildingName]),
442
+ address2: buildStreetLine(lookup.buildingNumberRange, lookup.street),
443
+ address3: buildAddressLine([lookup.doubleDependentLocality, lookup.dependentLocality]),
444
+ county: lookup.county ?? null,
445
+ city: city ?? null,
446
+ country: country ?? null,
447
+ postcode: postcode ?? null
448
+ });
449
+ var formatManualAddress = (manual, city, country, postcode) => ({
450
+ address1: manual.line1 ?? null,
451
+ address2: manual.line2 ?? null,
452
+ address3: manual.line3 ?? null,
453
+ city: city ?? null,
454
+ county: manual.line4 ?? null,
455
+ country: country ?? null,
456
+ postcode: postcode ?? null
457
+ });
458
+ var formatOriginalAddress = (originalAddress) => {
459
+ const { lookup, manual, city, country, postcode } = originalAddress;
460
+ return lookup.uprn ? formatLookupAddress(lookup, city, country, postcode) : formatManualAddress(manual, city, country, postcode);
461
+ };
462
+ var formatChangedAddress = (changeBusinessAddress) => {
463
+ if (!changeBusinessAddress.uprn) {
464
+ return changeBusinessAddress;
465
+ }
466
+ const {
467
+ pafOrganisationName,
468
+ flatName,
469
+ buildingName,
470
+ buildingNumberRange,
471
+ street,
472
+ doubleDependentLocality,
473
+ dependentLocality,
474
+ city,
475
+ county,
476
+ country,
477
+ postcode
478
+ } = changeBusinessAddress;
479
+ return {
480
+ address1: buildAddressLine([pafOrganisationName, flatName, buildingName]),
481
+ address2: buildStreetLine(buildingNumberRange, street),
482
+ address3: buildAddressLine([doubleDependentLocality, dependentLocality]),
483
+ city: city ?? null,
484
+ county: county ?? null,
485
+ country: country ?? null,
486
+ postcode: postcode ?? null
487
+ };
488
+ };
489
+ var formatDisplayAddresses = (addresses, previouslyPickedAddress) => {
490
+ const displayAddresses = addresses.map((address) => ({
491
+ value: `${address.uprn}${address.displayAddress}`,
492
+ text: address.displayAddress,
493
+ selected: (previouslyPickedAddress == null ? void 0 : previouslyPickedAddress.uprn) === address.uprn && (previouslyPickedAddress == null ? void 0 : previouslyPickedAddress.displayAddress) === address.displayAddress
494
+ }));
495
+ const hasSelectedAddress = displayAddresses.some((addr) => addr.selected);
496
+ const text = addresses.length === 1 ? "1 address found" : `${addresses.length} addresses found`;
497
+ displayAddresses.unshift({
498
+ value: "display",
499
+ text,
500
+ selected: !hasSelectedAddress
501
+ });
502
+ return displayAddresses;
503
+ };
504
+ var sortErrorsBySectionOrder = (errors, orderedSectionsToFix, SECTION_FIELD_ORDER) => {
505
+ const sortedErrors = [];
506
+ for (const section of orderedSectionsToFix) {
507
+ const fieldsInSection = SECTION_FIELD_ORDER[section] || [];
508
+ for (const field of fieldsInSection) {
509
+ if (errors[field]) {
510
+ sortedErrors.push({
511
+ field,
512
+ ...errors[field]
513
+ });
514
+ }
515
+ }
516
+ }
517
+ return sortedErrors;
518
+ };
519
+
520
+ // src/presenters/presenters.js
521
+ var presenters = {
522
+ formatBackLink,
523
+ formatNumber,
524
+ formatDisplayAddress,
525
+ formatOriginalAddress,
526
+ formatChangedAddress,
527
+ formatDisplayAddresses,
528
+ sortErrorsBySectionOrder
529
+ };
121
530
  export {
122
531
  mappers,
532
+ presenters,
123
533
  schemas,
124
534
  utils
125
535
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/fcp-sfd-frontend-engine",
3
- "version": "0.2.12",
3
+ "version": "0.4.0",
4
4
  "description": "Shared frontend engine used by both the internal and external frontend applications.",
5
5
  "main": "./dist/index.cjs",
6
6
  "exports": {
@@ -47,4 +47,4 @@
47
47
  "dependencies": {
48
48
  "joi": "18.2.1"
49
49
  }
50
- }
50
+ }
@@ -0,0 +1,16 @@
1
+ export {
2
+ FIRST_NAME_MAX,
3
+ LAST_NAME_MAX,
4
+ MIDDLE_NAMES_MAX,
5
+ EMAIL_MAX,
6
+ PHONE_NUMBER_MIN,
7
+ PHONE_NUMBER_MAX,
8
+ MAX_AGE_YEARS,
9
+ ADDRESS_LINE_MAX,
10
+ TOWN_CITY_MAX,
11
+ COUNTY_MAX,
12
+ COUNTRY_MAX,
13
+ POSTCODE_MAX,
14
+ PHONE_NUMBER_PATTERN,
15
+ MONTH_MAP
16
+ } from './validation-fields-export.js'
@@ -0,0 +1,26 @@
1
+ export const MONTH_MAP = {
2
+ january: 1,
3
+ jan: 1,
4
+ february: 2,
5
+ feb: 2,
6
+ march: 3,
7
+ mar: 3,
8
+ april: 4,
9
+ apr: 4,
10
+ may: 5,
11
+ june: 6,
12
+ jun: 6,
13
+ july: 7,
14
+ jul: 7,
15
+ august: 8,
16
+ aug: 8,
17
+ september: 9,
18
+ sep: 9,
19
+ sept: 9,
20
+ october: 10,
21
+ oct: 10,
22
+ november: 11,
23
+ nov: 11,
24
+ december: 12,
25
+ dec: 12
26
+ }
@@ -0,0 +1 @@
1
+ export const PHONE_NUMBER_PATTERN = /^\+?[0-9 ()]+$/
@@ -0,0 +1,17 @@
1
+ export {
2
+ FIRST_NAME_MAX,
3
+ LAST_NAME_MAX,
4
+ MIDDLE_NAMES_MAX,
5
+ EMAIL_MAX,
6
+ PHONE_NUMBER_MIN,
7
+ PHONE_NUMBER_MAX,
8
+ MAX_AGE_YEARS,
9
+ ADDRESS_LINE_MAX,
10
+ TOWN_CITY_MAX,
11
+ COUNTY_MAX,
12
+ COUNTRY_MAX,
13
+ POSTCODE_MAX
14
+ } from './validation-fields.js'
15
+
16
+ export { PHONE_NUMBER_PATTERN } from './patterns.js'
17
+ export { MONTH_MAP } from './month-map.js'
@@ -0,0 +1,14 @@
1
+ export const FIRST_NAME_MAX = 100
2
+ export const LAST_NAME_MAX = 100
3
+ export const MIDDLE_NAMES_MAX = 100
4
+ export const EMAIL_MAX = 254
5
+ export const PHONE_NUMBER_MIN = 10
6
+ export const PHONE_NUMBER_MAX = 50
7
+ export const MAX_AGE_YEARS = 120
8
+
9
+ // Address
10
+ export const ADDRESS_LINE_MAX = 100
11
+ export const TOWN_CITY_MAX = 60
12
+ export const COUNTY_MAX = 60
13
+ export const COUNTRY_MAX = 60
14
+ export const POSTCODE_MAX = 8
package/src/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { mappers } from './mappers/mappers.js'
2
2
  export { schemas } from './schemas/schemas.js'
3
3
  export { utils } from './utils/utils.js'
4
+ export { presenters } from './presenters/presenters.js'