diff --git a/CHANGELOG.md b/CHANGELOG.md index 128475bb5..7f7c3a6bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,89 @@ All notable changes to this project will be documented in this file. Dates are d Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). +#### [v0.24.0](https://github.com/DEFRA/water-abstraction-system/compare/v0.23.0...v0.24.0) + +- Import licence document roles for a licence [`#1414`](https://github.com/DEFRA/water-abstraction-system/pull/1414) +- Bump eslint-plugin-jsdoc from 50.4.1 to 50.4.3 [`#1420`](https://github.com/DEFRA/water-abstraction-system/pull/1420) +- Bump @hapi/lab from 25.3.1 to 25.3.2 [`#1421`](https://github.com/DEFRA/water-abstraction-system/pull/1421) +- Bump @aws-sdk/client-s3 from 3.673.0 to 3.674.0 [`#1422`](https://github.com/DEFRA/water-abstraction-system/pull/1422) +- Remove POST create bill run route [`#1418`](https://github.com/DEFRA/water-abstraction-system/pull/1418) +- Exclude VOID returns from two-part tariff billing [`#1419`](https://github.com/DEFRA/water-abstraction-system/pull/1419) +- 2PT use full authorised volume rather than Nil [`#1404`](https://github.com/DEFRA/water-abstraction-system/pull/1404) +- Bump @aws-sdk/client-s3 from 3.670.0 to 3.673.0 [`#1417`](https://github.com/DEFRA/water-abstraction-system/pull/1417) +- Rename gauging stations to monitoring stations [`#1415`](https://github.com/DEFRA/water-abstraction-system/pull/1415) +- Fix auth scope on view monitoring station route [`#1413`](https://github.com/DEFRA/water-abstraction-system/pull/1413) +- Import licence document for a licence [`#1392`](https://github.com/DEFRA/water-abstraction-system/pull/1392) +- Bump eslint-plugin-jsdoc from 50.3.2 to 50.4.1 [`#1411`](https://github.com/DEFRA/water-abstraction-system/pull/1411) +- Add option to run tests without cleaning [`#1409`](https://github.com/DEFRA/water-abstraction-system/pull/1409) +- Handle missing Monitoring station metadata [`#1405`](https://github.com/DEFRA/water-abstraction-system/pull/1405) +- Bump eslint-plugin-jsdoc from 50.3.1 to 50.3.2 [`#1408`](https://github.com/DEFRA/water-abstraction-system/pull/1408) +- Bump @aws-sdk/client-s3 from 3.669.0 to 3.670.0 [`#1406`](https://github.com/DEFRA/water-abstraction-system/pull/1406) +- Bump mock-fs from 5.3.0 to 5.4.0 [`#1407`](https://github.com/DEFRA/water-abstraction-system/pull/1407) +- Refactor the import legacy persist logic [`#1387`](https://github.com/DEFRA/water-abstraction-system/pull/1387) +- Return log start and end dates to be determined using the correct cycle dates [`#1397`](https://github.com/DEFRA/water-abstraction-system/pull/1397) +- Bump govuk-frontend from 4.8.0 to 4.9.0 [`#1402`](https://github.com/DEFRA/water-abstraction-system/pull/1402) +- Bump @aws-sdk/client-s3 from 3.668.0 to 3.669.0 [`#1403`](https://github.com/DEFRA/water-abstraction-system/pull/1403) +- Fix error when seeding in AWS non-production [`#1400`](https://github.com/DEFRA/water-abstraction-system/pull/1400) +- Fix error with return cycles seeder [`#1399`](https://github.com/DEFRA/water-abstraction-system/pull/1399) +- Remove setting return cycle in ReturnLogHelper [`#1398`](https://github.com/DEFRA/water-abstraction-system/pull/1398) +- Fix QA issues in new Monitoring stations page [`#1395`](https://github.com/DEFRA/water-abstraction-system/pull/1395) +- Handle special characters in rtn req. bookmarks [`#1394`](https://github.com/DEFRA/water-abstraction-system/pull/1394) +- Bump @aws-sdk/client-s3 from 3.667.0 to 3.668.0 [`#1396`](https://github.com/DEFRA/water-abstraction-system/pull/1396) +- Bump @aws-sdk/client-s3 from 3.665.0 to 3.667.0 [`#1393`](https://github.com/DEFRA/water-abstraction-system/pull/1393) +- Refactor logic for creating return cycles when importing new licence [`#1389`](https://github.com/DEFRA/water-abstraction-system/pull/1389) +- Migrate monitoring station view page [`#1340`](https://github.com/DEFRA/water-abstraction-system/pull/1340) +- Handle empty site description in returns setup [`#1384`](https://github.com/DEFRA/water-abstraction-system/pull/1384) +- Import company address data for a licence [`#1385`](https://github.com/DEFRA/water-abstraction-system/pull/1385) +- Add abstraction method to lic. purpose points [`#1375`](https://github.com/DEFRA/water-abstraction-system/pull/1375) +- Stop display null site desc. in view licence rtn [`#1383`](https://github.com/DEFRA/water-abstraction-system/pull/1383) +- Bump @aws-sdk/client-s3 from 3.664.0 to 3.665.0 [`#1386`](https://github.com/DEFRA/water-abstraction-system/pull/1386) +- Import company address data for a licence [`#1371`](https://github.com/DEFRA/water-abstraction-system/pull/1371) +- Bump @aws-sdk/client-s3 from 3.663.0 to 3.664.0 [`#1381`](https://github.com/DEFRA/water-abstraction-system/pull/1381) +- Handle return requirements with no site desc. [`#1380`](https://github.com/DEFRA/water-abstraction-system/pull/1380) +- Handle invalid points better in view lic. summary [`#1374`](https://github.com/DEFRA/water-abstraction-system/pull/1374) +- Stop adding link on due returns for unauth users [`#1378`](https://github.com/DEFRA/water-abstraction-system/pull/1378) +- Fix down migrations for return logs view [`#1377`](https://github.com/DEFRA/water-abstraction-system/pull/1377) +- Bump @aws-sdk/client-s3 from 3.662.0 to 3.663.0 [`#1376`](https://github.com/DEFRA/water-abstraction-system/pull/1376) +- Bump eslint-plugin-jsdoc from 50.3.0 to 50.3.1 [`#1373`](https://github.com/DEFRA/water-abstraction-system/pull/1373) +- Bump @aws-sdk/client-s3 from 3.658.1 to 3.662.0 [`#1372`](https://github.com/DEFRA/water-abstraction-system/pull/1372) +- Create return cycle [`#1353`](https://github.com/DEFRA/water-abstraction-system/pull/1353) +- Stop generating optional data in company helper [`#1369`](https://github.com/DEFRA/water-abstraction-system/pull/1369) +- Bump sass from 1.79.3 to 1.79.4 [`#1370`](https://github.com/DEFRA/water-abstraction-system/pull/1370) +- Incorrect status showing on licence set up page [`#1367`](https://github.com/DEFRA/water-abstraction-system/pull/1367) +- Update view licence summary to use new points data [`#1316`](https://github.com/DEFRA/water-abstraction-system/pull/1316) +- Bump mock-fs from 5.2.0 to 5.3.0 [`#1366`](https://github.com/DEFRA/water-abstraction-system/pull/1366) +- Bump eslint-plugin-jsdoc from 50.2.5 to 50.3.0 [`#1365`](https://github.com/DEFRA/water-abstraction-system/pull/1365) +- Include water.points in data tear down [`#1364`](https://github.com/DEFRA/water-abstraction-system/pull/1364) +- Incorrect status showing on licence set up page [`#1363`](https://github.com/DEFRA/water-abstraction-system/pull/1363) +- Remove import jobs stats from /health/info [`#1336`](https://github.com/DEFRA/water-abstraction-system/pull/1336) +- Import addresses data for a licence [`#1355`](https://github.com/DEFRA/water-abstraction-system/pull/1355) +- Remove `DatabaseSupport.clean()` from unit tests [`#1327`](https://github.com/DEFRA/water-abstraction-system/pull/1327) +- Bump objection from 3.1.4 to 3.1.5 [`#1361`](https://github.com/DEFRA/water-abstraction-system/pull/1361) +- Bump @aws-sdk/client-s3 from 3.658.0 to 3.658.1 [`#1362`](https://github.com/DEFRA/water-abstraction-system/pull/1362) +- Bump eslint-plugin-jsdoc from 50.2.4 to 50.2.5 [`#1360`](https://github.com/DEFRA/water-abstraction-system/pull/1360) +- Alter 2PT agreement logic in rtn req setup journey [`#1359`](https://github.com/DEFRA/water-abstraction-system/pull/1359) +- Add site description to rtn. req. bookmark links [`#1358`](https://github.com/DEFRA/water-abstraction-system/pull/1358) +- Fix show unregistered label in view licence page [`#1357`](https://github.com/DEFRA/water-abstraction-system/pull/1357) +- Remove check for licence ended in approve rtn vers [`#1356`](https://github.com/DEFRA/water-abstraction-system/pull/1356) +- Bump @aws-sdk/client-s3 from 3.657.0 to 3.658.0 [`#1354`](https://github.com/DEFRA/water-abstraction-system/pull/1354) +- Update the 'points' data solution [`#1352`](https://github.com/DEFRA/water-abstraction-system/pull/1352) +- Import contact data for a licence [`#1317`](https://github.com/DEFRA/water-abstraction-system/pull/1317) +- Add pre sroc returns flagging to service [`#1335`](https://github.com/DEFRA/water-abstraction-system/pull/1335) +- Bump @aws-sdk/client-s3 from 3.654.0 to 3.657.0 [`#1351`](https://github.com/DEFRA/water-abstraction-system/pull/1351) +- Bump sass from 1.79.2 to 1.79.3 [`#1350`](https://github.com/DEFRA/water-abstraction-system/pull/1350) +- Bump sass from 1.79.1 to 1.79.2 [`#1349`](https://github.com/DEFRA/water-abstraction-system/pull/1349) +- Add external id to addresses view. [`#1346`](https://github.com/DEFRA/water-abstraction-system/pull/1346) +- Bump @aws-sdk/client-s3 from 3.651.1 to 3.654.0 [`#1348`](https://github.com/DEFRA/water-abstraction-system/pull/1348) +- Bump eslint-plugin-jsdoc from 50.2.3 to 50.2.4 [`#1347`](https://github.com/DEFRA/water-abstraction-system/pull/1347) +- Update Licence role helper to use reference data [`#1345`](https://github.com/DEFRA/water-abstraction-system/pull/1345) +- Create return logs for newly imported licences [`#1294`](https://github.com/DEFRA/water-abstraction-system/pull/1294) +- Update seeder to prevent duplicate key issue `duplicate key value violates unique constraint "points_external_id_unique"` [`8d5aec0`](https://github.com/DEFRA/water-abstraction-system/commit/8d5aec003b6ad992fefeaeedab82bc89fbc658da) + #### [v0.23.0](https://github.com/DEFRA/water-abstraction-system/compare/v0.22.0...v0.23.0) +> 18 September 2024 + - Bump sass from 1.78.0 to 1.79.1 [`#1343`](https://github.com/DEFRA/water-abstraction-system/pull/1343) - Bump pg-query-stream from 4.6.0 to 4.7.0 [`#1344`](https://github.com/DEFRA/water-abstraction-system/pull/1344) - Bump pg from 8.12.0 to 8.13.0 [`#1342`](https://github.com/DEFRA/water-abstraction-system/pull/1342) @@ -1056,7 +1137,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). - Add back in our check endpoint [`#302`](https://github.com/DEFRA/water-abstraction-system/pull/302) - Bump @aws-sdk/client-s3 from 3.367.0 to 3.369.0 [`#303`](https://github.com/DEFRA/water-abstraction-system/pull/303) -#### [v0.7.0](https://github.com/DEFRA/water-abstraction-system/compare/v0.5.0...v0.7.0) +#### [v0.7.0](https://github.com/DEFRA/water-abstraction-system/compare/v0.6.0...v0.7.0) > 10 July 2023 @@ -1111,6 +1192,11 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). - Bump @aws-sdk/client-s3 from 3.342.0 to 3.344.0 [`#252`](https://github.com/DEFRA/water-abstraction-system/pull/252) - Split `ProcessBillingPeriodService` into multiple services [`#241`](https://github.com/DEFRA/water-abstraction-system/pull/241) - Bump dotenv from 16.1.1 to 16.1.3 [`#251`](https://github.com/DEFRA/water-abstraction-system/pull/251) + +#### [v0.6.0](https://github.com/DEFRA/water-abstraction-system/compare/v0.5.0...v0.6.0) + +> 1 June 2023 + - Export a schema [`#238`](https://github.com/DEFRA/water-abstraction-system/pull/238) - Bump dotenv from 16.0.3 to 16.1.1 [`#248`](https://github.com/DEFRA/water-abstraction-system/pull/248) - Bump @aws-sdk/client-s3 from 3.341.0 to 3.342.0 [`#249`](https://github.com/DEFRA/water-abstraction-system/pull/249) diff --git a/app/presenters/import/legacy/licence-document-role.presenter.js b/app/presenters/import/legacy/licence-document-role.presenter.js new file mode 100644 index 000000000..7a4fbc695 --- /dev/null +++ b/app/presenters/import/legacy/licence-document-role.presenter.js @@ -0,0 +1,31 @@ +'use strict' + +/** + * Maps legacy NALD licence data to the WRLS licence document role format + * @module LicenceDocumentRolePresenter + */ + +/** + * Maps legacy NALD licence data to the WRLS licence document role format + * + * @param {ImportLegacyLicenceDocumentRoleType} licenceDocumentRole - the legacy NALD licence + * @param {string} licenceRef - the licence ref for the licence document + * + * @returns {object} the NALD licence data transformed into the WRLS licence document role format + * ready for validation and persisting + */ +function go (licenceDocumentRole, licenceRef) { + return { + addressId: licenceDocumentRole.address_id, + companyId: licenceDocumentRole.company_id, + contactId: licenceDocumentRole.contact_id, + documentId: licenceRef, + endDate: licenceDocumentRole.end_date, + licenceRoleId: licenceDocumentRole.licence_role_id, + startDate: licenceDocumentRole.start_date + } +} + +module.exports = { + go +} diff --git a/app/presenters/import/legacy/licence-document.presenter.js b/app/presenters/import/legacy/licence-document.presenter.js index a12412276..99f53a172 100644 --- a/app/presenters/import/legacy/licence-document.presenter.js +++ b/app/presenters/import/legacy/licence-document.presenter.js @@ -15,6 +15,8 @@ */ function go (licenceDocument) { return { + // Add an empty array property ready for when transforming and attaching licence document roles + licenceDocumentRoles: [], licenceRef: licenceDocument.licence_ref, endDate: licenceDocument.end_date, startDate: licenceDocument.start_date diff --git a/app/services/import/legacy/fetch-licence-document-roles.service.js b/app/services/import/legacy/fetch-licence-document-roles.service.js new file mode 100644 index 000000000..a6a16d61c --- /dev/null +++ b/app/services/import/legacy/fetch-licence-document-roles.service.js @@ -0,0 +1,72 @@ +'use strict' + +/** + * Fetches the licence document roles data from the import data + * @module FetchLicenceDocumentRolesService + */ + +const { db } = require('../../../../db/db.js') + +/** + * Fetches the licence document roles data from the import data + * + * This will come from the licence versions and the licence roles. + * + * We are only interest in the returnsTo licence role from the import.NALD_LIC_ROLES + * + * @param {string} regionCode - The NALD region code + * @param {string} licenceId - The NALD licence ID + * + * @returns {Promise} + */ +async function go (regionCode, licenceId) { + const query = _query() + + const { rows } = await db.raw(query, [regionCode, licenceId]) + + return rows +} + +function _query () { + return ` + SELECT + lr.id as licence_role_id, + TO_DATE(nlr."EFF_ST_DATE", 'DD/MM/YYY' ) as start_date, + TO_DATE(NULLIF(nlr."EFF_END_DATE", 'null'), 'DD/MM/YYY' ) as end_date, + concat_ws(':', nlr."FGAC_REGION_CODE", nlr."ACON_AADD_ID") as address_id, + concat_ws(':', nlr."FGAC_REGION_CODE", nlr."ACON_APAR_ID") as company_id, + ( + CASE + WHEN np."APAR_TYPE" = 'ORG' + THEN NULL + ELSE concat_ws(':', nlr."FGAC_REGION_CODE", nlr."ACON_APAR_ID") + END + ) as contact_id + FROM import."NALD_LIC_ROLES" nlr + INNER JOIN public.licence_roles lr + ON lr.name = 'returnsTo' + Inner JOIN import."NALD_PARTIES" np + ON np."FGAC_REGION_CODE" = nlr."FGAC_REGION_CODE" + AND np."ID" = nlr."ACON_APAR_ID" + WHERE + nlr."ALRT_CODE" = 'RT' + AND nlr."FGAC_REGION_CODE" = ? + AND nlr."AABL_ID" = ?; + ` +} + +module.exports = { + go +} + +/** + * Representation of a licence document fetched from the NALD data + * @typedef {object} ImportLegacyLicenceDocumentRoleType + * + * @property { guid } licence_role_id - The licence role id from WRLS + * @property { Date } start_date - EFF_ST_DATE + * @property { Date } end_date - EFF_END_DATE + * @property { string } address_id - used to link to the address (external id) + * @property { string } company_id - used to link to the company (external id) + * @property { string } contact_id - a person should have a contact id + */ diff --git a/app/services/import/legacy/process-licence.service.js b/app/services/import/legacy/process-licence.service.js index c91789b92..37fefc170 100644 --- a/app/services/import/legacy/process-licence.service.js +++ b/app/services/import/legacy/process-licence.service.js @@ -10,6 +10,7 @@ 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 TransformLicenceDocumentService = require('./transform-licence-document.service.js') +const TransformLicenceDocumentRolesService = require('./transform-licence-document-roles.service.js') const TransformCompaniesService = require('./transform-companies.service.js') const TransformCompanyAddressesService = require('./transform-company-addresses.service.js') const TransformContactsService = require('./transform-contacts.service.js') @@ -41,6 +42,7 @@ async function go (licenceRef) { // Document await TransformLicenceDocumentService.go(regionCode, naldLicenceId, transformedLicence) + await TransformLicenceDocumentRolesService.go(regionCode, naldLicenceId, transformedLicence, licenceRef) // Transform the company data const { transformedCompanies } = await TransformCompaniesService.go(regionCode, naldLicenceId) diff --git a/app/services/import/legacy/transform-licence-document-roles.service.js b/app/services/import/legacy/transform-licence-document-roles.service.js new file mode 100644 index 000000000..1db600a3a --- /dev/null +++ b/app/services/import/legacy/transform-licence-document-roles.service.js @@ -0,0 +1,37 @@ +'use strict' + +/** + * Transforms all NALD data into an object that matches the WRLS licence document role structure + * @module ImportLegacyTransformLicenceDocumentRolesService + */ + +const FetchLicenceDocumentRolesService = require('./fetch-licence-document-roles.service.js') +const ImportLicenceDocumentRoleValidator = require('../../../validators/import/licence-document-role.validator.js') +const LicenceDocumentRolePresenter = require('../../../presenters/import/legacy/licence-document-role.presenter.js') + +/** + * Transforms all NALD data into an object that matches the WRLS licence document role structure + * + * NALD does not have a concept of a licence document or licence document role it is a legacy WRLS construct + * + * @param {string} regionCode - The NALD region code for the licence being imported + * @param {string} naldLicenceId - The NALD ID for the licence being imported + * @param {object} transformedLicence - An object representing a valid WRLS licence + * @param licenceRef + * + */ +async function go (regionCode, naldLicenceId, transformedLicence, licenceRef) { + const naldLicenceDocumentRoles = await FetchLicenceDocumentRolesService.go(regionCode, naldLicenceId) + + for (const licenceDocumentRole of naldLicenceDocumentRoles) { + const transformedLicenceDocumentRole = LicenceDocumentRolePresenter.go(licenceDocumentRole, licenceRef) + + ImportLicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + + transformedLicence.licenceDocument.licenceDocumentRoles.push(transformedLicenceDocumentRole) + } +} + +module.exports = { + go +} diff --git a/app/services/import/persist/persist-licence-document.service.js b/app/services/import/persist/persist-licence-document.service.js index 56e3a829d..0a06af68f 100644 --- a/app/services/import/persist/persist-licence-document.service.js +++ b/app/services/import/persist/persist-licence-document.service.js @@ -6,6 +6,7 @@ */ const LicenceDocumentModel = require('../../../models/licence-document.model.js') +const { db } = require('../../../../db/db.js') /** * Creates or updates a licence document @@ -18,11 +19,15 @@ const LicenceDocumentModel = require('../../../models/licence-document.model.js' */ async function go (trx, updatedAt, transformedLicence) { await _persistLicenceDocument(trx, updatedAt, transformedLicence.licenceDocument) + + await _persistLicenceDocumentRoles(trx, updatedAt, transformedLicence.licenceDocument.licenceDocumentRoles) } async function _persistLicenceDocument (trx, updatedAt, licenceDocument) { + const { licenceDocumentRoles, ...propertiesToPersist } = licenceDocument + return LicenceDocumentModel.query(trx) - .insert({ ...licenceDocument, updatedAt }) + .insert({ ...propertiesToPersist, updatedAt }) .onConflict('licenceRef') .merge([ 'endDate', @@ -31,6 +36,35 @@ async function _persistLicenceDocument (trx, updatedAt, licenceDocument) { ]) } +async function _persistLicenceDocumentRoles (trx, updatedAt, licenceDocumentRoles) { + for (const licenceDocumentRole of licenceDocumentRoles) { + await _persistLicenceDocumentRole(trx, updatedAt, licenceDocumentRole) + } +} + +async function _persistLicenceDocumentRole (trx, updatedAt, licenceDocument) { + const { addressId, companyId, contactId, documentId, licenceRoleId, startDate, endDate } = licenceDocument + + return db.raw(` + INSERT INTO public."licence_document_roles" (address_id, company_id, contact_id, licence_document_id, licence_role_id, start_date, end_date, updated_at) + SELECT add.id, com.id, con.id, ld.id, lr.id, ? ,?, ? + FROM public.licence_documents ld + JOIN public."licence_roles" lr on lr.id = ? + JOIN public.addresses add ON add.external_id = ? + JOIN public.companies com ON com.external_id = ? + LEFT JOIN public.contacts con ON con.external_id = ? + WHERE ld.licence_ref = ? + ON CONFLICT (licence_document_id, licence_role_id, start_date) + DO UPDATE SET + company_id=EXCLUDED.company_id, + contact_id=EXCLUDED.contact_id, + address_id=EXCLUDED.address_id, + end_date=EXCLUDED.end_date, + updated_at = EXCLUDED.updated_at + `, [startDate, endDate, updatedAt, licenceRoleId, addressId, companyId, contactId, documentId]) + .transacting(trx) +} + module.exports = { go } diff --git a/app/services/import/persist/persist-licence.service.js b/app/services/import/persist/persist-licence.service.js index 52bb59fde..4076b7a8c 100644 --- a/app/services/import/persist/persist-licence.service.js +++ b/app/services/import/persist/persist-licence.service.js @@ -23,7 +23,7 @@ async function go (trx, updatedAt, transformedLicence) { } async function _persistLicence (trx, updatedAt, licence) { - const { licenceVersions, ...propertiesToPersist } = licence + const { licenceVersions, licenceDocument, ...propertiesToPersist } = licence return LicenceModel.query(trx) .insert({ ...propertiesToPersist, updatedAt }) diff --git a/app/validators/import/licence-document-role.validator.js b/app/validators/import/licence-document-role.validator.js new file mode 100644 index 000000000..136e99715 --- /dev/null +++ b/app/validators/import/licence-document-role.validator.js @@ -0,0 +1,38 @@ +'use strict' + +/** + * @module ImportLicenceDocumentRoleValidator + */ + +const Joi = require('joi') + +/** + * Checks that imported licence data that has been transformed is valid for persisting to WRLS + * as a licence document role + * + * @param {object} licenceDocumentRole - The transformed licence data into a licence document role + * + * @throws {Joi.ValidationError} - throws a Joi validation error if the validation fails + */ +function go (licenceDocumentRole) { + const schema = Joi.object({ + addressId: Joi.string().required(), + companyId: Joi.string().required(), + contactId: Joi.string().allow(null), + documentId: Joi.string().required(), + endDate: Joi.date().required().allow(null), + licenceRoleId: Joi.string().guid().required(), + startDate: Joi.date().required() + + }) + + const result = schema.validate(licenceDocumentRole, { convert: false }) + + if (result.error) { + throw result.error + } +} + +module.exports = { + go +} diff --git a/app/validators/import/licence-document.validator.js b/app/validators/import/licence-document.validator.js index 97b12e2fe..d6531896e 100644 --- a/app/validators/import/licence-document.validator.js +++ b/app/validators/import/licence-document.validator.js @@ -17,7 +17,8 @@ function go (licenceDocument) { const schema = Joi.object({ licenceRef: Joi.string().required(), endDate: Joi.date().required().allow(null), - startDate: Joi.date().required() + startDate: Joi.date().required(), + licenceDocumentRoles: Joi.array().required() }) const result = schema.validate(licenceDocument, { convert: false }) diff --git a/package-lock.json b/package-lock.json index d1028cc09..565bb2ecd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "water-abstraction-system", - "version": "0.23.0", + "version": "0.24.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "water-abstraction-system", - "version": "0.23.0", + "version": "0.24.0", "hasInstallScript": true, "license": "OGL-UK-3.0", "dependencies": { @@ -303,9 +303,9 @@ } }, "node_modules/@aws-sdk/client-s3": { - "version": "3.673.0", - "resolved": "https://registry.npmjs.org/@aws-sdk/client-s3/-/client-s3-3.673.0.tgz", - "integrity": "sha512-GREQlgG13Jveu9I+4pH2wfxHnl8TN0geVdUO8vOh6VbudmtfdC8hXUuMiTMX7VZ6c4zyOFStzBwysUv0Ze7kFg==", + "version": "3.674.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-s3/-/client-s3-3.674.0.tgz", + "integrity": "sha512-/pbAGFXUFqhZPrb4vACYdJzAc/lX/MPW1sxRoiX46BVjvpT7QkYwRjlzuifskLyDf2TjkGNsXu+F6AgPGCXFUg==", "dependencies": { "@aws-crypto/sha1-browser": "5.2.0", "@aws-crypto/sha256-browser": "5.2.0", @@ -321,11 +321,11 @@ "@aws-sdk/middleware-location-constraint": "3.667.0", "@aws-sdk/middleware-logger": "3.667.0", "@aws-sdk/middleware-recursion-detection": "3.667.0", - "@aws-sdk/middleware-sdk-s3": "3.669.0", + "@aws-sdk/middleware-sdk-s3": "3.674.0", "@aws-sdk/middleware-ssec": "3.667.0", "@aws-sdk/middleware-user-agent": "3.669.0", "@aws-sdk/region-config-resolver": "3.667.0", - "@aws-sdk/signature-v4-multi-region": "3.669.0", + "@aws-sdk/signature-v4-multi-region": "3.674.0", "@aws-sdk/types": "3.667.0", "@aws-sdk/util-endpoints": "3.667.0", "@aws-sdk/util-user-agent-browser": "3.670.0", @@ -1378,9 +1378,9 @@ } }, "node_modules/@aws-sdk/middleware-sdk-s3": { - "version": "3.669.0", - "resolved": "https://registry.npmjs.org/@aws-sdk/middleware-sdk-s3/-/middleware-sdk-s3-3.669.0.tgz", - "integrity": "sha512-b2QUQ7DcIcVCUFhvmFEDI90BemvQhO0ntIajllLqQSy88PSNdLDCVx5mIzfxaaK/1tdY/UsEDRRm1kMQHJDQpg==", + "version": "3.674.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/middleware-sdk-s3/-/middleware-sdk-s3-3.674.0.tgz", + "integrity": "sha512-IvXnWrKy4mO+I44kLYHd6Wlw+FdB4sg1jvHCmnZo1KNaAFIA3x1iXgOaZynKoBdEmol3xfr2uDbeXUQvIwoIgg==", "dependencies": { "@aws-sdk/core": "3.667.0", "@aws-sdk/types": "3.667.0", @@ -1516,11 +1516,11 @@ } }, "node_modules/@aws-sdk/signature-v4-multi-region": { - "version": "3.669.0", - "resolved": "https://registry.npmjs.org/@aws-sdk/signature-v4-multi-region/-/signature-v4-multi-region-3.669.0.tgz", - "integrity": "sha512-TVwlWAxfBHnFjnfTBQWUhzVJzjwVhkq1+KR0JZV7JrfqeyBOdZjAaV9ie3VNY9HUouecq1fDuKaSwe4JiWQsHg==", + "version": "3.674.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/signature-v4-multi-region/-/signature-v4-multi-region-3.674.0.tgz", + "integrity": "sha512-VMQWbtcbg4FV/fILrODADV21pPg9AghuEzQlW2kH0hCtacvBwFl7eBxIiCBLLtkNple+CVPJvyBcqOZdBkEv/w==", "dependencies": { - "@aws-sdk/middleware-sdk-s3": "3.669.0", + "@aws-sdk/middleware-sdk-s3": "3.674.0", "@aws-sdk/types": "3.667.0", "@smithy/protocol-http": "^4.1.4", "@smithy/signature-v4": "^4.2.0", @@ -2432,9 +2432,9 @@ } }, "node_modules/@hapi/lab": { - "version": "25.3.1", - "resolved": "https://registry.npmjs.org/@hapi/lab/-/lab-25.3.1.tgz", - "integrity": "sha512-y5JL9O12F0voH5J2ROMcfskZVyqz9mp1oMv+TyMf67uco91Y9FJ0broYfeQYXwV78NF+W10/A+yCDYN6oV/ASA==", + "version": "25.3.2", + "resolved": "https://registry.npmjs.org/@hapi/lab/-/lab-25.3.2.tgz", + "integrity": "sha512-jYNrdTiB+/NSyF0SNNCFg9BZYagD9k3M0JhxAkPHQRgb7txyVuvmQPFnZy41dX54w2o9EPAjMBh7DhYMlyCNuQ==", "dev": true, "dependencies": { "@babel/core": "^7.16.0", @@ -5708,9 +5708,9 @@ } }, "node_modules/eslint-plugin-jsdoc": { - "version": "50.4.1", - "resolved": "https://registry.npmjs.org/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-50.4.1.tgz", - "integrity": "sha512-OXIq+JJQPCLAKL473/esioFOwbXyRE5MAQ4HbZjcp3e+K3zdxt2uDpGs3FR+WezUXNStzEtTfgx15T+JFrVwBA==", + "version": "50.4.3", + "resolved": "https://registry.npmjs.org/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-50.4.3.tgz", + "integrity": "sha512-uWtwFxGRv6B8sU63HZM5dAGDhgsatb+LONwmILZJhdRALLOkCX2HFZhdL/Kw2ls8SQMAVEfK+LmnEfxInRN8HA==", "dev": true, "dependencies": { "@es-joy/jsdoccomment": "~0.49.0", @@ -10663,9 +10663,9 @@ } }, "@aws-sdk/client-s3": { - "version": "3.673.0", - "resolved": "https://registry.npmjs.org/@aws-sdk/client-s3/-/client-s3-3.673.0.tgz", - "integrity": "sha512-GREQlgG13Jveu9I+4pH2wfxHnl8TN0geVdUO8vOh6VbudmtfdC8hXUuMiTMX7VZ6c4zyOFStzBwysUv0Ze7kFg==", + "version": "3.674.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-s3/-/client-s3-3.674.0.tgz", + "integrity": "sha512-/pbAGFXUFqhZPrb4vACYdJzAc/lX/MPW1sxRoiX46BVjvpT7QkYwRjlzuifskLyDf2TjkGNsXu+F6AgPGCXFUg==", "requires": { "@aws-crypto/sha1-browser": "5.2.0", "@aws-crypto/sha256-browser": "5.2.0", @@ -10681,11 +10681,11 @@ "@aws-sdk/middleware-location-constraint": "3.667.0", "@aws-sdk/middleware-logger": "3.667.0", "@aws-sdk/middleware-recursion-detection": "3.667.0", - "@aws-sdk/middleware-sdk-s3": "3.669.0", + "@aws-sdk/middleware-sdk-s3": "3.674.0", "@aws-sdk/middleware-ssec": "3.667.0", "@aws-sdk/middleware-user-agent": "3.669.0", "@aws-sdk/region-config-resolver": "3.667.0", - "@aws-sdk/signature-v4-multi-region": "3.669.0", + "@aws-sdk/signature-v4-multi-region": "3.674.0", "@aws-sdk/types": "3.667.0", "@aws-sdk/util-endpoints": "3.667.0", "@aws-sdk/util-user-agent-browser": "3.670.0", @@ -11560,9 +11560,9 @@ } }, "@aws-sdk/middleware-sdk-s3": { - "version": "3.669.0", - "resolved": "https://registry.npmjs.org/@aws-sdk/middleware-sdk-s3/-/middleware-sdk-s3-3.669.0.tgz", - "integrity": "sha512-b2QUQ7DcIcVCUFhvmFEDI90BemvQhO0ntIajllLqQSy88PSNdLDCVx5mIzfxaaK/1tdY/UsEDRRm1kMQHJDQpg==", + "version": "3.674.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/middleware-sdk-s3/-/middleware-sdk-s3-3.674.0.tgz", + "integrity": "sha512-IvXnWrKy4mO+I44kLYHd6Wlw+FdB4sg1jvHCmnZo1KNaAFIA3x1iXgOaZynKoBdEmol3xfr2uDbeXUQvIwoIgg==", "requires": { "@aws-sdk/core": "3.667.0", "@aws-sdk/types": "3.667.0", @@ -11676,11 +11676,11 @@ } }, "@aws-sdk/signature-v4-multi-region": { - "version": "3.669.0", - "resolved": "https://registry.npmjs.org/@aws-sdk/signature-v4-multi-region/-/signature-v4-multi-region-3.669.0.tgz", - "integrity": "sha512-TVwlWAxfBHnFjnfTBQWUhzVJzjwVhkq1+KR0JZV7JrfqeyBOdZjAaV9ie3VNY9HUouecq1fDuKaSwe4JiWQsHg==", + "version": "3.674.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/signature-v4-multi-region/-/signature-v4-multi-region-3.674.0.tgz", + "integrity": "sha512-VMQWbtcbg4FV/fILrODADV21pPg9AghuEzQlW2kH0hCtacvBwFl7eBxIiCBLLtkNple+CVPJvyBcqOZdBkEv/w==", "requires": { - "@aws-sdk/middleware-sdk-s3": "3.669.0", + "@aws-sdk/middleware-sdk-s3": "3.674.0", "@aws-sdk/types": "3.667.0", "@smithy/protocol-http": "^4.1.4", "@smithy/signature-v4": "^4.2.0", @@ -12413,9 +12413,9 @@ } }, "@hapi/lab": { - "version": "25.3.1", - "resolved": "https://registry.npmjs.org/@hapi/lab/-/lab-25.3.1.tgz", - "integrity": "sha512-y5JL9O12F0voH5J2ROMcfskZVyqz9mp1oMv+TyMf67uco91Y9FJ0broYfeQYXwV78NF+W10/A+yCDYN6oV/ASA==", + "version": "25.3.2", + "resolved": "https://registry.npmjs.org/@hapi/lab/-/lab-25.3.2.tgz", + "integrity": "sha512-jYNrdTiB+/NSyF0SNNCFg9BZYagD9k3M0JhxAkPHQRgb7txyVuvmQPFnZy41dX54w2o9EPAjMBh7DhYMlyCNuQ==", "dev": true, "requires": { "@babel/core": "^7.16.0", @@ -15100,9 +15100,9 @@ } }, "eslint-plugin-jsdoc": { - "version": "50.4.1", - "resolved": "https://registry.npmjs.org/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-50.4.1.tgz", - "integrity": "sha512-OXIq+JJQPCLAKL473/esioFOwbXyRE5MAQ4HbZjcp3e+K3zdxt2uDpGs3FR+WezUXNStzEtTfgx15T+JFrVwBA==", + "version": "50.4.3", + "resolved": "https://registry.npmjs.org/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-50.4.3.tgz", + "integrity": "sha512-uWtwFxGRv6B8sU63HZM5dAGDhgsatb+LONwmILZJhdRALLOkCX2HFZhdL/Kw2ls8SQMAVEfK+LmnEfxInRN8HA==", "dev": true, "requires": { "@es-joy/jsdoccomment": "~0.49.0", diff --git a/package.json b/package.json index a16d57e57..9a8965407 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "water-abstraction-system", - "version": "0.23.0", + "version": "0.24.0", "description": "System for managing water abstraction licences, including alerts, returns and billing", "homepage": "https://github.com/DEFRA/water-abstraction-team", "main": "index.js", diff --git a/test/presenters/import/legacy/licence-document-role.presenter.test.js b/test/presenters/import/legacy/licence-document-role.presenter.test.js new file mode 100644 index 000000000..e3338793c --- /dev/null +++ b/test/presenters/import/legacy/licence-document-role.presenter.test.js @@ -0,0 +1,59 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') + +const { describe, it, beforeEach } = exports.lab = Lab.script() +const { expect } = Code + +// Test helpers +const { generateLicenceRef } = require('../../../support/helpers/licence.helper.js') +const { generateUUID } = require('../../../../app/lib/general.lib.js') + +// Thing under test +const LicenceDocumentRolePresenter = require('../../../../app/presenters/import/legacy/licence-document-role.presenter.js') + +describe('Import Legacy Licence Document role presenter', () => { + let legacyLicenceDocumentRole + let licenceRef + let licenceRoleId + + beforeEach(() => { + licenceRef = generateLicenceRef() + licenceRoleId = generateUUID() + + legacyLicenceDocumentRole = _legacyLicenceDocumentRole(licenceRef, licenceRoleId) + }) + + it('correctly transforms the data', () => { + const result = LicenceDocumentRolePresenter.go(legacyLicenceDocumentRole, licenceRef) + + expect(result).to.equal({ + addressId: '1:007', + companyId: '1:007', + contactId: '1:008', + documentId: licenceRef, + licenceRoleId, + endDate: null, + startDate: new Date('1999-01-01') + }) + }) + + it('correctly sets the "documentId" as the licence ref', () => { + const result = LicenceDocumentRolePresenter.go(legacyLicenceDocumentRole, licenceRef) + + expect(result.documentId).to.equal(licenceRef) + }) +}) + +function _legacyLicenceDocumentRole (licenceRef, licenceRoleId) { + return { + address_id: '1:007', + company_id: '1:007', + contact_id: '1:008', + end_date: null, + start_date: new Date('1999-01-01'), + licence_role_id: licenceRoleId + } +} diff --git a/test/presenters/import/legacy/licence-document.presenter.test.js b/test/presenters/import/legacy/licence-document.presenter.test.js index 7c3b1669e..eace2af7e 100644 --- a/test/presenters/import/legacy/licence-document.presenter.test.js +++ b/test/presenters/import/legacy/licence-document.presenter.test.js @@ -29,6 +29,7 @@ describe('Import Legacy Licence Document presenter', () => { expect(result).to.equal({ licenceRef, endDate: null, + licenceDocumentRoles: [], startDate: new Date('1999-01-01') }) }) diff --git a/test/services/import/legacy/process-licence.service.test.js b/test/services/import/legacy/process-licence.service.test.js index e6b66eced..2fe375d34 100644 --- a/test/services/import/legacy/process-licence.service.test.js +++ b/test/services/import/legacy/process-licence.service.test.js @@ -20,6 +20,7 @@ const TransformCompaniesService = require('../../../../app/services/import/legac const TransformCompanyAddressesService = require('../../../../app/services/import/legacy/transform-company-addresses.service.js') const TransformContactsService = require('../../../../app/services/import/legacy/transform-contacts.service.js') const TransformLicenceDocumentService = require('../../../../app/services/import/legacy/transform-licence-document.service.js') +const TransformLicenceDocumentRolesService = require('../../../../app/services/import/legacy/transform-licence-document-roles.service.js') const TransformLicenceService = require('../../../../app/services/import/legacy/transform-licence.service.js') const TransformLicenceVersionPurposeConditionsService = require('../../../../app/services/import/legacy/transform-licence-version-purpose-conditions.service.js') const TransformLicenceVersionPurposesService = require('../../../../app/services/import/legacy/transform-licence-version-purposes.service.js') @@ -51,6 +52,7 @@ describe('Import Legacy Process Licence service', () => { Sinon.stub(TransformLicenceVersionPurposesService, 'go').resolves(transformedLicence) Sinon.stub(TransformLicenceVersionPurposeConditionsService, 'go').resolves(transformedLicence) Sinon.stub(TransformLicenceDocumentService, 'go').resolves() + Sinon.stub(TransformLicenceDocumentRolesService, 'go').resolves() Sinon.stub(TransformCompaniesService, 'go').resolves({ company: [], transformedCompany: [] }) Sinon.stub(TransformContactsService, 'go').resolves() Sinon.stub(TransformAddressesService, 'go').resolves() diff --git a/test/services/import/legacy/transform-licence-document-role.service.test.js b/test/services/import/legacy/transform-licence-document-role.service.test.js new file mode 100644 index 000000000..888ef2b43 --- /dev/null +++ b/test/services/import/legacy/transform-licence-document-role.service.test.js @@ -0,0 +1,88 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') +const Sinon = require('sinon') + +const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() +const { expect } = Code + +// Test helpers +const { generateLicenceRef } = require('../../../support/helpers/licence.helper.js') + +// Things to stub +const FetchLicenceDocumentRolesService = require('../../../../app/services/import/legacy/fetch-licence-document-roles.service.js') +const { generateUUID } = require('../../../../app/lib/general.lib.js') + +// Thing under test +const TransformLicenceDocumentRolesService = + require('../../../../app/services/import/legacy/transform-licence-document-roles.service.js') + +describe('Import Legacy Transform Licence Document Role service', () => { + // NOTE: Clearly this is an incomplete representation of the licence returned from TransformedLicenceService. But for + // the purposes of this service it is all that is needed + const transformedLicence = { licenceDocument: { licenceDocumentRoles: [] } } + + const naldLicenceId = '2113' + const regionCode = '6' + + let legacyLicenceDocument + let licenceRef + let licenceRoleId + + beforeEach(() => { + licenceRef = generateLicenceRef() + licenceRoleId = generateUUID() + + legacyLicenceDocument = _legacyLicenceDocumentRole(licenceRoleId) + }) + + afterEach(() => { + Sinon.restore() + }) + + describe('when a licence has licence document roles', () => { + beforeEach(() => { + Sinon.stub(FetchLicenceDocumentRolesService, 'go').resolves([legacyLicenceDocument]) + }) + + it('attaches the record transformed and validated for WRLS to the transformed licence document roles', async () => { + await TransformLicenceDocumentRolesService.go(regionCode, naldLicenceId, transformedLicence, licenceRef) + + expect(transformedLicence.licenceDocument.licenceDocumentRoles).to.equal([ + { + addressId: '1:007', + companyId: '1:007', + contactId: '1:008', + documentId: licenceRef, + endDate: null, + licenceRoleId, + startDate: new Date('1999-01-01') + } + ]) + }) + }) + + describe('when the fetch for a licence document role fails', () => { + beforeEach(() => { + Sinon.stub(FetchLicenceDocumentRolesService, 'go').resolves(null) + }) + + it('throws an error', async () => { + await expect(TransformLicenceDocumentRolesService.go(regionCode, naldLicenceId, transformedLicence, licenceRef)) + .to.reject() + }) + }) +}) + +function _legacyLicenceDocumentRole (licenceRoleId) { + return { + address_id: '1:007', + company_id: '1:007', + contact_id: '1:008', + end_date: null, + start_date: new Date('1999-01-01'), + licence_role_id: licenceRoleId + } +} diff --git a/test/services/import/legacy/transform-licence-document.service.test.js b/test/services/import/legacy/transform-licence-document.service.test.js index 980afb256..8c7148457 100644 --- a/test/services/import/legacy/transform-licence-document.service.test.js +++ b/test/services/import/legacy/transform-licence-document.service.test.js @@ -50,6 +50,7 @@ describe('Import Legacy Transform Licence Document service', () => { expect(transformedLicence.licenceDocument).to.equal({ licenceRef, endDate: null, + licenceDocumentRoles: [], startDate: new Date('1999-01-01') }) }) @@ -70,6 +71,7 @@ function _legacyLicenceDocument (licenceRef) { return { end_date: null, start_date: new Date('1999-01-01'), - licence_ref: licenceRef + licence_ref: licenceRef, + licenceDocumentRoles: [] } } diff --git a/test/services/import/persist/persist-licence-document.service.test.js b/test/services/import/persist/persist-licence-document.service.test.js index e9b8828a1..cc4afedc0 100644 --- a/test/services/import/persist/persist-licence-document.service.test.js +++ b/test/services/import/persist/persist-licence-document.service.test.js @@ -8,8 +8,13 @@ const { describe, it, afterEach, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers +const AddressHelper = require('../../../support/helpers/address.helper.js') +const CompanyHelper = require('../../../support/helpers/company.helper.js') +const ContactHelper = require('../../../support/helpers/contact.helper.js') const LicenceDocumentHelper = require('../../../support/helpers/licence-document.helper.js') +const LicenceDocumentRoleHelper = require('../../../support/helpers/licence-document-role.helper.js') const LicenceDocumentModel = require('../../../../app/models/licence-document.model.js') +const LicenceRoleHelper = require('../../../support/helpers/licence-role.helper.js') const { generateLicenceRef } = require('../../../support/helpers/licence.helper.js') const { timestampForPostgres } = require('../../../../app/lib/general.lib.js') const { transaction } = require('objection') @@ -20,15 +25,35 @@ const PersistLicenceDocumentService = require('../../../../app/services/import/p describe('Persist licence document service', () => { const transformedLicence = {} + let address + let company + let contact let licenceDocument + let licenceDocumentRole let licenceRef + let licenceRoleId let trx let updatedAt beforeEach(async () => { - updatedAt = timestampForPostgres() + // A company and address is/will be the same external id + const externalId = CompanyHelper.generateExternalId() + + company = await CompanyHelper.add({ + externalId + }) + + contact = await ContactHelper.add({ + externalId: CompanyHelper.generateExternalId() + }) + + address = await AddressHelper.add({ + externalId + }) + + licenceRoleId = LicenceRoleHelper.select('returnsTo').id - licenceRef = generateLicenceRef() + updatedAt = timestampForPostgres() trx = await transaction.start(LicenceDocumentModel.knex()) }) @@ -42,9 +67,14 @@ describe('Persist licence document service', () => { describe('when given a valid transformed licence document', () => { describe('and that licence does not already exist', () => { beforeEach(() => { + licenceRef = generateLicenceRef() + + licenceDocumentRole = _transformedLicenceDocumentRole( + licenceRef, licenceRoleId, company.externalId, address.externalId, contact.externalId) + licenceDocument = _transformedLicenceDocument(licenceRef) - transformedLicence.licenceDocument = licenceDocument + transformedLicence.licenceDocument = { ...licenceDocument, licenceDocumentRoles: [licenceDocumentRole] } }) it('creates a new licence document record', async () => { @@ -55,23 +85,34 @@ describe('Persist licence document service', () => { const newLicenceDocument = await _fetchPersistedLicenceDocument(licenceDocument.licenceRef) + // Licence Document expect(licenceDocument.licenceRef).to.equal(newLicenceDocument.licenceRef) expect(licenceDocument.startDate).to.equal(newLicenceDocument.startDate) expect(licenceDocument.endDate).to.equal(newLicenceDocument.endDate) + + // Licence Document Role + const [newLicenceDocumentRole] = newLicenceDocument.licenceDocumentRoles + + expect(licenceDocumentRole.startDate).to.equal(newLicenceDocumentRole.startDate) + expect(licenceDocumentRole.endDate).to.equal(newLicenceDocumentRole.endDate) + expect(licenceRoleId).to.equal(newLicenceDocumentRole.licenceRoleId) }) }) describe('and that licence document already exists', () => { - const existingLicence = {} - beforeEach(async () => { - const existing = await _createExistingRecords(licenceRef) + licenceRef = generateLicenceRef() + + licenceDocumentRole = _transformedLicenceDocumentRole( + licenceRef, licenceRoleId, company.externalId, address.externalId, contact.externalId) - existingLicence.licenceDocument = existing.licenceDocument + await _createExistingRecords( + licenceRef, licenceRoleId, company, address, contact) transformedLicence.licenceDocument = { ..._transformedLicenceDocument(licenceRef), - endDate: null + endDate: null, + licenceDocumentRoles: [{ ...licenceDocumentRole, endDate: new Date('2010-01-01') }] } }) @@ -89,6 +130,15 @@ describe('Persist licence document service', () => { // Check the updated licence expect(updatedLicenceDocument.licenceRef).to.equal(transformedLicence.licenceDocument.licenceRef) expect(updatedLicenceDocument.endDate).to.be.null() + + // Licence Document Role + // Check the update has worked and not an insert + expect(updatedLicenceDocument.licenceDocumentRoles.length).to.equal(1) + + const [updatedLicenceDocumentRole] = updatedLicenceDocument.licenceDocumentRoles + + expect(updatedLicenceDocumentRole.endDate).to.equal(new Date('2010-01-01')) + expect(updatedLicenceDocumentRole.licenceRoleId).to.equal(licenceRoleId) }) }) }) @@ -98,6 +148,7 @@ async function _fetchPersistedLicenceDocument (licenceRef) { return LicenceDocumentModel .query() .where('licenceRef', licenceRef) + .withGraphFetched('licenceDocumentRoles') .select('*') .limit(1) .first() @@ -111,13 +162,36 @@ function _transformedLicenceDocument (licenceRef) { } } -async function _createExistingRecords (licenceRef) { +function _transformedLicenceDocumentRole (licenceRef, licenceRoleId, companyId, addressId, contactId) { + return { + addressId, + companyId, + contactId, + documentId: licenceRef, + endDate: null, + licenceRoleId, + startDate: new Date('1999-01-01') + } +} + +async function _createExistingRecords (licenceRef, licenceRoleId, company, address, contact) { const licenceDocument = await LicenceDocumentHelper.add({ licenceRef, endDate: new Date('2001-01-01') }) + const licenceDocumentRole = await LicenceDocumentRoleHelper.add({ + licenceDocumentId: licenceDocument.id, + licenceRoleId, + companyId: company.id, + addressId: address.id, + contactId: contact.id, + startDate: new Date('1999-01-01'), + endDate: new Date('1999-01-02') + }) + return { - licenceDocument + licenceDocument, + licenceDocumentRole } } diff --git a/test/support/helpers/licence-document-role.helper.js b/test/support/helpers/licence-document-role.helper.js index 9cff602aa..ec26605d3 100644 --- a/test/support/helpers/licence-document-role.helper.js +++ b/test/support/helpers/licence-document-role.helper.js @@ -1,7 +1,7 @@ 'use strict' /** - * @module LicenceDocumentHelper + * @module LicenceDocumentRoleHelper */ const { generateUUID } = require('../../../app/lib/general.lib.js') diff --git a/test/validators/import/licence-document-role.validator.test.js b/test/validators/import/licence-document-role.validator.test.js new file mode 100644 index 000000000..18d67139a --- /dev/null +++ b/test/validators/import/licence-document-role.validator.test.js @@ -0,0 +1,261 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') + +const { describe, it, beforeEach } = exports.lab = Lab.script() +const { expect } = Code + +// Test helpers +const { generateLicenceRef } = require('../../support/helpers/licence.helper.js') +const { generateUUID } = require('../../../app/lib/general.lib.js') + +// Thing under test +const LicenceDocumentRoleValidator = require('../../../app/validators/import/licence-document-role.validator.js') + +describe('Import Licence Document role validator', () => { + let transformedLicenceDocumentRole + + beforeEach(async () => { + transformedLicenceDocumentRole = _transformedLicenceDocumentRoleRole() + }) + + describe('when valid data is provided', () => { + it('does not throw an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.not.throw() + }) + }) + + describe('the "addressId" property', () => { + describe('when it is not a string', () => { + beforeEach(() => { + transformedLicenceDocumentRole.addressId = 1 + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"addressId" must be a string') + }) + }) + + describe('when it is null', () => { + beforeEach(() => { + transformedLicenceDocumentRole.addressId = null + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"addressId" must be a string') + }) + }) + + describe('when it does not exist', () => { + beforeEach(() => { + delete transformedLicenceDocumentRole.addressId + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"addressId" is required') + }) + }) + }) + + describe('the "companyId" property', () => { + describe('when it is not a string', () => { + beforeEach(() => { + transformedLicenceDocumentRole.companyId = 1 + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"companyId" must be a string') + }) + }) + + describe('when it is null', () => { + beforeEach(() => { + transformedLicenceDocumentRole.companyId = null + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"companyId" must be a string') + }) + }) + + describe('when it does not exist', () => { + beforeEach(() => { + delete transformedLicenceDocumentRole.companyId + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"companyId" is required') + }) + }) + }) + + describe('the "contactId" property', () => { + describe('when it is not a string', () => { + beforeEach(() => { + transformedLicenceDocumentRole.contactId = 1 + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"contactId" must be a string') + }) + }) + + describe('when it is null', () => { + beforeEach(() => { + transformedLicenceDocumentRole.contactId = null + }) + + it('does not throw an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.not.throw() + }) + }) + + describe('when it does not exist', () => { + beforeEach(() => { + delete transformedLicenceDocumentRole.contactId + }) + + it('does not throw an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.not.throw() + }) + }) + }) + + describe('the "documentId" property', () => { + describe('when it is not a string', () => { + beforeEach(() => { + transformedLicenceDocumentRole.documentId = 1 + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"documentId" must be a string') + }) + }) + + describe('when it is null', () => { + beforeEach(() => { + transformedLicenceDocumentRole.documentId = null + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"documentId" must be a string') + }) + }) + }) + + describe('the "licenceRoleId" property', () => { + describe('when it is not a guid', () => { + beforeEach(() => { + transformedLicenceDocumentRole.licenceRoleId = '123' + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"licenceRoleId" must be a valid GUID') + }) + }) + + describe('when it is null', () => { + beforeEach(() => { + transformedLicenceDocumentRole.licenceRoleId = null + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"licenceRoleId" must be a string') + }) + }) + }) + + describe('the "endDate" property', () => { + describe('when it is not a date', () => { + beforeEach(() => { + transformedLicenceDocumentRole.endDate = 1 + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"endDate" must be a valid date') + }) + }) + + describe('when it is null', () => { + beforeEach(() => { + transformedLicenceDocumentRole.endDate = null + }) + + it('does not throw an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).not.to.throw() + }) + }) + }) + + describe('the "startDate" property', () => { + describe('when it is not a date', () => { + beforeEach(() => { + transformedLicenceDocumentRole.startDate = 1 + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"startDate" must be a valid date') + }) + }) + + describe('when it is null', () => { + beforeEach(() => { + transformedLicenceDocumentRole.startDate = null + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentRoleValidator.go(transformedLicenceDocumentRole) + }).to.throw('"startDate" must be a valid date') + }) + }) + }) +}) + +function _transformedLicenceDocumentRoleRole () { + return { + addressId: '1:007', + companyId: '1:007', + contactId: '1:008', + documentId: generateLicenceRef(), + licenceRoleId: generateUUID(), + endDate: null, + startDate: new Date('1999-01-01') + } +} diff --git a/test/validators/import/licence-document.validator.test.js b/test/validators/import/licence-document.validator.test.js index 078a2fba5..1bf22056b 100644 --- a/test/validators/import/licence-document.validator.test.js +++ b/test/validators/import/licence-document.validator.test.js @@ -105,12 +105,39 @@ describe('Import Licence Document validator', () => { }) }) }) + + describe('the "licenceDocumentRoles" property', () => { + describe('when it is not an array', () => { + beforeEach(() => { + transformedLicenceDocument.licenceDocumentRoles = 1 + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentValidator.go(transformedLicenceDocument) + }).to.throw('"licenceDocumentRoles" must be an array') + }) + }) + + describe('when it is null', () => { + beforeEach(() => { + transformedLicenceDocument.licenceDocumentRoles = null + }) + + it('throws an error', () => { + expect(() => { + LicenceDocumentValidator.go(transformedLicenceDocument) + }).to.throw('"licenceDocumentRoles" must be an array') + }) + }) + }) }) function _transformedLicenceDocument () { return { licenceRef: generateLicenceRef(), endDate: new Date('2052-06-23'), - startDate: new Date('1992-08-19') + startDate: new Date('1992-08-19'), + licenceDocumentRoles: [] } }