Skip to content

Commit

Permalink
Merge branch 'main' into rename-rtn-req-setup-folder-method
Browse files Browse the repository at this point in the history
  • Loading branch information
Cruikshanks authored Oct 20, 2024
2 parents 23c1636 + ea1a6c7 commit 46b0927
Show file tree
Hide file tree
Showing 21 changed files with 873 additions and 56 deletions.
88 changes: 87 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions app/presenters/import/legacy/licence-document-role.presenter.js
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions app/presenters/import/legacy/licence-document.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 72 additions & 0 deletions app/services/import/legacy/fetch-licence-document-roles.service.js
Original file line number Diff line number Diff line change
@@ -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<ImportLegacyLicenceDocumentRoleType[]>}
*/
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
*/
2 changes: 2 additions & 0 deletions app/services/import/legacy/process-licence.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
36 changes: 35 additions & 1 deletion app/services/import/persist/persist-licence-document.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

const LicenceDocumentModel = require('../../../models/licence-document.model.js')
const { db } = require('../../../../db/db.js')

/**
* Creates or updates a licence document
Expand All @@ -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',
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion app/services/import/persist/persist-licence.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
Loading

0 comments on commit 46b0927

Please sign in to comment.