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 charging model to review model relationships #1120

Merged
merged 8 commits into from
Jun 19, 2024
8 changes: 8 additions & 0 deletions app/models/charge-element.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ class ChargeElementModel extends BaseModel {
from: 'chargeElements.purposeId',
to: 'purposes.id'
}
},
reviewChargeElements: {
relation: Model.HasManyRelation,
modelClass: 'review-charge-element.model',
join: {
from: 'chargeElements.id',
to: 'reviewChargeElements.chargeElementId'
}
}
}
}
Expand Down
24 changes: 16 additions & 8 deletions app/models/charge-reference.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ class ChargeReferenceModel extends BaseModel {
to: 'billRunVolumes.chargeReferenceId'
}
},
chargeVersion: {
relation: Model.BelongsToOneRelation,
modelClass: 'charge-version.model',
join: {
from: 'chargeReferences.chargeVersionId',
to: 'chargeVersions.id'
}
},
chargeCategory: {
relation: Model.BelongsToOneRelation,
modelClass: 'charge-category.model',
Expand All @@ -48,6 +40,14 @@ class ChargeReferenceModel extends BaseModel {
to: 'chargeElements.chargeReferenceId'
}
},
chargeVersion: {
relation: Model.BelongsToOneRelation,
modelClass: 'charge-version.model',
join: {
from: 'chargeReferences.chargeVersionId',
to: 'chargeVersions.id'
}
},
purpose: {
relation: Model.BelongsToOneRelation,
modelClass: 'purpose.model',
Expand All @@ -56,6 +56,14 @@ class ChargeReferenceModel extends BaseModel {
to: 'purposes.id'
}
},
reviewChargeReferences: {
relation: Model.HasManyRelation,
modelClass: 'review-charge-reference.model',
join: {
from: 'chargeReferences.id',
to: 'reviewChargeReferences.chargeReferenceId'
}
},
transactions: {
relation: Model.HasManyRelation,
modelClass: 'transaction.model',
Expand Down
24 changes: 16 additions & 8 deletions app/models/charge-version.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ class ChargeVersionModel extends BaseModel {
to: 'billRunChargeVersionYears.chargeVersionId'
}
},
licence: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence.model',
join: {
from: 'chargeVersions.licenceId',
to: 'licences.id'
}
},
changeReason: {
relation: Model.BelongsToOneRelation,
modelClass: 'change-reason.model',
Expand All @@ -55,6 +47,22 @@ class ChargeVersionModel extends BaseModel {
from: 'chargeVersions.id',
to: 'chargeReferences.chargeVersionId'
}
},
licence: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence.model',
join: {
from: 'chargeVersions.licenceId',
to: 'licences.id'
}
},
reviewChargeVersions: {
relation: Model.HasManyRelation,
modelClass: 'review-charge-version.model',
join: {
from: 'chargeVersions.id',
to: 'reviewChargeVersions.chargeVersionId'
}
}
}
}
Expand Down
43 changes: 41 additions & 2 deletions test/models/charge-element.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const ChargeReferenceModel = require('../../app/models/charge-reference.model.js
const DatabaseSupport = require('../support/database.js')
const PurposeModel = require('../../app/models/purpose.model.js')
const PurposeHelper = require('../support/helpers/purpose.helper.js')
const ReviewChargeElementModel = require('../../app/models/review-charge-element.model.js')
const ReviewChargeElementHelper = require('../support/helpers/review-charge-element.helper.js')

// Thing under test
const ChargeElementModel = require('../../app/models/charge-element.model.js')
Expand All @@ -23,11 +25,13 @@ describe('Charge Element model', () => {

beforeEach(async () => {
await DatabaseSupport.clean()

testRecord = await ChargeElementHelper.add()
})

describe('Basic query', () => {
beforeEach(async () => {
testRecord = await ChargeElementHelper.add()
})

it('can successfully run a basic query', async () => {
const result = await ChargeElementModel.query().findById(testRecord.id)

Expand Down Expand Up @@ -96,5 +100,40 @@ describe('Charge Element model', () => {
expect(result.purpose).to.equal(testPurpose)
})
})

describe('when linking to review charge elements', () => {
let testReviewChargeElements

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

testReviewChargeElements = []
for (let i = 0; i < 2; i++) {
const reviewChargeElement = await ReviewChargeElementHelper.add({ chargeElementId: testRecord.id })
testReviewChargeElements.push(reviewChargeElement)
}
})

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

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

it('can eager load the review charge elements', async () => {
const result = await ChargeElementModel.query()
.findById(testRecord.id)
.withGraphFetched('reviewChargeElements')

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

expect(result.reviewChargeElements).to.be.an.array()
expect(result.reviewChargeElements[0]).to.be.an.instanceOf(ReviewChargeElementModel)
expect(result.reviewChargeElements).to.include(testReviewChargeElements[0])
expect(result.reviewChargeElements).to.include(testReviewChargeElements[1])
})
})
})
})
93 changes: 66 additions & 27 deletions test/models/charge-reference.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const ChargeVersionModel = require('../../app/models/charge-version.model.js')
const DatabaseSupport = require('../support/database.js')
const PurposeModel = require('../../app/models/purpose.model.js')
const PurposeHelper = require('../support/helpers/purpose.helper.js')
const ReviewChargeReferenceModel = require('../../app/models/review-charge-reference.model.js')
const ReviewChargeReferenceHelper = require('../support/helpers/review-charge-reference.helper.js')
const TransactionHelper = require('../support/helpers/transaction.helper.js')
const TransactionModel = require('../../app/models/transaction.model.js')

