Skip to content

Commit

Permalink
Import addresses data for a licence (#1355)
Browse files Browse the repository at this point in the history
* 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
jonathangoulding and Cruikshanks authored Sep 26, 2024
1 parent ac262b6 commit a38973c
Show file tree
Hide file tree
Showing 12 changed files with 801 additions and 13 deletions.
34 changes: 34 additions & 0 deletions app/presenters/import/legacy/address.presenter.js
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
}
121 changes: 121 additions & 0 deletions app/services/import/legacy/fetch-address.service.js
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
*/
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 @@ -8,6 +8,7 @@
const LicenceStructureValidator = require('../../../validators/import/licence-structure.validator.js')
const PersistLicenceService = require('../persist-licence.service.js')
const ProcessLicenceReturnLogsService = require('../../jobs/return-logs/process-licence-return-logs.service.js')
const TransformAddressesService = require('./transform-addresses.service.js')
const TransformCompaniesService = require('./transform-companies.service.js')
const TransformContactsService = require('./transform-contacts.service.js')
const TransformLicenceService = require('./transform-licence.service.js')
Expand Down Expand Up @@ -41,6 +42,7 @@ async function go (licenceRef) {

// Pass the transformed companies through each transformation step, building the company as we go
await TransformContactsService.go(regionCode, naldLicenceId, transformedCompanies)
await TransformAddressesService.go(regionCode, naldLicenceId, transformedCompanies)

// Ensure the built licence has all the valid child records we require
LicenceStructureValidator.go(transformedLicence)
Expand Down
44 changes: 44 additions & 0 deletions app/services/import/legacy/transform-addresses.service.js
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
}
30 changes: 28 additions & 2 deletions app/services/import/persist-licence.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* @module PersistLicenceService
*/

const AddressModel = require('../../models/address.model.js')
const CompanyModel = require('../../models/company.model.js')
const ContactModel = require('../../models/contact.model.js')
const LicenceModel = require('../../models/licence.model.js')
const LicenceVersionModel = require('../../models/licence-version.model.js')
const LicenceVersionPurposeConditionModel = require('../../models/licence-version-purpose-condition.model.js')
const LicenceVersionPurposeModel = require('../../models/licence-version-purpose.model.js')
const { timestampForPostgres } = require('../../lib/general.lib.js')
const { db } = require('../../../db/db.js')
const { timestampForPostgres } = require('../../lib/general.lib.js')

/**
* Creates or updates an imported licence and its child entities that have been transformed and validated
Expand All @@ -35,6 +36,29 @@ async function go (transformedLicence, transformedCompanies) {
})
}

async function _persistAddress (trx, updatedAt, address) {
return AddressModel.query(trx)
.insert({ ...address, updatedAt })
.onConflict('externalId')
.merge([
'address1',
'address2',
'address3',
'address4',
'address5',
'address6',
'country',
'postcode',
'updatedAt'
])
}

async function _persistAddresses (trx, updatedAt, addresses) {
for (const address of addresses) {
await _persistAddress(trx, updatedAt, address)
}
}

async function _persistLicence (trx, updatedAt, licence) {
const { licenceVersions, ...propertiesToPersist } = licence

Expand Down Expand Up @@ -150,11 +174,13 @@ async function _persistCompanies (trx, updatedAt, companies) {
if (company.companyContact) {
await _persistsCompanyContact(trx, updatedAt, company.companyContact)
}

await _persistAddresses(trx, updatedAt, company.addresses)
}
}

async function _persistCompany (trx, updatedAt, company) {
const { contact, companyContact, ...propertiesToPersist } = company
const { contact, companyContact, addresses, ...propertiesToPersist } = company

return CompanyModel.query(trx)
.insert({ ...propertiesToPersist, updatedAt })
Expand Down
39 changes: 39 additions & 0 deletions app/validators/import/address.validator.js
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
}
58 changes: 58 additions & 0 deletions test/presenters/import/legacy/address.presenter.test.js
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'
}
}
Loading

0 comments on commit a38973c

Please sign in to comment.