@defra/fcp-sfd-frontend-engine 0.34.0 → 0.36.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/fcp-sfd-frontend-engine",
3
- "version": "0.34.0",
3
+ "version": "0.36.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": {
@@ -11,7 +11,9 @@
11
11
 
12
12
  import { mapAddress } from './address-mapper.js'
13
13
 
14
- const asNullable = (value) => value ?? null
14
+ const asNullable = (value) => {
15
+ return value ?? null
16
+ }
15
17
 
16
18
  export const mapBusinessDetails = (value) => {
17
19
  const business = value?.business ?? {}
@@ -1,13 +1,15 @@
1
- import { mapPersonalBusinessDetails } from './personal-business-details-mapper.js'
1
+ import { addressLookupMapper } from './address-lookup-mapper.js'
2
2
  import { mapAddress } from './address-mapper.js'
3
- import { mapCustomerName } from './customer-name-mapper.js'
4
3
  import { mapBusinessDetails } from './business-details-mapper.js'
5
- import { addressLookupMapper } from './address-lookup-mapper.js'
4
+ import { mapCustomerName } from './customer-name-mapper.js'
5
+ import { mapPersonalBusinessDetails } from './personal-business-details-mapper.js'
6
+ import { mapPersonalDetails } from './personal-details-mapper.js'
6
7
 
7
8
  export const mappers = {
8
- personalBusinessDetails: mapPersonalBusinessDetails,
9
9
  address: mapAddress,
10
- customerName: mapCustomerName,
10
+ addressLookup: addressLookupMapper,
11
11
  businessDetails: mapBusinessDetails,
12
- addressLookup: addressLookupMapper
12
+ customerName: mapCustomerName,
13
+ personalBusinessDetails: mapPersonalBusinessDetails,
14
+ personalDetails: mapPersonalDetails
13
15
  }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Takes the raw personal details data from the DAL and maps it to a more usable format
3
+ *
4
+ * @param {Object} value - The data from the DAL
5
+ * @returns {Object} Formatted personal details data
6
+ * @throws {Error} If value is null, undefined, or missing required customer structure
7
+ */
8
+
9
+ import { mapAddress } from './address-mapper.js'
10
+ import { mapCustomerName } from './customer-name-mapper.js'
11
+ import { utils } from '../utils/utils.js'
12
+
13
+ const asNullable = (value) => {
14
+ return value ?? null
15
+ }
16
+
17
+ export const mapPersonalDetails = (value) => {
18
+ protectAgainstNull(value)
19
+
20
+ const customerInfo = value.customer.info
21
+ const customerName = customerInfo.name
22
+
23
+ const [year, month, day] = customerInfo.dateOfBirth ? customerInfo.dateOfBirth.split('-') : []
24
+
25
+ return {
26
+ crn: value.customer.crn ?? null,
27
+ userName: customerName ? mapCustomerName(customerName) : null,
28
+ fullName: {
29
+ first: asNullable(customerName?.first),
30
+ last: asNullable(customerName?.last),
31
+ middle: asNullable(customerName?.middle)
32
+ },
33
+ fullNameJoined: customerName ? utils.formatFullName(customerName) : '',
34
+ dateOfBirth: {
35
+ full: asNullable(customerInfo.dateOfBirth),
36
+ day: asNullable(day),
37
+ month: asNullable(month),
38
+ year: asNullable(year)
39
+ },
40
+ address: customerInfo.address ? mapAddress(customerInfo.address) : {},
41
+ email: asNullable(customerInfo.email?.address),
42
+ telephone: asNullable(customerInfo.phone?.landline),
43
+ mobile: asNullable(customerInfo.phone?.mobile)
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Validates that the input value has the required structure before mapping
49
+ *
50
+ * @param {Object} value - The data from the DAL
51
+ * @throws {Error} If value is null, undefined, or missing required customer structure
52
+ */
53
+ const protectAgainstNull = (value) => {
54
+ if (value === null || value === undefined) {
55
+ throw new Error('Personal details value cannot be null or undefined')
56
+ }
57
+ if (!value.customer) {
58
+ throw new Error('Personal details value must contain a customer object')
59
+ }
60
+
61
+ if (!value.customer.info) {
62
+ throw new Error('Customer must contain an info object')
63
+ }
64
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Updates any combination of business name, email, phone, VAT and address.
3
+ */
4
+
5
+ export const updateBusinessDetailsMutation = `
6
+ mutation UpdateBusinessAllFields($input: UpdateBusinessAllFieldsInput!) {
7
+ updateBusinessAllFields(input: $input) {
8
+ success
9
+ }
10
+ }
11
+ `
@@ -1,29 +1,31 @@
1
+ import { updateBusinessAddressMutation } from './business/update-business-address.js'
2
+ import { updateBusinessDetailsMutation } from './business/update-business-details.js'
1
3
  import { updateBusinessEmailMutation } from './business/update-business-email.js'
4
+ import { updateBusinessLegalStatusMutation } from './business/update-business-legal-status.js'
2
5
  import { updateBusinessNameMutation } from './business/update-business-name.js'
3
6
  import { updateBusinessPhoneNumbersMutation } from './business/update-business-phone-numbers.js'
4
- import { updateCustomerNameMutation } from './personal/update-customer-name.js'
5
- import { updateCustomerDobMutation } from './personal/update-customer-dob.js'
6
- import { updateCustomerPhoneMutation } from './personal/update-customer-phone.js'
7
- import { updateCustomerEmailMutation } from './personal/update-customer-email.js'
8
- import { updateCustomerAddressMutation } from './personal/update-customer-address.js'
9
- import { updateBusinessAddressMutation } from './business/update-business-address.js'
7
+ import { updateBusinessRegistrationNumbersMutation } from './business/update-business-registration-numbers.js'
10
8
  import { updateBusinessVatMutation } from './business/update-business-vat.js'
9
+ import { updateCustomerAddressMutation } from './personal/update-customer-address.js'
11
10
  import { updateCustomerDetailsMutation } from './personal/update-customer-details.js'
12
- import { updateBusinessLegalStatusMutation } from './business/update-business-legal-status.js'
13
- import { updateBusinessRegistrationNumbersMutation } from './business/update-business-registration-numbers.js'
11
+ import { updateCustomerDobMutation } from './personal/update-customer-dob.js'
12
+ import { updateCustomerEmailMutation } from './personal/update-customer-email.js'
13
+ import { updateCustomerNameMutation } from './personal/update-customer-name.js'
14
+ import { updateCustomerPhoneMutation } from './personal/update-customer-phone.js'
14
15
 
15
16
  export const mutations = {
17
+ updateBusinessAddress: updateBusinessAddressMutation,
18
+ updateBusinessDetails: updateBusinessDetailsMutation,
16
19
  updateBusinessEmail: updateBusinessEmailMutation,
20
+ updateBusinessLegalStatus: updateBusinessLegalStatusMutation,
17
21
  updateBusinessName: updateBusinessNameMutation,
18
22
  updateBusinessPhoneNumbers: updateBusinessPhoneNumbersMutation,
19
- updateBusinessAddress: updateBusinessAddressMutation,
20
- updateCustomerName: updateCustomerNameMutation,
21
- updateCustomerDob: updateCustomerDobMutation,
22
- updateCustomerPhone: updateCustomerPhoneMutation,
23
- updateCustomerEmail: updateCustomerEmailMutation,
24
- updateCustomerAddress: updateCustomerAddressMutation,
23
+ updateBusinessRegistrationNumbers: updateBusinessRegistrationNumbersMutation,
25
24
  updateBusinessVat: updateBusinessVatMutation,
25
+ updateCustomerAddress: updateCustomerAddressMutation,
26
26
  updateCustomerDetails: updateCustomerDetailsMutation,
27
- updateBusinessLegalStatus: updateBusinessLegalStatusMutation,
28
- updateBusinessRegistrationNumbers: updateBusinessRegistrationNumbersMutation
27
+ updateCustomerDob: updateCustomerDobMutation,
28
+ updateCustomerEmail: updateCustomerEmailMutation,
29
+ updateCustomerName: updateCustomerNameMutation,
30
+ updateCustomerPhone: updateCustomerPhoneMutation
29
31
  }
@@ -1,40 +1,11 @@
1
+ /**
2
+ * Updates any combination of personal name, email, phone, and address.
3
+ */
4
+
1
5
  export const updateCustomerDetailsMutation = `
2
6
  mutation UpdateCustomerAllFields($input: UpdateCustomerAllFieldsInput!) {
3
7
  updateCustomerAllFields(input: $input) {
4
8
  success
5
- customer {
6
- info {
7
- name {
8
- first
9
- middle
10
- last
11
- }
12
- dateOfBirth
13
- phone {
14
- mobile
15
- landline
16
- }
17
- email {
18
- address
19
- }
20
- address {
21
- line1
22
- line2
23
- line3
24
- line4
25
- line5
26
- buildingNumberRange
27
- buildingName
28
- flatName
29
- street
30
- city
31
- county
32
- postalCode
33
- country
34
- uprn
35
- }
36
- }
37
- }
38
9
  }
39
10
  }
40
11
  `
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Builds mutation variables for updating a business's details via the fix journey.
3
+ * Only includes the sections that actually need updating, using the
4
+ * unified `input` format for the GraphQL mutation.
5
+ *
6
+ * @module buildBusinessFixUpdateVariablesService
7
+ */
8
+
9
+ import { buildManualAddress } from '../build-address-variables-service.js'
10
+
11
+ const buildBusinessFixUpdateVariablesService = (businessDetails) => {
12
+ const { orderedSectionsToFix, sbi } = businessDetails
13
+
14
+ const input = { sbi }
15
+
16
+ // Conditionally merge each section into input if it's been updated by the user
17
+ for (const applySection of sectionAppliers) {
18
+ applySection(input, businessDetails, orderedSectionsToFix)
19
+ }
20
+
21
+ return { input }
22
+ }
23
+
24
+ const applyAddressSection = (input, businessDetails, orderedSectionsToFix) => {
25
+ if (orderedSectionsToFix.includes('address') && businessDetails.changeBusinessAddress) {
26
+ input.address = { withoutUprn: buildManualAddress(businessDetails.changeBusinessAddress) }
27
+ }
28
+ }
29
+
30
+ const applyEmailSection = (input, businessDetails, orderedSectionsToFix) => {
31
+ if (orderedSectionsToFix.includes('email') && businessDetails.changeBusinessEmail) {
32
+ Object.assign(input, buildEmailInput(businessDetails.changeBusinessEmail))
33
+ }
34
+ }
35
+
36
+ const applyNameSection = (input, businessDetails, orderedSectionsToFix) => {
37
+ if (orderedSectionsToFix.includes('name') && businessDetails.changeBusinessName) {
38
+ input.name = businessDetails.changeBusinessName.businessName
39
+ }
40
+ }
41
+
42
+ const applyPhoneSection = (input, businessDetails, orderedSectionsToFix) => {
43
+ if (orderedSectionsToFix.includes('phone') && businessDetails.changeBusinessPhoneNumbers) {
44
+ Object.assign(input, buildPhoneInput(businessDetails.changeBusinessPhoneNumbers))
45
+ }
46
+ }
47
+
48
+ const applyVatSection = (input, businessDetails, orderedSectionsToFix) => {
49
+ if (orderedSectionsToFix.includes('vat') && businessDetails.changeBusinessVat) {
50
+ input.vat = businessDetails.changeBusinessVat.vatNumber ?? ''
51
+ }
52
+ }
53
+
54
+ const sectionAppliers = [
55
+ applyAddressSection,
56
+ applyEmailSection,
57
+ applyNameSection,
58
+ applyPhoneSection,
59
+ applyVatSection
60
+ ]
61
+
62
+ const buildEmailInput = (change) => {
63
+ return {
64
+ email: {
65
+ address: change.businessEmail
66
+ }
67
+ }
68
+ }
69
+
70
+ const buildPhoneInput = (change) => {
71
+ return {
72
+ phone: {
73
+ landline: change.businessTelephone ?? null,
74
+ mobile: change.businessMobile ?? null
75
+ }
76
+ }
77
+ }
78
+
79
+ export {
80
+ buildBusinessFixUpdateVariablesService
81
+ }
@@ -48,6 +48,7 @@ const initialiseFixJourneyService = (yar, source, journeyType) => {
48
48
 
49
49
  const sessionData = yar.get(sessionKey)
50
50
 
51
+ // yar.get() returns null for a missing/already-cleared key (e.g. journey already completed)
51
52
  if (!sessionData?.sectionsNeedingUpdate) {
52
53
  return sessionData
53
54
  }
@@ -1,20 +1,22 @@
1
- import { addressLookupService } from './os-places/address-lookup-service.js'
2
- import { buildUprnAddress, buildManualAddress } from './build-address-variables-service.js'
1
+ import { buildManualAddress, buildUprnAddress } from './build-address-variables-service.js'
2
+ import { buildFixSuccessMessageService } from './build-fix-success-message-service.js'
3
+ import { buildBusinessFixUpdateVariablesService } from './business/build-business-fix-update-variables-service.js'
3
4
  import { checkInterrupterJourneySessionService } from './check-interrupter-journey-session-service.js'
4
5
  import { initialiseFixJourneyService } from './initialise-fix-journey-service.js'
5
- import { validateFixDetailsService } from './validate-fix-details-service.js'
6
- import { setFixSessionDataService } from './set-fix-session-data-service.js'
7
- import { buildFixSuccessMessageService } from './build-fix-success-message-service.js'
6
+ import { addressLookupService } from './os-places/address-lookup-service.js'
8
7
  import { buildCustomerFixUpdateVariablesService } from './personal/build-customer-fix-update-variables-service.js'
8
+ import { setFixSessionDataService } from './set-fix-session-data-service.js'
9
+ import { validateFixDetailsService } from './validate-fix-details-service.js'
9
10
 
10
11
  export const services = {
11
12
  addressLookup: addressLookupService,
12
- buildUprnAddress,
13
+ buildBusinessFixUpdateVariables: buildBusinessFixUpdateVariablesService,
14
+ buildCustomerFixUpdateVariables: buildCustomerFixUpdateVariablesService,
15
+ buildFixSuccessMessage: buildFixSuccessMessageService,
13
16
  buildManualAddress,
17
+ buildUprnAddress,
14
18
  checkInterrupterJourneySession: checkInterrupterJourneySessionService,
15
19
  initialiseFixJourney: initialiseFixJourneyService,
16
- validateFixDetails: validateFixDetailsService,
17
20
  setFixSessionData: setFixSessionDataService,
18
- buildFixSuccessMessage: buildFixSuccessMessageService,
19
- buildCustomerFixUpdateVariables: buildCustomerFixUpdateVariablesService
21
+ validateFixDetails: validateFixDetailsService
20
22
  }