Skip to content

Commit

Permalink
Import Licence versions (#1195)
Browse files Browse the repository at this point in the history
* Import Licence  versions

We need to replace the import service logic to import a licence from NALD.

We will leave the import service running to add the NALD data in the import tables, but we will slowly replace the import-licence functionality and move this into the system repo.

https://eaflood.atlassian.net/browse/WATER-4575

This change will add the licence versions to the import logic.

The licence versions rely on the created / existing licence id.

This work needs to be done first to allow us to remove the licence and licence versions from the old import logic.

https://eaflood.atlassian.net/browse/WATER-4575

In this instance, we felt it was more constructive to provide the feedback for the original [Import Licence versions](#1195) proposed PR as a series of commits from where the code has been left.

This can be used to guide suggested changes, hopefully with better context than just PR comments.

---

As I went through making these changes, I realised I was aiming for three outcomes;

- simplify the work of the mappers by moving casting and some data generation into the fetch service queries
- separate as much as possible the process for importing each entity (licence, licence version etc)
- persist as one using a DB transaction to avoid risk of partial licences

The commits themselves go into more details but very happy and glad to walk through the changes I'm suggesting.

---------

Co-authored-by: Robert Parkinson <robertparkinson@users.noreply.github.com>
Co-authored-by: Alan Cruikshanks <alan.cruikshanks@gmail.com>
  • Loading branch information
3 people authored Aug 27, 2024
1 parent 23271ac commit f9b36c3
Show file tree
Hide file tree
Showing 48 changed files with 3,089 additions and 1,110 deletions.
32 changes: 15 additions & 17 deletions app/controllers/import.controller.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
'use strict'

const FeatureFlags = require('../../config/feature-flags.config.js')
const ImportLegacyProcessLicenceService = require('../services/import/legacy/process-licence.service.js')

/**
* Controller for /import
* @param request - the hapi request object
* @param h - the hapi response object
*
* @returns {object}
*
* @module ImportController
*/
async function licenceLegacy (request, h) {
const { licenceRef } = request.payload

const Boom = require('@hapi/boom')

const FeatureFlags = require('../../config/feature-flags.config.js')
const LegacyImportLicenceService = require('../services/import/legacy-licence.service.js')

async function licence (request, h) {
try {
const { licenceRef } = request.payload

if (FeatureFlags.enableSystemImportLegacyLicence) {
await LegacyImportLicenceService.go(licenceRef)
}

return h.response().code(204)
} catch (error) {
return Boom.badImplementation(error.message)
if (FeatureFlags.enableSystemImportLegacyLicence) {
ImportLegacyProcessLicenceService.go(licenceRef)
}

return h.response().code(204)
}

module.exports = {
licence
licenceLegacy
}
2 changes: 1 addition & 1 deletion app/lib/dates.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function formatStandardDateToISO (date) {
if (isValidDate(isoDateString)) {
return isoDateString
} else {
return null
throw new Error(`${isoDateString} is not a valid date`)
}
}

Expand Down
17 changes: 17 additions & 0 deletions app/lib/static-lookups.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ const returnRequirementReasons = {
'transfer-and-now-chargeable': 'Licence transferred and now chargeable'
}

/**
* NALD region prefix from import.NALD_ABS_LICENCES.AREP_EIUC_CODE will be mapped to one of the below regions
*
*/
const naldRegions = {
AN: 'Anglian',
MD: 'Midlands',
NO: 'Northumbria',
NW: 'North West',
SO: 'Southern',
SW: 'South West (incl Wessex)',
TH: 'Thames',
WL: 'Wales',
YO: 'Yorkshire'
}

const sources = [
'nald',
'wrls'
Expand Down Expand Up @@ -79,6 +95,7 @@ module.exports = {
organisationTypes,
returnRequirementFrequencies,
returnRequirementReasons,
naldRegions,
sources,
twoPartTariffReviewIssues
}
38 changes: 38 additions & 0 deletions app/presenters/import/legacy/licence-version-purpose.presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

/**
* Maps the legacy NALD licence version purpose data to the WRLS format
* @module LicenceVersionPurposePresenter
*/

/**
* Maps the legacy NALD licence version purpose data to the WRLS format
*
* @param {ImportLegacyLicenceVersionPurposeType} licenceVersionPurpose - the legacy NALD licence version purpose
*
* @returns {object} the NALD licence version purpose data transformed into the WRLS format ready for validation and
* persisting
*/
function go (licenceVersionPurpose) {
return {
abstractionPeriodEndDay: licenceVersionPurpose.abstraction_period_end_day,
abstractionPeriodEndMonth: licenceVersionPurpose.abstraction_period_end_month,
abstractionPeriodStartDay: licenceVersionPurpose.abstraction_period_start_day,
abstractionPeriodStartMonth: licenceVersionPurpose.abstraction_period_start_month,
annualQuantity: licenceVersionPurpose.annual_quantity,
dailyQuantity: licenceVersionPurpose.daily_quantity,
externalId: licenceVersionPurpose.external_id,
hourlyQuantity: licenceVersionPurpose.hourly_quantity,
instantQuantity: licenceVersionPurpose.instant_quantity,
notes: licenceVersionPurpose.notes,
primaryPurposeId: licenceVersionPurpose.primary_purpose_id,
purposeId: licenceVersionPurpose.purpose_id,
secondaryPurposeId: licenceVersionPurpose.secondary_purpose_id,
timeLimitedEndDate: licenceVersionPurpose.time_limited_end_date,
timeLimitedStartDate: licenceVersionPurpose.time_limited_start_date
}
}

module.exports = {
go
}
35 changes: 35 additions & 0 deletions app/presenters/import/legacy/licence-version.presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

/**
* Maps legacy NALD licence version data to the WRLS format
* @module LicenceVersionPresenter
*/

const NALD_STATUSES = {
CURR: 'current',
SUPER: 'superseded'
}

/**
* Maps legacy NALD licence version data to the WRLS format
*
* @param {ImportLegacyLicenceVersionTyp} licenceVersion - the legacy NALD licence version
*
* @returns {object} the NALD licence version data transformed into the WRLS format ready for validation and persisting
*/
function go (licenceVersion) {
return {
endDate: licenceVersion.effective_end_date,
externalId: licenceVersion.external_id,
increment: licenceVersion.increment_number,
issue: licenceVersion.issue_no,
// Add an empty array property ready for when transforming and attaching licence version purposes
licenceVersionPurposes: [],
startDate: licenceVersion.effective_start_date,
status: NALD_STATUSES[licenceVersion.status]
}
}

module.exports = {
go
}
57 changes: 57 additions & 0 deletions app/presenters/import/legacy/licence.presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict'

/**
* Maps the legacy NALD licence data to the WRLS format
* @module LicencePresenter
*/

const { naldRegions } = require('../../../lib/static-lookups.lib.js')

/**
* Maps the legacy NALD licence data to the WRLS format
*
* @param {ImportLegacyLicenceType} licence - the legacy NALD licence
*
* @returns {object} the NALD licence data transformed into the WRLS format ready for validation and persisting
*/
function go (licence) {
return {
expiredDate: licence.expiry_date,
lapsedDate: licence.lapsed_date,
licenceRef: licence.licence_ref,
// Add an empty array property ready for when transforming and attaching licence versions
licenceVersions: [],
regionId: licence.region_id,
regions: _regions(licence),
revokedDate: licence.revoked_date,
startDate: _startDate(licence),
waterUndertaker: licence.environmental_improvement_unit_charge_code.endsWith('SWC')
}
}

/**
* Creates a JSON object of the region data. This is stored as a JSON object in the licence.regions column.
*
* @private
*/
const _regions = (licence) => {
const historicalAreaCode = licence.historical_area_code
const regionPrefix = licence.environmental_improvement_unit_charge_code.substr(0, 2)
const regionalChargeArea = naldRegions[regionPrefix]
const standardUnitChargeCode = licence.standard_unit_charge_code
const localEnvironmentAgencyPlanCode = licence.local_environment_agency_plan_code

return { historicalAreaCode, regionalChargeArea, standardUnitChargeCode, localEnvironmentAgencyPlanCode }
}

function _startDate (licence) {
if (licence.original_effective_date) {
return licence.original_effective_date
}

return licence.earliest_version_start_date
}

module.exports = {
go
}
4 changes: 2 additions & 2 deletions app/routes/import.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const ImportController = require('../controllers/import.controller.js')
const routes = [
{
method: 'POST',
path: '/import/licence',
handler: ImportController.licence,
path: '/import/licence/legacy',
handler: ImportController.licenceLegacy,
options: {
app: {
plainOutput: true
Expand Down
21 changes: 0 additions & 21 deletions app/services/import/legacy-import/constants.js

This file was deleted.

This file was deleted.

76 changes: 0 additions & 76 deletions app/services/import/legacy-import/fetch-licence.service.js

This file was deleted.

Loading

0 comments on commit f9b36c3

Please sign in to comment.