diff --git a/app/services/import/legacy/fetch-company.service.js b/app/services/import/legacy/fetch-company.service.js new file mode 100644 index 0000000000..be5f4ddd00 --- /dev/null +++ b/app/services/import/legacy/fetch-company.service.js @@ -0,0 +1,87 @@ +'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 + * + * @param {string} regionCode - The NALD region code + * @param {string} licenceId - The NALD licence ID + * + * @returns {Promise} + */ +async function go (regionCode, licenceId) { + const query = _query() + + const { rows: [row] } = await db.raw(query, [regionCode, licenceId]) + + return row +} + +function _query () { + return ` + SELECT + CASE np."SALUTATION" + WHEN 'null' THEN NULL + ELSE np."SALUTATION" + END AS salutation, + CASE np."INITIALS" + WHEN 'null' THEN NULL + ELSE np."INITIALS" + END AS initials, + 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 + 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 fullName, + CONCAT_WS(':', np."FGAC_REGION_CODE", np."ID") AS external_id + FROM import."NALD_PARTIES" np + WHERE np."FGAC_REGION_CODE" = ? AND np."ID" = ?; + ` +} + +module.exports = { + go +} + +/** + * @typedef {object} ImportLegacyLicenceType + * + * @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} effective_end_date - Indicates whether the entry is a 'person' or an 'organisation'. + * @property {string|null} fullName - The full name, concatenated from salutation, forename/initials, and name. + * @property {string} external_id - The external ID, formatted as 'FGAC_REGION_CODE:ID'. + */ diff --git a/app/services/import/legacy/process-licence.service.js b/app/services/import/legacy/process-licence.service.js index 9b895a577c..cad267973c 100644 --- a/app/services/import/legacy/process-licence.service.js +++ b/app/services/import/legacy/process-licence.service.js @@ -5,6 +5,7 @@ * @module ImportLegacyProcessLicenceService */ +const ImportLegacyTransformCompanyService = require('./transform-company.service.js') const LicenceStructureValidator = require('../../../validators/import/licence-structure.validator.js') const PersistLicenceService = require('../persist-licence.service.js') const TransformLicenceService = require('./transform-licence.service.js') @@ -32,6 +33,9 @@ async function go (licenceRef) { await TransformLicenceVersionPurposesService.go(regionCode, naldLicenceId, transformedLicence) await TransformLicenceVersionPurposeConditionsService.go(regionCode, naldLicenceId, transformedLicence) + // Transform the company data + const { company } = await ImportLegacyTransformCompanyService.go(regionCode, naldLicenceId) + // Ensure the built licence has all the valid child records we require LicenceStructureValidator.go(transformedLicence) diff --git a/app/services/import/legacy/transform-company.service.js b/app/services/import/legacy/transform-company.service.js new file mode 100644 index 0000000000..e549085f53 --- /dev/null +++ b/app/services/import/legacy/transform-company.service.js @@ -0,0 +1,28 @@ +'use strict' + +/** + * Transforms NALD company data into a valid object that matches the WRLS structure + * @module ImportLegacyTransformCompanyService + */ + +const FetchCompanyService = require('./fetch-company.service.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} an object representing a valid WRLS licence + */ +async function go (regionCode, licenceId) { + const naldCompanyData = await FetchCompanyService.go(regionCode, licenceId) + + return { + company: naldCompanyData + } +} + +module.exports = { + go +}