Skip to content

Commit

Permalink
Update Licence entity role to Has Many Relation (#1669)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4879

As part of the work to show the "Primary user" and "Returns agent" in the licence contact details page. We noticed our previous assumption of a licence entity role having one relation was incorrect.

A licence entity role can have many relations. This is commonly found when a licence entity role with a 'Primary user' also has a 'Returns agent' (know as a 'user'agent' in the db)

This change updates the `licenceEntityRole` to `licenceEntityRoles` and updates any necessary code / tests that fail.
  • Loading branch information
jonathangoulding authored Jan 30, 2025
1 parent 82a3c82 commit 6c8f297
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/models/licence-document-header.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class LicenceDocumentHeaderModel extends BaseModel {
to: 'licences.licenceRef'
}
},
licenceEntityRole: {
relation: Model.HasOneRelation,
licenceEntityRoles: {
relation: Model.HasManyRelation,
modelClass: 'licence-entity-role.model',
join: {
from: 'licenceDocumentHeaders.companyEntityId',
Expand Down
21 changes: 14 additions & 7 deletions app/models/licence.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,16 @@ class LicenceModel extends BaseModel {
.modifyGraph('licenceDocumentHeader', (builder) => {
builder.select(['id'])
})
.withGraphFetched('licenceDocumentHeader.licenceEntityRole')
.modifyGraph('licenceDocumentHeader.licenceEntityRole', (builder) => {
.withGraphFetched('licenceDocumentHeader.licenceEntityRoles')
.modifyGraph('licenceDocumentHeader.licenceEntityRoles', (builder) => {
builder.select(['id']).where('role', 'primary_user')
})
.withGraphFetched('licenceDocumentHeader.licenceEntityRole.licenceEntity')
.modifyGraph('licenceDocumentHeader.licenceEntityRole.licenceEntity', (builder) => {
.withGraphFetched('licenceDocumentHeader.licenceEntityRoles.licenceEntity')
.modifyGraph('licenceDocumentHeader.licenceEntityRoles.licenceEntity', (builder) => {
builder.select(['id'])
})
.withGraphFetched('licenceDocumentHeader.licenceEntityRole.licenceEntity.user')
.modifyGraph('licenceDocumentHeader.licenceEntityRole.licenceEntity.user', (builder) => {
.withGraphFetched('licenceDocumentHeader.licenceEntityRoles.licenceEntity.user')
.modifyGraph('licenceDocumentHeader.licenceEntityRoles.licenceEntity.user', (builder) => {
builder.select(['id', 'username'])
})
}
Expand Down Expand Up @@ -411,11 +411,18 @@ class LicenceModel extends BaseModel {
*
* Within the UI this what determines whether you see the "Registered to" link in the view licence page's top section.
*
* This query will find the `licenceEntityRole` with the role 'primary_user'. As we can expect multiple
* `licenceEntityRoles` we need to take the first (and expected to be only) `licenceEntityRole` from the received
* array of `licenceEntityRoles`.
*
* > We understand that `licenceEntityRoles` can be associated with the same `company_entity_id`. A common example
* of this is having a 'Primary user' and a 'Returns agent' (known as `user_agent` in the database)
*
* @returns {(module:UserModel|null)} the primary user if the licence has one and the additional properties needed to
* to determine it have been set, else `null`
*/
$primaryUser() {
const primaryUser = this?.licenceDocumentHeader?.licenceEntityRole?.licenceEntity?.user
const primaryUser = this?.licenceDocumentHeader?.licenceEntityRoles?.[0]?.licenceEntity?.user

return primaryUser || null
}
Expand Down
14 changes: 8 additions & 6 deletions test/models/licence-document-header.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,25 @@ describe('Licence Document Header model', () => {
})
})

describe('when linking to licence entity role', () => {
describe('when linking to licence entity roles', () => {
it('can successfully run a related query', async () => {
const query = await LicenceDocumentHeaderModel.query().innerJoinRelated('licenceEntityRole')
const query = await LicenceDocumentHeaderModel.query().innerJoinRelated('licenceEntityRoles')

expect(query).to.exist()
})

it('can eager load the licence entity role', async () => {
it('can eager load the licence entity roles', async () => {
const result = await LicenceDocumentHeaderModel.query()
.findById(testRecord.id)
.withGraphFetched('licenceEntityRole')
.withGraphFetched('licenceEntityRoles')

expect(result).to.be.instanceOf(LicenceDocumentHeaderModel)
expect(result.id).to.equal(testRecord.id)

expect(result.licenceEntityRole).to.be.an.instanceOf(LicenceEntityRoleModel)
expect(result.licenceEntityRole).to.equal(testLicenceEntityRole)
const [licenceEntityRole] = result.licenceEntityRoles

expect(licenceEntityRole).to.be.an.instanceOf(LicenceEntityRoleModel)
expect(licenceEntityRole).to.equal(testLicenceEntityRole)
})
})
})
Expand Down
20 changes: 11 additions & 9 deletions test/presenters/licences/view-licence.presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('View Licence presenter', () => {

describe('when the licence does not have a primary user (registered user)', () => {
beforeEach(() => {
licence.licenceDocumentHeader.licenceEntityRole = null
licence.licenceDocumentHeader.licenceEntityRoles = []
})

it('returns "Unregistered licence"', () => {
Expand Down Expand Up @@ -347,16 +347,18 @@ function _licence() {
licenceDocumentHeader: {
id: 'e8f491f0-0c60-4083-9d41-d2be69f17a1e',
licenceName: 'Between two ferns',
licenceEntityRole: {
id: 'd7eecfc1-7afa-49f7-8bef-5dc477696a2d',
licenceEntity: {
id: 'ba7702cf-cd87-4419-a04c-8cea4e0cfdc2',
user: {
id: 10036,
username: '[email protected]'
licenceEntityRoles: [
{
id: 'd7eecfc1-7afa-49f7-8bef-5dc477696a2d',
licenceEntity: {
id: 'ba7702cf-cd87-4419-a04c-8cea4e0cfdc2',
user: {
id: 10036,
username: '[email protected]'
}
}
}
}
]
},
licenceSupplementaryYears: [],
workflows: [{ id: 'b6f44c94-25e4-4ca8-a7db-364534157ba7', status: 'to_setup' }]
Expand Down
18 changes: 10 additions & 8 deletions test/services/licences/view-licence.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,18 @@ function _licence() {
licenceDocumentHeader: {
id: 'e8f491f0-0c60-4083-9d41-d2be69f17a1e',
licenceName: 'Between two ferns',
licenceEntityRole: {
id: 'd7eecfc1-7afa-49f7-8bef-5dc477696a2d',
licenceEntity: {
id: 'ba7702cf-cd87-4419-a04c-8cea4e0cfdc2',
user: {
id: 10036,
username: '[email protected]'
licenceEntityRoles: [
{
id: 'd7eecfc1-7afa-49f7-8bef-5dc477696a2d',
licenceEntity: {
id: 'ba7702cf-cd87-4419-a04c-8cea4e0cfdc2',
user: {
id: 10036,
username: '[email protected]'
}
}
}
}
]
},
licenceSupplementaryYears: [],
workflows: [{ id: 'b6f44c94-25e4-4ca8-a7db-364534157ba7', status: 'to_setup' }]
Expand Down

0 comments on commit 6c8f297

Please sign in to comment.