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 model relationship for return logs & licences #972

Merged
merged 7 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/models/licence.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ class LicenceModel extends BaseModel {
to: 'regions.id'
}
},
returnLogs: {
relation: Model.HasManyRelation,
modelClass: 'return-log.model',
join: {
from: 'licences.licenceRef',
to: 'returnLogs.licenceRef'
}
},
reviewLicences: {
relation: Model.HasManyRelation,
modelClass: 'review-licence.model',
Expand Down
8 changes: 8 additions & 0 deletions app/models/return-log.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class ReturnLogModel extends BaseModel {

static get relationMappings () {
return {
licence: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence.model',
join: {
from: 'returnLogs.licenceRef',
to: 'licences.licenceRef'
}
},
returnSubmissions: {
relation: Model.HasManyRelation,
modelClass: 'return-submission.model',
Expand Down
37 changes: 37 additions & 0 deletions test/models/licence.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const LicenceVersionHelper = require('../support/helpers/licence-version.helper.
const LicenceVersionModel = require('../../app/models/licence-version.model.js')
const RegionHelper = require('../support/helpers/region.helper.js')
const RegionModel = require('../../app/models/region.model.js')
const ReturnLogHelper = require('../support/helpers/return-log.helper.js')
const ReturnLogModel = require('../../app/models/return-log.model.js')
const RegisteredToAndLicenceNameSeeder = require('../support/seeders/registered-to-and-licence-name.seeder.js')
const ReviewLicenceHelper = require('../support/helpers/review-licence.helper.js')
const ReviewLicenceModel = require('../../app/models/review-licence.model.js')
Expand Down Expand Up @@ -249,6 +251,41 @@ describe('Licence model', () => {
})
})

describe('when linking to return logs', () => {
let testReturnLogs

beforeEach(async () => {
const { licenceRef } = testRecord

testReturnLogs = []
for (let i = 0; i < 2; i++) {
const returnLog = await ReturnLogHelper.add({ licenceRef })
testReturnLogs.push(returnLog)
}
})

it('can successfully run a related query', async () => {
const query = await LicenceModel.query()
.innerJoinRelated('returnLogs')

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

it('can eager load the workflows', async () => {
const result = await LicenceModel.query()
.findById(testRecord.id)
.withGraphFetched('returnLogs')

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

expect(result.returnLogs).to.be.an.array()
expect(result.returnLogs[0]).to.be.an.instanceOf(ReturnLogModel)
expect(result.returnLogs).to.include(testReturnLogs[0])
expect(result.returnLogs).to.include(testReturnLogs[1])
})
})

describe('when linking to review licences', () => {
let testReviewLicences

Expand Down
32 changes: 32 additions & 0 deletions test/models/return-log.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const { expect } = Code

// Test helpers
const DatabaseSupport = require('../support/database.js')
const LicenceHelper = require('../support/helpers/licence.helper.js')
const LicenceModel = require('../../app/models/licence.model.js')
const ReturnLogHelper = require('../support/helpers/return-log.helper.js')
const ReturnSubmissionHelper = require('../support/helpers/return-submission.helper.js')
const ReturnSubmissionModel = require('../../app/models/return-submission.model.js')
Expand All @@ -35,6 +37,36 @@ describe('Return Log model', () => {
})

describe('Relationships', () => {
describe('when linking to licence', () => {
let testLicence

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

const { licenceRef } = testLicence
testRecord = await ReturnLogHelper.add({ licenceRef })
})

it('can successfully run a related query', async () => {
const query = await ReturnLogModel.query()
.innerJoinRelated('licence')

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

it('can eager load the licence', async () => {
const result = await ReturnLogModel.query()
.findById(testRecord.id)
.withGraphFetched('licence')

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

expect(result.licence).to.be.an.instanceOf(LicenceModel)
expect(result.licence).to.equal(testLicence)
})
})

describe('when linking to return submissions', () => {
let returnSubmissions

Expand Down
Loading