Expand All @@ -31,11 +33,13 @@ describe('Charge Reference model', () => {

beforeEach(async () => {
await DatabaseSupport.clean()

testRecord = await ChargeReferenceHelper.add()
})

describe('Basic query', () => {
beforeEach(async () => {
testRecord = await ChargeReferenceHelper.add()
})

it('can successfully run a basic query', async () => {
const result = await ChargeReferenceModel.query().findById(testRecord.id)

Expand Down Expand Up @@ -115,11 +119,11 @@ describe('Charge Reference model', () => {
let testChargeElements

beforeEach(async () => {
const { id } = testRecord
testRecord = await ChargeReferenceHelper.add()

testChargeElements = []
for (let i = 0; i < 2; i++) {
const chargeElement = await ChargeElementHelper.add({ description: `CP ${i}`, chargeReferenceId: id })
const chargeElement = await ChargeElementHelper.add({ chargeReferenceId: testRecord.id })
testChargeElements.push(chargeElement)
}
})
Expand All @@ -146,6 +150,36 @@ describe('Charge Reference model', () => {
})
})

describe('when linking to charge version', () => {
let testChargeVersion

beforeEach(async () => {
testChargeVersion = await ChargeVersionHelper.add()

const { id: chargeVersionId } = testChargeVersion
testRecord = await ChargeReferenceHelper.add({ chargeVersionId })
})

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

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

it('can eager load the charge version', async () => {
const result = await ChargeReferenceModel.query()
.findById(testRecord.id)
.withGraphFetched('chargeVersion')

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

expect(result.chargeVersion).to.be.an.instanceOf(ChargeVersionModel)
expect(result.chargeVersion).to.equal(testChargeVersion)
})
})

describe('when linking to purpose', () => {
let testPurpose

Expand Down Expand Up @@ -176,68 +210,73 @@ describe('Charge Reference model', () => {
})
})

describe('when linking to transactions', () => {
let testTransactions
describe('when linking to review charge references', () => {
let testReviewChargeReferences

beforeEach(async () => {
const { id } = testRecord
testRecord = await ChargeReferenceHelper.add()

testTransactions = []
testReviewChargeReferences = []
for (let i = 0; i < 2; i++) {
const transaction = await TransactionHelper.add({ description: `TEST TRANSACTION ${i}`, chargeReferenceId: id })
testTransactions.push(transaction)
const reviewChargeReference = await ReviewChargeReferenceHelper.add({ chargeReferenceId: testRecord.id })
testReviewChargeReferences.push(reviewChargeReference)
}
})

it('can successfully run a related query', async () => {
const query = await ChargeReferenceModel.query()
.innerJoinRelated('transactions')
.innerJoinRelated('reviewChargeReferences')

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

it('can eager load the transactions', async () => {
it('can eager load the review charge references', async () => {
const result = await ChargeReferenceModel.query()
.findById(testRecord.id)
.withGraphFetched('transactions')
.withGraphFetched('reviewChargeReferences')

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

expect(result.transactions).to.be.an.array()
expect(result.transactions[0]).to.be.an.instanceOf(TransactionModel)
expect(result.transactions).to.include(testTransactions[0])
expect(result.transactions).to.include(testTransactions[1])
expect(result.reviewChargeReferences).to.be.an.array()
expect(result.reviewChargeReferences[0]).to.be.an.instanceOf(ReviewChargeReferenceModel)
expect(result.reviewChargeReferences).to.include(testReviewChargeReferences[0])
expect(result.reviewChargeReferences).to.include(testReviewChargeReferences[1])
})
})

describe('when linking to charge version', () => {
let testChargeVersion
describe('when linking to transactions', () => {
let testTransactions

beforeEach(async () => {
testChargeVersion = await ChargeVersionHelper.add()
testRecord = await ChargeReferenceHelper.add()

const { id: chargeVersionId } = testChargeVersion
testRecord = await ChargeReferenceHelper.add({ chargeVersionId })
testTransactions = []
for (let i = 0; i < 2; i++) {
const transaction = await TransactionHelper.add({ chargeReferenceId: testRecord.id })
testTransactions.push(transaction)
}
})

it('can successfully run a related query', async () => {
const query = await ChargeReferenceModel.query()
.innerJoinRelated('chargeVersion')
.innerJoinRelated('transactions')

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

it('can eager load the charge version', async () => {
it('can eager load the transactions', async () => {
const result = await ChargeReferenceModel.query()
.findById(testRecord.id)
.withGraphFetched('chargeVersion')
.withGraphFetched('transactions')

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

expect(result.chargeVersion).to.be.an.instanceOf(ChargeVersionModel)
expect(result.chargeVersion).to.equal(testChargeVersion)
expect(result.transactions).to.be.an.array()
expect(result.transactions[0]).to.be.an.instanceOf(TransactionModel)
expect(result.transactions).to.include(testTransactions[0])
expect(result.transactions).to.include(testTransactions[1])
})
})
})
Expand Down
Loading
Loading