Skip to content

Commit

Permalink
Import company data for a licence
Browse files Browse the repository at this point in the history
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 company data. This means going forwards we will only import company when the licence is imported.

We will insert this imported data in the relevant public views.

This change focuses on the company data and not address or contacts tables which will be done after this work.
  • Loading branch information
jonathangoulding committed Sep 3, 2024
1 parent a386f84 commit ef2f160
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
87 changes: 87 additions & 0 deletions app/services/import/legacy/fetch-company.service.js
Original file line number Diff line number Diff line change
@@ -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<ImportLegacyLicenceType>}
*/
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'.
*/
4 changes: 4 additions & 0 deletions app/services/import/legacy/process-licence.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)

Check failure on line 37 in app/services/import/legacy/process-licence.service.js

View workflow job for this annotation

GitHub Actions / build

'company' is assigned a value but never used

// Ensure the built licence has all the valid child records we require
LicenceStructureValidator.go(transformedLicence)

Expand Down
28 changes: 28 additions & 0 deletions app/services/import/legacy/transform-company.service.js
Original file line number Diff line number Diff line change
@@ -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<object>} 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
}

0 comments on commit ef2f160

Please sign in to comment.