@defra/fcp-sfd-frontend-engine 0.18.0 → 0.19.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
@@ -184,338 +184,6 @@ var mapBusinessDetails = (value) => {
184
184
  };
185
185
  };
186
186
 
187
- // src/mappers/mappers.js
188
- var mappers = {
189
- personalBusinessDetails: mapPersonalBusinessDetails,
190
- address: mapAddress,
191
- customerName: mapCustomerName,
192
- businessDetails: mapBusinessDetails
193
- };
194
-
195
- // src/mutations/business/update-business-email.js
196
- var updateBusinessEmailMutation = `
197
- mutation Mutation($input: UpdateBusinessEmailInput!) {
198
- updateBusinessEmail(input: $input) {
199
- business {
200
- info {
201
- email {
202
- address
203
- }
204
- }
205
- }
206
- success
207
- }
208
- }
209
- `;
210
-
211
- // src/mutations/business/update-business-name.js
212
- var updateBusinessNameMutation = `
213
- mutation UpdateBusinessName($input: UpdateBusinessNameInput!) {
214
- updateBusinessName(input: $input) {
215
- business {
216
- info {
217
- name
218
- }
219
- }
220
- success
221
- }
222
- }
223
- `;
224
-
225
- // src/mutations/personal/update-customer-name.js
226
- var updateCustomerNameMutation = `
227
- mutation UpdateCustomerName($input: UpdateCustomerNameInput!) {
228
- updateCustomerName(input: $input) {
229
- customer {
230
- info {
231
- name {
232
- first
233
- last
234
- middle
235
- }
236
- }
237
- }
238
- }
239
- }
240
- `;
241
-
242
- // src/mutations/personal/update-customer-dob.js
243
- var updateCustomerDobMutation = `
244
- mutation UpdateCustomerDateOfBirth($input: UpdateCustomerDateOfBirthInput!) {
245
- updateCustomerDateOfBirth(input: $input) {
246
- customer {
247
- info {
248
- dateOfBirth
249
- }
250
- }
251
- }
252
- }
253
- `;
254
-
255
- // src/mutations/personal/update-customer-phone.js
256
- var updateCustomerPhoneMutation = `
257
- mutation UpdateCustomerPhone($input: UpdateCustomerPhoneInput!) {
258
- updateCustomerPhone(input: $input) {
259
- customer {
260
- info {
261
- phone {
262
- landline
263
- mobile
264
- }
265
- }
266
- }
267
- }
268
- }
269
- `;
270
-
271
- // src/mutations/personal/update-customer-email.js
272
- var updateCustomerEmailMutation = `
273
- mutation UpdateCustomerEmail($input: UpdateCustomerEmailInput!) {
274
- updateCustomerEmail(input: $input) {
275
- customer {
276
- info {
277
- email {
278
- address
279
- }
280
- }
281
- }
282
- }
283
- }
284
- `;
285
-
286
- // src/mutations/mutations.js
287
- var mutations = {
288
- updateBusinessEmail: updateBusinessEmailMutation,
289
- updateBusinessName: updateBusinessNameMutation,
290
- updateCustomerName: updateCustomerNameMutation,
291
- updateCustomerDob: updateCustomerDobMutation,
292
- updateCustomerPhone: updateCustomerPhoneMutation,
293
- updateCustomerEmail: updateCustomerEmailMutation
294
- };
295
-
296
- // src/presenters/base-presenter.js
297
- var BACK_LINK_DISPLAY_MAX = 50;
298
- var formatBackLink = (businessName) => {
299
- if (businessName.length > BACK_LINK_DISPLAY_MAX) {
300
- return `Back to ${businessName.slice(0, BACK_LINK_DISPLAY_MAX)}\u2026`;
301
- }
302
- return `Back to ${businessName}`;
303
- };
304
- var formatNumber = (payloadNumber, changedNumber, originalNumber) => {
305
- if (payloadNumber !== void 0) {
306
- return payloadNumber;
307
- }
308
- if (changedNumber !== void 0) {
309
- return changedNumber;
310
- }
311
- return originalNumber;
312
- };
313
- var formatDatePart = (changed, original) => {
314
- return changed ?? (original == null ? void 0 : original.toString()) ?? "";
315
- };
316
- var formatDateInputValues = (payloadDob, changedDob, originalDob) => {
317
- if (payloadDob) {
318
- return {
319
- day: payloadDob.day ?? "",
320
- month: payloadDob.month ?? "",
321
- year: payloadDob.year ?? ""
322
- };
323
- }
324
- return {
325
- day: formatDatePart(changedDob == null ? void 0 : changedDob.day, originalDob == null ? void 0 : originalDob.day),
326
- month: formatDatePart(changedDob == null ? void 0 : changedDob.month, originalDob == null ? void 0 : originalDob.month),
327
- year: formatDatePart(changedDob == null ? void 0 : changedDob.year, originalDob == null ? void 0 : originalDob.year)
328
- };
329
- };
330
- var sortErrorsBySectionOrder = (errors, orderedSectionsToFix, SECTION_FIELD_ORDER) => {
331
- const sortedErrors = [];
332
- for (const section of orderedSectionsToFix) {
333
- const fieldsInSection = SECTION_FIELD_ORDER[section] || [];
334
- for (const field of fieldsInSection) {
335
- if (errors[field]) {
336
- sortedErrors.push({
337
- field,
338
- ...errors[field]
339
- });
340
- }
341
- }
342
- }
343
- return sortedErrors;
344
- };
345
-
346
- // src/presenters/address-presenter.js
347
- var addressChangeLink = (postcodeLookup, context) => {
348
- if (context === "business") {
349
- if (postcodeLookup) {
350
- return "/business-address-change";
351
- }
352
- return "/business-address-enter";
353
- }
354
- if (context === "personal") {
355
- if (postcodeLookup) {
356
- return "/account-address-change";
357
- }
358
- return "/account-address-enter";
359
- }
360
- return null;
361
- };
362
- var addressBackLink = (postcodeLookup, context) => {
363
- if (context === "business") {
364
- if (postcodeLookup) {
365
- return { href: "/business-address-select" };
366
- }
367
- return { href: "/business-address-enter" };
368
- }
369
- if (context === "personal") {
370
- if (postcodeLookup) {
371
- return { href: "/account-address-select" };
372
- }
373
- return { href: "/account-address-enter" };
374
- }
375
- return null;
376
- };
377
- var buildAddressLine = (parts) => {
378
- return parts.filter(Boolean).join(", ") || null;
379
- };
380
- var buildStreetLine = (buildingRange, street) => {
381
- return [buildingRange, street].filter(Boolean).join(" ") || null;
382
- };
383
- var formatLookupAddress = (lookup, city, country, postcode) => ({
384
- address1: buildAddressLine([lookup.pafOrganisationName, lookup.flatName, lookup.buildingName]),
385
- address2: buildStreetLine(lookup.buildingNumberRange, lookup.street),
386
- address3: buildAddressLine([lookup.doubleDependentLocality, lookup.dependentLocality]),
387
- county: lookup.county ?? null,
388
- city: city ?? null,
389
- country: country ?? null,
390
- postcode: postcode ?? null
391
- });
392
- var formatManualAddress = (manual, city, country, postcode) => ({
393
- address1: manual.line1 ?? null,
394
- address2: manual.line2 ?? null,
395
- address3: manual.line3 ?? null,
396
- city: city ?? null,
397
- county: manual.line4 ?? null,
398
- country: country ?? null,
399
- postcode: postcode ?? null
400
- });
401
- var formatDisplayAddress = (address) => {
402
- const { lookup, manual, postcode, country, city } = address;
403
- let addressLines = [];
404
- if (lookup.uprn) {
405
- const buildingAndStreet = [
406
- lookup.buildingNumberRange,
407
- lookup.street
408
- ].filter(Boolean).join(" ");
409
- addressLines = [
410
- lookup.pafOrganisationName,
411
- lookup.flatName,
412
- lookup.buildingName,
413
- buildingAndStreet,
414
- lookup.doubleDependentLocality,
415
- lookup.dependentLocality,
416
- city,
417
- lookup.county
418
- ];
419
- } else {
420
- addressLines = [
421
- manual.line1,
422
- manual.line2,
423
- manual.line3,
424
- city,
425
- manual.line4,
426
- // County
427
- manual.line5
428
- ];
429
- }
430
- return [
431
- ...addressLines.filter(Boolean),
432
- postcode,
433
- country
434
- ];
435
- };
436
- var formatOriginalAddress = (originalAddress) => {
437
- const { lookup, manual, city, country, postcode } = originalAddress;
438
- return lookup.uprn ? formatLookupAddress(lookup, city, country, postcode) : formatManualAddress(manual, city, country, postcode);
439
- };
440
- var formatChangedAddress = (changeBusinessAddress) => {
441
- if (!changeBusinessAddress.uprn) {
442
- return changeBusinessAddress;
443
- }
444
- const {
445
- pafOrganisationName,
446
- flatName,
447
- buildingName,
448
- buildingNumberRange,
449
- street,
450
- doubleDependentLocality,
451
- dependentLocality,
452
- city,
453
- county,
454
- country,
455
- postcode
456
- } = changeBusinessAddress;
457
- return {
458
- address1: buildAddressLine([pafOrganisationName, flatName, buildingName]),
459
- address2: buildStreetLine(buildingNumberRange, street),
460
- address3: buildAddressLine([doubleDependentLocality, dependentLocality]),
461
- city: city ?? null,
462
- county: county ?? null,
463
- country: country ?? null,
464
- postcode: postcode ?? null
465
- };
466
- };
467
- var formatDisplayAddresses = (addresses, previouslyPickedAddress) => {
468
- const displayAddresses = addresses.map((address) => ({
469
- value: `${address.uprn}${address.displayAddress}`,
470
- text: address.displayAddress,
471
- selected: (previouslyPickedAddress == null ? void 0 : previouslyPickedAddress.uprn) === address.uprn && (previouslyPickedAddress == null ? void 0 : previouslyPickedAddress.displayAddress) === address.displayAddress
472
- }));
473
- const hasSelectedAddress = displayAddresses.some((addr) => addr.selected);
474
- const text = addresses.length === 1 ? "1 address found" : `${addresses.length} addresses found`;
475
- displayAddresses.unshift({
476
- value: "display",
477
- text,
478
- selected: !hasSelectedAddress
479
- });
480
- return displayAddresses;
481
- };
482
-
483
- // src/presenters/business-details-presenter.js
484
- var getActionText = (value) => {
485
- return value ? "Change" : "Add";
486
- };
487
- var formatCph = (countyParishHoldings) => {
488
- return (countyParishHoldings || []).filter((cph) => cph == null ? void 0 : cph.cphNumber).map((cph) => cph.cphNumber);
489
- };
490
- var formatCphText = (count) => {
491
- return `County Parish Holding (CPH) number${count !== 1 ? "s" : ""}`;
492
- };
493
- var formatBusinessAddress = (businessAddress) => {
494
- var _a, _b;
495
- if (((_a = businessAddress == null ? void 0 : businessAddress.lookup) == null ? void 0 : _a.uprn) || ((_b = businessAddress == null ? void 0 : businessAddress.manual) == null ? void 0 : _b.line1)) {
496
- return formatDisplayAddress(businessAddress);
497
- }
498
- return [];
499
- };
500
-
501
- // src/presenters/presenters.js
502
- var presenters = {
503
- formatBackLink,
504
- formatNumber,
505
- formatDateInputValues,
506
- formatDisplayAddress,
507
- formatOriginalAddress,
508
- formatChangedAddress,
509
- formatDisplayAddresses,
510
- sortErrorsBySectionOrder,
511
- getActionText,
512
- formatCph,
513
- formatCphText,
514
- formatBusinessAddress,
515
- addressBackLink,
516
- addressChangeLink
517
- };
518
-
519
187
  // src/utils/joi.js
