-
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 LicenceDocumentRoleModel from document_roles (#630)
https://eaflood.atlassian.net/browse/WATER-4292 > This is part of a series of changes to add the ability to identify the licence holder for a licence This change adds the migration, model, test helper and unit tests for a new `LicenceDocumentRoleModel`. We added [LicenceDocumentModel](#618). Though pointless, it's the only thing that points us to who the licence holder is. In the `crm_v2` schema it does this in `crm_v2.document_roles` which is the join table between `crm_v2.documents` and `crm_v2.roles`. `roles` is a lookup table of the 'role' a `crm_v2.contact` or `crm_c2.company` has, for example, licence holder! We added that as [LicenceRoleModel](#629). We are now on the final step which is adding the model that allows us to identify and extract the `crm_v2.document_roles` record that identifies _who_ is the current licence holder for a licence.
- Loading branch information
1 parent
2e4755e
commit f2c9e7c
Showing
15 changed files
with
660 additions
and
4 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
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
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,63 @@ | ||
'use strict' | ||
|
||
/** | ||
* Model for licence_document_roles (crm_v2.document_roles) | ||
* @module LicenceDocumentRoleModel | ||
*/ | ||
|
||
const { Model } = require('objection') | ||
|
||
const BaseModel = require('./base.model.js') | ||
|
||
class LicenceDocumentRoleModel extends BaseModel { | ||
static get tableName () { | ||
return 'licenceDocumentRoles' | ||
} | ||
|
||
static get relationMappings () { | ||
return { | ||
address: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: 'address.model', | ||
join: { | ||
from: 'licenceDocumentRoles.addressId', | ||
to: 'addresses.id' | ||
} | ||
}, | ||
company: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: 'company.model', | ||
join: { | ||
from: 'licenceDocumentRoles.companyId', | ||
to: 'companies.id' | ||
} | ||
}, | ||
contact: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: 'contact.model', | ||
join: { | ||
from: 'licenceDocumentRoles.contactId', | ||
to: 'contacts.id' | ||
} | ||
}, | ||
licenceDocument: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: 'licence-document.model', | ||
join: { | ||
from: 'licenceDocumentRoles.licenceDocumentId', | ||
to: 'licenceDocuments.id' | ||
} | ||
}, | ||
licenceRole: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: 'licence-role.model', | ||
join: { | ||
from: 'licenceDocumentRoles.licenceRoleId', | ||
to: 'licenceRoles.id' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = LicenceDocumentRoleModel |
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
48 changes: 48 additions & 0 deletions
48
db/migrations/legacy/20240103145832_create-crm-v2-document-roles.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,48 @@ | ||
'use strict' | ||
|
||
const tableName = 'document_roles' | ||
|
||
exports.up = function (knex) { | ||
return knex | ||
.schema | ||
.withSchema('crm_v2') | ||
.createTable(tableName, (table) => { | ||
// Primary Key | ||
table.uuid('document_role_id').primary().defaultTo(knex.raw('gen_random_uuid()')) | ||
|
||
// Data | ||
table.uuid('document_id').notNullable() | ||
table.uuid('company_id') | ||
table.uuid('contact_id') | ||
table.uuid('address_id') | ||
table.uuid('role_id').notNullable() | ||
table.date('start_date').notNullable() | ||
table.date('end_date') | ||
table.uuid('invoice_account_id') | ||
table.boolean('is_test').notNullable().defaultTo(false) | ||
|
||
// Legacy timestamps | ||
table.timestamp('date_created').notNullable().defaultTo(knex.fn.now()) | ||
table.timestamp('date_updated').notNullable().defaultTo(knex.fn.now()) | ||
|
||
// Constraints | ||
table.unique(['document_id', 'role_id', 'start_date'], { useConstraint: true }) | ||
}) | ||
// If it was a simple check constraint we could have used https://knexjs.org/guide/schema-builder.html#checks | ||
// But because of the complexity of the constraint we have had to drop to using raw() to add the constraint after | ||
// Knex has created the table. | ||
.raw(` | ||
ALTER TABLE crm_v2.document_roles | ||
ADD CONSTRAINT company_or_invoice_account | ||
CHECK ( | ||
((company_id IS NOT NULL AND address_id IS NOT NULL) OR invoice_account_id IS NOT NULL) | ||
); | ||
`) | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex | ||
.schema | ||
.withSchema('crm_v2') | ||
.dropTableIfExists(tableName) | ||
} |
31 changes: 31 additions & 0 deletions
31
db/migrations/public/20240103152905_create-licence-document-roles-view.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,31 @@ | ||
'use strict' | ||
|
||
const viewName = 'licence_document_roles' | ||
|
||
exports.up = function (knex) { | ||
return knex | ||
.schema | ||
.createView(viewName, (view) => { | ||
// NOTE: We have commented out unused columns from the source table | ||
view.as(knex('document_roles').withSchema('crm_v2').select([ | ||
'document_role_id AS id', | ||
'document_id AS licence_document_id', | ||
'company_id', | ||
'contact_id', | ||
'address_id', | ||
'role_id AS licence_role_id', | ||
// 'invoice_account_id', | ||
'start_date', | ||
'end_date', | ||
// 'is_test', | ||
'date_created AS created_at', | ||
'date_updated AS updated_at' | ||
])) | ||
}) | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex | ||
.schema | ||
.dropViewIfExists(viewName) | ||
} |
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
Oops, something went wrong.