From 4c01d51f6b0324280a79b6aed0e0c7fc7471e533 Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Mon, 7 Oct 2024 11:09:32 +0100 Subject: [PATCH 01/17] Refactor the import legacy persist logic We have recently introduced functionality to import data from NALD using for a licence and companies for that particular licence. This change will break out the persist functionality into logical groups of functionality. --- .../import/legacy/process-licence.service.js | 2 +- app/services/import/persist-import.service.js | 38 ++ .../import/persist-licence.service.js | 257 --------- .../import/persist/persist-company.service.js | 140 +++++ .../persist-licence-versions.service.js | 113 ++++ .../import/persist/persist-licence.service.js | 45 ++ .../legacy/process-licence.service.test.js | 2 +- .../import/persist-licence.service.test.js | 543 ++++++++++-------- 8 files changed, 633 insertions(+), 507 deletions(-) create mode 100644 app/services/import/persist-import.service.js delete mode 100644 app/services/import/persist-licence.service.js create mode 100644 app/services/import/persist/persist-company.service.js create mode 100644 app/services/import/persist/persist-licence-versions.service.js create mode 100644 app/services/import/persist/persist-licence.service.js diff --git a/app/services/import/legacy/process-licence.service.js b/app/services/import/legacy/process-licence.service.js index e3bd4096d1..f4cc7bcc2a 100644 --- a/app/services/import/legacy/process-licence.service.js +++ b/app/services/import/legacy/process-licence.service.js @@ -6,7 +6,7 @@ */ const LicenceStructureValidator = require('../../../validators/import/licence-structure.validator.js') -const PersistLicenceService = require('../persist-licence.service.js') +const PersistLicenceService = require('../persist-import.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') diff --git a/app/services/import/persist-import.service.js b/app/services/import/persist-import.service.js new file mode 100644 index 0000000000..a99d114300 --- /dev/null +++ b/app/services/import/persist-import.service.js @@ -0,0 +1,38 @@ +'use strict' + +/** + * Creates or updates an imported licence and its child entities that have been transformed and validated + * @module PersistLicenceService + */ + +const PersistImportService = require('./persist/persist-licence.service.js') +const PersistLicenceVersionsService = require('./persist/persist-licence-versions.service.js') +const PersistCompanyService = require('./persist/persist-company.service.js') +const LicenceModel = require('../../models/licence.model.js') +const { timestampForPostgres } = require('../../lib/general.lib.js') + +/** + * Creates or updates an imported licence and its child entities that have been transformed and validated + * + * @param {object} transformedLicence - An object representing a valid WRLS licence + * @param {object[]} transformedCompanies - an array of companies representing a WRLS company + * + * @returns {Promise} + */ +async function go (transformedLicence, transformedCompanies) { + return LicenceModel.transaction(async (trx) => { + const updatedAt = timestampForPostgres() + + const id = await PersistImportService.go(trx, updatedAt, transformedLicence) + + await PersistLicenceVersionsService.go(trx, updatedAt, transformedLicence, id) + + await PersistCompanyService.go(trx, updatedAt, transformedCompanies) + + return id + }) +} + +module.exports = { + go +} diff --git a/app/services/import/persist-licence.service.js b/app/services/import/persist-licence.service.js deleted file mode 100644 index 3073a36b8c..0000000000 --- a/app/services/import/persist-licence.service.js +++ /dev/null @@ -1,257 +0,0 @@ -'use strict' - -/** - * Creates or updates an imported licence and its child entities that have been transformed and validated - * @module PersistLicenceService - */ - -const AddressModel = require('../../models/address.model.js') -const CompanyModel = require('../../models/company.model.js') -const ContactModel = require('../../models/contact.model.js') -const LicenceModel = require('../../models/licence.model.js') -const LicenceVersionModel = require('../../models/licence-version.model.js') -const LicenceVersionPurposeConditionModel = require('../../models/licence-version-purpose-condition.model.js') -const LicenceVersionPurposeModel = require('../../models/licence-version-purpose.model.js') -const { db } = require('../../../db/db.js') -const { timestampForPostgres } = require('../../lib/general.lib.js') - -/** - * Creates or updates an imported licence and its child entities that have been transformed and validated - * - * @param {object} transformedLicence - An object representing a valid WRLS licence - * @param {object[]} transformedCompanies - an array of companies representing a WRLS company - * - * @returns {Promise} - */ -async function go (transformedLicence, transformedCompanies) { - return LicenceModel.transaction(async (trx) => { - const updatedAt = timestampForPostgres() - const { id } = await _persistLicence(trx, updatedAt, transformedLicence) - - await _persistLicenceVersions(trx, updatedAt, transformedLicence.licenceVersions, id) - - await _persistCompanies(trx, updatedAt, transformedCompanies) - - return id - }) -} - -async function _persistAddress (trx, updatedAt, address) { - return AddressModel.query(trx) - .insert({ ...address, updatedAt }) - .onConflict('externalId') - .merge([ - 'address1', - 'address2', - 'address3', - 'address4', - 'address5', - 'address6', - 'country', - 'postcode', - 'updatedAt' - ]) -} - -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) - } -} - -async function _persistLicence (trx, updatedAt, licence) { - const { licenceVersions, ...propertiesToPersist } = licence - - return LicenceModel.query(trx) - .insert({ ...propertiesToPersist, updatedAt }) - .onConflict('licenceRef') - .merge([ - 'expiredDate', - 'lapsedDate', - 'regions', - 'revokedDate', - 'startDate', - 'updatedAt', - 'waterUndertaker' - ]) - .returning('id') -} - -async function _persistLicenceVersion (trx, updatedAt, licenceVersion, licenceId) { - const { licenceVersionPurposes, ...propertiesToPersist } = licenceVersion - - return LicenceVersionModel.query(trx) - .insert({ ...propertiesToPersist, licenceId, updatedAt }) - .onConflict('externalId') - .merge([ - 'endDate', - 'startDate', - 'status', - 'updatedAt' - ]) - .returning('id') -} - -async function _persistLicenceVersions (trx, updatedAt, licenceVersions, licenceId) { - for (const licenceVersion of licenceVersions) { - const { id } = await _persistLicenceVersion(trx, updatedAt, licenceVersion, licenceId) - - await _persistLicenceVersionPurposes(trx, updatedAt, licenceVersion.licenceVersionPurposes, id) - } -} - -async function _persistLicenceVersionPurpose (trx, updatedAt, licenceVersionPurpose, licenceVersionId) { - const { ...propertiesToPersist } = licenceVersionPurpose - - return LicenceVersionPurposeModel.query(trx) - .insert({ ...propertiesToPersist, licenceVersionId, updatedAt }) - .onConflict('externalId') - .merge([ - 'abstractionPeriodEndDay', - 'abstractionPeriodEndMonth', - 'abstractionPeriodStartDay', - 'abstractionPeriodStartMonth', - 'annualQuantity', - 'dailyQuantity', - 'hourlyQuantity', - 'instantQuantity', - 'notes', - 'primaryPurposeId', - 'purposeId', - 'secondaryPurposeId', - 'timeLimitedEndDate', - 'timeLimitedStartDate', - 'updatedAt' - ]) - .returning('id') -} - -async function _persistLicenceVersionPurposes (trx, updatedAt, licenceVersionPurposes, licenceVersionId) { - for (const licenceVersionPurpose of licenceVersionPurposes) { - const licenceVersionPurposeConditions = licenceVersionPurpose.licenceVersionPurposeConditions - - const { id } = await _persistLicenceVersionPurpose( - trx, updatedAt, licenceVersionPurpose, licenceVersionId) - - await _persistLicenceVersionPurposeConditions(trx, updatedAt, licenceVersionPurposeConditions, id) - } -} - -async function _persistLicenceVersionPurposeConditions ( - trx, updatedAt, licenceVersionPurposeConditions, licenceVersionPurposeId) { - for (const licenceVersionPurposeCondition of licenceVersionPurposeConditions) { - await _persistLicenceVersionPurposeCondition( - trx, updatedAt, licenceVersionPurposeCondition, licenceVersionPurposeId) - } -} - -async function _persistLicenceVersionPurposeCondition ( - trx, updatedAt, licenceVersionPurposeConditions, licenceVersionPurposeId) { - const { ...propertiesToPersist } = licenceVersionPurposeConditions - - return LicenceVersionPurposeConditionModel.query(trx) - .insert({ ...propertiesToPersist, licenceVersionPurposeId, updatedAt }) - .onConflict('externalId') - .merge([ - 'licenceVersionPurposeConditionTypeId', - 'param1', - 'param2', - 'notes', - 'updatedAt' - ]) - .returning('id') -} - -// Companies -async function _persistCompanies (trx, updatedAt, companies) { - for (const company of companies) { - await _persistCompany(trx, updatedAt, company) - - if (company.contact) { - await _persistContact(trx, updatedAt, company.contact) - } - - if (company.companyContact) { - await _persistsCompanyContact(trx, updatedAt, company.companyContact) - } - - await _persistAddresses(trx, updatedAt, company.addresses) - - await _persistCompanyAddresses(trx, updatedAt, company.companyAddresses) - } -} - -async function _persistCompany (trx, updatedAt, company) { - const { contact, companyContact, addresses, companyAddresses, ...propertiesToPersist } = company - - return CompanyModel.query(trx) - .insert({ ...propertiesToPersist, updatedAt }) - .onConflict('externalId') - .merge([ - 'name', - 'type', - 'updatedAt' - ]) -} - -async function _persistContact (trx, updatedAt, contact) { - return ContactModel.query(trx) - .insert({ ...contact, updatedAt }) - .onConflict('externalId') - .merge([ - 'salutation', - 'initials', - 'firstName', - 'lastName', - 'updatedAt' - ]) -} - -async function _persistsCompanyContact (trx, updatedAt, companyContact) { - const { externalId, startDate, licenceRoleId } = companyContact - - return db.raw(` - INSERT INTO public."company_contacts" (company_id, contact_id, licence_role_id, start_date, "default", created_at, updated_at) - SELECT com.id, con.id, lr.id, ?, true, NOW(), ? - FROM public.companies com - JOIN public."licence_roles" lr on lr.id = ? - JOIN public.contacts con ON con.external_id = ? - WHERE com.external_id = ? - ON CONFLICT (company_id, contact_id, licence_role_id, start_date) - DO UPDATE SET - contact_id = EXCLUDED.contact_id, - "default" = EXCLUDED."default", - updated_at = EXCLUDED.updated_at - `, [startDate, updatedAt, licenceRoleId, externalId, externalId]) - .transacting(trx) -} - -module.exports = { - go -} diff --git a/app/services/import/persist/persist-company.service.js b/app/services/import/persist/persist-company.service.js new file mode 100644 index 0000000000..6b84edf68a --- /dev/null +++ b/app/services/import/persist/persist-company.service.js @@ -0,0 +1,140 @@ +'use strict' + +/** + * Creates or updates an imported licence and its child entities that have been transformed and validated + * @module PersistCompanyService + */ + +const AddressModel = require('../../../models/address.model.js') +const CompanyModel = require('../../../models/company.model.js') +const ContactModel = require('../../../models/contact.model.js') +const { db } = require('../../../../db/db.js') + +/** + * Creates or updates an imported licence and its child entities that have been transformed and validated + * + * @param trx + * @param updatedAt + * @param {object} transformedCompanies - An object representing a valid WRLS licence + * + * @returns {Promise} - the licence id from WRLS + */ +async function go (trx, updatedAt, transformedCompanies) { + await _persistCompanies(trx, updatedAt, transformedCompanies) +} + +async function _persistAddress (trx, updatedAt, address) { + return AddressModel.query(trx) + .insert({ ...address, updatedAt }) + .onConflict('externalId') + .merge([ + 'address1', + 'address2', + 'address3', + 'address4', + 'address5', + 'address6', + 'country', + 'postcode', + 'updatedAt' + ]) +} + +async function _persistAddresses (trx, updatedAt, addresses) { + for (const address of addresses) { + await _persistAddress(trx, updatedAt, address) + } +} + +async function _persistCompanies (trx, updatedAt, companies) { + for (const company of companies) { + await _persistCompany(trx, updatedAt, company) + + if (company.contact) { + await _persistContact(trx, updatedAt, company.contact) + } + + if (company.companyContact) { + await _persistsCompanyContact(trx, updatedAt, company.companyContact) + } + + await _persistAddresses(trx, updatedAt, company.addresses) + + await _persistCompanyAddresses(trx, updatedAt, company.companyAddresses) + } +} + +async function _persistCompany (trx, updatedAt, company) { + const { contact, companyContact, addresses, companyAddresses, ...propertiesToPersist } = company + + return CompanyModel.query(trx) + .insert({ ...propertiesToPersist, updatedAt }) + .onConflict('externalId') + .merge([ + 'name', + 'type', + 'updatedAt' + ]) +} + +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 _persistsCompanyContact (trx, updatedAt, companyContact) { + const { externalId, startDate, licenceRoleId } = companyContact + + return db.raw(` + INSERT INTO public."company_contacts" (company_id, contact_id, licence_role_id, start_date, "default", created_at, updated_at) + SELECT com.id, con.id, lr.id, ?, true, NOW(), ? + FROM public.companies com + JOIN public."licence_roles" lr on lr.id = ? + JOIN public.contacts con ON con.external_id = ? + WHERE com.external_id = ? + ON CONFLICT (company_id, contact_id, licence_role_id, start_date) + DO UPDATE SET + contact_id = EXCLUDED.contact_id, + "default" = EXCLUDED."default", + updated_at = EXCLUDED.updated_at + `, [startDate, updatedAt, licenceRoleId, externalId, externalId]) + .transacting(trx) +} + +async function _persistContact (trx, updatedAt, contact) { + return ContactModel.query(trx) + .insert({ ...contact, updatedAt }) + .onConflict('externalId') + .merge([ + 'salutation', + 'initials', + 'firstName', + 'lastName', + 'updatedAt' + ]) +} + +module.exports = { + go +} diff --git a/app/services/import/persist/persist-licence-versions.service.js b/app/services/import/persist/persist-licence-versions.service.js new file mode 100644 index 0000000000..f8a1e50351 --- /dev/null +++ b/app/services/import/persist/persist-licence-versions.service.js @@ -0,0 +1,113 @@ +'use strict' + +/** + * Creates or updates an imported licence and its child entities that have been transformed and validated + * @module PersistLicenceVersionsService + */ + +const LicenceVersionPurposeConditionModel = require('../../../models/licence-version-purpose-condition.model') +const LicenceVersionPurposeModel = require('../../../models/licence-version-purpose.model') +const LicenceVersionModel = require('../../../models/licence-version.model') + +/** + * Creates or updates an imported licence and its child entities that have been transformed and validated + * + * @param trx + * @param updatedAt + * @param {object} transformedLicence - An object representing a valid WRLS licence + * + * @param id + * @returns {Promise} - the licence id from WRLS + */ +async function go (trx, updatedAt, transformedLicence, id) { + await _persistLicenceVersions(trx, updatedAt, transformedLicence.licenceVersions, id) +} + +async function _persistLicenceVersion (trx, updatedAt, licenceVersion, licenceId) { + const { licenceVersionPurposes, ...propertiesToPersist } = licenceVersion + + return LicenceVersionModel.query(trx) + .insert({ ...propertiesToPersist, licenceId, updatedAt }) + .onConflict('externalId') + .merge([ + 'endDate', + 'startDate', + 'status', + 'updatedAt' + ]) + .returning('id') +} + +async function _persistLicenceVersions (trx, updatedAt, licenceVersions, licenceId) { + for (const licenceVersion of licenceVersions) { + const { id } = await _persistLicenceVersion(trx, updatedAt, licenceVersion, licenceId) + + await _persistLicenceVersionPurposes(trx, updatedAt, licenceVersion.licenceVersionPurposes, id) + } +} + +async function _persistLicenceVersionPurpose (trx, updatedAt, licenceVersionPurpose, licenceVersionId) { + const { ...propertiesToPersist } = licenceVersionPurpose + + return LicenceVersionPurposeModel.query(trx) + .insert({ ...propertiesToPersist, licenceVersionId, updatedAt }) + .onConflict('externalId') + .merge([ + 'abstractionPeriodEndDay', + 'abstractionPeriodEndMonth', + 'abstractionPeriodStartDay', + 'abstractionPeriodStartMonth', + 'annualQuantity', + 'dailyQuantity', + 'hourlyQuantity', + 'instantQuantity', + 'notes', + 'primaryPurposeId', + 'purposeId', + 'secondaryPurposeId', + 'timeLimitedEndDate', + 'timeLimitedStartDate', + 'updatedAt' + ]) + .returning('id') +} + +async function _persistLicenceVersionPurposes (trx, updatedAt, licenceVersionPurposes, licenceVersionId) { + for (const licenceVersionPurpose of licenceVersionPurposes) { + const licenceVersionPurposeConditions = licenceVersionPurpose.licenceVersionPurposeConditions + + const { id } = await _persistLicenceVersionPurpose( + trx, updatedAt, licenceVersionPurpose, licenceVersionId) + + await _persistLicenceVersionPurposeConditions(trx, updatedAt, licenceVersionPurposeConditions, id) + } +} + +async function _persistLicenceVersionPurposeCondition ( + trx, updatedAt, licenceVersionPurposeConditions, licenceVersionPurposeId) { + const { ...propertiesToPersist } = licenceVersionPurposeConditions + + return LicenceVersionPurposeConditionModel.query(trx) + .insert({ ...propertiesToPersist, licenceVersionPurposeId, updatedAt }) + .onConflict('externalId') + .merge([ + 'licenceVersionPurposeConditionTypeId', + 'param1', + 'param2', + 'notes', + 'updatedAt' + ]) + .returning('id') +} + +async function _persistLicenceVersionPurposeConditions ( + trx, updatedAt, licenceVersionPurposeConditions, licenceVersionPurposeId) { + for (const licenceVersionPurposeCondition of licenceVersionPurposeConditions) { + await _persistLicenceVersionPurposeCondition( + trx, updatedAt, licenceVersionPurposeCondition, licenceVersionPurposeId) + } +} + +module.exports = { + go +} diff --git a/app/services/import/persist/persist-licence.service.js b/app/services/import/persist/persist-licence.service.js new file mode 100644 index 0000000000..0987748055 --- /dev/null +++ b/app/services/import/persist/persist-licence.service.js @@ -0,0 +1,45 @@ +'use strict' + +/** + * Creates or updates an imported licence and its child entities that have been transformed and validated + * @module PersistLicenceService + */ + +const LicenceModel = require('../../../models/licence.model.js') + +/** + * Creates or updates an imported licence and its child entities that have been transformed and validated + * + * @param trx + * @param updatedAt + * @param {object} transformedLicence - An object representing a valid WRLS licence + * + * @returns {Promise} - the licence id from WRLS + */ +async function go (trx, updatedAt, transformedLicence) { + const { id } = await _persistLicence(trx, updatedAt, transformedLicence) + + return id +} + +async function _persistLicence (trx, updatedAt, licence) { + const { licenceVersions, ...propertiesToPersist } = licence + + return LicenceModel.query(trx) + .insert({ ...propertiesToPersist, updatedAt }) + .onConflict('licenceRef') + .merge([ + 'expiredDate', + 'lapsedDate', + 'regions', + 'revokedDate', + 'startDate', + 'updatedAt', + 'waterUndertaker' + ]) + .returning('id') +} + +module.exports = { + go +} diff --git a/test/services/import/legacy/process-licence.service.test.js b/test/services/import/legacy/process-licence.service.test.js index 5833b49edd..c01d3272b6 100644 --- a/test/services/import/legacy/process-licence.service.test.js +++ b/test/services/import/legacy/process-licence.service.test.js @@ -13,7 +13,7 @@ const { generateUUID } = require('../../../../app/lib/general.lib.js') const { generateLicenceRef } = require('../../../support/helpers/licence.helper.js') // Things to stub -const PersistLicenceService = require('../../../../app/services/import/persist-licence.service.js') +const PersistLicenceService = require('../../../../app/services/import/persist-import.service.js') 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') diff --git a/test/services/import/persist-licence.service.test.js b/test/services/import/persist-licence.service.test.js index a4628676fd..cde2f5b9d3 100644 --- a/test/services/import/persist-licence.service.test.js +++ b/test/services/import/persist-licence.service.test.js @@ -30,21 +30,26 @@ const SecondaryPurposeHelper = require('../../support/helpers/secondary-purpose. const { randomInteger } = require('../../support/general.js') // Thing under test -const PersistLicenceService = require('../../../app/services/import/persist-licence.service.js') +const PersistImportService = require('../../../app/services/import/persist-import.service.js') -describe('Persist licence service', () => { +describe.only('Persist licence service', () => { const companyContactStartDate = new Date('1999-01-01') + let address + let addressExternalId + let company + let contact + let licenceHolderRoleId let licenceVersionPurposeConditionType + let newLicence let primaryPurpose let purpose let region + let result let secondaryPurpose let transformedCompanies let transformedCompany let transformedLicence - let licenceHolderRoleId - let addressExternalId beforeEach(async () => { licenceVersionPurposeConditionType = LicenceVersionPurposeConditionTypeHelper.select() @@ -67,80 +72,97 @@ describe('Persist licence service', () => { describe('when given a valid transformed licence', () => { describe('and that licence does not already exist', () => { - it('creates a new licence record plus child records in WRLS and returns the licence ID', async () => { - const result = await PersistLicenceService.go(transformedLicence, transformedCompanies) + beforeEach(async () => { + // Call the thing under test + result = await PersistImportService.go(transformedLicence, transformedCompanies) + + // Get the persisted data + newLicence = await _fetchPersistedLicence(transformedLicence.licenceRef) + company = await _fetchPersistedCompany(transformedCompany.externalId) + contact = await _fetchPersistedContact(transformedCompany.externalId) + address = await _fetchPersistedAddress(transformedCompany.addresses[0].externalId) + }) + + describe('address', () => { + it('creates an address', () => { + expect(address.address1).to.equal('4 Privet Drive') + expect(address.address2).to.be.null() + expect(address.address5).to.equal('Little Whinging') + expect(address.address6).to.equal('Surrey') + expect(address.country).to.equal('United Kingdom') + expect(address.dataSource).to.equal('nald') + expect(address.externalId).to.equal(addressExternalId) + expect(address.postcode).to.equal('HP11') + expect(address.uprn).to.be.null() + }) + }) - const newLicence = await _fetchPersistedLicence(transformedLicence.licenceRef) + describe('licence', () => { + it('creates a new licence record plus child records in WRLS and returns the licence ID', () => { + expect(result).to.equal(newLicence.id) + }) - // Licence - expect(result).to.equal(newLicence.id) - - // Licence version - const newLicenceVersion = newLicence.licenceVersions[0] - - expect(newLicenceVersion.externalId).to.equal(transformedLicence.licenceVersions[0].externalId) - - // Licence version purpose - const newLicenceVersionPurpose = newLicence.licenceVersions[0].licenceVersionPurposes[0] - - expect(newLicenceVersionPurpose.externalId).to.equal( - transformedLicence.licenceVersions[0].licenceVersionPurposes[0].externalId - ) - - // Licence version purpose conditions - const newLicenceVersionPurposeCondition = newLicence.licenceVersions[0] - .licenceVersionPurposes[0].licenceVersionPurposeConditions[0] - - expect(newLicenceVersionPurposeCondition.externalId).to.equal( - transformedLicence.licenceVersions[0].licenceVersionPurposes[0].licenceVersionPurposeConditions[0].externalId) - - // Companies - const company = await _fetchPersistedCompany(transformedCompany.externalId) - - expect(company.name).to.equal('ACME') - expect(company.type).to.equal('person') - expect(company.externalId).to.equal(transformedCompany.externalId) - - // Contact - const contact = await _fetchPersistedContact(transformedCompany.externalId) - - expect(contact.salutation).to.equal('Mr') - expect(contact.initials).to.equal('H') - expect(contact.firstName).to.equal('James') - expect(contact.lastName).to.equal('Bond') - expect(contact.dataSource).to.equal('nald') - - // Company contact - the company and contact id are used to relate the contact to the company - const companyContact = contact.companyContacts[0] - - expect(companyContact.companyId).to.equal(company.id) - expect(companyContact.contactId).to.equal(contact.id) - expect(companyContact.licenceRoleId).to.equal(licenceHolderRoleId) - expect(companyContact.startDate).to.equal(transformedCompany.companyContact.startDate) - expect(companyContact.default).to.be.true() - - // Addresses - const address = await _fetchPersistedAddress(transformedCompany.addresses[0].externalId) - - expect(address.address1).to.equal('4 Privet Drive') - expect(address.address2).to.be.null() - expect(address.address5).to.equal('Little Whinging') - expect(address.address6).to.equal('Surrey') - expect(address.country).to.equal('United Kingdom') - expect(address.dataSource).to.equal('nald') - 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')) + it('creates a licence version', () => { + const newLicenceVersion = newLicence.licenceVersions[0] + + expect(newLicenceVersion.externalId).to.equal(transformedLicence.licenceVersions[0].externalId) + }) + + it('creates a licence version purpose', () => { + const newLicenceVersionPurpose = newLicence.licenceVersions[0].licenceVersionPurposes[0] + + expect(newLicenceVersionPurpose.externalId).to.equal( + transformedLicence.licenceVersions[0].licenceVersionPurposes[0].externalId + ) + }) + + it('creates a licence version purpose conditions', () => { + const newLicenceVersionPurposeCondition = newLicence.licenceVersions[0] + .licenceVersionPurposes[0].licenceVersionPurposeConditions[0] + + expect(newLicenceVersionPurposeCondition.externalId).to.equal( + transformedLicence.licenceVersions[0].licenceVersionPurposes[0] + .licenceVersionPurposeConditions[0].externalId) + }) + }) + + describe('contact', () => { + it('creates a contact', () => { + expect(contact.salutation).to.equal('Mr') + expect(contact.initials).to.equal('H') + expect(contact.firstName).to.equal('James') + expect(contact.lastName).to.equal('Bond') + expect(contact.dataSource).to.equal('nald') + }) + }) + + describe('company, companyContact and companyAddresses', () => { + it('creates a company', () => { + expect(company.name).to.equal('ACME') + expect(company.type).to.equal('person') + expect(company.externalId).to.equal(transformedCompany.externalId) + }) + + it('creates a new company contact - links the company to the contact', () => { + const companyContact = contact.companyContacts[0] + + expect(companyContact.companyId).to.equal(company.id) + expect(companyContact.contactId).to.equal(contact.id) + expect(companyContact.licenceRoleId).to.equal(licenceHolderRoleId) + expect(companyContact.startDate).to.equal(transformedCompany.companyContact.startDate) + expect(companyContact.default).to.be.true() + }) + + it('creates a new company address - links the company to the contact', () => { + 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')) + }) }) }) @@ -152,8 +174,15 @@ describe('Persist licence service', () => { let existingLicenceVersion let existingLicenceVersionPurpose let existingLicenceVersionPurposeCondition + let updatedLicence beforeEach(async () => { + // Address Data + exisitngAddress = await AddressHelper.add({ + ...transformedCompany.addresses[0] + }) + + // Licence existingLicence = await LicenceHelper.add({ expiredDate: new Date('2052-06-23'), lapsedDate: new Date('2050-07-24'), @@ -168,6 +197,7 @@ describe('Persist licence service', () => { revokedDate: new Date('2049-08-25'), startDate: new Date('1992-08-19') }) + existingLicenceVersion = await LicenceVersionHelper.add({ endDate: new Date('2052-06-23'), externalId: transformedLicence.licenceVersions[0].externalId, @@ -175,6 +205,7 @@ describe('Persist licence service', () => { startDate: new Date('1999-01-01'), status: 'current' }) + existingLicenceVersionPurpose = await LicenceVersionPurposeHelper.add({ abstractionPeriodEndDay: 30, abstractionPeriodEndMonth: 9, @@ -202,11 +233,13 @@ describe('Persist licence service', () => { source: 'nald' }) - existingCompany = await CompanyHelper.add({ + // Contact Data + exisitngContact = await ContactHelper.add({ externalId: transformedCompany.externalId }) - exisitngContact = await ContactHelper.add({ + // Company Data + existingCompany = await CompanyHelper.add({ externalId: transformedCompany.externalId }) @@ -217,10 +250,6 @@ describe('Persist licence service', () => { startDate: companyContactStartDate }) - exisitngAddress = await AddressHelper.add({ - ...transformedCompany.addresses[0] - }) - await CompanyAddressHelper.add({ addressId: exisitngAddress.id, companyId: existingCompany.id, @@ -251,105 +280,123 @@ describe('Persist licence service', () => { } ] }] + + // Call the thing under test + result = await PersistImportService.go(transformedLicence, transformedCompanies) + + // Get the persisted data + updatedLicence = await _fetchPersistedLicence(transformedLicence.licenceRef) + newLicence = await _fetchPersistedLicence(transformedLicence.licenceRef) + company = await _fetchPersistedCompany(existingCompany.externalId) + contact = await _fetchPersistedContact(exisitngContact.externalId) + address = await _fetchPersistedAddress(transformedCompany.addresses[0].externalId) }) - it('updates the licence record plus child records in WRLS and returns the licence ID', async () => { - const result = await PersistLicenceService.go(transformedLicence, transformedCompanies) - - expect(result).to.equal(existingLicence.id) - - // Licence comparison - const updatedLicence = await _fetchPersistedLicence(transformedLicence.licenceRef) - - expect(updatedLicence.expiredDate).to.equal(transformedLicence.expiredDate) - expect(updatedLicence.lapsedDate).to.equal(transformedLicence.lapsedDate) - expect(updatedLicence.regions).to.equal(transformedLicence.regions) - expect(updatedLicence.revokedDate).to.equal(transformedLicence.revokedDate) - expect(updatedLicence.startDate).to.equal(transformedLicence.startDate) - - // Licence version comparison - const updatedLicVer = updatedLicence.licenceVersions[0] - const transformedLicVer = transformedLicence.licenceVersions[0] - - expect(updatedLicVer.id).to.equal(existingLicenceVersion.id) - expect(updatedLicVer.endDate).to.equal(transformedLicVer.endDate) - expect(updatedLicVer.startDate).to.equal(transformedLicVer.startDate) - expect(updatedLicVer.status).to.equal(transformedLicVer.status) - - // Licence version purpose comparison - const updatedLicVerPur = updatedLicence.licenceVersions[0].licenceVersionPurposes[0] - const transformedLicVerPur = transformedLicence.licenceVersions[0].licenceVersionPurposes[0] - - expect(updatedLicVerPur.id).to.equal(existingLicenceVersionPurpose.id) - expect(updatedLicVerPur.abstractionPeriodEndDay).to.equal(transformedLicVerPur.abstractionPeriodEndDay) - expect(updatedLicVerPur.abstractionPeriodEndMonth).to.equal(transformedLicVerPur.abstractionPeriodEndMonth) - expect(updatedLicVerPur.abstractionPeriodStartDay).to.equal(transformedLicVerPur.abstractionPeriodStartDay) - expect(updatedLicVerPur.abstractionPeriodStartMonth).to.equal(transformedLicVerPur.abstractionPeriodStartMonth) - expect(updatedLicVerPur.annualQuantity).to.equal(transformedLicVerPur.annualQuantity) - expect(updatedLicVerPur.dailyQuantity).to.equal(transformedLicVerPur.dailyQuantity) - expect(updatedLicVerPur.hourlyQuantity).to.equal(transformedLicVerPur.hourlyQuantity) - expect(updatedLicVerPur.instantQuantity).to.equal(transformedLicVerPur.instantQuantity) - expect(updatedLicVerPur.notes).to.equal(transformedLicVerPur.notes) - expect(updatedLicVerPur.primaryPurposeId).to.equal(transformedLicVerPur.primaryPurposeId) - expect(updatedLicVerPur.purposeId).to.equal(transformedLicVerPur.purposeId) - expect(updatedLicVerPur.secondaryPurposeId).to.equal(transformedLicVerPur.secondaryPurposeId) - expect(updatedLicVerPur.timeLimitedEndDate).to.equal(transformedLicVerPur.timeLimitedEndDate) - expect(updatedLicVerPur.timeLimitedStartDate).to.equal(transformedLicVerPur.timeLimitedStartDate) - - // Licence version purpose conditions comparison - const updatedLicVerPurCon = updatedLicence.licenceVersions[0] - .licenceVersionPurposes[0].licenceVersionPurposeConditions[0] - const transformedLicVerPurCon = transformedLicence.licenceVersions[0] - .licenceVersionPurposes[0].licenceVersionPurposeConditions[0] - - expect(updatedLicVerPurCon.id).to.equal(existingLicenceVersionPurposeCondition.id) - expect(updatedLicVerPurCon.externalId).to.equal(transformedLicVerPurCon.externalId) - expect(updatedLicVerPurCon.licenceVersionPurposeConditionTypeId) - .to.equal(transformedLicVerPurCon.licenceVersionPurposeConditionTypeId) - expect(updatedLicVerPurCon.notes).to.equal(transformedLicVerPurCon.notes) - expect(updatedLicVerPurCon.param1).to.equal(transformedLicVerPurCon.param1) - expect(updatedLicVerPurCon.param2).to.equal(transformedLicVerPurCon.param2) - expect(updatedLicVerPurCon.source).to.equal(transformedLicVerPurCon.source) - - // Companies - const company = await _fetchPersistedCompany(existingCompany.externalId) - - expect(company.name).to.equal('Example Trading Ltd') - expect(company.type).to.equal('organisation') - expect(company.externalId).to.equal(existingCompany.externalId) - - // Contact - const contact = await _fetchPersistedContact(exisitngContact.externalId) - - expect(contact.salutation).to.be.null() - expect(contact.initials).to.be.null() - expect(contact.firstName).to.equal('Amara') - expect(contact.lastName).to.equal('Gupta') - expect(contact.dataSource).to.equal('wrls') - - // Company contact - the company and contact id are used to relate the contact to the company - const companyContact = contact.companyContacts[0] - - expect(companyContact.companyId).to.equal(existingCompany.id) - expect(companyContact.contactId).to.equal(exisitngContact.id) - expect(companyContact.licenceRoleId).to.equal(licenceHolderRoleId) - expect(companyContact.startDate).to.equal(new Date('1999-01-01')) - expect(companyContact.default).to.be.true() - - // Addresses - const address = await _fetchPersistedAddress(transformedCompany.addresses[0].externalId) - - 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')) + describe('address', () => { + it('updates an address', () => { + expect(address.address1).to.equal('ENVIRONMENT AGENCY') + expect(address.dataSource).to.equal('nald') + expect(address.externalId).to.equal(addressExternalId) + }) + }) + + describe('company, companyContact and companyAddresses', () => { + it('updates a company', () => { + expect(company.name).to.equal('Example Trading Ltd') + expect(company.type).to.equal('organisation') + expect(company.externalId).to.equal(existingCompany.externalId) + }) + + it('updates a company contact - links the company to the contact', () => { + const companyContact = contact.companyContacts[0] + + expect(companyContact.companyId).to.equal(existingCompany.id) + expect(companyContact.contactId).to.equal(exisitngContact.id) + expect(companyContact.licenceRoleId).to.equal(licenceHolderRoleId) + expect(companyContact.startDate).to.equal(new Date('1999-01-01')) + expect(companyContact.default).to.be.true() + }) + + it('updates a company address - links the company to the 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')) + }) + }) + + describe('contact', () => { + it('updates a contact', () => { + expect(contact.salutation).to.be.null() + expect(contact.initials).to.be.null() + expect(contact.firstName).to.equal('Amara') + expect(contact.lastName).to.equal('Gupta') + expect(contact.dataSource).to.equal('wrls') + }) + }) + + describe('licence', () => { + it('should return the updates licence id', () => { + expect(result).to.equal(existingLicence.id) + }) + + it('checks the updated licence', async () => { + expect(updatedLicence.expiredDate).to.equal(transformedLicence.expiredDate) + expect(updatedLicence.lapsedDate).to.equal(transformedLicence.lapsedDate) + expect(updatedLicence.regions).to.equal(transformedLicence.regions) + expect(updatedLicence.revokedDate).to.equal(transformedLicence.revokedDate) + expect(updatedLicence.startDate).to.equal(transformedLicence.startDate) + }) + + it('updates a licence version', () => { + const updatedLicVer = updatedLicence.licenceVersions[0] + const transformedLicVer = transformedLicence.licenceVersions[0] + + expect(updatedLicVer.id).to.equal(existingLicenceVersion.id) + expect(updatedLicVer.endDate).to.equal(transformedLicVer.endDate) + expect(updatedLicVer.startDate).to.equal(transformedLicVer.startDate) + expect(updatedLicVer.status).to.equal(transformedLicVer.status) + }) + + it('updates a licence version purpose', () => { + const updatedLicVerPur = updatedLicence.licenceVersions[0].licenceVersionPurposes[0] + const transformedLicVerPur = transformedLicence.licenceVersions[0].licenceVersionPurposes[0] + + expect(updatedLicVerPur.id).to.equal(existingLicenceVersionPurpose.id) + expect(updatedLicVerPur.abstractionPeriodEndDay).to.equal(transformedLicVerPur.abstractionPeriodEndDay) + expect(updatedLicVerPur.abstractionPeriodEndMonth).to.equal(transformedLicVerPur.abstractionPeriodEndMonth) + expect(updatedLicVerPur.abstractionPeriodStartDay).to.equal(transformedLicVerPur.abstractionPeriodStartDay) + expect(updatedLicVerPur.abstractionPeriodStartMonth).to + .equal(transformedLicVerPur.abstractionPeriodStartMonth) + expect(updatedLicVerPur.annualQuantity).to.equal(transformedLicVerPur.annualQuantity) + expect(updatedLicVerPur.dailyQuantity).to.equal(transformedLicVerPur.dailyQuantity) + expect(updatedLicVerPur.hourlyQuantity).to.equal(transformedLicVerPur.hourlyQuantity) + expect(updatedLicVerPur.instantQuantity).to.equal(transformedLicVerPur.instantQuantity) + expect(updatedLicVerPur.notes).to.equal(transformedLicVerPur.notes) + expect(updatedLicVerPur.primaryPurposeId).to.equal(transformedLicVerPur.primaryPurposeId) + expect(updatedLicVerPur.purposeId).to.equal(transformedLicVerPur.purposeId) + expect(updatedLicVerPur.secondaryPurposeId).to.equal(transformedLicVerPur.secondaryPurposeId) + expect(updatedLicVerPur.timeLimitedEndDate).to.equal(transformedLicVerPur.timeLimitedEndDate) + expect(updatedLicVerPur.timeLimitedStartDate).to.equal(transformedLicVerPur.timeLimitedStartDate) + }) + + it('updates a licence version purpose conditions', () => { + const updatedLicVerPurCon = updatedLicence.licenceVersions[0] + .licenceVersionPurposes[0].licenceVersionPurposeConditions[0] + const transformedLicVerPurCon = transformedLicence.licenceVersions[0] + .licenceVersionPurposes[0].licenceVersionPurposeConditions[0] + + expect(updatedLicVerPurCon.id).to.equal(existingLicenceVersionPurposeCondition.id) + expect(updatedLicVerPurCon.externalId).to.equal(transformedLicVerPurCon.externalId) + expect(updatedLicVerPurCon.licenceVersionPurposeConditionTypeId) + .to.equal(transformedLicVerPurCon.licenceVersionPurposeConditionTypeId) + expect(updatedLicVerPurCon.notes).to.equal(transformedLicVerPurCon.notes) + expect(updatedLicVerPurCon.param1).to.equal(transformedLicVerPurCon.param1) + expect(updatedLicVerPurCon.param2).to.equal(transformedLicVerPurCon.param2) + expect(updatedLicVerPurCon.source).to.equal(transformedLicVerPurCon.source) + }) }) }) }) @@ -362,7 +409,7 @@ describe('Persist licence service', () => { }) it('throws an error and persists nothing', async () => { - await expect(PersistLicenceService.go(transformedLicence)).to.reject() + await expect(PersistImportService.go(transformedLicence, transformedCompanies)).to.reject() const newLicence = await _fetchPersistedLicence(transformedLicence.licenceRef) @@ -371,6 +418,28 @@ describe('Persist licence service', () => { }) }) +async function _fetchPersistedAddress (externalId) { + return AddressModel.query().where('externalId', externalId).limit(1).first().withGraphFetched('companyAddresses') +} + +async function _fetchPersistedCompany (externalId) { + return CompanyModel + .query() + .where('externalId', externalId) + .withGraphFetched('companyContacts') + .limit(1) + .first() +} + +async function _fetchPersistedContact (externalId) { + return ContactModel + .query() + .where('externalId', externalId) + .withGraphFetched('companyContacts') + .limit(1) + .first() +} + async function _fetchPersistedLicence (licenceRef) { return LicenceModel .query() @@ -382,6 +451,52 @@ async function _fetchPersistedLicence (licenceRef) { .first() } +function _transformedCompany (licenceHolderRoleId, addressExternalId) { + const externalId = CompanyHelper.generateExternalId() + + return { + externalId, + name: 'ACME', + type: 'person', + contact: { + salutation: 'Mr', + initials: 'H', + firstName: 'James', + lastName: 'Bond', + externalId, + dataSource: 'nald' + }, + companyContact: { + externalId, + startDate: new Date('1999-01-01'), + licenceRoleId: licenceHolderRoleId + }, + addresses: [ + { + address1: '4 Privet Drive', + address2: null, + address3: null, + address4: null, + address5: 'Little Whinging', + address6: 'Surrey', + country: 'United Kingdom', + externalId: addressExternalId, + postcode: 'HP11', + dataSource: 'nald' + } + ], + companyAddresses: [ + { + addressId: addressExternalId, + companyId: externalId, + startDate: new Date('2020-01-01'), + endDate: new Date('2022-02-02'), + licenceRoleId: licenceHolderRoleId + } + ] + } +} + function _transformedLicence (regionId, primaryPurposeId, purposeId, secondaryPurposeId, licenceVersionPurposeConditionTypeId) { return { @@ -439,71 +554,3 @@ function _transformedLicence (regionId, primaryPurposeId, purposeId, secondaryPu ] } } - -async function _fetchPersistedCompany (externalId) { - return CompanyModel - .query() - .where('externalId', externalId) - .withGraphFetched('companyContacts') - .limit(1) - .first() -} - -async function _fetchPersistedContact (externalId) { - return ContactModel - .query() - .where('externalId', externalId) - .withGraphFetched('companyContacts') - .limit(1) - .first() -} - -async function _fetchPersistedAddress (externalId) { - return AddressModel.query().where('externalId', externalId).limit(1).first().withGraphFetched('companyAddresses') -} - -function _transformedCompany (licenceHolderRoleId, addressExternalId) { - const externalId = CompanyHelper.generateExternalId() - - return { - externalId, - name: 'ACME', - type: 'person', - contact: { - salutation: 'Mr', - initials: 'H', - firstName: 'James', - lastName: 'Bond', - externalId, - dataSource: 'nald' - }, - companyContact: { - externalId, - startDate: new Date('1999-01-01'), - licenceRoleId: licenceHolderRoleId - }, - addresses: [ - { - address1: '4 Privet Drive', - address2: null, - address3: null, - address4: null, - address5: 'Little Whinging', - address6: 'Surrey', - country: 'United Kingdom', - externalId: addressExternalId, - postcode: 'HP11', - dataSource: 'nald' - } - ], - companyAddresses: [ - { - addressId: addressExternalId, - companyId: externalId, - startDate: new Date('2020-01-01'), - endDate: new Date('2022-02-02'), - licenceRoleId: licenceHolderRoleId - } - ] - } -} From aa47f0cd5d5bafef0308f30d98ff7a2823aeb883 Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Mon, 7 Oct 2024 11:12:11 +0100 Subject: [PATCH 02/17] chore: remove only in test --- test/services/import/persist-licence.service.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/services/import/persist-licence.service.test.js b/test/services/import/persist-licence.service.test.js index cde2f5b9d3..36d457d19a 100644 --- a/test/services/import/persist-licence.service.test.js +++ b/test/services/import/persist-licence.service.test.js @@ -32,7 +32,7 @@ const { randomInteger } = require('../../support/general.js') // Thing under test const PersistImportService = require('../../../app/services/import/persist-import.service.js') -describe.only('Persist licence service', () => { +describe('Persist licence service', () => { const companyContactStartDate = new Date('1999-01-01') let address From 02f42992a618b85c3efb9cbaa04f4a8ca546d879 Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Mon, 7 Oct 2024 11:14:36 +0100 Subject: [PATCH 03/17] chore: fix missing file extensions --- .../import/persist/persist-licence-versions.service.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/services/import/persist/persist-licence-versions.service.js b/app/services/import/persist/persist-licence-versions.service.js index f8a1e50351..2e6a15d76c 100644 --- a/app/services/import/persist/persist-licence-versions.service.js +++ b/app/services/import/persist/persist-licence-versions.service.js @@ -5,9 +5,9 @@ * @module PersistLicenceVersionsService */ -const LicenceVersionPurposeConditionModel = require('../../../models/licence-version-purpose-condition.model') -const LicenceVersionPurposeModel = require('../../../models/licence-version-purpose.model') -const LicenceVersionModel = require('../../../models/licence-version.model') +const LicenceVersionPurposeConditionModel = require('../../../models/licence-version-purpose-condition.model.js') +const LicenceVersionPurposeModel = require('../../../models/licence-version-purpose.model.js') +const LicenceVersionModel = require('../../../models/licence-version.model.js') /** * Creates or updates an imported licence and its child entities that have been transformed and validated From 8f3295e70d6f8ce7d2616c078557de462f02c9e7 Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Mon, 7 Oct 2024 11:19:10 +0100 Subject: [PATCH 04/17] chore: update docs --- app/services/import/persist-import.service.js | 2 +- app/services/import/persist/persist-company.service.js | 4 ++-- .../import/persist/persist-licence-versions.service.js | 4 ++-- app/services/import/persist/persist-licence.service.js | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/services/import/persist-import.service.js b/app/services/import/persist-import.service.js index a99d114300..9869c44dba 100644 --- a/app/services/import/persist-import.service.js +++ b/app/services/import/persist-import.service.js @@ -2,7 +2,7 @@ /** * Creates or updates an imported licence and its child entities that have been transformed and validated - * @module PersistLicenceService + * @module PersistImportService */ const PersistImportService = require('./persist/persist-licence.service.js') diff --git a/app/services/import/persist/persist-company.service.js b/app/services/import/persist/persist-company.service.js index 6b84edf68a..a44ede7fcd 100644 --- a/app/services/import/persist/persist-company.service.js +++ b/app/services/import/persist/persist-company.service.js @@ -1,7 +1,7 @@ 'use strict' /** - * Creates or updates an imported licence and its child entities that have been transformed and validated + * Creates or updates an imported company and its child entities that have been transformed and validated * @module PersistCompanyService */ @@ -11,7 +11,7 @@ const ContactModel = require('../../../models/contact.model.js') const { db } = require('../../../../db/db.js') /** - * Creates or updates an imported licence and its child entities that have been transformed and validated + * Creates or updates an imported company and its child entities that have been transformed and validated * * @param trx * @param updatedAt diff --git a/app/services/import/persist/persist-licence-versions.service.js b/app/services/import/persist/persist-licence-versions.service.js index 2e6a15d76c..4a3ab4b2a6 100644 --- a/app/services/import/persist/persist-licence-versions.service.js +++ b/app/services/import/persist/persist-licence-versions.service.js @@ -1,7 +1,7 @@ 'use strict' /** - * Creates or updates an imported licence and its child entities that have been transformed and validated + * Creates or updates an imported licence's versions and its child entities that have been transformed and validated * @module PersistLicenceVersionsService */ @@ -10,7 +10,7 @@ const LicenceVersionPurposeModel = require('../../../models/licence-version-purp const LicenceVersionModel = require('../../../models/licence-version.model.js') /** - * Creates or updates an imported licence and its child entities that have been transformed and validated + * Creates or updates an imported licence's versions and its child entities that have been transformed and validated * * @param trx * @param updatedAt diff --git a/app/services/import/persist/persist-licence.service.js b/app/services/import/persist/persist-licence.service.js index 0987748055..758e79eb7c 100644 --- a/app/services/import/persist/persist-licence.service.js +++ b/app/services/import/persist/persist-licence.service.js @@ -1,14 +1,14 @@ 'use strict' /** - * Creates or updates an imported licence and its child entities that have been transformed and validated + * Creates or updates an imported licence that have been transformed and validated * @module PersistLicenceService */ const LicenceModel = require('../../../models/licence.model.js') /** - * Creates or updates an imported licence and its child entities that have been transformed and validated + * Creates or updates an imported licence that have been transformed and validated * * @param trx * @param updatedAt From cd512c58c5d7c9cf7a37b0c6268bd7b45e9867a5 Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Mon, 7 Oct 2024 16:00:23 +0100 Subject: [PATCH 05/17] refactor: test into separate folders --- .../import/persist-licence.service.test.js | 522 ------------------ .../persist/persist-company.service.test.js | 304 ++++++++++ .../persist-licence-versions.service.test.js | 319 +++++++++++ .../persist/persist-licence.service.test.js | 141 +++++ 4 files changed, 764 insertions(+), 522 deletions(-) create mode 100644 test/services/import/persist/persist-company.service.test.js create mode 100644 test/services/import/persist/persist-licence-versions.service.test.js create mode 100644 test/services/import/persist/persist-licence.service.test.js diff --git a/test/services/import/persist-licence.service.test.js b/test/services/import/persist-licence.service.test.js index 36d457d19a..b190e05e8d 100644 --- a/test/services/import/persist-licence.service.test.js +++ b/test/services/import/persist-licence.service.test.js @@ -8,399 +8,14 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() 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') -const ContactHelper = require('../../support/helpers/contact.helper.js') -const ContactModel = require('../../../app/models/contact.model.js') -const LicenceHelper = require('../../support/helpers/licence.helper.js') -const LicenceModel = require('../../../app/models/licence.model.js') -const LicenceRoleHelper = require('../../support/helpers/licence-role.helper.js') -const LicenceVersionHelper = require('../../support/helpers/licence-version.helper.js') -const LicenceVersionPurposeConditionHelper = require('../../support/helpers/licence-version-purpose-condition.helper.js') -const LicenceVersionPurposeConditionTypeHelper = require('../../support/helpers/licence-version-purpose-condition-type.helper.js') -const LicenceVersionPurposeHelper = require('../../support/helpers/licence-version-purpose.helper.js') -const PrimaryPurposeHelper = require('../../support/helpers/primary-purpose.helper.js') -const PurposeHelper = require('../../support/helpers/purpose.helper.js') -const RegionHelper = require('../../support/helpers/region.helper.js') -const SecondaryPurposeHelper = require('../../support/helpers/secondary-purpose.helper.js') -const { randomInteger } = require('../../support/general.js') // Thing under test const PersistImportService = require('../../../app/services/import/persist-import.service.js') describe('Persist licence service', () => { - const companyContactStartDate = new Date('1999-01-01') - - let address - let addressExternalId - let company - let contact - let licenceHolderRoleId - let licenceVersionPurposeConditionType - let newLicence - let primaryPurpose - let purpose - let region - let result - let secondaryPurpose let transformedCompanies - let transformedCompany let transformedLicence - beforeEach(async () => { - licenceVersionPurposeConditionType = LicenceVersionPurposeConditionTypeHelper.select() - primaryPurpose = PrimaryPurposeHelper.select() - purpose = PurposeHelper.select() - region = RegionHelper.select() - secondaryPurpose = SecondaryPurposeHelper.select() - - transformedLicence = _transformedLicence(region.id, primaryPurpose.id, purpose.id, secondaryPurpose.id, - licenceVersionPurposeConditionType.id) - - licenceHolderRoleId = LicenceRoleHelper.select().id - - addressExternalId = AddressHelper.generateExternalId() - - transformedCompany = _transformedCompany(licenceHolderRoleId, addressExternalId) - - transformedCompanies = [{ ...transformedCompany }] - }) - - describe('when given a valid transformed licence', () => { - describe('and that licence does not already exist', () => { - beforeEach(async () => { - // Call the thing under test - result = await PersistImportService.go(transformedLicence, transformedCompanies) - - // Get the persisted data - newLicence = await _fetchPersistedLicence(transformedLicence.licenceRef) - company = await _fetchPersistedCompany(transformedCompany.externalId) - contact = await _fetchPersistedContact(transformedCompany.externalId) - address = await _fetchPersistedAddress(transformedCompany.addresses[0].externalId) - }) - - describe('address', () => { - it('creates an address', () => { - expect(address.address1).to.equal('4 Privet Drive') - expect(address.address2).to.be.null() - expect(address.address5).to.equal('Little Whinging') - expect(address.address6).to.equal('Surrey') - expect(address.country).to.equal('United Kingdom') - expect(address.dataSource).to.equal('nald') - expect(address.externalId).to.equal(addressExternalId) - expect(address.postcode).to.equal('HP11') - expect(address.uprn).to.be.null() - }) - }) - - describe('licence', () => { - it('creates a new licence record plus child records in WRLS and returns the licence ID', () => { - expect(result).to.equal(newLicence.id) - }) - - it('creates a licence version', () => { - const newLicenceVersion = newLicence.licenceVersions[0] - - expect(newLicenceVersion.externalId).to.equal(transformedLicence.licenceVersions[0].externalId) - }) - - it('creates a licence version purpose', () => { - const newLicenceVersionPurpose = newLicence.licenceVersions[0].licenceVersionPurposes[0] - - expect(newLicenceVersionPurpose.externalId).to.equal( - transformedLicence.licenceVersions[0].licenceVersionPurposes[0].externalId - ) - }) - - it('creates a licence version purpose conditions', () => { - const newLicenceVersionPurposeCondition = newLicence.licenceVersions[0] - .licenceVersionPurposes[0].licenceVersionPurposeConditions[0] - - expect(newLicenceVersionPurposeCondition.externalId).to.equal( - transformedLicence.licenceVersions[0].licenceVersionPurposes[0] - .licenceVersionPurposeConditions[0].externalId) - }) - }) - - describe('contact', () => { - it('creates a contact', () => { - expect(contact.salutation).to.equal('Mr') - expect(contact.initials).to.equal('H') - expect(contact.firstName).to.equal('James') - expect(contact.lastName).to.equal('Bond') - expect(contact.dataSource).to.equal('nald') - }) - }) - - describe('company, companyContact and companyAddresses', () => { - it('creates a company', () => { - expect(company.name).to.equal('ACME') - expect(company.type).to.equal('person') - expect(company.externalId).to.equal(transformedCompany.externalId) - }) - - it('creates a new company contact - links the company to the contact', () => { - const companyContact = contact.companyContacts[0] - - expect(companyContact.companyId).to.equal(company.id) - expect(companyContact.contactId).to.equal(contact.id) - expect(companyContact.licenceRoleId).to.equal(licenceHolderRoleId) - expect(companyContact.startDate).to.equal(transformedCompany.companyContact.startDate) - expect(companyContact.default).to.be.true() - }) - - it('creates a new company address - links the company to the contact', () => { - 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 updatedLicence - - beforeEach(async () => { - // Address Data - exisitngAddress = await AddressHelper.add({ - ...transformedCompany.addresses[0] - }) - - // Licence - existingLicence = await LicenceHelper.add({ - expiredDate: new Date('2052-06-23'), - lapsedDate: new Date('2050-07-24'), - licenceRef: transformedLicence.licenceRef, - regionId: region.id, - regions: { - historicalAreaCode: 'RIDIN', - regionalChargeArea: 'Yorkshire', - standardUnitChargeCode: 'YORKI', - localEnvironmentAgencyPlanCode: 'AIREL' - }, - revokedDate: new Date('2049-08-25'), - startDate: new Date('1992-08-19') - }) - - existingLicenceVersion = await LicenceVersionHelper.add({ - endDate: new Date('2052-06-23'), - externalId: transformedLicence.licenceVersions[0].externalId, - licenceId: existingLicence.id, - startDate: new Date('1999-01-01'), - status: 'current' - }) - - existingLicenceVersionPurpose = await LicenceVersionPurposeHelper.add({ - abstractionPeriodEndDay: 30, - abstractionPeriodEndMonth: 9, - abstractionPeriodStartDay: 1, - abstractionPeriodStartMonth: 5, - annualQuantity: 61371, - dailyQuantity: 1091, - externalId: transformedLicence.licenceVersions[0].licenceVersionPurposes[0].externalId, - hourlyQuantity: 68, - instantQuantity: 18.89, - licenceVersionId: existingLicenceVersion.id, - notes: 'I was here first', - primaryPurposeId: PrimaryPurposeHelper.select().id, - purposeId: PurposeHelper.select().id, - secondaryPurposeId: SecondaryPurposeHelper.select().id, - timeLimitedEndDate: new Date('1992-08-19'), - timeLimitedStartDate: new Date('2052-06-23') - }) - - existingLicenceVersionPurposeCondition = await LicenceVersionPurposeConditionHelper.add({ - licenceVersionPurposeId: existingLicenceVersionPurpose.id, - licenceVersionPurposeConditionTypeId: licenceVersionPurposeConditionType.id, - externalId: transformedLicence.licenceVersions[0].licenceVersionPurposes[0] - .licenceVersionPurposeConditions[0].externalId, - source: 'nald' - }) - - // Contact Data - exisitngContact = await ContactHelper.add({ - externalId: transformedCompany.externalId - }) - - // Company Data - existingCompany = await CompanyHelper.add({ - externalId: transformedCompany.externalId - }) - - await CompanyContactHelper.add({ - companyId: existingCompany.id, - contactId: exisitngContact.id, - licenceRoleId: licenceHolderRoleId, - startDate: companyContactStartDate - }) - - await CompanyAddressHelper.add({ - addressId: exisitngAddress.id, - companyId: existingCompany.id, - licenceRoleId: licenceHolderRoleId, - endDate: new Date('1999-01-01') - }) - - transformedCompanies = [{ - ...existingCompany, - contact: exisitngContact, - companyContact: { - externalId: existingCompany.externalId, - startDate: companyContactStartDate, - licenceRoleId: licenceHolderRoleId - }, - addresses: [{ - 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 - } - ] - }] - - // Call the thing under test - result = await PersistImportService.go(transformedLicence, transformedCompanies) - - // Get the persisted data - updatedLicence = await _fetchPersistedLicence(transformedLicence.licenceRef) - newLicence = await _fetchPersistedLicence(transformedLicence.licenceRef) - company = await _fetchPersistedCompany(existingCompany.externalId) - contact = await _fetchPersistedContact(exisitngContact.externalId) - address = await _fetchPersistedAddress(transformedCompany.addresses[0].externalId) - }) - - describe('address', () => { - it('updates an address', () => { - expect(address.address1).to.equal('ENVIRONMENT AGENCY') - expect(address.dataSource).to.equal('nald') - expect(address.externalId).to.equal(addressExternalId) - }) - }) - - describe('company, companyContact and companyAddresses', () => { - it('updates a company', () => { - expect(company.name).to.equal('Example Trading Ltd') - expect(company.type).to.equal('organisation') - expect(company.externalId).to.equal(existingCompany.externalId) - }) - - it('updates a company contact - links the company to the contact', () => { - const companyContact = contact.companyContacts[0] - - expect(companyContact.companyId).to.equal(existingCompany.id) - expect(companyContact.contactId).to.equal(exisitngContact.id) - expect(companyContact.licenceRoleId).to.equal(licenceHolderRoleId) - expect(companyContact.startDate).to.equal(new Date('1999-01-01')) - expect(companyContact.default).to.be.true() - }) - - it('updates a company address - links the company to the 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')) - }) - }) - - describe('contact', () => { - it('updates a contact', () => { - expect(contact.salutation).to.be.null() - expect(contact.initials).to.be.null() - expect(contact.firstName).to.equal('Amara') - expect(contact.lastName).to.equal('Gupta') - expect(contact.dataSource).to.equal('wrls') - }) - }) - - describe('licence', () => { - it('should return the updates licence id', () => { - expect(result).to.equal(existingLicence.id) - }) - - it('checks the updated licence', async () => { - expect(updatedLicence.expiredDate).to.equal(transformedLicence.expiredDate) - expect(updatedLicence.lapsedDate).to.equal(transformedLicence.lapsedDate) - expect(updatedLicence.regions).to.equal(transformedLicence.regions) - expect(updatedLicence.revokedDate).to.equal(transformedLicence.revokedDate) - expect(updatedLicence.startDate).to.equal(transformedLicence.startDate) - }) - - it('updates a licence version', () => { - const updatedLicVer = updatedLicence.licenceVersions[0] - const transformedLicVer = transformedLicence.licenceVersions[0] - - expect(updatedLicVer.id).to.equal(existingLicenceVersion.id) - expect(updatedLicVer.endDate).to.equal(transformedLicVer.endDate) - expect(updatedLicVer.startDate).to.equal(transformedLicVer.startDate) - expect(updatedLicVer.status).to.equal(transformedLicVer.status) - }) - - it('updates a licence version purpose', () => { - const updatedLicVerPur = updatedLicence.licenceVersions[0].licenceVersionPurposes[0] - const transformedLicVerPur = transformedLicence.licenceVersions[0].licenceVersionPurposes[0] - - expect(updatedLicVerPur.id).to.equal(existingLicenceVersionPurpose.id) - expect(updatedLicVerPur.abstractionPeriodEndDay).to.equal(transformedLicVerPur.abstractionPeriodEndDay) - expect(updatedLicVerPur.abstractionPeriodEndMonth).to.equal(transformedLicVerPur.abstractionPeriodEndMonth) - expect(updatedLicVerPur.abstractionPeriodStartDay).to.equal(transformedLicVerPur.abstractionPeriodStartDay) - expect(updatedLicVerPur.abstractionPeriodStartMonth).to - .equal(transformedLicVerPur.abstractionPeriodStartMonth) - expect(updatedLicVerPur.annualQuantity).to.equal(transformedLicVerPur.annualQuantity) - expect(updatedLicVerPur.dailyQuantity).to.equal(transformedLicVerPur.dailyQuantity) - expect(updatedLicVerPur.hourlyQuantity).to.equal(transformedLicVerPur.hourlyQuantity) - expect(updatedLicVerPur.instantQuantity).to.equal(transformedLicVerPur.instantQuantity) - expect(updatedLicVerPur.notes).to.equal(transformedLicVerPur.notes) - expect(updatedLicVerPur.primaryPurposeId).to.equal(transformedLicVerPur.primaryPurposeId) - expect(updatedLicVerPur.purposeId).to.equal(transformedLicVerPur.purposeId) - expect(updatedLicVerPur.secondaryPurposeId).to.equal(transformedLicVerPur.secondaryPurposeId) - expect(updatedLicVerPur.timeLimitedEndDate).to.equal(transformedLicVerPur.timeLimitedEndDate) - expect(updatedLicVerPur.timeLimitedStartDate).to.equal(transformedLicVerPur.timeLimitedStartDate) - }) - - it('updates a licence version purpose conditions', () => { - const updatedLicVerPurCon = updatedLicence.licenceVersions[0] - .licenceVersionPurposes[0].licenceVersionPurposeConditions[0] - const transformedLicVerPurCon = transformedLicence.licenceVersions[0] - .licenceVersionPurposes[0].licenceVersionPurposeConditions[0] - - expect(updatedLicVerPurCon.id).to.equal(existingLicenceVersionPurposeCondition.id) - expect(updatedLicVerPurCon.externalId).to.equal(transformedLicVerPurCon.externalId) - expect(updatedLicVerPurCon.licenceVersionPurposeConditionTypeId) - .to.equal(transformedLicVerPurCon.licenceVersionPurposeConditionTypeId) - expect(updatedLicVerPurCon.notes).to.equal(transformedLicVerPurCon.notes) - expect(updatedLicVerPurCon.param1).to.equal(transformedLicVerPurCon.param1) - expect(updatedLicVerPurCon.param2).to.equal(transformedLicVerPurCon.param2) - expect(updatedLicVerPurCon.source).to.equal(transformedLicVerPurCon.source) - }) - }) - }) - }) - describe('when an error is thrown when persisting the licence', () => { beforeEach(() => { // We cause the error by making the last record to be persisted have an invalid value. This demonstrates the @@ -417,140 +32,3 @@ describe('Persist licence service', () => { }) }) }) - -async function _fetchPersistedAddress (externalId) { - return AddressModel.query().where('externalId', externalId).limit(1).first().withGraphFetched('companyAddresses') -} - -async function _fetchPersistedCompany (externalId) { - return CompanyModel - .query() - .where('externalId', externalId) - .withGraphFetched('companyContacts') - .limit(1) - .first() -} - -async function _fetchPersistedContact (externalId) { - return ContactModel - .query() - .where('externalId', externalId) - .withGraphFetched('companyContacts') - .limit(1) - .first() -} - -async function _fetchPersistedLicence (licenceRef) { - return LicenceModel - .query() - .where('licenceRef', licenceRef) - .withGraphFetched('licenceVersions') - .withGraphFetched('licenceVersions.licenceVersionPurposes') - .withGraphFetched('licenceVersions.licenceVersionPurposes.licenceVersionPurposeConditions') - .limit(1) - .first() -} - -function _transformedCompany (licenceHolderRoleId, addressExternalId) { - const externalId = CompanyHelper.generateExternalId() - - return { - externalId, - name: 'ACME', - type: 'person', - contact: { - salutation: 'Mr', - initials: 'H', - firstName: 'James', - lastName: 'Bond', - externalId, - dataSource: 'nald' - }, - companyContact: { - externalId, - startDate: new Date('1999-01-01'), - licenceRoleId: licenceHolderRoleId - }, - addresses: [ - { - address1: '4 Privet Drive', - address2: null, - address3: null, - address4: null, - address5: 'Little Whinging', - address6: 'Surrey', - country: 'United Kingdom', - externalId: addressExternalId, - postcode: 'HP11', - dataSource: 'nald' - } - ], - companyAddresses: [ - { - addressId: addressExternalId, - companyId: externalId, - startDate: new Date('2020-01-01'), - endDate: new Date('2022-02-02'), - licenceRoleId: licenceHolderRoleId - } - ] - } -} - -function _transformedLicence (regionId, primaryPurposeId, purposeId, secondaryPurposeId, - licenceVersionPurposeConditionTypeId) { - return { - expiredDate: null, - lapsedDate: null, - licenceRef: LicenceHelper.generateLicenceRef(), - regionId, - regions: { - historicalAreaCode: 'KAEA', - regionalChargeArea: 'Southern', - standardUnitChargeCode: 'SUCSO', - localEnvironmentAgencyPlanCode: 'LEME' - }, - revokedDate: null, - startDate: new Date('1992-08-19'), - waterUndertaker: false, - licenceVersions: [ - { - endDate: new Date('2052-06-23'), - externalId: LicenceVersionHelper.generateLicenceVersionExternalId(), - increment: 0, - issue: 100, - startDate: new Date('1999-01-01'), - status: 'superseded', - licenceVersionPurposes: [ - { - abstractionPeriodEndDay: 31, - abstractionPeriodEndMonth: 3, - abstractionPeriodStartDay: 1, - abstractionPeriodStartMonth: 4, - annualQuantity: 545520.1, - dailyQuantity: 1500.2, - externalId: LicenceVersionPurposeHelper.generateLicenceVersionPurposeExternalId(), - hourlyQuantity: 140.929, - instantQuantity: null, - notes: 'This is a note', - primaryPurposeId, - purposeId, - secondaryPurposeId, - timeLimitedEndDate: null, - timeLimitedStartDate: null, - licenceVersionPurposeConditions: [ - { - externalId: `${randomInteger(1, 99999)}:${randomInteger(1, 9)}:${randomInteger(1, 99999999)}`, - licenceVersionPurposeConditionTypeId, - notes: 'At each abstraction borehole', - param1: null, - param2: null, - source: 'nald' - } - ] - } - ] - } - ] - } -} diff --git a/test/services/import/persist/persist-company.service.test.js b/test/services/import/persist/persist-company.service.test.js new file mode 100644 index 0000000000..f108424ed8 --- /dev/null +++ b/test/services/import/persist/persist-company.service.test.js @@ -0,0 +1,304 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') + +const { describe, it, afterEach, beforeEach } = exports.lab = Lab.script() +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') +const ContactHelper = require('../../../support/helpers/contact.helper.js') +const ContactModel = require('../../../../app/models/contact.model.js') +const LicenceModel = require('../../../../app/models/licence.model.js') +const LicenceRoleHelper = require('../../../support/helpers/licence-role.helper.js') +const { timestampForPostgres } = require('../../../../app/lib/general.lib.js') +const { transaction } = require('objection') + +// Thing under test +const PersistCompanyService = require('../../../../app/services/import/persist/persist-company.service.js') + +describe('Persist company service', () => { + const companyContactStartDate = new Date('1999-01-01') + + let addressExternalId + let updatedAt + let trx + let licenceHolderRoleId + + beforeEach(async () => { + addressExternalId = AddressHelper.generateExternalId() + licenceHolderRoleId = LicenceRoleHelper.select().id + + updatedAt = timestampForPostgres() + + trx = await transaction.start(LicenceModel.knex()) + }) + + afterEach(async () => { + if (!trx.isCompleted()) { + await trx.rollback() + } + }) + + describe('when given a valid transformed licence', () => { + describe('and that licence does not already exist', () => { + let transformedCompany + let transformedCompanies + + beforeEach(() => { + transformedCompany = _transformedCompany(licenceHolderRoleId, addressExternalId) + + transformedCompanies = [{ ...transformedCompany }] + }) + + it('creates a new licence record plus child records in WRLS and returns the licence ID', async () => { + await PersistCompanyService.go(trx, updatedAt, transformedCompanies) + + // Commit the transaction so the data is saved to the database + await trx.commit() + + // Get the persisted data + const company = await _fetchPersistedCompany(transformedCompany.externalId) + const contact = await _fetchPersistedContact(transformedCompany.externalId) + const address = await _fetchPersistedAddress(transformedCompany.addresses[0].externalId) + + // Address + expect(address.address1).to.equal('4 Privet Drive') + expect(address.address2).to.be.null() + expect(address.address5).to.equal('Little Whinging') + expect(address.address6).to.equal('Surrey') + expect(address.country).to.equal('United Kingdom') + expect(address.dataSource).to.equal('nald') + expect(address.externalId).to.equal(addressExternalId) + expect(address.postcode).to.equal('HP11') + expect(address.uprn).to.be.null() + + // Contact + expect(contact.salutation).to.equal('Mr') + expect(contact.initials).to.equal('H') + expect(contact.firstName).to.equal('James') + expect(contact.lastName).to.equal('Bond') + expect(contact.dataSource).to.equal('nald') + + // Company + expect(company.name).to.equal('ACME') + expect(company.type).to.equal('person') + expect(company.externalId).to.equal(transformedCompany.externalId) + + // Company Contact + const companyContact = contact.companyContacts[0] + + expect(companyContact.companyId).to.equal(company.id) + expect(companyContact.contactId).to.equal(contact.id) + expect(companyContact.licenceRoleId).to.equal(licenceHolderRoleId) + expect(companyContact.startDate).to.equal(transformedCompany.companyContact.startDate) + expect(companyContact.default).to.be.true() + + // Company Addresses + 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 exisitngContact + let existingCompany + let transformedCompany + let transformedCompanies + + beforeEach(async () => { + transformedCompany = _transformedCompany(licenceHolderRoleId, addressExternalId) + + const existing = await _createExistingRecords(transformedCompany, licenceHolderRoleId, companyContactStartDate) + + existingCompany = existing.company + exisitngContact = existing.contact + + transformedCompanies = [{ + ...existingCompany, + contact: exisitngContact, + companyContact: { + externalId: existingCompany.externalId, + startDate: companyContactStartDate, + licenceRoleId: licenceHolderRoleId + }, + addresses: [{ + 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 + } + ] + }] + }) + + it('should return the updated company', async () => { + // Call the thing under test + await PersistCompanyService.go(trx, updatedAt, transformedCompanies) + + // Commit the transaction so the data is saved to the database + await trx.commit() + + // Get the persisted data + const company = await _fetchPersistedCompany(existingCompany.externalId) + const contact = await _fetchPersistedContact(exisitngContact.externalId) + const address = await _fetchPersistedAddress(transformedCompany.addresses[0].externalId) + + // Address + expect(address.address1).to.equal('ENVIRONMENT AGENCY') + expect(address.dataSource).to.equal('nald') + expect(address.externalId).to.equal(addressExternalId) + + // Company + expect(company.name).to.equal('Example Trading Ltd') + expect(company.type).to.equal('organisation') + expect(company.externalId).to.equal(existingCompany.externalId) + + // Company Contacts + const companyContact = contact.companyContacts[0] + + expect(companyContact.companyId).to.equal(existingCompany.id) + expect(companyContact.contactId).to.equal(exisitngContact.id) + expect(companyContact.licenceRoleId).to.equal(licenceHolderRoleId) + expect(companyContact.startDate).to.equal(new Date('1999-01-01')) + expect(companyContact.default).to.be.true() + + // Company 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')) + + // Contact + expect(contact.salutation).to.be.null() + expect(contact.initials).to.be.null() + expect(contact.firstName).to.equal('Amara') + expect(contact.lastName).to.equal('Gupta') + expect(contact.dataSource).to.equal('wrls') + }) + }) + }) +}) + +async function _fetchPersistedAddress (externalId) { + return AddressModel.query().where('externalId', externalId).limit(1).first().withGraphFetched('companyAddresses') +} + +async function _fetchPersistedCompany (externalId) { + return CompanyModel + .query() + .where('externalId', externalId) + .withGraphFetched('companyContacts') + .limit(1) + .first() +} + +async function _fetchPersistedContact (externalId) { + return ContactModel + .query() + .where('externalId', externalId) + .withGraphFetched('companyContacts') + .limit(1) + .first() +} + +function _transformedCompany (licenceHolderRoleId, addressExternalId) { + const externalId = CompanyHelper.generateExternalId() + + return { + externalId, + name: 'ACME', + type: 'person', + contact: { + salutation: 'Mr', + initials: 'H', + firstName: 'James', + lastName: 'Bond', + externalId, + dataSource: 'nald' + }, + companyContact: { + externalId, + startDate: new Date('1999-01-01'), + licenceRoleId: licenceHolderRoleId + }, + addresses: [ + { + address1: '4 Privet Drive', + address2: null, + address3: null, + address4: null, + address5: 'Little Whinging', + address6: 'Surrey', + country: 'United Kingdom', + externalId: addressExternalId, + postcode: 'HP11', + dataSource: 'nald' + } + ], + companyAddresses: [ + { + addressId: addressExternalId, + companyId: externalId, + startDate: new Date('2020-01-01'), + endDate: new Date('2022-02-02'), + licenceRoleId: licenceHolderRoleId + } + ] + } +} + +async function _createExistingRecords (transformedCompany, licenceHolderRoleId, companyContactStartDate) { + const address = await AddressHelper.add({ + ...transformedCompany.addresses[0] + }) + + const contact = await ContactHelper.add({ + externalId: transformedCompany.externalId + }) + + const company = await CompanyHelper.add({ + externalId: transformedCompany.externalId + }) + + await CompanyContactHelper.add({ + companyId: company.id, + contactId: contact.id, + licenceRoleId: licenceHolderRoleId, + startDate: companyContactStartDate + }) + + await CompanyAddressHelper.add({ + addressId: address.id, + companyId: company.id, + licenceRoleId: licenceHolderRoleId, + endDate: new Date('1999-01-01') + }) + + return { + contact, + company + } +} diff --git a/test/services/import/persist/persist-licence-versions.service.test.js b/test/services/import/persist/persist-licence-versions.service.test.js new file mode 100644 index 0000000000..118a4896b3 --- /dev/null +++ b/test/services/import/persist/persist-licence-versions.service.test.js @@ -0,0 +1,319 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') + +const { describe, it, afterEach, beforeEach } = exports.lab = Lab.script() +const { expect } = Code + +// Test helpers +const LicenceHelper = require('../../../support/helpers/licence.helper.js') +const LicenceModel = require('../../../../app/models/licence.model.js') +const LicenceVersionHelper = require('../../../support/helpers/licence-version.helper.js') +const LicenceVersionPurposeConditionHelper = require('../../../support/helpers/licence-version-purpose-condition.helper.js') +const LicenceVersionPurposeConditionTypeHelper = require('../../../support/helpers/licence-version-purpose-condition-type.helper.js') +const LicenceVersionPurposeHelper = require('../../../support/helpers/licence-version-purpose.helper.js') +const PrimaryPurposeHelper = require('../../../support/helpers/primary-purpose.helper.js') +const PurposeHelper = require('../../../support/helpers/purpose.helper.js') +const RegionHelper = require('../../../support/helpers/region.helper.js') +const SecondaryPurposeHelper = require('../../../support/helpers/secondary-purpose.helper.js') +const { randomInteger } = require('../../../support/general.js') +const { timestampForPostgres } = require('../../../../app/lib/general.lib.js') +const { transaction } = require('objection') + +// Thing under test +const PersistLicenceVersionsService = require('../../../../app/services/import/persist/persist-licence-versions.service.js') + +describe.only('Persist licence versions service', () => { + let licenceVersionPurposeConditionType + let primaryPurpose + let purpose + let region + let secondaryPurpose + let trx + let updatedAt + + beforeEach(async () => { + licenceVersionPurposeConditionType = LicenceVersionPurposeConditionTypeHelper.select() + primaryPurpose = PrimaryPurposeHelper.select() + purpose = PurposeHelper.select() + region = RegionHelper.select() + secondaryPurpose = SecondaryPurposeHelper.select() + + updatedAt = timestampForPostgres() + + trx = await transaction.start(LicenceModel.knex()) + }) + + afterEach(async () => { + if (!trx.isCompleted()) { + await trx.rollback() + } + }) + + describe('when given a valid transformed licence versions', () => { + describe('and that licence versions does not already exist', () => { + let licenceId + let transformedLicence + + beforeEach(async () => { + transformedLicence = _transformedLicence(region.id, primaryPurpose.id, purpose.id, secondaryPurpose.id, + licenceVersionPurposeConditionType.id) + + // Licence + const licence = await LicenceHelper.add({ + expiredDate: new Date('2052-06-23'), + lapsedDate: new Date('2050-07-24'), + licenceRef: transformedLicence.licenceRef, + regionId: region.id, + regions: { + historicalAreaCode: 'RIDIN', + regionalChargeArea: 'Yorkshire', + standardUnitChargeCode: 'YORKI', + localEnvironmentAgencyPlanCode: 'AIREL' + }, + revokedDate: new Date('2049-08-25'), + startDate: new Date('1992-08-19') + }) + + licenceId = licence.id + }) + + it('creates a new licence record plus child records in WRLS and returns the licence ID', async () => { + await PersistLicenceVersionsService.go(trx, updatedAt, transformedLicence, licenceId) + + // Commit the transaction so the data is saved to the database + await trx.commit() + + // Get the persisted data + const newLicence = await _fetchPersistedLicence(transformedLicence.licenceRef) + + // Licence Version + const newLicenceVersion = newLicence.licenceVersions[0] + + expect(newLicenceVersion.externalId).to.equal(transformedLicence.licenceVersions[0].externalId) + + // Licence Version Purpose + const newLicenceVersionPurpose = newLicence.licenceVersions[0].licenceVersionPurposes[0] + + expect(newLicenceVersionPurpose.externalId).to.equal( + transformedLicence.licenceVersions[0].licenceVersionPurposes[0].externalId + ) + + // Licence Version Purpose Condition + const newLicenceVersionPurposeCondition = newLicence.licenceVersions[0] + .licenceVersionPurposes[0].licenceVersionPurposeConditions[0] + + expect(newLicenceVersionPurposeCondition.externalId).to.equal( + transformedLicence.licenceVersions[0].licenceVersionPurposes[0] + .licenceVersionPurposeConditions[0].externalId) + }) + }) + + describe('and that licence versions already exists', () => { + let existingLicence + let existingLicenceVersion + let existingLicenceVersionPurpose + let existingLicenceVersionPurposeCondition + let transformedLicence + let updatedLicence + + beforeEach(async () => { + transformedLicence = _transformedLicence(region.id, primaryPurpose.id, purpose.id, secondaryPurpose.id, + licenceVersionPurposeConditionType.id) + + const existing = await _createExistingRecords(transformedLicence) + + existingLicence = existing.licence + existingLicenceVersion = existing.licenceVersion + existingLicenceVersionPurpose = existing.licenceVersionPurpose + existingLicenceVersionPurposeCondition = existing.licenceVersionPurposeCondition + }) + + it('should return the updated licence', async () => { + // Call the thing under test + await PersistLicenceVersionsService.go(trx, updatedAt, transformedLicence, existingLicence.id) + + // Commit the transaction so the data is saved to the database + await trx.commit() + + // Get the persisted data + updatedLicence = await _fetchPersistedLicence(existingLicence.licenceRef) + + // Licence Version + const updatedLicVer = updatedLicence.licenceVersions[0] + const transformedLicVer = transformedLicence.licenceVersions[0] + + expect(updatedLicVer.id).to.equal(existingLicenceVersion.id) + expect(updatedLicVer.endDate).to.equal(transformedLicVer.endDate) + expect(updatedLicVer.startDate).to.equal(transformedLicVer.startDate) + expect(updatedLicVer.status).to.equal(transformedLicVer.status) + + // Licence Version Purpose + const updatedLicVerPur = updatedLicence.licenceVersions[0].licenceVersionPurposes[0] + const transformedLicVerPur = transformedLicence.licenceVersions[0].licenceVersionPurposes[0] + + expect(updatedLicVerPur.id).to.equal(existingLicenceVersionPurpose.id) + expect(updatedLicVerPur.abstractionPeriodEndDay).to.equal(transformedLicVerPur.abstractionPeriodEndDay) + expect(updatedLicVerPur.abstractionPeriodEndMonth).to.equal(transformedLicVerPur.abstractionPeriodEndMonth) + expect(updatedLicVerPur.abstractionPeriodStartDay).to.equal(transformedLicVerPur.abstractionPeriodStartDay) + expect(updatedLicVerPur.abstractionPeriodStartMonth).to + .equal(transformedLicVerPur.abstractionPeriodStartMonth) + expect(updatedLicVerPur.annualQuantity).to.equal(transformedLicVerPur.annualQuantity) + expect(updatedLicVerPur.dailyQuantity).to.equal(transformedLicVerPur.dailyQuantity) + expect(updatedLicVerPur.hourlyQuantity).to.equal(transformedLicVerPur.hourlyQuantity) + expect(updatedLicVerPur.instantQuantity).to.equal(transformedLicVerPur.instantQuantity) + expect(updatedLicVerPur.notes).to.equal(transformedLicVerPur.notes) + expect(updatedLicVerPur.primaryPurposeId).to.equal(transformedLicVerPur.primaryPurposeId) + expect(updatedLicVerPur.purposeId).to.equal(transformedLicVerPur.purposeId) + expect(updatedLicVerPur.secondaryPurposeId).to.equal(transformedLicVerPur.secondaryPurposeId) + expect(updatedLicVerPur.timeLimitedEndDate).to.equal(transformedLicVerPur.timeLimitedEndDate) + expect(updatedLicVerPur.timeLimitedStartDate).to.equal(transformedLicVerPur.timeLimitedStartDate) + + // Licence Version Purpose Conditions + const updatedLicVerPurCon = updatedLicence.licenceVersions[0] + .licenceVersionPurposes[0].licenceVersionPurposeConditions[0] + const transformedLicVerPurCon = transformedLicence.licenceVersions[0] + .licenceVersionPurposes[0].licenceVersionPurposeConditions[0] + + expect(updatedLicVerPurCon.id).to.equal(existingLicenceVersionPurposeCondition.id) + expect(updatedLicVerPurCon.externalId).to.equal(transformedLicVerPurCon.externalId) + expect(updatedLicVerPurCon.licenceVersionPurposeConditionTypeId) + .to.equal(transformedLicVerPurCon.licenceVersionPurposeConditionTypeId) + expect(updatedLicVerPurCon.notes).to.equal(transformedLicVerPurCon.notes) + expect(updatedLicVerPurCon.param1).to.equal(transformedLicVerPurCon.param1) + expect(updatedLicVerPurCon.param2).to.equal(transformedLicVerPurCon.param2) + expect(updatedLicVerPurCon.source).to.equal(transformedLicVerPurCon.source) + }) + }) + }) +}) + +async function _fetchPersistedLicence (licenceRef) { + return LicenceModel + .query() + .where('licenceRef', licenceRef) + .withGraphFetched('licenceVersions') + .withGraphFetched('licenceVersions.licenceVersionPurposes') + .withGraphFetched('licenceVersions.licenceVersionPurposes.licenceVersionPurposeConditions') + .limit(1) + .first() +} + +function _transformedLicence (regionId, primaryPurposeId, purposeId, secondaryPurposeId, + licenceVersionPurposeConditionTypeId) { + return { + expiredDate: null, + lapsedDate: null, + licenceRef: LicenceHelper.generateLicenceRef(), + regionId, + regions: { + historicalAreaCode: 'KAEA', + regionalChargeArea: 'Southern', + standardUnitChargeCode: 'SUCSO', + localEnvironmentAgencyPlanCode: 'LEME' + }, + revokedDate: null, + startDate: new Date('1992-08-19'), + waterUndertaker: false, + licenceVersions: [ + { + endDate: new Date('2052-06-23'), + externalId: LicenceVersionHelper.generateLicenceVersionExternalId(), + increment: 0, + issue: 100, + startDate: new Date('1999-01-01'), + status: 'superseded', + licenceVersionPurposes: [ + { + abstractionPeriodEndDay: 31, + abstractionPeriodEndMonth: 3, + abstractionPeriodStartDay: 1, + abstractionPeriodStartMonth: 4, + annualQuantity: 545520.1, + dailyQuantity: 1500.2, + externalId: LicenceVersionPurposeHelper.generateLicenceVersionPurposeExternalId(), + hourlyQuantity: 140.929, + instantQuantity: null, + notes: 'This is a note', + primaryPurposeId, + purposeId, + secondaryPurposeId, + timeLimitedEndDate: null, + timeLimitedStartDate: null, + licenceVersionPurposeConditions: [ + { + externalId: `${randomInteger(1, 99999)}:${randomInteger(1, 9)}:${randomInteger(1, 99999999)}`, + licenceVersionPurposeConditionTypeId, + notes: 'At each abstraction borehole', + param1: null, + param2: null, + source: 'nald' + } + ] + } + ] + } + ] + } +} + +async function _createExistingRecords (transformedLicence) { + const licence = await LicenceHelper.add({ + expiredDate: new Date('2052-06-23'), + lapsedDate: new Date('2050-07-24'), + licenceRef: transformedLicence.licenceRef, + regionId: RegionHelper.select().id, + regions: { + historicalAreaCode: 'RIDIN', + regionalChargeArea: 'Yorkshire', + standardUnitChargeCode: 'YORKI', + localEnvironmentAgencyPlanCode: 'AIREL' + }, + revokedDate: new Date('2049-08-25'), + startDate: new Date('1992-08-19') + }) + + const licenceVersion = await LicenceVersionHelper.add({ + endDate: new Date('2052-06-23'), + externalId: transformedLicence.licenceVersions[0].externalId, + licenceId: licence.id, + startDate: new Date('1999-01-01'), + status: 'current' + }) + + const licenceVersionPurpose = await LicenceVersionPurposeHelper.add({ + abstractionPeriodEndDay: 30, + abstractionPeriodEndMonth: 9, + abstractionPeriodStartDay: 1, + abstractionPeriodStartMonth: 5, + annualQuantity: 61371, + dailyQuantity: 1091, + externalId: transformedLicence.licenceVersions[0].licenceVersionPurposes[0].externalId, + hourlyQuantity: 68, + instantQuantity: 18.89, + licenceVersionId: licenceVersion.id, + notes: 'I was here first', + primaryPurposeId: PrimaryPurposeHelper.select().id, + purposeId: PurposeHelper.select().id, + secondaryPurposeId: SecondaryPurposeHelper.select().id, + timeLimitedEndDate: new Date('1992-08-19'), + timeLimitedStartDate: new Date('2052-06-23') + }) + + const licenceVersionPurposeCondition = await LicenceVersionPurposeConditionHelper.add({ + licenceVersionPurposeId: licenceVersionPurpose.id, + licenceVersionPurposeConditionTypeId: LicenceVersionPurposeConditionTypeHelper.select().id, + externalId: transformedLicence.licenceVersions[0].licenceVersionPurposes[0] + .licenceVersionPurposeConditions[0].externalId, + source: 'nald' + }) + + return { + licence, + licenceVersion, + licenceVersionPurposeCondition, + licenceVersionPurpose + } +} diff --git a/test/services/import/persist/persist-licence.service.test.js b/test/services/import/persist/persist-licence.service.test.js new file mode 100644 index 0000000000..fc033711ea --- /dev/null +++ b/test/services/import/persist/persist-licence.service.test.js @@ -0,0 +1,141 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') + +const { describe, it, afterEach, beforeEach } = exports.lab = Lab.script() +const { expect } = Code + +// Test helpers +const { transaction } = require('objection') +const LicenceHelper = require('../../../support/helpers/licence.helper.js') +const LicenceModel = require('../../../../app/models/licence.model.js') +const RegionHelper = require('../../../support/helpers/region.helper.js') +const { timestampForPostgres } = require('../../../../app/lib/general.lib.js') + +// Thing under test +const PersistLicenceService = require('../../../../app/services/import/persist/persist-licence.service.js') + +describe('Persist licence service', () => { + let newLicence + let region + let updatedAt + let trx + + beforeEach(async () => { + region = RegionHelper.select() + + updatedAt = timestampForPostgres() + + trx = await transaction.start(LicenceModel.knex()) + }) + + afterEach(async () => { + if (!trx.isCompleted()) { + await trx.rollback() + } + }) + + describe('when given a valid transformed licence', () => { + describe('and that licence does not already exist', () => { + let transformedLicence + + beforeEach(() => { + transformedLicence = _transformedLicence(region.id) + }) + + it('creates a new licence record plus child records in WRLS and returns the licence ID', async () => { + const result = await PersistLicenceService.go(trx, updatedAt, transformedLicence) + + // Commit the transaction so the data is saved to the database + await trx.commit() + + newLicence = await _fetchPersistedLicence(transformedLicence.licenceRef) + + expect(result).to.equal(newLicence.id) + }) + }) + + describe('and that licence already exists', () => { + let existingLicence + let updatedLicence + + beforeEach(async () => { + const existing = await _createExistingRecords(region) + + existingLicence = existing.licence + }) + + it('should return the updated licence', async () => { + // Call the thing under test + const result = await PersistLicenceService.go(trx, updatedAt, existingLicence) + + // Commit the transaction so the data is saved to the database + await trx.commit() + + // Get the persisted data + updatedLicence = await _fetchPersistedLicence(existingLicence.licenceRef) + + // Check the licence has updates the correct licence id + expect(result).to.equal(existingLicence.id) + + // Check the updated licence + expect(updatedLicence.expiredDate).to.equal(existingLicence.expiredDate) + expect(updatedLicence.lapsedDate).to.equal(existingLicence.lapsedDate) + expect(updatedLicence.regions).to.equal(existingLicence.regions) + expect(updatedLicence.revokedDate).to.equal(existingLicence.revokedDate) + expect(updatedLicence.startDate).to.equal(existingLicence.startDate) + }) + }) + }) +}) + +async function _fetchPersistedLicence (licenceRef) { + return LicenceModel + .query() + .where('licenceRef', licenceRef) + .select('*') + .limit(1) + .first() +} + +function _transformedLicence (regionId) { + return { + expiredDate: null, + lapsedDate: null, + licenceRef: LicenceHelper.generateLicenceRef(), + regionId, + regions: { + historicalAreaCode: 'KAEA', + regionalChargeArea: 'Southern', + standardUnitChargeCode: 'SUCSO', + localEnvironmentAgencyPlanCode: 'LEME' + }, + revokedDate: null, + startDate: new Date('1992-08-19'), + waterUndertaker: false, + licenceVersions: [] + } +} + +async function _createExistingRecords (region) { + const licence = await LicenceHelper.add({ + expiredDate: new Date('2052-06-23'), + lapsedDate: new Date('2050-07-24'), + licenceRef: LicenceHelper.generateLicenceRef(), + regionId: region.id, + regions: { + historicalAreaCode: 'RIDIN', + regionalChargeArea: 'Yorkshire', + standardUnitChargeCode: 'YORKI', + localEnvironmentAgencyPlanCode: 'AIREL' + }, + revokedDate: new Date('2049-08-25'), + startDate: new Date('1992-08-19') + }) + + return { + licence + } +} From 5ac72f0d57998579b14585ced49c0a5c7679589c Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Mon, 7 Oct 2024 16:01:09 +0100 Subject: [PATCH 06/17] refactor: test into separate folders --- .../import/persist/persist-licence-versions.service.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/services/import/persist/persist-licence-versions.service.test.js b/test/services/import/persist/persist-licence-versions.service.test.js index 118a4896b3..28012e7e2e 100644 --- a/test/services/import/persist/persist-licence-versions.service.test.js +++ b/test/services/import/persist/persist-licence-versions.service.test.js @@ -25,7 +25,7 @@ const { transaction } = require('objection') // Thing under test const PersistLicenceVersionsService = require('../../../../app/services/import/persist/persist-licence-versions.service.js') -describe.only('Persist licence versions service', () => { +describe('Persist licence versions service', () => { let licenceVersionPurposeConditionType let primaryPurpose let purpose From 1596c5ff81eeafb1cb8cc7cd681161080c801a17 Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Mon, 7 Oct 2024 16:04:24 +0100 Subject: [PATCH 07/17] chore: pre pr checks --- .../import/persist/persist-company.service.test.js | 13 +++++-------- .../import/persist/persist-licence.service.test.js | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/test/services/import/persist/persist-company.service.test.js b/test/services/import/persist/persist-company.service.test.js index f108424ed8..c06af81251 100644 --- a/test/services/import/persist/persist-company.service.test.js +++ b/test/services/import/persist/persist-company.service.test.js @@ -28,14 +28,17 @@ describe('Persist company service', () => { const companyContactStartDate = new Date('1999-01-01') let addressExternalId - let updatedAt - let trx let licenceHolderRoleId + let transformedCompany + let trx + let updatedAt beforeEach(async () => { addressExternalId = AddressHelper.generateExternalId() licenceHolderRoleId = LicenceRoleHelper.select().id + transformedCompany = _transformedCompany(licenceHolderRoleId, addressExternalId) + updatedAt = timestampForPostgres() trx = await transaction.start(LicenceModel.knex()) @@ -49,12 +52,9 @@ describe('Persist company service', () => { describe('when given a valid transformed licence', () => { describe('and that licence does not already exist', () => { - let transformedCompany let transformedCompanies beforeEach(() => { - transformedCompany = _transformedCompany(licenceHolderRoleId, addressExternalId) - transformedCompanies = [{ ...transformedCompany }] }) @@ -116,12 +116,9 @@ describe('Persist company service', () => { describe('and that licence already exists', () => { let exisitngContact let existingCompany - let transformedCompany let transformedCompanies beforeEach(async () => { - transformedCompany = _transformedCompany(licenceHolderRoleId, addressExternalId) - const existing = await _createExistingRecords(transformedCompany, licenceHolderRoleId, companyContactStartDate) existingCompany = existing.company diff --git a/test/services/import/persist/persist-licence.service.test.js b/test/services/import/persist/persist-licence.service.test.js index fc033711ea..44bb9c4dd3 100644 --- a/test/services/import/persist/persist-licence.service.test.js +++ b/test/services/import/persist/persist-licence.service.test.js @@ -8,11 +8,11 @@ const { describe, it, afterEach, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const { transaction } = require('objection') const LicenceHelper = require('../../../support/helpers/licence.helper.js') const LicenceModel = require('../../../../app/models/licence.model.js') const RegionHelper = require('../../../support/helpers/region.helper.js') const { timestampForPostgres } = require('../../../../app/lib/general.lib.js') +const { transaction } = require('objection') // Thing under test const PersistLicenceService = require('../../../../app/services/import/persist/persist-licence.service.js') @@ -20,8 +20,8 @@ const PersistLicenceService = require('../../../../app/services/import/persist/p describe('Persist licence service', () => { let newLicence let region - let updatedAt let trx + let updatedAt beforeEach(async () => { region = RegionHelper.select() From a83fd3de42e087d0d4299b0f77d34add183b1e0a Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Tue, 8 Oct 2024 09:51:05 +0100 Subject: [PATCH 08/17] chore: fix js docs --- .../import/persist/persist-company.service.js | 9 ++++----- .../persist/persist-licence-versions.service.js | 15 +++++++-------- .../import/persist/persist-licence.service.js | 10 +++++----- ...ice.test.js => persist-import.service.test.js} | 0 4 files changed, 16 insertions(+), 18 deletions(-) rename test/services/import/{persist-licence.service.test.js => persist-import.service.test.js} (100%) diff --git a/app/services/import/persist/persist-company.service.js b/app/services/import/persist/persist-company.service.js index a44ede7fcd..b1e69b64d0 100644 --- a/app/services/import/persist/persist-company.service.js +++ b/app/services/import/persist/persist-company.service.js @@ -11,13 +11,12 @@ const ContactModel = require('../../../models/contact.model.js') const { db } = require('../../../../db/db.js') /** - * Creates or updates an imported company and its child entities that have been transformed and validated + * Creates or updates an imported company and its child entities that have been transformed and validated. * - * @param trx - * @param updatedAt - * @param {object} transformedCompanies - An object representing a valid WRLS licence + * @param {object} trx - An Objection.js transaction object for PostgreSQL. + * @param {string} updatedAt - The timestamp indicating when the entity was last updated. + * @param {object} transformedCompanies - An object representing a valid WRLS Company. * - * @returns {Promise} - the licence id from WRLS */ async function go (trx, updatedAt, transformedCompanies) { await _persistCompanies(trx, updatedAt, transformedCompanies) diff --git a/app/services/import/persist/persist-licence-versions.service.js b/app/services/import/persist/persist-licence-versions.service.js index 4a3ab4b2a6..8941968537 100644 --- a/app/services/import/persist/persist-licence-versions.service.js +++ b/app/services/import/persist/persist-licence-versions.service.js @@ -10,17 +10,16 @@ const LicenceVersionPurposeModel = require('../../../models/licence-version-purp const LicenceVersionModel = require('../../../models/licence-version.model.js') /** - * Creates or updates an imported licence's versions and its child entities that have been transformed and validated + * Creates or updates an imported licence's versions and its child entities that have been transformed and validated. * - * @param trx - * @param updatedAt - * @param {object} transformedLicence - An object representing a valid WRLS licence + * @param {object} trx - An Objection.js transaction object for PostgreSQL. + * @param {string} updatedAt - The timestamp indicating when the entity was last updated. + * @param {object} transformedLicence - An object representing a valid WRLS licence. + * @param {string} licenceId - A licence id from WRLS * - * @param id - * @returns {Promise} - the licence id from WRLS */ -async function go (trx, updatedAt, transformedLicence, id) { - await _persistLicenceVersions(trx, updatedAt, transformedLicence.licenceVersions, id) +async function go (trx, updatedAt, transformedLicence, licenceId) { + await _persistLicenceVersions(trx, updatedAt, transformedLicence.licenceVersions, licenceId) } async function _persistLicenceVersion (trx, updatedAt, licenceVersion, licenceId) { diff --git a/app/services/import/persist/persist-licence.service.js b/app/services/import/persist/persist-licence.service.js index 758e79eb7c..52bb59fde1 100644 --- a/app/services/import/persist/persist-licence.service.js +++ b/app/services/import/persist/persist-licence.service.js @@ -8,13 +8,13 @@ const LicenceModel = require('../../../models/licence.model.js') /** - * Creates or updates an imported licence that have been transformed and validated + * Creates or updates an imported licence and its child entities that have been transformed and validated. * - * @param trx - * @param updatedAt - * @param {object} transformedLicence - An object representing a valid WRLS licence + * @param {object} trx - An Objection.js transaction object for PostgreSQL. + * @param {string} updatedAt - The timestamp indicating when the entity was last updated. + * @param {object} transformedLicence - An object representing a valid WRLS licence. * - * @returns {Promise} - the licence id from WRLS + * @returns {Promise} - The licence ID from WRLS. */ async function go (trx, updatedAt, transformedLicence) { const { id } = await _persistLicence(trx, updatedAt, transformedLicence) diff --git a/test/services/import/persist-licence.service.test.js b/test/services/import/persist-import.service.test.js similarity index 100% rename from test/services/import/persist-licence.service.test.js rename to test/services/import/persist-import.service.test.js From 3f0685fd5529481d19bca73ebc2b5f60e2950747 Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Tue, 8 Oct 2024 10:14:22 +0100 Subject: [PATCH 09/17] test: update persist import service --- .../import/persist-import.service.test.js | 67 ++++++++++++++++--- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/test/services/import/persist-import.service.test.js b/test/services/import/persist-import.service.test.js index b190e05e8d..5c6a9a03a8 100644 --- a/test/services/import/persist-import.service.test.js +++ b/test/services/import/persist-import.service.test.js @@ -3,32 +3,77 @@ // Test framework dependencies const Lab = require('@hapi/lab') const Code = require('@hapi/code') +const Sinon = require('sinon') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers +const LicenceModel = require('../../../app/models/licence.model.js') +const PersistCompanyService = require('../../../app/services/import/persist/persist-company.service.js') +const PersistLicenceService = require('../../../app/services/import/persist/persist-licence.service.js') +const PersistLicenceVersionsService = require('../../../app/services/import/persist/persist-licence-versions.service.js') // Thing under test const PersistImportService = require('../../../app/services/import/persist-import.service.js') describe('Persist licence service', () => { - let transformedCompanies - let transformedLicence + const transformedCompanies = [] + const transformedLicence = [] - describe('when an error is thrown when persisting the licence', () => { + afterEach(() => { + Sinon.restore() + }) + + describe('when the services are used', () => { beforeEach(() => { - // We cause the error by making the last record to be persisted have an invalid value. This demonstrates the - // purpose of the DB transaction: either all records are persisted or none of them - transformedLicence.licenceVersions[0].licenceVersionPurposes[0].primaryPurposeId = 'boom' + Sinon.stub(LicenceModel, 'transaction').callsFake(async (callback) => { + try { + return await callback() // Execute the callback within the transaction + } catch (error) { + return Promise.reject(error) // Properly handle and pass through the rejection + } + }) + }) + + describe('when a licence has been successfully creates or updated', () => { + beforeEach(() => { + Sinon.stub(PersistLicenceService, 'go').resolves('1234') + Sinon.stub(PersistLicenceVersionsService, 'go').resolves() + Sinon.stub(PersistCompanyService, 'go').resolves() + }) + + it('should return the licence id', async () => { + const result = await PersistImportService.go(transformedLicence, transformedCompanies) + + expect(result).to.equal('1234') + }) }) - it('throws an error and persists nothing', async () => { - await expect(PersistImportService.go(transformedLicence, transformedCompanies)).to.reject() + describe('when a service has failed', () => { + beforeEach(() => { + Sinon.stub(PersistLicenceService, 'go').resolves('1234') + Sinon.stub(PersistLicenceVersionsService, 'go').resolves() + Sinon.stub(PersistCompanyService, 'go').rejects(new Error('boom')) + }) - const newLicence = await _fetchPersistedLicence(transformedLicence.licenceRef) + it('should throw an error', async () => { + await expect(PersistImportService.go(transformedLicence, transformedCompanies)).to.reject(Error, 'boom') + }) + }) + }) + + describe('when the transaction has failed', () => { + beforeEach(() => { + Sinon.stub(PersistLicenceService, 'go').resolves('1234') + Sinon.stub(PersistLicenceVersionsService, 'go').resolves() + Sinon.stub(PersistCompanyService, 'go').resolves() + + Sinon.stub(LicenceModel, 'transaction').rejects(new Error('boom')) + }) - expect(newLicence).to.be.undefined() + it('should throw an error', async () => { + await expect(PersistImportService.go(transformedLicence, transformedCompanies)).to.reject(Error, 'boom') }) }) }) From 5462a5ea191b7703d8b3bd4a983438d742f42884 Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Tue, 8 Oct 2024 10:31:36 +0100 Subject: [PATCH 10/17] chore: pre pr checks --- test/services/import/persist-import.service.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/services/import/persist-import.service.test.js b/test/services/import/persist-import.service.test.js index 5c6a9a03a8..9cf544785d 100644 --- a/test/services/import/persist-import.service.test.js +++ b/test/services/import/persist-import.service.test.js @@ -36,7 +36,7 @@ describe('Persist licence service', () => { }) }) - describe('when a licence has been successfully creates or updated', () => { + describe('when a licence has been successfully created or updated', () => { beforeEach(() => { Sinon.stub(PersistLicenceService, 'go').resolves('1234') Sinon.stub(PersistLicenceVersionsService, 'go').resolves() From a396a09ee208ba425d93bb67e11e49eb959ec3ba Mon Sep 17 00:00:00 2001 From: Jonathan Goulding <58443816+jonathangoulding@users.noreply.github.com> Date: Thu, 10 Oct 2024 10:22:12 +0100 Subject: [PATCH 11/17] Update test/services/import/legacy/process-licence.service.test.js Co-authored-by: Alan Cruikshanks --- test/services/import/legacy/process-licence.service.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/services/import/legacy/process-licence.service.test.js b/test/services/import/legacy/process-licence.service.test.js index c01d3272b6..8b5525bb83 100644 --- a/test/services/import/legacy/process-licence.service.test.js +++ b/test/services/import/legacy/process-licence.service.test.js @@ -13,7 +13,7 @@ const { generateUUID } = require('../../../../app/lib/general.lib.js') const { generateLicenceRef } = require('../../../support/helpers/licence.helper.js') // Things to stub -const PersistLicenceService = require('../../../../app/services/import/persist-import.service.js') +const PersistImportService = require('../../../../app/services/import/persist-import.service.js') 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') From 586183d7de240c3f2bc992c03c0715c100572637 Mon Sep 17 00:00:00 2001 From: Jonathan Goulding <58443816+jonathangoulding@users.noreply.github.com> Date: Thu, 10 Oct 2024 10:41:59 +0100 Subject: [PATCH 12/17] Update app/services/import/legacy/process-licence.service.js Co-authored-by: Alan Cruikshanks --- app/services/import/legacy/process-licence.service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/import/legacy/process-licence.service.js b/app/services/import/legacy/process-licence.service.js index f4cc7bcc2a..1db1ef2ebe 100644 --- a/app/services/import/legacy/process-licence.service.js +++ b/app/services/import/legacy/process-licence.service.js @@ -6,7 +6,7 @@ */ const LicenceStructureValidator = require('../../../validators/import/licence-structure.validator.js') -const PersistLicenceService = require('../persist-import.service.js') +const PersistImportService = require('../persist-import.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') From c78358238272ece4910f622bd32b2d283da94767 Mon Sep 17 00:00:00 2001 From: Jonathan Goulding <58443816+jonathangoulding@users.noreply.github.com> Date: Thu, 10 Oct 2024 10:42:08 +0100 Subject: [PATCH 13/17] Update app/services/import/persist/persist-company.service.js Co-authored-by: Alan Cruikshanks --- app/services/import/persist/persist-company.service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/import/persist/persist-company.service.js b/app/services/import/persist/persist-company.service.js index b1e69b64d0..8dd605b2c9 100644 --- a/app/services/import/persist/persist-company.service.js +++ b/app/services/import/persist/persist-company.service.js @@ -19,7 +19,7 @@ const { db } = require('../../../../db/db.js') * */ async function go (trx, updatedAt, transformedCompanies) { - await _persistCompanies(trx, updatedAt, transformedCompanies) + return _persistCompanies(trx, updatedAt, transformedCompanies) } async function _persistAddress (trx, updatedAt, address) { From 814b9df4f0b9e3ed9e8920206e06b9a3f6c625fd Mon Sep 17 00:00:00 2001 From: Jonathan Goulding <58443816+jonathangoulding@users.noreply.github.com> Date: Thu, 10 Oct 2024 10:42:42 +0100 Subject: [PATCH 14/17] Update app/services/import/persist-import.service.js Co-authored-by: Alan Cruikshanks --- app/services/import/persist-import.service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/import/persist-import.service.js b/app/services/import/persist-import.service.js index 9869c44dba..cd97a50cf1 100644 --- a/app/services/import/persist-import.service.js +++ b/app/services/import/persist-import.service.js @@ -5,7 +5,7 @@ * @module PersistImportService */ -const PersistImportService = require('./persist/persist-licence.service.js') +const PersistLicenceService = require('./persist/persist-licence.service.js') const PersistLicenceVersionsService = require('./persist/persist-licence-versions.service.js') const PersistCompanyService = require('./persist/persist-company.service.js') const LicenceModel = require('../../models/licence.model.js') From c71ced305885050491a8ee52f4fe28cabb580852 Mon Sep 17 00:00:00 2001 From: Jonathan Goulding <58443816+jonathangoulding@users.noreply.github.com> Date: Thu, 10 Oct 2024 10:43:01 +0100 Subject: [PATCH 15/17] Update app/services/import/persist/persist-licence-versions.service.js Co-authored-by: Alan Cruikshanks --- app/services/import/persist/persist-licence-versions.service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/import/persist/persist-licence-versions.service.js b/app/services/import/persist/persist-licence-versions.service.js index 8941968537..19c40a528b 100644 --- a/app/services/import/persist/persist-licence-versions.service.js +++ b/app/services/import/persist/persist-licence-versions.service.js @@ -19,7 +19,7 @@ const LicenceVersionModel = require('../../../models/licence-version.model.js') * */ async function go (trx, updatedAt, transformedLicence, licenceId) { - await _persistLicenceVersions(trx, updatedAt, transformedLicence.licenceVersions, licenceId) + return _persistLicenceVersions(trx, updatedAt, transformedLicence.licenceVersions, licenceId) } async function _persistLicenceVersion (trx, updatedAt, licenceVersion, licenceId) { From e740f3401a8c8093a6524a3a948121776047db44 Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Thu, 10 Oct 2024 10:45:05 +0100 Subject: [PATCH 16/17] chore: fix pr issues --- app/services/import/legacy/process-licence.service.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/import/legacy/process-licence.service.js b/app/services/import/legacy/process-licence.service.js index 1db1ef2ebe..e52d7cda1c 100644 --- a/app/services/import/legacy/process-licence.service.js +++ b/app/services/import/legacy/process-licence.service.js @@ -9,8 +9,8 @@ const LicenceStructureValidator = require('../../../validators/import/licence-st const PersistImportService = require('../persist-import.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 TransformCompanyAddressesService = require('./transform-company-addresses.service.js') const TransformContactsService = require('./transform-contacts.service.js') const TransformLicenceService = require('./transform-licence.service.js') const TransformLicenceVersionPurposeConditionsService = require('./transform-licence-version-purpose-conditions.service.js') @@ -50,7 +50,7 @@ async function go (licenceRef) { LicenceStructureValidator.go(transformedLicence) // Either insert or update the licence in WRLS - const licenceId = await PersistLicenceService.go(transformedLicence, transformedCompanies) + const licenceId = await PersistImportService.go(transformedLicence, transformedCompanies) if (wrlsLicenceId) { await ProcessLicenceReturnLogsService.go(wrlsLicenceId) From a9ae425cd036ce70ac97352117985a4587bc0cbe Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Thu, 10 Oct 2024 10:54:30 +0100 Subject: [PATCH 17/17] chore: fix pr issues --- app/services/import/persist-import.service.js | 2 +- .../import/legacy/process-licence.service.test.js | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/services/import/persist-import.service.js b/app/services/import/persist-import.service.js index cd97a50cf1..c9256dfd28 100644 --- a/app/services/import/persist-import.service.js +++ b/app/services/import/persist-import.service.js @@ -23,7 +23,7 @@ async function go (transformedLicence, transformedCompanies) { return LicenceModel.transaction(async (trx) => { const updatedAt = timestampForPostgres() - const id = await PersistImportService.go(trx, updatedAt, transformedLicence) + const id = await PersistLicenceService.go(trx, updatedAt, transformedLicence) await PersistLicenceVersionsService.go(trx, updatedAt, transformedLicence, id) diff --git a/test/services/import/legacy/process-licence.service.test.js b/test/services/import/legacy/process-licence.service.test.js index 8b5525bb83..fba14e3f2f 100644 --- a/test/services/import/legacy/process-licence.service.test.js +++ b/test/services/import/legacy/process-licence.service.test.js @@ -34,7 +34,7 @@ describe('Import Legacy Process Licence service', () => { let licenceId let licenceRef let notifierStub - let persistLicenceServiceStub + let PersistImportServiceStub let processLicenceReturnLogsServiceStub let transformedLicence let wrlsLicenceId @@ -69,14 +69,14 @@ describe('Import Legacy Process Licence service', () => { describe('when there is a valid NALD licence to import with an existing licence', () => { beforeEach(() => { Sinon.stub(TransformLicenceService, 'go').resolves({ naldLicenceId, regionCode, transformedLicence, wrlsLicenceId }) - persistLicenceServiceStub = Sinon.stub(PersistLicenceService, 'go').resolves(licenceId) + PersistImportServiceStub = Sinon.stub(PersistImportService, 'go').resolves(licenceId) processLicenceReturnLogsServiceStub = Sinon.stub(ProcessLicenceReturnLogsService, 'go').resolves() }) it('saves the imported licence and creates the return logs', async () => { await ProcessLicenceService.go(licenceRef) - expect(persistLicenceServiceStub.calledWith(transformedLicence)).to.be.true() + expect(PersistImportServiceStub.calledWith(transformedLicence)).to.be.true() expect(processLicenceReturnLogsServiceStub.calledWith(wrlsLicenceId)).to.be.true() }) @@ -98,21 +98,21 @@ describe('Import Legacy Process Licence service', () => { describe('when there is a valid NALD licence to import without an existing licence', () => { beforeEach(() => { Sinon.stub(TransformLicenceService, 'go').resolves({ naldLicenceId, regionCode, transformedLicence }) - persistLicenceServiceStub = Sinon.stub(PersistLicenceService, 'go').resolves(licenceId) + PersistImportServiceStub = Sinon.stub(PersistImportService, 'go').resolves(licenceId) processLicenceReturnLogsServiceStub = Sinon.stub(ProcessLicenceReturnLogsService, 'go').resolves() }) it('saves the imported licence but does not process the return logs', async () => { await ProcessLicenceService.go(licenceRef) - expect(persistLicenceServiceStub.calledWith(transformedLicence)).to.be.true() + expect(PersistImportServiceStub.calledWith(transformedLicence)).to.be.true() expect(processLicenceReturnLogsServiceStub.calledWith(wrlsLicenceId)).to.be.false() }) }) describe('when the service errors', () => { beforeEach(() => { - persistLicenceServiceStub = Sinon.stub(PersistLicenceService, 'go').rejects() + PersistImportServiceStub = Sinon.stub(PersistImportService, 'go').rejects() }) it('handles the error', async () => {