-
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 company data for a licence (#1304)
* Import companies (parties) for a licence https://eaflood.atlassian.net/browse/WATER-4649 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 companies (parties) data. This means going forwards we will only import companies (parties) when the licence is imported. We will insert this imported data in the relevant public views. This change focuses on the companies (parties) data and not address or contacts tables which will be done after this work.
- Loading branch information
1 parent
7fa1b87
commit 366538d
Showing
12 changed files
with
561 additions
and
6 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,26 @@ | ||
'use strict' | ||
|
||
/** | ||
* Maps the legacy NALD company data to the WRLS format | ||
* @module CompanyPresenter | ||
*/ | ||
|
||
/** | ||
* Maps the legacy NALD company data to the WRLS format | ||
* | ||
* @param {ImportLegacyCompanyType} company - the legacy NALD company | ||
* | ||
* @returns {object} the NALD company data transformed into the WRLS format for a company | ||
* ready for validation and persisting | ||
*/ | ||
function go (company) { | ||
return { | ||
name: company.name, | ||
type: company.type, | ||
externalId: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
'use strict' | ||
|
||
/** | ||
* Fetches the company data from the import.NALD_PARTIES table for the licence ref | ||
* @module FetchCompanyService | ||
*/ | ||
|
||
const { db } = require('../../../../db/db.js') | ||
|
||
/** | ||
* Fetches the licence data from the import.NALD_PARTIES table for the licence ref | ||
* | ||
* From this point parties will be referred to as companies and a party will be known as a company | ||
* | ||
* @param {string} regionCode - The NALD region code | ||
* @param {string} licenceId - The NALD licence ID | ||
* | ||
* @returns {Promise<ImportLegacyCompanyType[]>} | ||
*/ | ||
async function go (regionCode, licenceId) { | ||
const query = _query() | ||
|
||
const { rows } = await db.raw(query, [regionCode, licenceId, regionCode, licenceId]) | ||
|
||
return rows | ||
} | ||
|
||
function _query () { | ||
return ` | ||
SELECT DISTINCT ON (np."ID") | ||
(concat_ws(':', np."FGAC_REGION_CODE", np."ID")) AS external_id, | ||
( | ||
CASE np."SALUTATION" | ||
WHEN 'null' THEN NULL | ||
ELSE np."SALUTATION" | ||
END | ||
) AS salutation, | ||
( | ||
CASE np."FORENAME" | ||
WHEN 'null' THEN NULL | ||
ELSE np."FORENAME" | ||
END | ||
) AS firstName, | ||
( | ||
CASE np."NAME" | ||
WHEN 'null' THEN NULL | ||
ELSE np."NAME" | ||
END | ||
) AS lastName, | ||
( | ||
CASE np."APAR_TYPE" | ||
WHEN 'PER' THEN 'person' | ||
ELSE 'organisation' | ||
END | ||
) AS type, | ||
TRIM(BOTH ' ' FROM ( | ||
CASE | ||
-- If FORENAME or INITIALS are NULL, use NAME directly | ||
WHEN np."FORENAME" = 'null' AND np."INITIALS" = 'null' THEN np."NAME" | ||
ELSE CONCAT_WS(' ', | ||
CASE | ||
WHEN np."SALUTATION" = 'null' THEN NULL | ||
ELSE np."SALUTATION" | ||
END, | ||
CASE | ||
WHEN np."FORENAME" = 'null' THEN np."INITIALS" | ||
ELSE np."FORENAME" | ||
END, | ||
CASE | ||
WHEN np."NAME" = 'null' THEN NULL | ||
ELSE np."NAME" | ||
END | ||
) | ||
END | ||
)) AS name, | ||
np."ID" as id | ||
FROM import."NALD_PARTIES" np | ||
LEFT JOIN import."NALD_ABS_LIC_VERSIONS" nalv | ||
ON nalv."ACON_APAR_ID" = np."ID" | ||
AND nalv."FGAC_REGION_CODE" = np."FGAC_REGION_CODE" | ||
LEFT JOIN import."NALD_LIC_ROLES" nlr | ||
ON nlr."ACON_APAR_ID" = np."ID" | ||
AND nlr."FGAC_REGION_CODE" = np."FGAC_REGION_CODE" | ||
WHERE | ||
(nalv."FGAC_REGION_CODE" = ? AND nalv."AABL_ID" = ?) | ||
OR | ||
(nlr."FGAC_REGION_CODE" = ? AND nlr."AABL_ID" = ?) | ||
` | ||
} | ||
|
||
module.exports = { | ||
go | ||
} | ||
|
||
/** | ||
* @typedef {object} ImportLegacyCompanyType | ||
* | ||
* @property {string} - The party id | ||
* @property {string|null} salutation - The salutation of the person, or null if not applicable. | ||
* @property {string|null} firstName - The first name of the person, or null if not applicable. | ||
* @property {string|null} lastName - The last name of the person, or null if not applicable. | ||
* @property {string} type - Indicates whether the entry is a 'person' or an 'organisation'. | ||
* @property {string|null} name - The full name, concatenated from salutation, forename/initials, and name. | ||
* @property {string} external_id - The external ID, formatted as 'FGAC_REGION_CODE:ID'. | ||
* | ||
*/ |
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,42 @@ | ||
'use strict' | ||
|
||
/** | ||
* Transforms NALD company data into a valid object that matches the WRLS structure | ||
* @module ImportLegacyTransformCompaniesService | ||
*/ | ||
|
||
const FetchCompanyService = require('./fetch-company.service.js') | ||
const CompanyPresenter = require('../../../presenters/import/legacy/company.presenter.js') | ||
const ImportCompanyValidator = require('../../../validators/import/company.validator.js') | ||
|
||
/** | ||
* Transforms NALD company data into a validated object that matches the WRLS structure | ||
* | ||
* @param {string} regionCode - The NALD region code | ||
* @param {string} licenceId - The NALD licence ID | ||
* | ||
* @returns {Promise<object>} an object representing an array of valid WRLS transformed companies and | ||
* an array of companies from the db | ||
*/ | ||
async function go (regionCode, licenceId) { | ||
const companies = await FetchCompanyService.go(regionCode, licenceId) | ||
|
||
const transformedCompanies = [] | ||
|
||
companies.forEach((company) => { | ||
const transformedCompany = CompanyPresenter.go(company) | ||
|
||
ImportCompanyValidator.go(transformedCompany) | ||
|
||
transformedCompanies.push(transformedCompany) | ||
}) | ||
|
||
return { | ||
transformedCompanies, | ||
companies | ||
} | ||
} | ||
|
||
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,32 @@ | ||
'use strict' | ||
|
||
/** | ||
* @module ImportCompanyValidator | ||
*/ | ||
|
||
const Joi = require('joi') | ||
|
||
/** | ||
* Checks that the imported company data has been transformed and is valid for persisting to WRLS | ||
* | ||
* @param {object} company - The transformed company data | ||
* | ||
* @throws {Joi.ValidationError} - throws a Joi validation error if the validation fails | ||
*/ | ||
function go (company) { | ||
const schema = Joi.object({ | ||
name: Joi.string().required(), | ||
type: Joi.string().valid('organisation', 'person').required(), | ||
externalId: Joi.string().required() | ||
}) | ||
|
||
const result = schema.validate(company, { 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,37 @@ | ||
'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 CompanyPresenter = require('../../../../app/presenters/import/legacy/company.presenter.js') | ||
|
||
describe('Import Legacy Company presenter', () => { | ||
let legacyCompany | ||
|
||
beforeEach(() => { | ||
legacyCompany = _legacyCompany() | ||
}) | ||
|
||
it('correctly transforms the data', () => { | ||
const result = CompanyPresenter.go(legacyCompany) | ||
|
||
expect(result).to.equal({ | ||
externalId: '1:1940', | ||
name: 'ACME', | ||
type: 'organisation' | ||
}) | ||
}) | ||
}) | ||
|
||
function _legacyCompany () { | ||
return { | ||
name: 'ACME', | ||
type: 'organisation', | ||
external_id: '1:1940' | ||
} | ||
} |
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
Oops, something went wrong.