520
188
  import BaseJoi from "joi";
521
189
  var Joi = BaseJoi.extend((joi) => ({
@@ -590,332 +258,731 @@ var businessNameSchema = Joi.object({
590
258
  })
591
259
  });
592
260
 
593
- // src/schemas/business/business-email-schema.js
594
- var businessEmailSchema = Joi.object({
595
- businessEmail: Joi.string().required().max(EMAIL_MAX).email({
261
+ // src/schemas/business/business-email-schema.js
262
+ var businessEmailSchema = Joi.object({
263
+ businessEmail: Joi.string().required().max(EMAIL_MAX).email({
264
+ minDomainSegments: 2,
265
+ tlds: {
266
+ allow: true
267
+ }
268
+ }).messages({
269
+ "string.max": `Business email address must be ${EMAIL_MAX} characters or less`,
270
+ "string.empty": "Enter business email address",
271
+ "string.email": "Enter an email address, like name@example.com",
272
+ "string.noControlChars": "Business email address must not contain invalid characters"
273
+ })
274
+ });
275
+
276
+ // src/schemas/business/business-phone-schema.js
277
+ var businessPhoneSchema = Joi.object({
278
+ businessTelephone: Joi.string().trim().empty("").min(PHONE_NUMBER_MIN).max(PHONE_NUMBER_MAX).pattern(PHONE_NUMBER_PATTERN).messages({
279
+ "string.min": `Business telephone number must be ${PHONE_NUMBER_MIN} characters or more`,
280
+ "string.max": `Business telephone number must be ${PHONE_NUMBER_MAX} characters or less`,
281
+ "string.pattern.base": "Business telephone number must only include numbers 0 to 9 and special characters such as spaces, brackets and +",
282
+ "string.noControlChars": "Business telephone number must not contain invalid characters"
283
+ }),
284
+ businessMobile: Joi.string().trim().empty("").min(PHONE_NUMBER_MIN).max(PHONE_NUMBER_MAX).pattern(PHONE_NUMBER_PATTERN).messages({
285
+ "string.min": `Business mobile phone number must be ${PHONE_NUMBER_MIN} characters or more`,
286
+ "string.max": `Business mobile phone number must be ${PHONE_NUMBER_MAX} characters or less`,
287
+ "string.pattern.base": "Business mobile phone number must only include numbers 0 to 9 and special characters such as spaces, brackets and +",
288
+ "string.noControlChars": "Business mobile phone number must not contain invalid characters"
289
+ })
290
+ }).or("businessTelephone", "businessMobile").messages({
291
+ "object.missing": "Enter at least one phone number"
292
+ });
293
+
294
+ // src/schemas/business/business-vat-schema.js
295
+ var businessVatSchema = Joi.object({
296
+ vatNumber: Joi.string().pattern(/^\d{9}$/).allow("").optional().messages({
297
+ "string.pattern.base": "Enter a VAT registration number, like 123456789",
298
+ "string.noControlChars": "VAT registration number must not contain invalid characters"
299
+ })
300
+ });
301
+
302
+ // src/schemas/business/business-vat-change-schema.js
303
+ var businessVatChangeSchema = Joi.object({
304
+ vatNumber: Joi.string().pattern(/^\d{9}$/).required().messages({
305
+ "string.pattern.base": "Enter a VAT registration number, like 123456789",
306
+ "string.empty": "Enter a VAT registration number",
307
+ "any.required": "Enter a VAT registration number",
308
+ "string.noControlChars": "VAT registration number must not contain invalid characters"
309
+ })
310
+ });
311
+
312
+ // src/schemas/business/business-vat-remove-schema.js
313
+ var businessVatRemoveSchema = Joi.object({
314
+ confirmRemove: Joi.string().valid("yes", "no").required().messages({
315
+ "any.required": "Select yes if you want to remove your VAT registration number",
316
+ "any.only": "Select yes if you want to remove your VAT registration number"
317
+ })
318
+ });
319
+
320
+ // src/schemas/business/business-schemas.js
321
+ var businessSchemas = {
322
+ sbi: businessSbiSchema,
323
+ details: {
324
+ name: businessNameSchema,
325
+ address: addressSchema,
326
+ phone: businessPhoneSchema,
327
+ email: businessEmailSchema,
328
+ vat: businessVatSchema
329
+ },
330
+ vat: {
331
+ change: businessVatChangeSchema,
332
+ remove: businessVatRemoveSchema
333
+ }
334
+ };
335
+
336
+ // src/schemas/customer/customer-crn-schema.js
337
+ var customerCrnSchema = Joi.object({
338
+ crn: Joi.string().pattern(/^\d{10}$/).allow("").optional().messages({
339
+ "string.pattern.base": "Enter the full CRN",
340
+ "string.noControlChars": "CRN must not contain invalid characters"
341
+ })
342
+ });
343
+
344
+ // src/schemas/customer/customer-schemas.js
345
+ var customerSchemas = {
346
+ crn: customerCrnSchema
347
+ };
348
+
349
+ // src/schemas/personal/personal-name-schema.js
350
+ var personalNameSchema = Joi.object({
351
+ first: Joi.string().trim().required().max(FIRST_NAME_MAX).messages({
352
+ "string.empty": "Enter first name",
353
+ "string.max": `First name must be ${FIRST_NAME_MAX} characters or less`,
354
+ "any.required": "Enter first name",
355
+ "string.noControlChars": "First name must not contain invalid characters"
356
+ }),
357
+ last: Joi.string().trim().required().max(LAST_NAME_MAX).messages({
358
+ "string.empty": "Enter last name",
359
+ "string.max": `Last name must be ${LAST_NAME_MAX} characters or less`,
360
+ "any.required": "Enter last name",
361
+ "string.noControlChars": "Last name must not contain invalid characters"
362
+ }),
363
+ middle: Joi.string().trim().allow("").max(MIDDLE_NAMES_MAX).messages({
364
+ "string.max": `Middle names must be ${MIDDLE_NAMES_MAX} characters or less`,
365
+ "string.noControlChars": "Middle names must not contain invalid characters"
366
+ })
367
+ });
368
+
369
+ // src/schemas/personal/personal-dob-schema.js
370
+ var personalDobSchema = Joi.object({
371
+ day: Joi.string().allow(""),
372
+ month: Joi.string().allow(""),
373
+ year: Joi.string().allow("")
374
+ }).custom((value, helpers) => {
375
+ const { day, month, year } = value;
376
+ const missingFieldsError = checkMissingFields(day, month, year, helpers);
377
+ if (missingFieldsError) {
378
+ return missingFieldsError;
379
+ }
380
+ if (year && year.length !== 4) {
381
+ return makeError(helpers, "dob.yearLength", ["year"]);
382
+ }
383
+ const monthValue = getMonthNumber(month, helpers);
384
+ if (monthValue.isJoiError) {
385
+ return monthValue;
386
+ }
387
+ const fullDate = getFullDate(day, monthValue, year, helpers);
388
+ if (fullDate.isJoiError) {
389
+ return fullDate;
390
+ }
391
+ const today = /* @__PURE__ */ new Date();
392
+ today.setUTCHours(0, 0, 0, 0);
393
+ if (fullDate >= today) {
394
+ return makeError(helpers, "dob.future", ["day", "month", "year"]);
395
+ }
396
+ const tooOldError = checkNotTooOld(fullDate, helpers);
397
+ if (tooOldError) {
398
+ return tooOldError;
399
+ }
400
+ return value;
401
+ }).messages({
402
+ "dob.missingAll": "Enter your date of birth",
403
+ "dob.missingDay": "Date of birth must include a day",
404
+ "dob.missingMonth": "Date of birth must include a month",
405
+ "dob.missingYear": "Date of birth must include a year",
406
+ "dob.missingDayMonth": "Date of birth must include a day and month",
407
+ "dob.missingDayYear": "Date of birth must include a day and year",
408
+ "dob.missingMonthYear": "Date of birth must include a month and year",
409
+ "dob.yearLength": "Enter a year with 4 numbers, like 1975",
410
+ "dob.invalid": "Date of birth must be a real date",
411
+ "dob.future": "Date of birth must be in the past",
412
+ "dob.tooOld": "Date of birth must be on or after {{#oldest}}",
413
+ "string.noControlChars": "Date of birth must not contain invalid characters"
414
+ });
415
+ var checkNotTooOld = (fullDate, helpers) => {
416
+ const oldestDateAllowed = getOldestAllowedDate();
417
+ if (fullDate < oldestDateAllowed) {
418
+ const oldestDateString = oldestDateAllowed.toLocaleDateString("en-GB", {
419
+ day: "numeric",
420
+ month: "long",
421
+ year: "numeric"
422
+ });
423
+ const error = helpers.error("dob.tooOld", { oldest: oldestDateString });
424
+ error.path = ["day", "month", "year"];
425
+ return error;
426
+ }
427
+ return null;
428
+ };
429
+ var getOldestAllowedDate = () => {
430
+ const date = /* @__PURE__ */ new Date();
431
+ date.setUTCHours(0, 0, 0, 0);
432
+ date.setUTCFullYear(date.getUTCFullYear() - MAX_AGE_YEARS);
433
+ return date;
434
+ };
435
+ var getFullDate = (day, monthValue, year, helpers) => {
436
+ const dayValue = Number.parseInt(day, 10);
437
+ const yearValue = Number.parseInt(year, 10);
438
+ const date = new Date(Date.UTC(yearValue, monthValue - 1, dayValue));
439
+ if (date.getUTCFullYear() !== yearValue || date.getUTCMonth() + 1 !== monthValue || date.getUTCDate() !== dayValue) {
440
+ return makeError(helpers, "dob.invalid", ["day", "month", "year"]);
441
+ }
442
+ return date;
443
+ };
444
+ var getMonthNumber = (month, helpers) => {
445
+ if (Number.isNaN(Number(month))) {
446
+ const lower = month.toLowerCase();
447
+ const mapped = MONTH_MAP[lower];
448
+ if (!mapped) {
449
+ return makeError(helpers, "dob.invalid", ["month"]);
450
+ }
451
+ return mapped;
452
+ }
453
+ const parsedMonth = Number.parseInt(month, 10);
454
+ if (parsedMonth < 1 || parsedMonth > 12) {
455
+ return makeError(helpers, "dob.invalid", ["month"]);
456
+ }
457
+ return parsedMonth;
458
+ };
459
+ var checkMissingFields = (day, month, year, helpers) => {
460
+ if (!day && !month && !year) {
461
+ return makeError(helpers, "dob.missingAll", ["day", "month", "year"]);
462
+ }
463
+ if (!day && month && year) {
464
+ return makeError(helpers, "dob.missingDay", ["day"]);
465
+ }
466
+ if (day && !month && year) {
467
+ return makeError(helpers, "dob.missingMonth", ["month"]);
468
+ }
469
+ if (day && month && !year) {
470
+ return makeError(helpers, "dob.missingYear", ["year"]);
471
+ }
472
+ if (!day && !month && year) {
473
+ return makeError(helpers, "dob.missingDayMonth", ["day", "month"]);
474
+ }
475
+ if (!day && month && !year) {
476
+ return makeError(helpers, "dob.missingDayYear", ["day", "year"]);
477
+ }
478
+ if (day && !month && !year) {
479
+ return makeError(helpers, "dob.missingMonthYear", ["month", "year"]);
480
+ }
481
+ return null;
482
+ };
483
+ var makeError = (helpers, code, fields) => {
484
+ const error = helpers.error(code);
485
+ error.path = fields;
486
+ error.isJoiError = true;
487
+ return error;
488
+ };
489
+
490
+ // src/schemas/personal/personal-email-schema.js
491
+ var personalEmailSchema = Joi.object({
492
+ personalEmail: Joi.string().required().max(EMAIL_MAX).email({
596
493
  minDomainSegments: 2,
597
494
  tlds: {
598
- allow: true
495
+ allow: true,
496
+ min: 2
599
497
  }
600
498
  }).messages({
601
- "string.max": `Business email address must be ${EMAIL_MAX} characters or less`,
602
- "string.empty": "Enter business email address",
499
+ "string.max": `Email address must be ${EMAIL_MAX} characters or less`,
500
+ "string.empty": "Enter a personal email address",
603
501
  "string.email": "Enter an email address, like name@example.com",
604
- "string.noControlChars": "Business email address must not contain invalid characters"
502
+ "string.noControlChars": "Personal email address must not contain invalid characters"
605
503
  })
606
504
  });
607
505
 
608
- // src/schemas/business/business-phone-schema.js
609
- var businessPhoneSchema = Joi.object({
610
- businessTelephone: Joi.string().trim().empty("").min(PHONE_NUMBER_MIN).max(PHONE_NUMBER_MAX).pattern(PHONE_NUMBER_PATTERN).messages({
611
- "string.min": `Business telephone number must be ${PHONE_NUMBER_MIN} characters or more`,
612
- "string.max": `Business telephone number must be ${PHONE_NUMBER_MAX} characters or less`,
613
- "string.pattern.base": "Business telephone number must only include numbers 0 to 9 and special characters such as spaces, brackets and +",
614
- "string.noControlChars": "Business telephone number must not contain invalid characters"
506
+ // src/schemas/personal/personal-phone-schema.js
507
+ var personalPhoneSchema = Joi.object({
508
+ personalTelephone: Joi.string().trim().empty("").min(PHONE_NUMBER_MIN).max(PHONE_NUMBER_MAX).pattern(PHONE_NUMBER_PATTERN).messages({
509
+ "string.min": `Personal telephone number must be ${PHONE_NUMBER_MIN} characters or more`,
510
+ "string.max": `Personal telephone number must be ${PHONE_NUMBER_MAX} characters or less`,
511
+ "string.pattern.base": "Personal telephone number must only include numbers 0 to 9 and special characters such as spaces, brackets and +",
512
+ "string.noControlChars": "Personal telephone number must not contain invalid characters"
615
513
  }),
616
- businessMobile: Joi.string().trim().empty("").min(PHONE_NUMBER_MIN).max(PHONE_NUMBER_MAX).pattern(PHONE_NUMBER_PATTERN).messages({
617
- "string.min": `Business mobile phone number must be ${PHONE_NUMBER_MIN} characters or more`,
618
- "string.max": `Business mobile phone number must be ${PHONE_NUMBER_MAX} characters or less`,
619
- "string.pattern.base": "Business mobile phone number must only include numbers 0 to 9 and special characters such as spaces, brackets and +",
620
- "string.noControlChars": "Business mobile phone number must not contain invalid characters"
514
+ personalMobile: Joi.string().trim().empty("").min(PHONE_NUMBER_MIN).max(PHONE_NUMBER_MAX).pattern(PHONE_NUMBER_PATTERN).messages({
515
+ "string.min": `Personal mobile phone number must be ${PHONE_NUMBER_MIN} characters or more`,
516
+ "string.max": `Personal mobile phone number must be ${PHONE_NUMBER_MAX} characters or less`,
517
+ "string.pattern.base": "Personal mobile phone number must only include numbers 0 to 9 and special characters such as spaces, brackets and +",
518
+ "string.noControlChars": "Personal mobile phone number must not contain invalid characters"
621
519
  })
622
- }).or("businessTelephone", "businessMobile").messages({
520
+ }).or("personalTelephone", "personalMobile").messages({
623
521
  "object.missing": "Enter at least one phone number"
624
522
  });
625
523
 
626
- // src/schemas/business/business-vat-schema.js
627
- var businessVatSchema = Joi.object({
628
- vatNumber: Joi.string().pattern(/^\d{9}$/).allow("").optional().messages({
629
- "string.pattern.base": "Enter a VAT registration number, like 123456789",
630
- "string.noControlChars": "VAT registration number must not contain invalid characters"
631
- })
632
- });
524
+ // src/schemas/personal/personal-schemas.js
525
+ var personalSchemas = {
526
+ name: personalNameSchema,
527
+ dob: personalDobSchema,
528
+ address: addressSchema,
529
+ phone: personalPhoneSchema,
530
+ email: personalEmailSchema
531
+ };
633
532
 
634
- // src/schemas/business/business-vat-change-schema.js
635
- var businessVatChangeSchema = Joi.object({
636
- vatNumber: Joi.string().pattern(/^\d{9}$/).required().messages({
637
- "string.pattern.base": "Enter a VAT registration number, like 123456789",
638
- "string.empty": "Enter a VAT registration number",
639
- "any.required": "Enter a VAT registration number",
640
- "string.noControlChars": "VAT registration number must not contain invalid characters"
533
+ // src/schemas/os-places/address-lookup-schema.js
534
+ var addressLookupSchema = Joi.object({
535
+ properties: Joi.object({
536
+ UPRN: Joi.string().required(),
537
+ ADDRESS: Joi.string().required(),
538
+ ORGANISATION_NAME: Joi.string().allow(null),
539
+ DEPARTMENT_NAME: Joi.string().allow(null),
540
+ SUB_BUILDING_NAME: Joi.string().allow(null),
541
+ BUILDING_NAME: Joi.string().allow(null),
542
+ BUILDING_NUMBER: Joi.string().allow(null),
543
+ DEPENDENT_THOROUGHFARE_NAME: Joi.string().allow(null),
544
+ THOROUGHFARE_NAME: Joi.string().allow(null),
545
+ DOUBLE_DEPENDENT_LOCALITY: Joi.string().allow(null),
546
+ DEPENDENT_LOCALITY: Joi.string().allow(null),
547
+ POST_TOWN: Joi.string().required(),
548
+ POSTCODE: Joi.string().required(),
549
+ LOCAL_CUSTODIAN_CODE_DESCRIPTION: Joi.string().allow(null),
550
+ COUNTRY_CODE: Joi.string().required()
551
+ }).unknown(true).required()
552
+ }).unknown(true);
553
+
554
+ // src/schemas/os-places/addresses-schema.js
555
+ var CHOOSE_ADDRESS_ERROR = "Choose an address";
556
+ var addressesSchema = Joi.object({
557
+ addresses: Joi.string().invalid("display").required().messages({
558
+ "any.required": CHOOSE_ADDRESS_ERROR,
559
+ "string.empty": CHOOSE_ADDRESS_ERROR,
560
+ "any.invalid": CHOOSE_ADDRESS_ERROR
641
561
  })
642
562
  });
643
563
 
644
- // src/schemas/business/business-vat-remove-schema.js
645
- var businessVatRemoveSchema = Joi.object({
646
- confirmRemove: Joi.string().valid("yes", "no").required().messages({
647
- "any.required": "Select yes if you want to remove your VAT registration number",
648
- "any.only": "Select yes if you want to remove your VAT registration number"
564
+ // src/schemas/os-places/uk-postcode-schema.js
565
+ var ukPostcodeSchema = Joi.object({
566
+ postcode: Joi.string().trim().required().uppercase().max(POSTCODE_MAX).pattern(/^([A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}|GIR ?0A{2})$/).messages({
567
+ "any.required": "Enter a postcode",
568
+ "string.empty": "Enter a postcode",
569
+ "string.max": `Postal code must be ${POSTCODE_MAX} characters or less`,
570
+ "string.pattern.base": "Enter a full UK postcode, like AA3 1AB"
649
571
  })
650
572
  });
651
573
 
652
- // src/schemas/business/business-schemas.js
653
- var businessSchemas = {
654
- sbi: businessSbiSchema,
655
- details: {
656
- name: businessNameSchema,
657
- address: addressSchema,
658
- phone: businessPhoneSchema,
659
- email: businessEmailSchema,
660
- vat: businessVatSchema
661
- },
662
- vat: {
663
- change: businessVatChangeSchema,
664
- remove: businessVatRemoveSchema
574
+ // src/schemas/os-places/os-places-schemas.js
575
+ var osPlacesSchemas = {
576
+ addressLookup: addressLookupSchema,
577
+ addresses: addressesSchema,
578
+ ukPostcode: ukPostcodeSchema
579
+ };
580
+
581
+ // src/schemas/schemas.js
582
+ var schemas = {
583
+ business: businessSchemas,
584
+ customer: customerSchemas,
585
+ personal: personalSchemas,
586
+ osPlaces: osPlacesSchemas
587
+ };
588
+
589
+ // src/constants/country-names.js
590
+ var COUNTRY_NAMES = {
591
+ E: "ENGLAND",
592
+ W: "WALES",
593
+ S: "SCOTLAND",
594
+ N: "NORTHERN IRELAND",
595
+ L: "CHANNEL ISLANDS",
596
+ M: "ISLE OF MAN"
597
+ };
598
+
599
+ // src/mappers/address-lookup-mapper.js
600
+ var addressLookupMapper = (addresses) => {
601
+ if (!Array.isArray(addresses)) {
602
+ return [];
603
+ }
604
+ return addresses.map((address) => {
605
+ const { error } = schemas.osPlaces.addressLookup.validate(address);
606
+ if (error) {
607
+ return null;
608
+ }
609
+ const {
610
+ UPRN,
611
+ ADDRESS,
612
+ PO_BOX_NUMBER,
613
+ ORGANISATION_NAME,
614
+ DEPARTMENT_NAME,
615
+ SUB_BUILDING_NAME,
616
+ BUILDING_NAME,
617
+ BUILDING_NUMBER,
618
+ DEPENDENT_THOROUGHFARE_NAME,
619
+ THOROUGHFARE_NAME,
620
+ DOUBLE_DEPENDENT_LOCALITY,
621
+ DEPENDENT_LOCALITY,
622
+ POST_TOWN,
623
+ POSTCODE,
624
+ LOCAL_CUSTODIAN_CODE_DESCRIPTION,
625
+ COUNTRY_CODE
626
+ } = address.properties;
627
+ const buildingName = PO_BOX_NUMBER ? `PO BOX ${PO_BOX_NUMBER}` : BUILDING_NAME || null;
628
+ return {
629
+ displayAddress: ADDRESS,
630
+ pafOrganisationName: filterAndJoin([ORGANISATION_NAME, DEPARTMENT_NAME]),
631
+ flatName: SUB_BUILDING_NAME ?? null,
632
+ buildingName,
633
+ buildingNumberRange: BUILDING_NUMBER ?? null,
634
+ street: filterAndJoin([DEPENDENT_THOROUGHFARE_NAME, THOROUGHFARE_NAME]),
635
+ dependentLocality: DEPENDENT_LOCALITY ?? null,
636
+ doubleDependentLocality: DOUBLE_DEPENDENT_LOCALITY ?? null,
637
+ city: POST_TOWN,
638
+ county: formatCounty(LOCAL_CUSTODIAN_CODE_DESCRIPTION, POST_TOWN),
639
+ postcode: POSTCODE,
640
+ country: COUNTRY_NAMES[COUNTRY_CODE] ?? null,
641
+ uprn: UPRN
642
+ };
643
+ }).filter(Boolean);
644
+ };
645
+ var formatCounty = (localCustodianCodeDescription, postTown) => {
646
+ if (localCustodianCodeDescription === "ORDNANCE SURVEY" || localCustodianCodeDescription === postTown) {
647
+ return null;
648
+ }
649
+ return localCustodianCodeDescription;
650
+ };
651
+ var filterAndJoin = (addressesProperties) => {
652
+ return addressesProperties.filter(Boolean).join(", ") || null;
653
+ };
654
+
655
+ // src/mappers/mappers.js
656
+ var mappers = {
657
+ personalBusinessDetails: mapPersonalBusinessDetails,
658
+ address: mapAddress,
659
+ customerName: mapCustomerName,
660
+ businessDetails: mapBusinessDetails,
661
+ addressLookup: addressLookupMapper
662
+ };
663
+
664
+ // src/mutations/business/update-business-email.js
665
+ var updateBusinessEmailMutation = `
666
+ mutation Mutation($input: UpdateBusinessEmailInput!) {
667
+ updateBusinessEmail(input: $input) {
668
+ business {
669
+ info {
670
+ email {
671
+ address
672
+ }
673
+ }
674
+ }
675
+ success
676
+ }
677
+ }
678
+ `;
679
+
680
+ // src/mutations/business/update-business-name.js
681
+ var updateBusinessNameMutation = `
682
+ mutation UpdateBusinessName($input: UpdateBusinessNameInput!) {
683
+ updateBusinessName(input: $input) {
684
+ business {
685
+ info {
686
+ name
687
+ }
688
+ }
689
+ success
690
+ }
691
+ }
692
+ `;
693
+
694
+ // src/mutations/personal/update-customer-name.js
695
+ var updateCustomerNameMutation = `
696
+ mutation UpdateCustomerName($input: UpdateCustomerNameInput!) {
697
+ updateCustomerName(input: $input) {
698
+ customer {
699
+ info {
700
+ name {
701
+ first
702
+ last
703
+ middle
704
+ }
705
+ }
706
+ }
707
+ }
708
+ }
709
+ `;
710
+
711
+ // src/mutations/personal/update-customer-dob.js
712
+ var updateCustomerDobMutation = `
713
+ mutation UpdateCustomerDateOfBirth($input: UpdateCustomerDateOfBirthInput!) {
714
+ updateCustomerDateOfBirth(input: $input) {
715
+ customer {
716
+ info {
717
+ dateOfBirth
718
+ }
719
+ }
720
+ }
721
+ }
722
+ `;
723
+
724
+ // src/mutations/personal/update-customer-phone.js
725
+ var updateCustomerPhoneMutation = `
726
+ mutation UpdateCustomerPhone($input: UpdateCustomerPhoneInput!) {
727
+ updateCustomerPhone(input: $input) {
728
+ customer {
729
+ info {
730
+ phone {
731
+ landline
732
+ mobile
733
+ }
734
+ }
735
+ }
736
+ }
665
737
  }
666
- };
738
+ `;
667
739
 
668
- // src/schemas/customer/customer-crn-schema.js
669
- var customerCrnSchema = Joi.object({
670
- crn: Joi.string().pattern(/^\d{10}$/).allow("").optional().messages({
671
- "string.pattern.base": "Enter the full CRN",
672
- "string.noControlChars": "CRN must not contain invalid characters"
673
- })
674
- });
740
+ // src/mutations/personal/update-customer-email.js
741
+ var updateCustomerEmailMutation = `
742
+ mutation UpdateCustomerEmail($input: UpdateCustomerEmailInput!) {
743
+ updateCustomerEmail(input: $input) {
744
+ customer {
745
+ info {
746
+ email {
747
+ address
748
+ }
749
+ }
750
+ }
751
+ }
752
+ }
753
+ `;
675
754
 
676
- // src/schemas/customer/customer-schemas.js
677
- var customerSchemas = {
678
- crn: customerCrnSchema
755
+ // src/mutations/mutations.js
756
+ var mutations = {
757
+ updateBusinessEmail: updateBusinessEmailMutation,
758
+ updateBusinessName: updateBusinessNameMutation,
759
+ updateCustomerName: updateCustomerNameMutation,
760
+ updateCustomerDob: updateCustomerDobMutation,
761
+ updateCustomerPhone: updateCustomerPhoneMutation,
762
+ updateCustomerEmail: updateCustomerEmailMutation
679
763
  };
680
764
 
681
- // src/schemas/personal/personal-name-schema.js
682
- var personalNameSchema = Joi.object({
683
- first: Joi.string().trim().required().max(FIRST_NAME_MAX).messages({
684
- "string.empty": "Enter first name",
685
- "string.max": `First name must be ${FIRST_NAME_MAX} characters or less`,
686
- "any.required": "Enter first name",
687
- "string.noControlChars": "First name must not contain invalid characters"
688
- }),
689
- last: Joi.string().trim().required().max(LAST_NAME_MAX).messages({
690
- "string.empty": "Enter last name",
691
- "string.max": `Last name must be ${LAST_NAME_MAX} characters or less`,
692
- "any.required": "Enter last name",
693
- "string.noControlChars": "Last name must not contain invalid characters"
694
- }),
695
- middle: Joi.string().trim().allow("").max(MIDDLE_NAMES_MAX).messages({
696
- "string.max": `Middle names must be ${MIDDLE_NAMES_MAX} characters or less`,
697
- "string.noControlChars": "Middle names must not contain invalid characters"
698
- })
699
- });
700
-
701
- // src/schemas/personal/personal-dob-schema.js
702
- var personalDobSchema = Joi.object({
703
- day: Joi.string().allow(""),
704
- month: Joi.string().allow(""),
705
- year: Joi.string().allow("")
706
- }).custom((value, helpers) => {
707
- const { day, month, year } = value;
708
- const missingFieldsError = checkMissingFields(day, month, year, helpers);
709
- if (missingFieldsError) {
710
- return missingFieldsError;
765
+ // src/presenters/base-presenter.js
766
+ var BACK_LINK_DISPLAY_MAX = 50;
767
+ var formatBackLink = (businessName) => {
768
+ if (businessName.length > BACK_LINK_DISPLAY_MAX) {
769
+ return `Back to ${businessName.slice(0, BACK_LINK_DISPLAY_MAX)}\u2026`;
711
770
  }
712
- if (year && year.length !== 4) {
713
- return makeError(helpers, "dob.yearLength", ["year"]);
771
+ return `Back to ${businessName}`;
772
+ };
773
+ var formatNumber = (payloadNumber, changedNumber, originalNumber) => {
774
+ if (payloadNumber !== void 0) {
775
+ return payloadNumber;
714
776
  }
715
- const monthValue = getMonthNumber(month, helpers);
716
- if (monthValue.isJoiError) {
717
- return monthValue;
777
+ if (changedNumber !== void 0) {
778
+ return changedNumber;
718
779
  }
719
- const fullDate = getFullDate(day, monthValue, year, helpers);
720
- if (fullDate.isJoiError) {
721
- return fullDate;
780
+ return originalNumber;
781
+ };
782
+ var formatDatePart = (changed, original) => {
783
+ return changed ?? (original == null ? void 0 : original.toString()) ?? "";
784
+ };
785
+ var formatDateInputValues = (payloadDob, changedDob, originalDob) => {
786
+ if (payloadDob) {
787
+ return {
788
+ day: payloadDob.day ?? "",
789
+ month: payloadDob.month ?? "",
790
+ year: payloadDob.year ?? ""
791
+ };
722
792
  }
723
- const today = /* @__PURE__ */ new Date();
724
- today.setUTCHours(0, 0, 0, 0);
725
- if (fullDate >= today) {
726
- return makeError(helpers, "dob.future", ["day", "month", "year"]);
793
+ return {
794
+ day: formatDatePart(changedDob == null ? void 0 : changedDob.day, originalDob == null ? void 0 : originalDob.day),
795
+ month: formatDatePart(changedDob == null ? void 0 : changedDob.month, originalDob == null ? void 0 : originalDob.month),
796
+ year: formatDatePart(changedDob == null ? void 0 : changedDob.year, originalDob == null ? void 0 : originalDob.year)
797
+ };
798
+ };
799
+ var sortErrorsBySectionOrder = (errors, orderedSectionsToFix, SECTION_FIELD_ORDER) => {
800
+ const sortedErrors = [];
801
+ for (const section of orderedSectionsToFix) {
802
+ const fieldsInSection = SECTION_FIELD_ORDER[section] || [];
803
+ for (const field of fieldsInSection) {
804
+ if (errors[field]) {
805
+ sortedErrors.push({
806
+ field,
807
+ ...errors[field]
808
+ });
809
+ }
810
+ }
727
811
  }
728
- const tooOldError = checkNotTooOld(fullDate, helpers);
729
- if (tooOldError) {
730
- return tooOldError;
812
+ return sortedErrors;
813
+ };
814
+
815
+ // src/presenters/address-presenter.js
816
+ var addressChangeLink = (postcodeLookup, context) => {
817
+ if (context === "business") {
818
+ if (postcodeLookup) {
819
+ return "/business-address-change";
820
+ }
821
+ return "/business-address-enter";
731
822
  }
732
- return value;
733
- }).messages({
734
- "dob.missingAll": "Enter your date of birth",
735
- "dob.missingDay": "Date of birth must include a day",
736
- "dob.missingMonth": "Date of birth must include a month",
737
- "dob.missingYear": "Date of birth must include a year",
738
- "dob.missingDayMonth": "Date of birth must include a day and month",
739
- "dob.missingDayYear": "Date of birth must include a day and year",
740
- "dob.missingMonthYear": "Date of birth must include a month and year",
741
- "dob.yearLength": "Enter a year with 4 numbers, like 1975",
742
- "dob.invalid": "Date of birth must be a real date",
743
- "dob.future": "Date of birth must be in the past",
744
- "dob.tooOld": "Date of birth must be on or after {{#oldest}}",
745
- "string.noControlChars": "Date of birth must not contain invalid characters"
746
- });
747
- var checkNotTooOld = (fullDate, helpers) => {
748
- const oldestDateAllowed = getOldestAllowedDate();
749
- if (fullDate < oldestDateAllowed) {
750
- const oldestDateString = oldestDateAllowed.toLocaleDateString("en-GB", {
751
- day: "numeric",
752
- month: "long",
753
- year: "numeric"
754
- });
755
- const error = helpers.error("dob.tooOld", { oldest: oldestDateString });
756
- error.path = ["day", "month", "year"];
757
- return error;
823
+ if (context === "personal") {
824
+ if (postcodeLookup) {
825
+ return "/account-address-change";
826
+ }
827
+ return "/account-address-enter";
758
828
  }
759
829
  return null;
760
830
  };
761
- var getOldestAllowedDate = () => {
762
- const date = /* @__PURE__ */ new Date();
763
- date.setUTCHours(0, 0, 0, 0);
764
- date.setUTCFullYear(date.getUTCFullYear() - MAX_AGE_YEARS);
765
- return date;
766
- };
767
- var getFullDate = (day, monthValue, year, helpers) => {
768
- const dayValue = Number.parseInt(day, 10);
769
- const yearValue = Number.parseInt(year, 10);
770
- const date = new Date(Date.UTC(yearValue, monthValue - 1, dayValue));
771
- if (date.getUTCFullYear() !== yearValue || date.getUTCMonth() + 1 !== monthValue || date.getUTCDate() !== dayValue) {
772
- return makeError(helpers, "dob.invalid", ["day", "month", "year"]);
831
+ var addressBackLink = (postcodeLookup, context) => {
832
+ if (context === "business") {
833
+ if (postcodeLookup) {
834
+ return { href: "/business-address-select" };
835
+ }
836
+ return { href: "/business-address-enter" };
773
837
  }
774
- return date;
775
- };
776
- var getMonthNumber = (month, helpers) => {
777
- if (Number.isNaN(Number(month))) {
778
- const lower = month.toLowerCase();
779
- const mapped = MONTH_MAP[lower];
780
- if (!mapped) {
781
- return makeError(helpers, "dob.invalid", ["month"]);
838
+ if (context === "personal") {
839
+ if (postcodeLookup) {
840
+ return { href: "/account-address-select" };
782
841
  }
783
- return mapped;
842
+ return { href: "/account-address-enter" };
784
843
  }
785
- const parsedMonth = Number.parseInt(month, 10);
786
- if (parsedMonth < 1 || parsedMonth > 12) {
787
- return makeError(helpers, "dob.invalid", ["month"]);
844
+ return null;
845
+ };
846
+ var buildAddressLine = (parts) => {
847
+ return parts.filter(Boolean).join(", ") || null;
848
+ };
849
+ var buildStreetLine = (buildingRange, street) => {
850
+ return [buildingRange, street].filter(Boolean).join(" ") || null;
851
+ };
852
+ var formatLookupAddress = (lookup, city, country, postcode) => ({
853
+ address1: buildAddressLine([lookup.pafOrganisationName, lookup.flatName, lookup.buildingName]),
854
+ address2: buildStreetLine(lookup.buildingNumberRange, lookup.street),
855
+ address3: buildAddressLine([lookup.doubleDependentLocality, lookup.dependentLocality]),
856
+ county: lookup.county ?? null,
857
+ city: city ?? null,
858
+ country: country ?? null,
859
+ postcode: postcode ?? null
860
+ });
861
+ var formatManualAddress = (manual, city, country, postcode) => ({
862
+ address1: manual.line1 ?? null,
863
+ address2: manual.line2 ?? null,
864
+ address3: manual.line3 ?? null,
865
+ city: city ?? null,
866
+ county: manual.line4 ?? null,
867
+ country: country ?? null,
868
+ postcode: postcode ?? null
869
+ });
870
+ var formatDisplayAddress = (address) => {
871
+ const { lookup, manual, postcode, country, city } = address;
872
+ let addressLines = [];
873
+ if (lookup.uprn) {
874
+ const buildingAndStreet = [
875
+ lookup.buildingNumberRange,
876
+ lookup.street
877
+ ].filter(Boolean).join(" ");
878
+ addressLines = [
879
+ lookup.pafOrganisationName,
880
+ lookup.flatName,
881
+ lookup.buildingName,
882
+ buildingAndStreet,
883
+ lookup.doubleDependentLocality,
884
+ lookup.dependentLocality,
885
+ city,
886
+ lookup.county
887
+ ];
888
+ } else {
889
+ addressLines = [
890
+ manual.line1,
891
+ manual.line2,
892
+ manual.line3,
893
+ city,
894
+ manual.line4,
895
+ // County
896
+ manual.line5
897
+ ];
788
898
  }
789
- return parsedMonth;
899
+ return [
900
+ ...addressLines.filter(Boolean),
901
+ postcode,
902
+ country
903
+ ];
790
904
  };
791
- var checkMissingFields = (day, month, year, helpers) => {
792
- if (!day && !month && !year) {
793
- return makeError(helpers, "dob.missingAll", ["day", "month", "year"]);
794
- }
795
- if (!day && month && year) {
796
- return makeError(helpers, "dob.missingDay", ["day"]);
797
- }
798
- if (day && !month && year) {
799
- return makeError(helpers, "dob.missingMonth", ["month"]);
800
- }
801
- if (day && month && !year) {
802
- return makeError(helpers, "dob.missingYear", ["year"]);
803
- }
804
- if (!day && !month && year) {
805
- return makeError(helpers, "dob.missingDayMonth", ["day", "month"]);
806
- }
807
- if (!day && month && !year) {
808
- return makeError(helpers, "dob.missingDayYear", ["day", "year"]);
809
- }
810
- if (day && !month && !year) {
811
- return makeError(helpers, "dob.missingMonthYear", ["month", "year"]);
905
+ var formatOriginalAddress = (originalAddress) => {
906
+ const { lookup, manual, city, country, postcode } = originalAddress;
907
+ return lookup.uprn ? formatLookupAddress(lookup, city, country, postcode) : formatManualAddress(manual, city, country, postcode);
908
+ };
909
+ var formatChangedAddress = (changeBusinessAddress) => {
910
+ if (!changeBusinessAddress.uprn) {
911
+ return changeBusinessAddress;
812
912
  }
813
- return null;
913
+ const {
914
+ pafOrganisationName,
915
+ flatName,
916
+ buildingName,
917
+ buildingNumberRange,
918
+ street,
919
+ doubleDependentLocality,
920
+ dependentLocality,
921
+ city,
922
+ county,
923
+ country,
924
+ postcode
925
+ } = changeBusinessAddress;
926
+ return {
927
+ address1: buildAddressLine([pafOrganisationName, flatName, buildingName]),
928
+ address2: buildStreetLine(buildingNumberRange, street),
929
+ address3: buildAddressLine([doubleDependentLocality, dependentLocality]),
930
+ city: city ?? null,
931
+ county: county ?? null,
932
+ country: country ?? null,
933
+ postcode: postcode ?? null
934
+ };
814
935
  };
815
- var makeError = (helpers, code, fields) => {
816
- const error = helpers.error(code);
817
- error.path = fields;
818
- error.isJoiError = true;
819
- return error;
936
+ var formatDisplayAddresses = (addresses, previouslyPickedAddress) => {
937
+ const displayAddresses = addresses.map((address) => ({
938
+ value: `${address.uprn}${address.displayAddress}`,
939
+ text: address.displayAddress,
940
+ selected: (previouslyPickedAddress == null ? void 0 : previouslyPickedAddress.uprn) === address.uprn && (previouslyPickedAddress == null ? void 0 : previouslyPickedAddress.displayAddress) === address.displayAddress
941
+ }));
942
+ const hasSelectedAddress = displayAddresses.some((addr) => addr.selected);
943
+ const text = addresses.length === 1 ? "1 address found" : `${addresses.length} addresses found`;
944
+ displayAddresses.unshift({
945
+ value: "display",
946
+ text,
947
+ selected: !hasSelectedAddress
948
+ });
949
+ return displayAddresses;
820
950
  };
821
951
 
822
- // src/schemas/personal/personal-email-schema.js
823
- var personalEmailSchema = Joi.object({
824
- personalEmail: Joi.string().required().max(EMAIL_MAX).email({
825
- minDomainSegments: 2,
826
- tlds: {
827
- allow: true,
828
- min: 2
829
- }
830
- }).messages({
831
- "string.max": `Email address must be ${EMAIL_MAX} characters or less`,
832
- "string.empty": "Enter a personal email address",
833
- "string.email": "Enter an email address, like name@example.com",
834
- "string.noControlChars": "Personal email address must not contain invalid characters"
835
- })
836
- });
837
-
838
- // src/schemas/personal/personal-phone-schema.js
839
- var personalPhoneSchema = Joi.object({
840
- personalTelephone: Joi.string().trim().empty("").min(PHONE_NUMBER_MIN).max(PHONE_NUMBER_MAX).pattern(PHONE_NUMBER_PATTERN).messages({
841
- "string.min": `Personal telephone number must be ${PHONE_NUMBER_MIN} characters or more`,
842
- "string.max": `Personal telephone number must be ${PHONE_NUMBER_MAX} characters or less`,
843
- "string.pattern.base": "Personal telephone number must only include numbers 0 to 9 and special characters such as spaces, brackets and +",
844
- "string.noControlChars": "Personal telephone number must not contain invalid characters"
845
- }),
846
- personalMobile: Joi.string().trim().empty("").min(PHONE_NUMBER_MIN).max(PHONE_NUMBER_MAX).pattern(PHONE_NUMBER_PATTERN).messages({
847
- "string.min": `Personal mobile phone number must be ${PHONE_NUMBER_MIN} characters or more`,
848
- "string.max": `Personal mobile phone number must be ${PHONE_NUMBER_MAX} characters or less`,
849
- "string.pattern.base": "Personal mobile phone number must only include numbers 0 to 9 and special characters such as spaces, brackets and +",
850
- "string.noControlChars": "Personal mobile phone number must not contain invalid characters"
851
- })
852
- }).or("personalTelephone", "personalMobile").messages({
853
- "object.missing": "Enter at least one phone number"
854
- });
855
-
856
- // src/schemas/personal/personal-schemas.js
857
- var personalSchemas = {
858
- name: personalNameSchema,
859
- dob: personalDobSchema,
860
- address: addressSchema,
861
- phone: personalPhoneSchema,
862
- email: personalEmailSchema
952
+ // src/presenters/business-details-presenter.js
953
+ var getActionText = (value) => {
954
+ return value ? "Change" : "Add";
863
955
  };
864
-
865
- // src/schemas/os-places/address-lookup-schema.js
866
- var addressLookupSchema = Joi.object({
867
- properties: Joi.object({
868
- UPRN: Joi.string().required(),
869
- ADDRESS: Joi.string().required(),
870
- ORGANISATION_NAME: Joi.string().allow(null),
871
- DEPARTMENT_NAME: Joi.string().allow(null),
872
- SUB_BUILDING_NAME: Joi.string().allow(null),
873
- BUILDING_NAME: Joi.string().allow(null),
874
- BUILDING_NUMBER: Joi.string().allow(null),
875
- DEPENDENT_THOROUGHFARE_NAME: Joi.string().allow(null),
876
- THOROUGHFARE_NAME: Joi.string().allow(null),
877
- DOUBLE_DEPENDENT_LOCALITY: Joi.string().allow(null),
878
- DEPENDENT_LOCALITY: Joi.string().allow(null),
879
- POST_TOWN: Joi.string().required(),
880
- POSTCODE: Joi.string().required(),
881
- LOCAL_CUSTODIAN_CODE_DESCRIPTION: Joi.string().allow(null),
882
- COUNTRY_CODE: Joi.string().required()
883
- }).unknown(true).required()
884
- }).unknown(true);
885
-
886
- // src/schemas/os-places/addresses-schema.js
887
- var CHOOSE_ADDRESS_ERROR = "Choose an address";
888
- var addressesSchema = Joi.object({
889
- addresses: Joi.string().invalid("display").required().messages({
890
- "any.required": CHOOSE_ADDRESS_ERROR,
891
- "string.empty": CHOOSE_ADDRESS_ERROR,
892
- "any.invalid": CHOOSE_ADDRESS_ERROR
893
- })
894
- });
895
-
896
- // src/schemas/os-places/uk-postcode-schema.js
897
- var ukPostcodeSchema = Joi.object({
898
- postcode: Joi.string().trim().required().uppercase().max(POSTCODE_MAX).pattern(/^([A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}|GIR ?0A{2})$/).messages({
899
- "any.required": "Enter a postcode",
900
- "string.empty": "Enter a postcode",
901
- "string.max": `Postal code must be ${POSTCODE_MAX} characters or less`,
902
- "string.pattern.base": "Enter a full UK postcode, like AA3 1AB"
903
- })
904
- });
905
-
906
- // src/schemas/os-places/os-places-schemas.js
907
- var osPlacesSchemas = {
908
- addressLookup: addressLookupSchema,
909
- addresses: addressesSchema,
910
- ukPostcode: ukPostcodeSchema
956
+ var formatCph = (countyParishHoldings) => {
957
+ return (countyParishHoldings || []).filter((cph) => cph == null ? void 0 : cph.cphNumber).map((cph) => cph.cphNumber);
958
+ };
959
+ var formatCphText = (count) => {
960
+ return `County Parish Holding (CPH) number${count !== 1 ? "s" : ""}`;
961
+ };
962
+ var formatBusinessAddress = (businessAddress) => {
963
+ var _a, _b;
964
+ if (((_a = businessAddress == null ? void 0 : businessAddress.lookup) == null ? void 0 : _a.uprn) || ((_b = businessAddress == null ? void 0 : businessAddress.manual) == null ? void 0 : _b.line1)) {
965
+ return formatDisplayAddress(businessAddress);
966
+ }
967
+ return [];
911
968
  };
912
969
 
913
- // src/schemas/schemas.js
914
- var schemas = {
915
- business: businessSchemas,
916
- customer: customerSchemas,
917
- personal: personalSchemas,
918
- osPlaces: osPlacesSchemas
970
+ // src/presenters/presenters.js
971
+ var presenters = {
972
+ formatBackLink,
973
+ formatNumber,
974
+ formatDateInputValues,
975
+ formatDisplayAddress,
976
+ formatOriginalAddress,
977
+ formatChangedAddress,
978
+ formatDisplayAddresses,
979
+ sortErrorsBySectionOrder,
980
+ getActionText,
981
+ formatCph,
982
+ formatCphText,
983
+ formatBusinessAddress,
984
+ addressBackLink,
985
+ addressChangeLink
919
986
  };
920
987
 
921
988
  // src/utils/format-full-name.js
@@ -970,11 +1037,259 @@ var utils = {
970
1037
  buildUpdateBusinessEmailVariables,
971
1038
  buildUpdateBusinessNameVariables
972
1039
  };
1040
+
1041
+ // src/services/os-places/address-lookup-service.js
1042
+ import { placesAPI } from "osdatahub";
1043
+
1044
+ // src/mock-data/mock-os-places-addresses.js
1045
+ var mockAddresses = [
1046
+ {
1047
+ properties: {
1048
+ UPRN: "10000001",
1049
+ ADDRESS: "APARTMENT 1, REDBRIDGE HOUSE, 1 ROSE COURT, LONDON, SW1A 1AA",
1050
+ SUB_BUILDING_NAME: "APARTMENT 1",
1051
+ BUILDING_NAME: "REDBRIDGE HOUSE",
1052
+ BUILDING_NUMBER: "1",
1053
+ THOROUGHFARE_NAME: "ROSE COURT",
1054
+ POST_TOWN: "LONDON",
1055
+ POSTCODE: "SW1A 1AA",
1056
+ LOCAL_CUSTODIAN_CODE_DESCRIPTION: "GREATER LONDON",
1057
+ COUNTRY_CODE: "E"
1058
+ }
1059
+ },
1060
+ {
1061
+ properties: {
1062
+ UPRN: "10000002",
1063
+ ADDRESS: "FLAT 2B, KINGSLEY APARTMENTS, 12 VICTORIA SQUARE, LONDON, SW1A 1AA",
1064
+ SUB_BUILDING_NAME: "FLAT 2B",
1065
+ BUILDING_NAME: "KINGSLEY APARTMENTS",
1066
+ BUILDING_NUMBER: "12",
1067
+ THOROUGHFARE_NAME: "VICTORIA SQUARE",
1068
+ DEPENDENT_LOCALITY: "",
1069
+ POST_TOWN: "LONDON",
1070
+ POSTCODE: "SW1A 1AA",
1071
+ LOCAL_CUSTODIAN_CODE_DESCRIPTION: "GREATER LONDON",
1072
+ COUNTRY_CODE: "E"
1073
+ }
1074
+ },
1075
+ {
1076
+ properties: {
1077
+ UPRN: "10000003",
1078
+ ADDRESS: "THE GROUND FLOOR, THE OLD GRANARY, 5 CHURCH LANE, LONDON, SW1A 1AA",
1079
+ ORGANISATION_NAME: "OLD GRANARY STUDIOS",
1080
+ SUB_BUILDING_NAME: "GROUND FLOOR",
1081
+ BUILDING_NAME: "THE OLD GRANARY",
1082
+ BUILDING_NUMBER: "5",
1083
+ THOROUGHFARE_NAME: "CHURCH LANE",
1084
+ POST_TOWN: "LONDON",
1085
+ POSTCODE: "SW1A 1AA",
1086
+ LOCAL_CUSTODIAN_CODE_DESCRIPTION: "GREATER LONDON",
1087
+ COUNTRY_CODE: "E"
1088
+ }
1089
+ },
1090
+ {
1091
+ properties: {
1092
+ UPRN: "10000004",
1093
+ ADDRESS: "SUITE 3, WELLINGTON HOUSE, 20 QUEEN STREET, LONDON, SW1A 1AA",
1094
+ ORGANISATION_NAME: "WELLINGTON CONSULTING LTD",
1095
+ DEPARTMENT_NAME: "CLIENT SERVICES",
1096
+ SUB_BUILDING_NAME: "SUITE 3",
1097
+ BUILDING_NAME: "WELLINGTON HOUSE",
1098
+ BUILDING_NUMBER: "20",
1099
+ THOROUGHFARE_NAME: "QUEEN STREET",
1100
+ POST_TOWN: "LONDON",
1101
+ POSTCODE: "SW1A 1AA",
1102
+ LOCAL_CUSTODIAN_CODE_DESCRIPTION: "GREATER LONDON",
1103
+ COUNTRY_CODE: "E"
1104
+ }
1105
+ },
1106
+ {
1107
+ properties: {
1108
+ UPRN: "10000005",
1109
+ ADDRESS: "UNIT 6, MARKET ROW, 3 MARKET YARD, LONDON, SW1A 1AA",
1110
+ ORGANISATION_NAME: "MARKET ROW TRADERS",
1111
+ SUB_BUILDING_NAME: "UNIT 6",
1112
+ BUILDING_NAME: "MARKET ROW",
1113
+ BUILDING_NUMBER: "3",
1114
+ THOROUGHFARE_NAME: "MARKET YARD",
1115
+ POST_TOWN: "LONDON",
1116
+ POSTCODE: "SW1A 1AA",
1117
+ LOCAL_CUSTODIAN_CODE_DESCRIPTION: "GREATER LONDON",
1118
+ COUNTRY_CODE: "E"
1119
+ }
1120
+ },
1121
+ {
1122
+ properties: {
1123
+ UPRN: "20000001",
1124
+ ADDRESS: "1 ORCHARD COTTAGES, WESTFIELD LANE, SHEPTON MALLET, BS14 8XX",
1125
+ BUILDING_NAME: "ORCHARD COTTAGES",
1126
+ BUILDING_NUMBER: "1",
1127
+ THOROUGHFARE_NAME: "WESTFIELD LANE",
1128
+ POST_TOWN: "SHEPTON MALLET",
1129
+ POSTCODE: "BS14 8XX",
1130
+ LOCAL_CUSTODIAN_CODE_DESCRIPTION: "SOMERSET",
1131
+ COUNTRY_CODE: "E"
1132
+ }
1133
+ },
1134
+ {
1135
+ properties: {
1136
+ UPRN: "20000002",
1137
+ ADDRESS: "THE STABLES, MANOR FARM, HOLLOW ROAD, SHEPTON MALLET, BS14 8XX",
1138
+ ORGANISATION_NAME: "MANOR FARM",
1139
+ SUB_BUILDING_NAME: "THE STABLES",
1140
+ BUILDING_NAME: "MANOR FARM",
1141
+ THOROUGHFARE_NAME: "HOLLOW ROAD",
1142
+ POST_TOWN: "SHEPTON MALLET",
1143
+ POSTCODE: "BS14 8XX",
1144
+ LOCAL_CUSTODIAN_CODE_DESCRIPTION: "SOMERSET",
1145
+ COUNTRY_CODE: "E"
1146
+ }
1147
+ },
1148
+ {
1149
+ properties: {
1150
+ UPRN: "30000001",
1151
+ ADDRESS: "FLAT 4, BISHOP COURT, 9 FLEET STREET, LONDON, EC1A 1BB",
1152
+ SUB_BUILDING_NAME: "FLAT 4",
1153
+ BUILDING_NAME: "BISHOP COURT",
1154
+ BUILDING_NUMBER: "9",
1155
+ THOROUGHFARE_NAME: "FLEET STREET",
1156
+ POST_TOWN: "LONDON",
1157
+ POSTCODE: "EC1A 1BB",
1158
+ LOCAL_CUSTODIAN_CODE_DESCRIPTION: "CITY OF LONDON",
1159
+ COUNTRY_CODE: "E"
1160
+ }
1161
+ },
1162
+ {
1163
+ properties: {
1164
+ UPRN: "40000001",
1165
+ ADDRESS: "2A PICCADILLY ARCADE, MANCHESTER, M1 1AE",
1166
+ ORGANISATION_NAME: "PICCADILLY ARCADE LTD",
1167
+ SUB_BUILDING_NAME: "2A",
1168
+ BUILDING_NAME: "PICCADILLY ARCADE",
1169
+ BUILDING_NUMBER: "2A",
1170
+ THOROUGHFARE_NAME: "PICCADILLY",
1171
+ POST_TOWN: "MANCHESTER",
1172
+ POSTCODE: "M1 1AE",
1173
+ LOCAL_CUSTODIAN_CODE_DESCRIPTION: "GREATER MANCHESTER",
1174
+ COUNTRY_CODE: "E"
1175
+ }
1176
+ },
1177
+ {
1178
+ properties: {
1179
+ UPRN: "50000001",
1180
+ ADDRESS: "3 WESTBOURNE TERRACE, LONDON, W1A 0AX",
1181
+ BUILDING_NAME: "WESTBOURNE TERRACE",
1182
+ BUILDING_NUMBER: "3",
1183
+ THOROUGHFARE_NAME: "WESTBOURNE TERRACE",
1184
+ POST_TOWN: "LONDON",
1185
+ POSTCODE: "W1A 0AX",
1186
+ LOCAL_CUSTODIAN_CODE_DESCRIPTION: "WESTMINSTER",
1187
+ COUNTRY_CODE: "E"
1188
+ }
1189
+ }
1190
+ ];
1191
+
1192
+ // src/services/os-places/os-places-stub.js
1193
+ var mockPostcode = (postcode) => {
1194
+ const formattedPostcode = postcode == null ? void 0 : postcode.toUpperCase().replaceAll(" ", "");
1195
+ const matchingAddresses = mockAddresses.filter((address) => {
1196
+ var _a;
1197
+ const addressPostcode = (_a = address.properties.POSTCODE) == null ? void 0 : _a.toUpperCase().replaceAll(" ", "");
1198
+ return addressPostcode === formattedPostcode;
1199
+ });
1200
+ return {
1201
+ features: matchingAddresses
1202
+ };
1203
+ };
1204
+
1205
+ // src/services/os-places/address-lookup-service.js
1206
+ var addressLookupService = async (postcode, osPlacesConfig) => {
1207
+ const addresses = await fetchAddressesFromPostcodeLookup(postcode, osPlacesConfig);
1208
+ if (addresses.error) {
1209
+ return addresses;
1210
+ }
1211
+ if (!(addresses == null ? void 0 : addresses.length)) {
1212
+ return {
1213
+ error: [
1214
+ {
1215
+ message: "No addresses found for this postcode",
1216
+ path: ["postcode"]
1217
+ }
1218
+ ]
1219
+ };
1220
+ }
1221
+ const mappedAddresses = addressLookupMapper(addresses);
1222
+ return mappedAddresses;
1223
+ };
1224
+ var fetchAddressesFromPostcodeLookup = async (postcode, osPlacesConfig) => {
1225
+ const MAX_RETRIES = 3;
1226
+ const INITIAL_BACKOFF_MS = 100;
1227
+ for (let attemptNumber = 1; attemptNumber <= MAX_RETRIES; attemptNumber++) {
1228
+ try {
1229
+ const { clientId, osPlacesStub } = osPlacesConfig;
1230
+ if (osPlacesStub) {
1231
+ const response2 = mockPostcode(postcode);
1232
+ return response2.features ?? [];
1233
+ }
1234
+ const response = await fetchFromPlacesAPI(clientId, postcode);
1235
+ return response.features ?? [];
1236
+ } catch (error) {
1237
+ const shouldRetry = isRetryable(error) && attemptNumber < MAX_RETRIES;
1238
+ if (!shouldRetry) {
1239
+ return buildErrorResponse(error.message);
1240
+ }
1241
+ const backoffMs = calculateExponentialBackoff(attemptNumber, INITIAL_BACKOFF_MS);
1242
+ await delayBeforeRetry(backoffMs);
1243
+ }
1244
+ }
1245
+ };
1246
+ var calculateExponentialBackoff = (attemptNumber, baseMs) => {
1247
+ const exponent = attemptNumber - 1;
1248
+ const powerOf2 = Math.pow(2, exponent);
1249
+ const backoffDuration = baseMs * powerOf2;
1250
+ return backoffDuration;
1251
+ };
1252
+ function delayBeforeRetry(ms) {
1253
+ const callback = (resolve) => {
1254
+ setTimeout(resolve, ms);
1255
+ };
1256
+ return new Promise(callback);
1257
+ }
1258
+ var buildErrorResponse = (message) => {
1259
+ return {
1260
+ error: [
1261
+ {
1262
+ message: message || "Failed to fetch addresses",
1263
+ path: ["postcode"]
1264
+ }
1265
+ ]
1266
+ };
1267
+ };
1268
+ var fetchFromPlacesAPI = async (clientId, postcode) => {
1269
+ const response = await placesAPI.postcode(clientId, postcode, { limit: 150 });
1270
+ return response;
1271
+ };
1272
+ var isRetryable = (error) => {
1273
+ const networkErrors = ["ECONNRESET", "ECONNREFUSED", "ETIMEDOUT"];
1274
+ if (networkErrors.includes(error.code)) {
1275
+ return true;
1276
+ }
1277
+ if (error.status && error.status >= INTERNAL_SERVER_ERROR) {
1278
+ return true;
1279
+ }
1280
+ return false;
1281
+ };
1282
+
1283
+ // src/services/services.js
1284
+ var services = {
1285
+ addressLookup: addressLookupService
1286
+ };
973
1287
  export {
974
1288
  constants,
975
1289
  mappers,
976
1290
  mutations,
977
1291
  presenters,
978
1292
  schemas,
1293
+ services,
979
1294
  utils
980
1295
  };