-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import addresses data for a licence (#1355)
* Import addresses data for a licence https://eaflood.atlassian.net/browse/WATER-4666 We need to replace the import service logic to import a licence from NALD. The current import service iterates all the companies (known as parties in the import.NALD_PARTIES table) and updates CRM_V2 tables. This change will use the nald licence id and region to update the addresses data. This will insert all the addresses found for a licence ref. We will insert this imported data in the relevant public views. --------- Co-authored-by: Alan Cruikshanks <[email protected]>
- Loading branch information
1 parent
ac262b6
commit a38973c
Showing
12 changed files
with
801 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict' | ||
|
||
/** | ||
* Maps the legacy NALD address data to the WRLS format | ||
* @module AddressPresenter | ||
*/ | ||
|
||
/** | ||
* Maps the legacy NALD address data to the WRLS format | ||
* | ||
* @param {ImportLegacyAddressType} address - the legacy NALD address | ||
* @param {string} dataSource | ||
* @returns {object} the NALD company data transformed into the WRLS format for an address | ||
* ready for validation and persisting | ||
*/ | ||
function go (address, dataSource) { | ||
return { | ||
address1: address.address1, | ||
address2: address.address2, | ||
address3: address.address3, | ||
address4: address.address4, | ||
address5: address.address5, | ||
address6: address.address6, | ||
postcode: address.postcode, | ||
country: address.country, | ||
externalId: address.external_id, | ||
dataSource | ||
} | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
'use strict' | ||
|
||
/** | ||
* Fetches the addresses data from the import.NALD_ADDRESSESS table for the licence ref | ||
* @module FetchAddressesService | ||
*/ | ||
|
||
const { db } = require('../../../../db/db.js') | ||
|
||
/** | ||
* Fetches the addresses data from the import.NALD_ADDRESSESS table for the licence ref | ||
* | ||
* @param {string} regionCode - The NALD region code | ||
* @param {string} licenceId - The NALD licence ID | ||
* | ||
* @returns {Promise<ImportLegacyAddressType[]>} | ||
*/ | ||
async function go (regionCode, licenceId) { | ||
const query = _query() | ||
|
||
const { rows } = await db.raw(query, [regionCode, licenceId, licenceId]) | ||
|
||
return rows | ||
} | ||
|
||
function _query () { | ||
return ` | ||
SELECT DISTINCT ON (external_id) | ||
( | ||
CASE na."ADDR_LINE1" | ||
WHEN 'null' THEN NULL | ||
ELSE na."ADDR_LINE1" | ||
END | ||
) AS address1, | ||
( | ||
CASE na."ADDR_LINE2" | ||
WHEN 'null' THEN NULL | ||
ELSE na."ADDR_LINE2" | ||
END | ||
) AS address2, | ||
( | ||
CASE na."ADDR_LINE3" | ||
WHEN 'null' THEN NULL | ||
ELSE na."ADDR_LINE3" | ||
END | ||
) AS address3, | ||
( | ||
CASE na."ADDR_LINE4" | ||
WHEN 'null' THEN NULL | ||
ELSE na."ADDR_LINE4" | ||
END | ||
) AS address4, | ||
( | ||
CASE na."TOWN" | ||
WHEN 'null' THEN NULL | ||
ELSE na."TOWN" | ||
END | ||
) AS address4, | ||
( | ||
CASE na."COUNTY" | ||
WHEN 'null' THEN NULL | ||
ELSE na."COUNTY" | ||
END | ||
) AS address5, | ||
( | ||
CASE na."POSTCODE" | ||
WHEN 'null' THEN NULL | ||
ELSE na."POSTCODE" | ||
END | ||
) AS postcode, | ||
( | ||
CASE na."COUNTRY" | ||
WHEN 'null' THEN NULL | ||
ELSE na."COUNTRY" | ||
END | ||
) AS country, | ||
(concat_ws(':', na."FGAC_REGION_CODE", na."ID")) AS external_id, | ||
(concat_ws(':', na."FGAC_REGION_CODE", np."ID")) AS company_external_id | ||
FROM | ||
"import"."NALD_ADDRESSES" na | ||
LEFT JOIN | ||
"import"."NALD_ABS_LIC_VERSIONS" nalv | ||
ON nalv."ACON_AADD_ID" = na."ID" | ||
AND nalv."FGAC_REGION_CODE" = na."FGAC_REGION_CODE" | ||
LEFT JOIN | ||
"import"."NALD_LIC_ROLES" nlr | ||
ON nlr."ACON_AADD_ID" = na."ID" | ||
AND nlr."FGAC_REGION_CODE" = na."FGAC_REGION_CODE" | ||
LEFT JOIN import."NALD_PARTIES" np | ||
ON | ||
(np."ID" = nalv."ACON_APAR_ID" and np."FGAC_REGION_CODE" = na."FGAC_REGION_CODE") | ||
OR | ||
( np."ID" = nlr."ACON_APAR_ID" and np."FGAC_REGION_CODE" = na."FGAC_REGION_CODE") | ||
WHERE | ||
na."FGAC_REGION_CODE" = ? | ||
AND ( | ||
(nalv."AABL_ID" = ? AND nalv."ACON_AADD_ID" IS NOT NULL) | ||
OR | ||
(nlr."AABL_ID" = ? AND nlr."ACON_AADD_ID" IS NOT NULL) | ||
); | ||
` | ||
} | ||
|
||
module.exports = { | ||
go | ||
} | ||
|
||
/** | ||
* @typedef {object} ImportLegacyAddressType | ||
* | ||
* @property {string} address1 - First address line (ADDR_LINE1) | ||
* @property {string} [address2] - Second address line (ADDR_LINE2), optional | ||
* @property {string} [address3] - Third address line (ADDR_LINE3), optional | ||
* @property {string} [address4] - Fourth address line (ADDR_LINE4), optional | ||
* @property {string} [address5] - The town (TOWN) | ||
* @property {string} [address6] - The county (COUNTY) | ||
* @property {string} postcode - The postcode (POSTCODE) | ||
* @property {string} country - The country (COUNTRY) | ||
* @property {string} external_id - The external identifier, combination of FGAC_REGION_CODE and ID | ||
* @property {string} company_external_id - The external identifier for a company to link the address to a company | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
/** | ||
* Transforms NALD addresses data into a valid object that matches the WRLS structure | ||
* @module ImportLegacyTransformAddressesService | ||
*/ | ||
|
||
const AddressPresenter = require('../../../presenters/import/legacy/address.presenter.js') | ||
const FetchAddressesService = require('./fetch-address.service.js') | ||
const ImportAddressValidator = require('../../../validators/import/address.validator.js') | ||
|
||
/** | ||
* Transforms NALD addresses data into a validated object that matches the WRLS structure | ||
* | ||
* Adds an addresses array to a company with at least one matching address | ||
* | ||
* @param {string} regionCode - The NALD region code | ||
* @param {string} licenceId - The NALD licence ID | ||
* @param {object[]} transformedCompanies | ||
* | ||
*/ | ||
async function go (regionCode, licenceId, transformedCompanies) { | ||
const naldAddresses = await FetchAddressesService.go(regionCode, licenceId) | ||
|
||
naldAddresses.forEach((naldAddress) => { | ||
const matchingCompany = _matchingCompany(transformedCompanies, naldAddress) | ||
|
||
const address = AddressPresenter.go(naldAddress, 'nald') | ||
|
||
ImportAddressValidator.go(address) | ||
|
||
matchingCompany.addresses = [...(matchingCompany?.addresses || []), address] | ||
}) | ||
} | ||
|
||
function _matchingCompany (transformedCompanies, naldAddress) { | ||
return transformedCompanies.find((company) => { | ||
return company.externalId === naldAddress.company_external_id | ||
}) | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict' | ||
|
||
/** | ||
* @module ImportAddressValidator | ||
*/ | ||
|
||
const Joi = require('joi') | ||
|
||
/** | ||
* Checks that the imported address data has been transformed and is valid for persisting to WRLS | ||
* | ||
* @param {object} address - The transformed address data | ||
* | ||
* @throws {Joi.ValidationError} - throws a Joi validation error if the validation fails | ||
*/ | ||
function go (address) { | ||
const schema = Joi.object({ | ||
address1: Joi.string().required(), | ||
address2: Joi.string().allow(null), | ||
address3: Joi.string().allow(null), | ||
address4: Joi.string().allow(null), | ||
address5: Joi.string().allow(null), | ||
address6: Joi.string().allow(null), | ||
country: Joi.string().allow(null), | ||
externalId: Joi.string().required(), | ||
postcode: Joi.string().allow(null), | ||
dataSource: Joi.string().required() | ||
}) | ||
|
||
const result = schema.validate(address, { convert: false }) | ||
|
||
if (result.error) { | ||
throw result.error | ||
} | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'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 | ||
|
||
// Thing under test | ||
const AddressPresenter = require('../../../../app/presenters/import/legacy/address.presenter.js') | ||
|
||
describe('Import Legacy Address presenter', () => { | ||
let legacyAddress | ||
|
||
const dateSource = 'nald' | ||
|
||
beforeEach(() => { | ||
legacyAddress = _legacyAddress() | ||
}) | ||
|
||
it('correctly transforms the data', () => { | ||
const result = AddressPresenter.go(legacyAddress, dateSource) | ||
|
||
expect(result).to.equal({ | ||
address1: '4 Privet Drive', | ||
address2: null, | ||
address3: null, | ||
address4: null, | ||
address5: 'Little Whinging', | ||
address6: 'Surrey', | ||
country: 'United Kingdom', | ||
externalId: '7:7777', | ||
postcode: 'HP11', | ||
dataSource: 'nald' | ||
}) | ||
}) | ||
|
||
it('correctly sets the data source provided', () => { | ||
const result = AddressPresenter.go(legacyAddress, dateSource) | ||
|
||
expect(result.dataSource).to.equal('nald') | ||
}) | ||
}) | ||
|
||
function _legacyAddress () { | ||
return { | ||
address1: '4 Privet Drive', | ||
address2: null, | ||
address3: null, | ||
address4: null, | ||
address5: 'Little Whinging', | ||
address6: 'Surrey', | ||
postcode: 'HP11', | ||
country: 'United Kingdom', | ||
external_id: '7:7777' | ||
} | ||
} |
Oops, something went wrong.