diff --git a/app/presenters/import/legacy/company-address.presenter.js b/app/presenters/import/legacy/company-address.presenter.js new file mode 100644 index 0000000000..c6100e9640 --- /dev/null +++ b/app/presenters/import/legacy/company-address.presenter.js @@ -0,0 +1,57 @@ +'use strict' + +/** + * Maps the legacy NALD data to a company address + * @module CompanyAddressPresenter + */ + +/** + * Maps the legacy NALD data to a company address + * + * @param {ImportLegacyCompanyAddressType} address - the legacy NALD data in company address format + * + * @returns {object} the NALD company address data transformed into the WRLS format for a company address + * ready for validation and persisting + */ +function go (address) { + return { + addressId: address.external_id, + companyId: address.company_external_id, + licenceRoleId: address.licence_role_id, + startDate: address.start_date, + endDate: address.licence_role_name === 'licenceHolder' ? _endDate(address) : address.end_date + } +} + +/** + * Calculate the licence holders company address end date + * + * A licence can have multiple versions, when one licence version ends the other should start. + * + * Unless a licence has not ended then the end date can be null to show it has not ended + * + * A licence can be ended, revoked, lapsed or expired. When this is the case we want to the oldest date of the options. + * + * @param {ImportLegacyCompanyAddressType} address - the legacy NALD address + * + * @returns {date | null} the end date for a licence holder + * @private + */ +function _endDate (address) { + const endDates = [ + address.end_date, + address.lapsed_date, + address.expired_date, + address.revoked_date + ].filter((endDate) => { + return endDate + }) + + const oldestDate = new Date(Math.max(...endDates)) + + return !isNaN(oldestDate) ? oldestDate : null +} + +module.exports = { + go +} diff --git a/app/presenters/import/legacy/company.presenter.js b/app/presenters/import/legacy/company.presenter.js index 43db6a8099..50f143903c 100644 --- a/app/presenters/import/legacy/company.presenter.js +++ b/app/presenters/import/legacy/company.presenter.js @@ -17,7 +17,9 @@ function go (company) { return { name: company.name, type: company.type, - externalId: company.external_id + externalId: company.external_id, + addresses: [], + companyAddresses: [] } } diff --git a/app/services/import/legacy/fetch-company-address.service.js b/app/services/import/legacy/fetch-company-address.service.js new file mode 100644 index 0000000000..8221e24cc8 --- /dev/null +++ b/app/services/import/legacy/fetch-company-address.service.js @@ -0,0 +1,89 @@ +'use strict' + +/** + * Fetches the NALD data for a company address using the licence ref + * @module FetchCompanyAddressesService + */ + +const { db } = require('../../../../db/db.js') + +/** + * Fetches the NALD data for a company address using the licence ref + * + * A Company Address is a WRLS concept. It is used to link an address to a company, with a role. + * + * @param {string} regionCode - The NALD region code + * @param {string} licenceId - The NALD licence ID + * + * @returns {Promise} + */ +async function go (regionCode, licenceId) { + const query = _query() + + const { rows } = await db.raw(query, [regionCode, licenceId, regionCode, licenceId]) + + return rows +} + +/** + * Fetches the NALD data for a licence version to create a company address for the licence holder + * + * @private + */ +function _query () { + return ` + WITH end_date_cte AS ( + SELECT + "ACON_AADD_ID", + CASE + WHEN COUNT("EFF_END_DATE") FILTER (WHERE "EFF_END_DATE" = 'null') > 0 THEN NULL + ELSE MAX(TO_DATE(NULLIF("EFF_END_DATE", 'null'), 'DD/MM/YYYY')) + END AS end_date + FROM import."NALD_ABS_LIC_VERSIONS" + WHERE "FGAC_REGION_CODE" = ? + AND "AABL_ID" = ? + GROUP BY "ACON_AADD_ID" + ) + SELECT DISTINCT ON (nalv."ACON_AADD_ID") + ed.end_date, + TO_DATE(NULLIF(nalv."EFF_ST_DATE", 'null'), 'DD/MM/YYYY') AS start_date, + TO_DATE(NULLIF(nal."REV_DATE", 'null'), 'DD/MM/YYYY') AS revoked_date, + TO_DATE(NULLIF(nal."EXPIRY_DATE", 'null'), 'DD/MM/YYYY') AS expired_date, + TO_DATE(NULLIF(nal."LAPSED_DATE", 'null'), 'DD/MM/YYYY') AS lapsed_date, + (concat_ws(':', nalv."FGAC_REGION_CODE", nalv."ACON_AADD_ID")) AS external_id, + (concat_ws(':', nalv."FGAC_REGION_CODE", nalv."ACON_APAR_ID")) AS company_external_id, + nalv."ACON_APAR_ID" AS party_id, + nalv."ACON_AADD_ID" AS address_id, + lr.id as licence_role_id, + lr.name as licence_role_name + FROM import."NALD_ABS_LIC_VERSIONS" AS nalv + INNER JOIN public.licence_roles AS lr + ON lr.name = 'licenceHolder' + INNER JOIN import."NALD_ABS_LICENCES" AS nal + ON nal."ID" = nalv."AABL_ID" + AND nal."FGAC_REGION_CODE" = nalv."FGAC_REGION_CODE" + LEFT JOIN end_date_cte AS ed + ON ed."ACON_AADD_ID" = nalv."ACON_AADD_ID" + WHERE nalv."FGAC_REGION_CODE" = ? + AND nalv."AABL_ID" = ? + AND nalv."ACON_AADD_ID" IS NOT NULL; + ` +} + +module.exports = { + go +} + +/** + * @typedef {object} ImportLegacyCompanyAddressType + * + * @property {string} external_id - The address id + * @property {string} company_external_id - The company id + * @property {string} licence_role_id - The licence role id from the reference data + * @property {string} start_date - The licence version with the earliest start date + * @property {string} end_date - The licence version latest end date, unless null then always null + * @property {string} revoked_date - The licence revoked date + * @property {string} expired_date - The licence expired date + * @property {string} lapsed_date - The licence lapsed licence date + * + */ diff --git a/app/services/import/legacy/process-licence.service.js b/app/services/import/legacy/process-licence.service.js index 3d755babca..e3bd4096d1 100644 --- a/app/services/import/legacy/process-licence.service.js +++ b/app/services/import/legacy/process-licence.service.js @@ -9,6 +9,7 @@ const LicenceStructureValidator = require('../../../validators/import/licence-st const PersistLicenceService = require('../persist-licence.service.js') const ProcessLicenceReturnLogsService = require('../../jobs/return-logs/process-licence-return-logs.service.js') const TransformAddressesService = require('./transform-addresses.service.js') +const TransformCompanyAddressesService = require('./transform-company-addresses.service.js') const TransformCompaniesService = require('./transform-companies.service.js') const TransformContactsService = require('./transform-contacts.service.js') const TransformLicenceService = require('./transform-licence.service.js') @@ -43,6 +44,7 @@ async function go (licenceRef) { // Pass the transformed companies through each transformation step, building the company as we go await TransformContactsService.go(regionCode, naldLicenceId, transformedCompanies) await TransformAddressesService.go(regionCode, naldLicenceId, transformedCompanies) + await TransformCompanyAddressesService.go(regionCode, naldLicenceId, transformedCompanies) // Ensure the built licence has all the valid child records we require LicenceStructureValidator.go(transformedLicence) diff --git a/app/services/import/legacy/transform-addresses.service.js b/app/services/import/legacy/transform-addresses.service.js index b52f488e2a..af731697c4 100644 --- a/app/services/import/legacy/transform-addresses.service.js +++ b/app/services/import/legacy/transform-addresses.service.js @@ -29,7 +29,7 @@ async function go (regionCode, licenceId, transformedCompanies) { ImportAddressValidator.go(address) - matchingCompany.addresses = [...(matchingCompany?.addresses || []), address] + matchingCompany.addresses.push(address) }) } diff --git a/app/services/import/legacy/transform-company-addresses.service.js b/app/services/import/legacy/transform-company-addresses.service.js new file mode 100644 index 0000000000..0960f8ef19 --- /dev/null +++ b/app/services/import/legacy/transform-company-addresses.service.js @@ -0,0 +1,39 @@ +'use strict' + +/** + * Transforms NALD data into a valid object that matches the WRLS structure for a company address + * @module ImportLegacyTransformCompanyAddressesService + */ + +const CompanyAddressPresenter = require('../../../presenters/import/legacy/company-address.presenter.js') +const FetchCompanyAddressesService = require('./fetch-company-address.service.js') + +/** + * Transforms NALD data into a valid object that matches the WRLS structure for a company address + * + * @param {string} regionCode - The NALD region code + * @param {string} licenceId - The NALD licence ID + * @param {object[]} transformedCompanies + * + */ +async function go (regionCode, licenceId, transformedCompanies) { + const naldAddresses = await FetchCompanyAddressesService.go(regionCode, licenceId) + + naldAddresses.forEach((naldAddress) => { + const matchingCompany = _matchingCompany(transformedCompanies, naldAddress) + + const address = CompanyAddressPresenter.go(naldAddress) + + matchingCompany.companyAddresses.push(address) + }) +} + +function _matchingCompany (transformedCompanies, naldAddress) { + return transformedCompanies.find((company) => { + return company.externalId === naldAddress.company_external_id + }) +} + +module.exports = { + go +} diff --git a/app/services/import/persist-licence.service.js b/app/services/import/persist-licence.service.js index 50b5f0feb4..3073a36b8c 100644 --- a/app/services/import/persist-licence.service.js +++ b/app/services/import/persist-licence.service.js @@ -53,6 +53,32 @@ async function _persistAddress (trx, updatedAt, address) { ]) } +async function _persistCompanyAddresses (trx, updatedAt, companyAddresses) { + for (const companyAddress of companyAddresses) { + await _persistCompanyAddress(trx, updatedAt, companyAddress) + } +} + +async function _persistCompanyAddress (trx, updatedAt, companyAddress) { + const { companyId, startDate, endDate, licenceRoleId, addressId } = companyAddress + + return db.raw(` + INSERT INTO public."company_addresses" (company_id, address_id, licence_role_id, start_date, end_date, "default", created_at, updated_at) + SELECT com.id, add.id, lr.id, ? ,?, true, NOW(), ? + FROM public.companies com + JOIN public."licence_roles" lr on lr.id = ? + JOIN public.addresses add ON add.external_id = ? + WHERE com.external_id = ? + ON CONFLICT (company_id, address_id, licence_role_id) + DO UPDATE SET + address_id=EXCLUDED.address_id, + "default" = EXCLUDED."default", + end_date = EXCLUDED.end_date, + updated_at = EXCLUDED.updated_at + `, [startDate, endDate, updatedAt, licenceRoleId, addressId, companyId]) + .transacting(trx) +} + async function _persistAddresses (trx, updatedAt, addresses) { for (const address of addresses) { await _persistAddress(trx, updatedAt, address) @@ -176,11 +202,13 @@ async function _persistCompanies (trx, updatedAt, companies) { } await _persistAddresses(trx, updatedAt, company.addresses) + + await _persistCompanyAddresses(trx, updatedAt, company.companyAddresses) } } async function _persistCompany (trx, updatedAt, company) { - const { contact, companyContact, addresses, ...propertiesToPersist } = company + const { contact, companyContact, addresses, companyAddresses, ...propertiesToPersist } = company return CompanyModel.query(trx) .insert({ ...propertiesToPersist, updatedAt }) diff --git a/app/validators/import/company.validator.js b/app/validators/import/company.validator.js index 489967c52b..81484b9daa 100644 --- a/app/validators/import/company.validator.js +++ b/app/validators/import/company.validator.js @@ -17,7 +17,9 @@ function go (company) { const schema = Joi.object({ name: Joi.string().required(), type: Joi.string().valid('organisation', 'person').required(), - externalId: Joi.string().required() + externalId: Joi.string().required(), + addresses: Joi.array().required(), + companyAddresses: Joi.array().required() }) const result = schema.validate(company, { convert: false }) diff --git a/test/presenters/import/legacy/company-address.presenter.test.js b/test/presenters/import/legacy/company-address.presenter.test.js new file mode 100644 index 0000000000..ed87b50442 --- /dev/null +++ b/test/presenters/import/legacy/company-address.presenter.test.js @@ -0,0 +1,136 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') + +const { describe, it, beforeEach } = exports.lab = Lab.script() +const { expect } = Code + +// Test helpers +const { generateUUID } = require('../../../../app/lib/general.lib.js') + +// Thing under test +const CompanyAddressPresenter = require('../../../../app/presenters/import/legacy/company-address.presenter.js') + +describe('Import Legacy Company Address presenter', () => { + const licenceRoleId = generateUUID() + + let legacyLicenceHolderAddress + + beforeEach(() => { + legacyLicenceHolderAddress = _legacyLicenceHolderCompanyAddress(licenceRoleId) + }) + + it('correctly transforms the data', () => { + const result = CompanyAddressPresenter.go(legacyLicenceHolderAddress) + + expect(result).to.equal({ + addressId: '7:777', + companyId: '1:007', + endDate: null, + licenceRoleId, + startDate: new Date('2020-01-01') + }) + }) + + describe('when the role is licence holder', () => { + describe('and all the dates are null', () => { + it('correctly sets end date to null', () => { + const { endDate } = CompanyAddressPresenter.go(legacyLicenceHolderAddress) + + expect(endDate).to.be.null() + }) + }) + + describe('and the end date is a date and the licence dates are null', () => { + beforeEach(() => { + const temp = _legacyLicenceHolderCompanyAddress(licenceRoleId) + + legacyLicenceHolderAddress = { ...temp, end_date: new Date('2022-02-02') } + }) + + it('correctly sets end date to the end date value', () => { + const { endDate } = CompanyAddressPresenter.go(legacyLicenceHolderAddress) + + expect(endDate).to.equal(new Date('2022-02-02')) + }) + }) + + describe('and the revoked date is a date and the other dates are null', () => { + beforeEach(() => { + const temp = _legacyLicenceHolderCompanyAddress(licenceRoleId) + + legacyLicenceHolderAddress = { ...temp, revoked_date: new Date('2023-03-03') } + }) + + it('correctly sets end date to the revoked date value', () => { + const { endDate } = CompanyAddressPresenter.go(legacyLicenceHolderAddress) + + expect(endDate).to.equal(new Date('2023-03-03')) + }) + }) + + describe('and the expired date is a date and the other dates are null', () => { + beforeEach(() => { + const temp = _legacyLicenceHolderCompanyAddress(licenceRoleId) + + legacyLicenceHolderAddress = { ...temp, expired_date: new Date('2024-04-04') } + }) + + it('correctly sets end date to the expired date value', () => { + const { endDate } = CompanyAddressPresenter.go(legacyLicenceHolderAddress) + + expect(endDate).to.equal(new Date('2024-04-04')) + }) + }) + + describe('and the lapsed date is a date and the other dates are null', () => { + beforeEach(() => { + const temp = _legacyLicenceHolderCompanyAddress(licenceRoleId) + + legacyLicenceHolderAddress = { ...temp, lapsed_date: new Date('2025-05-05') } + }) + + it('correctly sets end date to the lapsed date value', () => { + const { endDate } = CompanyAddressPresenter.go(legacyLicenceHolderAddress) + + expect(endDate).to.equal(new Date('2025-05-05')) + }) + }) + + describe('and all the dates have values', () => { + beforeEach(() => { + const temp = _legacyLicenceHolderCompanyAddress(licenceRoleId) + + legacyLicenceHolderAddress = { + ...temp, + end_date: new Date('2022-02-02'), + revoked_date: new Date('2023-03-03'), + expired_date: new Date('2024-04-04'), + lapsed_date: new Date('2025-05-05') + } + }) + + it('correctly sets end date latest date of the available dates', () => { + const { endDate } = CompanyAddressPresenter.go(legacyLicenceHolderAddress) + + expect(endDate).to.equal(new Date('2025-05-05')) + }) + }) + }) +}) + +function _legacyLicenceHolderCompanyAddress (licenceRoleId) { + return { + company_external_id: '1:007', + external_id: '7:777', + start_date: new Date('2020-01-01'), + end_date: null, + licence_role_id: licenceRoleId, + revoked_date: null, + expired_date: null, + lapsed_date: null, + licence_role_name: 'licenceHolder' + } +} diff --git a/test/presenters/import/legacy/company.presenter.test.js b/test/presenters/import/legacy/company.presenter.test.js index 77c6f0991d..02f8abec95 100644 --- a/test/presenters/import/legacy/company.presenter.test.js +++ b/test/presenters/import/legacy/company.presenter.test.js @@ -23,7 +23,9 @@ describe('Import Legacy Company presenter', () => { expect(result).to.equal({ externalId: '1:1940', name: 'ACME', - type: 'organisation' + type: 'organisation', + addresses: [], + companyAddresses: [] }) }) }) diff --git a/test/services/import/legacy/process-licence.service.test.js b/test/services/import/legacy/process-licence.service.test.js index fbe27edec4..5833b49edd 100644 --- a/test/services/import/legacy/process-licence.service.test.js +++ b/test/services/import/legacy/process-licence.service.test.js @@ -17,6 +17,7 @@ const PersistLicenceService = require('../../../../app/services/import/persist-l const ProcessLicenceReturnLogsService = require('../../../../app/services/jobs/return-logs/process-licence-return-logs.service.js') const TransformAddressesService = require('../../../../app/services/import/legacy/transform-addresses.service.js') const TransformCompaniesService = require('../../../../app/services/import/legacy/transform-companies.service.js') +const TransformCompanyAddressesService = require('../../../../app/services/import/legacy/transform-company-addresses.service.js') const TransformContactsService = require('../../../../app/services/import/legacy/transform-contacts.service.js') const TransformLicenceService = require('../../../../app/services/import/legacy/transform-licence.service.js') const TransformLicenceVersionPurposeConditionsService = require('../../../../app/services/import/legacy/transform-licence-version-purpose-conditions.service.js') @@ -51,6 +52,7 @@ describe('Import Legacy Process Licence service', () => { Sinon.stub(TransformCompaniesService, 'go').resolves({ company: [], transformedCompany: [] }) Sinon.stub(TransformContactsService, 'go').resolves() Sinon.stub(TransformAddressesService, 'go').resolves() + Sinon.stub(TransformCompanyAddressesService, 'go').resolves() // BaseRequest depends on the GlobalNotifier to have been set. This happens in app/plugins/global-notifier.plugin.js // when the app starts up and the plugin is registered. As we're not creating an instance of Hapi server in this diff --git a/test/services/import/legacy/transform-addresses.service.test.js b/test/services/import/legacy/transform-addresses.service.test.js index f42974e062..1be029f440 100644 --- a/test/services/import/legacy/transform-addresses.service.test.js +++ b/test/services/import/legacy/transform-addresses.service.test.js @@ -22,7 +22,7 @@ describe('Import Legacy Transform Addresses service', () => { let transformedCompanies beforeEach(() => { - transformedCompanies = [{ externalId: '1:007' }] + transformedCompanies = [{ externalId: '1:007', addresses: [] }] legacyAddress = _legacyAddress() }) @@ -68,7 +68,10 @@ describe('Import Legacy Transform Addresses service', () => { it('returns no contact object on the company', async () => { await TransformAddressesService.go(regionCode, naldLicenceId, transformedCompanies) - expect(transformedCompanies[0]).to.equal({ externalId: '1:007' }) + expect(transformedCompanies[0]).to.equal({ + externalId: '1:007', + addresses: [] + }) }) }) }) diff --git a/test/services/import/legacy/transform-companies.service.test.js b/test/services/import/legacy/transform-companies.service.test.js index 48bf1862e9..1610712510 100644 --- a/test/services/import/legacy/transform-companies.service.test.js +++ b/test/services/import/legacy/transform-companies.service.test.js @@ -52,7 +52,10 @@ describe('Import Legacy Transform Companies service', () => { { externalId: '1:1938', name: 'ACME', - type: 'organisation' + type: 'organisation', + addresses: [], + companyAddresses: [] + } ] }) diff --git a/test/services/import/legacy/transform-company-addresses.service.test.js b/test/services/import/legacy/transform-company-addresses.service.test.js new file mode 100644 index 0000000000..d2e99dec68 --- /dev/null +++ b/test/services/import/legacy/transform-company-addresses.service.test.js @@ -0,0 +1,87 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') +const Sinon = require('sinon') + +const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() +const { expect } = Code + +// Test helpers +const { generateUUID } = require('../../../../app/lib/general.lib.js') + +// Things to stub +const FetchCompanyAddressesService = require('../../../../app/services/import/legacy/fetch-company-address.service.js') + +// Thing under test +const TransformCompanyAddressesService = require('../../../../app/services/import/legacy/transform-company-addresses.service.js') + +describe('Import Legacy Transform Company Addresses service', () => { + const naldLicenceId = '2113' + const regionCode = '1' + const licenceRoleId = generateUUID() + + let legacyLicenceHolderAddress + let transformedCompanies + + beforeEach(() => { + transformedCompanies = [{ externalId: '1:007', companyAddresses: [] }] + + legacyLicenceHolderAddress = _legacyLicenceHolderCompanyAddress(licenceRoleId) + }) + + afterEach(() => { + Sinon.restore() + }) + + describe('when matching valid legacy address for a licence version is found', () => { + beforeEach(() => { + Sinon.stub(FetchCompanyAddressesService, 'go').resolves([legacyLicenceHolderAddress]) + }) + + it('attaches the record transformed and validated for WRLS to the transformed company', async () => { + await TransformCompanyAddressesService + .go(regionCode, naldLicenceId, transformedCompanies) + + expect(transformedCompanies[0]).to.equal({ + companyAddresses: [ + { + addressId: '7:777', + companyId: '1:007', + endDate: null, + licenceRoleId, + startDate: new Date('2020-01-01') + } + ], + externalId: '1:007' + }) + }) + }) + + describe('when no matching valid legacy address for a licence version is found', () => { + beforeEach(() => { + Sinon.stub(FetchCompanyAddressesService, 'go').resolves([]) + }) + + it('returns no contact object on the company', async () => { + await TransformCompanyAddressesService.go(regionCode, naldLicenceId, transformedCompanies) + + expect(transformedCompanies[0]).to.equal({ externalId: '1:007', companyAddresses: [] }) + }) + }) +}) + +function _legacyLicenceHolderCompanyAddress (licenceRoleId) { + return { + company_external_id: '1:007', + external_id: '7:777', + start_date: new Date('2020-01-01'), + end_date: null, + licence_role_id: licenceRoleId, + revoked_date: null, + expired_date: null, + lapsed_date: null, + licence_role_name: 'licenceHolder' + } +} diff --git a/test/services/import/persist-licence.service.test.js b/test/services/import/persist-licence.service.test.js index 9a12e893a3..a4628676fd 100644 --- a/test/services/import/persist-licence.service.test.js +++ b/test/services/import/persist-licence.service.test.js @@ -10,6 +10,7 @@ const { expect } = Code // Test helpers const AddressHelper = require('../../support/helpers/address.helper.js') const AddressModel = require('../../../app/models/address.model.js') +const CompanyAddressHelper = require('../../support/helpers/company-address.helper.js') const CompanyContactHelper = require('../../support/helpers/company-contact.helper.js') const CompanyHelper = require('../../support/helpers/company.helper.js') const CompanyModel = require('../../../app/models/company.model.js') @@ -130,16 +131,27 @@ describe('Persist licence service', () => { expect(address.externalId).to.equal(addressExternalId) expect(address.postcode).to.equal('HP11') expect(address.uprn).to.be.null() + + // Company Address - should link the company to the saved address + const companyAddress = address.companyAddresses[0] + + expect(companyAddress.addressId).to.equal(address.id) + expect(companyAddress.companyId).to.equal(company.id) + expect(companyAddress.licenceRoleId).to.equal(licenceHolderRoleId) + expect(companyAddress.default).to.be.true() + expect(companyAddress.startDate).to.equal(new Date('2020-01-01')) + expect(companyAddress.endDate).to.equal(new Date('2022-02-02')) }) }) describe('and that licence already exists', () => { + let exisitngAddress + let exisitngContact + let existingCompany let existingLicence let existingLicenceVersion let existingLicenceVersionPurpose let existingLicenceVersionPurposeCondition - let existingCompany - let exisitngContact beforeEach(async () => { existingLicence = await LicenceHelper.add({ @@ -205,10 +217,17 @@ describe('Persist licence service', () => { startDate: companyContactStartDate }) - await AddressHelper.add({ + exisitngAddress = await AddressHelper.add({ ...transformedCompany.addresses[0] }) + await CompanyAddressHelper.add({ + addressId: exisitngAddress.id, + companyId: existingCompany.id, + licenceRoleId: licenceHolderRoleId, + endDate: new Date('1999-01-01') + }) + transformedCompanies = [{ ...existingCompany, contact: exisitngContact, @@ -221,7 +240,16 @@ describe('Persist licence service', () => { address1: 'ENVIRONMENT AGENCY', externalId: addressExternalId, dataSource: 'nald' - }] + }], + companyAddresses: [ + { + addressId: addressExternalId, + companyId: existingCompany.externalId, + startDate: new Date('2020-03-03'), + endDate: new Date('2022-02-02'), + licenceRoleId: licenceHolderRoleId + } + ] }] }) @@ -314,6 +342,14 @@ describe('Persist licence service', () => { expect(address.address1).to.equal('ENVIRONMENT AGENCY') expect(address.dataSource).to.equal('nald') expect(address.externalId).to.equal(addressExternalId) + + // Company Address - should link the company to the saved address + const companyAddress = address.companyAddresses[0] + + expect(companyAddress.addressId).to.equal(address.id) + expect(companyAddress.companyId).to.equal(company.id) + expect(companyAddress.licenceRoleId).to.equal(licenceHolderRoleId) + expect(companyAddress.endDate).to.equal(new Date('2022-02-02')) }) }) }) @@ -423,10 +459,10 @@ async function _fetchPersistedContact (externalId) { } async function _fetchPersistedAddress (externalId) { - return AddressModel.query().where('externalId', externalId).limit(1).first() + return AddressModel.query().where('externalId', externalId).limit(1).first().withGraphFetched('companyAddresses') } -function _transformedCompany (licenceRoleId, addressExternalId) { +function _transformedCompany (licenceHolderRoleId, addressExternalId) { const externalId = CompanyHelper.generateExternalId() return { @@ -444,7 +480,7 @@ function _transformedCompany (licenceRoleId, addressExternalId) { companyContact: { externalId, startDate: new Date('1999-01-01'), - licenceRoleId + licenceRoleId: licenceHolderRoleId }, addresses: [ { @@ -459,6 +495,15 @@ function _transformedCompany (licenceRoleId, addressExternalId) { postcode: 'HP11', dataSource: 'nald' } + ], + companyAddresses: [ + { + addressId: addressExternalId, + companyId: externalId, + startDate: new Date('2020-01-01'), + endDate: new Date('2022-02-02'), + licenceRoleId: licenceHolderRoleId + } ] } } diff --git a/test/validators/import/company.validator.test.js b/test/validators/import/company.validator.test.js index e1b1586ba5..dd1766c596 100644 --- a/test/validators/import/company.validator.test.js +++ b/test/validators/import/company.validator.test.js @@ -114,12 +114,64 @@ describe('Import Company validator', () => { }) }) }) + + describe('the "addresses" property', () => { + describe('when it is not an array', () => { + beforeEach(() => { + transformedCompany.addresses = 1 + }) + + it('throws an error', async () => { + expect(() => { + ImportCompanyValidator.go(transformedCompany) + }).to.throw('"addresses" must be an array') + }) + }) + describe('when it is not set', () => { + beforeEach(() => { + delete transformedCompany.addresses + }) + + it('throws an error', async () => { + expect(() => { + ImportCompanyValidator.go(transformedCompany) + }).to.throw('"addresses" is required') + }) + }) + }) + + describe('the "companyAddresses" property', () => { + describe('when it is not an array', () => { + beforeEach(() => { + transformedCompany.companyAddresses = 1 + }) + + it('throws an error', async () => { + expect(() => { + ImportCompanyValidator.go(transformedCompany) + }).to.throw('"companyAddresses" must be an array') + }) + }) + describe('when it is not set', () => { + beforeEach(() => { + delete transformedCompany.companyAddresses + }) + + it('throws an error', async () => { + expect(() => { + ImportCompanyValidator.go(transformedCompany) + }).to.throw('"companyAddresses" is required') + }) + }) + }) }) function _transformedCompany () { return { name: 'ACME', type: 'person', - externalId: '1:1940' + externalId: '1:1940', + addresses: [], + companyAddresses: [] } }