Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'currentVersion' modifier to LicenceModel #1133

Merged
merged 13 commits into from
Jun 24, 2024
Prev Previous commit
Next Next commit
Implement modifier and custom instance method
Cruikshanks committed Jun 21, 2024
commit bf35ff426b9e21ae34bd04d8ef136cb6213f5525
45 changes: 45 additions & 0 deletions app/models/licence.model.js
Original file line number Diff line number Diff line change
@@ -134,6 +134,24 @@ class LicenceModel extends BaseModel {
*/
static get modifiers () {
return {
/**
* currentVersion modifier fetches only the current licence version record for this licence
*/
currentVersion (query) {
query
.withGraphFetched('licenceVersions')
.modifyGraph('licenceVersions', (builder) => {
builder
.select([
'id',
'startDate',
'status'
])
.where('status', 'current')
.orderBy('startDate', 'desc')
.limit(1)
})
},
/**
* licenceHolder modifier fetches all the joined records needed to identify the licence holder
*/
@@ -204,6 +222,33 @@ class LicenceModel extends BaseModel {
}
}

/**
* Of the licence versions against the licence instance, return the first 'current'
*
* > We recommend adding the `currentVersion` modifier to your query if you only care about the current licence
* version for the licence
*
* Minor changes to a licence will result in a new version of the licence being created in NALD. Purposes, points and
* agreements are then linked to the licence version purpose.
*
* It can be assumed that every licence has at least one licence version and that there will only ever be one with a
* status of 'current'. However, at times we have encountered licences without a licence version hence we cater for
* that in the function.
*
* @returns {module:LicenceVersion|null} `null` if this instance's `licenceVersions` has not been populated or there
* are none (we've found a couple of examples!). Else a `LicenceVersionModel` that is the 'current' version for this
* licence
*/
$currentVersion () {
if (!this.licenceVersions || this.licenceVersions.length === 0) {
return null
}

return this.licenceVersions.find((licenceVersion) => {
return licenceVersion.status === 'current'
})
}

/**
* Determine the 'end' date for the licence
*
44 changes: 44 additions & 0 deletions test/models/licence.model.test.js
Original file line number Diff line number Diff line change
@@ -474,6 +474,50 @@ describe('Licence model', () => {
})
})

describe('$currentVersion', () => {
let currentLicenceVersion

beforeEach(async () => {
testRecord = await LicenceHelper.add()

currentLicenceVersion = await LicenceVersionHelper.add({ licenceId: testRecord.id, status: 'current' })

// Add a second that isn't current
await LicenceVersionHelper.add({ licenceId: testRecord.id, status: 'superseded' })
})

describe('when instance does not have licence versions', () => {
it('returns null', () => {
const result = testRecord.$licenceHolder()

expect(result).to.be.null()
})
})

describe('when instance has licence versions', () => {
beforeEach(async () => {
testRecord = await LicenceHelper.add()

currentLicenceVersion = await LicenceVersionHelper.add({ licenceId: testRecord.id, status: 'current' })

// Add a second that isn't current
await LicenceVersionHelper.add({ licenceId: testRecord.id, status: 'superseded' })

testRecord = await LicenceModel.query().findById(testRecord.id).modify('currentVersion')
})

it('returns the "current" licence version', () => {
const result = testRecord.$currentVersion()

expect(result).to.equal({
id: currentLicenceVersion.id,
startDate: currentLicenceVersion.startDate,
status: currentLicenceVersion.status
})
})
})
})

describe('$ends', () => {
let expiredDate
let lapsedDate