-
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.
Add registered user modifier to LicenceModel (#693)
https://eaflood.atlassian.net/browse/WATER-4324 > This is part of a series of changes to allow us to replace the legacy view licence page When viewing a licence using the [legacy water-abstraction-ui](https://github.com/DEFRA/water-abstraction-ui) the page will display information on who the registered user is. The registered is an external user who has declared that they manage the licence via the external UI. They are not the licence holder, just the person declared to be responsible for managing it. Unfortunately, this functionality dates from the very start of the service and the data is in one of the oldest legacy schemas; `crm`. It seems this was one of the things which the previous team never managed to migrate to `crm_v2`, their intended better CRM structure. We have to go through a number of tables using a specific query to identify the registered user and whether they have set a custom name for the licence. This change adds a [modifier](https://vincit.github.io/objection.js/recipes/modifiers.html#modifiers) to the `LicenceModel` so we only have to do this once!
- Loading branch information
1 parent
20c50f5
commit 6a6239a
Showing
5 changed files
with
221 additions
and
3 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
54 changes: 54 additions & 0 deletions
54
db/migrations/public/20240127170505_alter-licence-document-headers.js
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,54 @@ | ||
'use strict' | ||
|
||
const viewName = 'licence_document_headers' | ||
|
||
exports.up = function (knex) { | ||
return knex | ||
.schema | ||
.dropView(viewName) | ||
.createView(viewName, (view) => { | ||
view.as(knex('document_header').withSchema('crm').select([ | ||
'document_id AS id', | ||
// This could be ignored as it is always set to the same ID. But that id comes from a single record in the | ||
// crm.entity table which has the `entity_type` regime. So, for the purposes of testing we just have to live | ||
// with always populating it even though we don't care about it. | ||
'regime_entity_id', | ||
// 'system_id', | ||
'system_internal_id AS nald_id', | ||
'system_external_id AS licence_ref', | ||
'metadata', | ||
'company_entity_id', | ||
// 'verification_id', | ||
'document_name AS licence_name', | ||
'date_created AS created_at', | ||
'date_updated AS updated_at', | ||
'date_deleted AS deleted_at' | ||
])) | ||
}) | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex | ||
.schema | ||
.dropView(viewName) | ||
.createView(viewName, (view) => { | ||
// NOTE: We have commented out unused columns from the source table | ||
view.as(knex('document_header').withSchema('crm').select([ | ||
'document_id AS id', | ||
// This could be ignored as it is always set to the same ID. But that id comes from a single record in the | ||
// crm.entity table which has the `entity_type` regime. So, for the purposes of testing we just have to live | ||
// with always populating it even though we don't care about it. | ||
'regime_entity_id', | ||
// 'system_id', | ||
'system_internal_id AS nald_id', | ||
'system_external_id AS licence_ref', | ||
'metadata', | ||
// 'company_entity_id', | ||
// 'verification_id', | ||
// 'document_name', | ||
'date_created AS created_at', | ||
'date_updated AS updated_at', | ||
'date_deleted AS deleted_at' | ||
])) | ||
}) | ||
} |
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
47 changes: 47 additions & 0 deletions
47
test/support/seeders/registered-to-and-licence-name.seeder.js
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,47 @@ | ||
'use strict' | ||
|
||
/** | ||
* Seeder to setup registered to and licence name details for a LicenceModel | ||
* @module RegisteredToAndLicenceNameSeeder | ||
*/ | ||
|
||
const LicenceDocumentHeaderHelper = require('../helpers/licence-document-header.helper.js') | ||
const LicenceEntityRoleHelper = require('../helpers/licence-entity-role.helper.js') | ||
const LicenceEntityHelper = require('../helpers/licence-entity.helper.js') | ||
|
||
/** | ||
* Sets up the additional records needed for `$licenceName()` and `%registeredTo()` on the licence to return values | ||
* | ||
* The registered to and licence name details are seen on the view licence page (if a licence has them). Unfortunately, | ||
* we have to link through a number of the legacy tables to extract the data. | ||
* | ||
* This seeder ensures the records needed are created and specifically support the `registeredToAndLicenceName()` | ||
* modifier on the `LicenceModel`. | ||
* | ||
* @param {module:LicenceModel} licence - The licence instance we are setting up the records for | ||
* @param {String} [licenceName] The custom licence name to use | ||
*/ | ||
async function seed (licence, licenceName = 'My custom licence name') { | ||
const { licenceRef } = licence | ||
|
||
// We get from the licence to the registered user via LicenceDocumentHeader and LicenceEntityRole which are linked | ||
// by the same companyEntityId | ||
const companyEntityId = 'c960a4a1-94f9-4c05-9db1-a70ce5d08738' | ||
|
||
// Create a licence document header record | ||
await LicenceDocumentHeaderHelper.add({ | ||
companyEntityId, | ||
licenceName, | ||
licenceRef | ||
}) | ||
|
||
// Create the licence entity record. It's `name` field holds the user email that the licence is registered to | ||
const { id: licenceEntityId } = await LicenceEntityHelper.add() | ||
|
||
// Create the licence entity role record that is the part of the link between the licence and the user email | ||
await LicenceEntityRoleHelper.add({ companyEntityId, licenceEntityId }) | ||
} | ||
|
||
module.exports = { | ||
seed | ||
} |