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 relationship between bill and billing account #827

Merged
merged 1 commit into from
Mar 16, 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/bill.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ class BillModel extends BaseModel {

static get relationMappings () {
return {
billingAccount: {
relation: Model.BelongsToOneRelation,
modelClass: 'billing-account.model',
join: {
from: 'bills.billingAccountId',
to: 'billingAccounts.id'
}
},
billRun: {
relation: Model.BelongsToOneRelation,
modelClass: 'bill-run.model',
Expand Down
8 changes: 8 additions & 0 deletions app/models/billing-account.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class BillingAccountModel extends BaseModel {
to: 'billingAccountAddresses.billingAccountId'
}
},
bills: {
relation: Model.HasManyRelation,
modelClass: 'bill.model',
join: {
from: 'billingAccounts.id',
to: 'bills.billingAccountId'
}
},
chargeVersions: {
relation: Model.HasManyRelation,
modelClass: 'charge-version.model',
Expand Down
34 changes: 33 additions & 1 deletion test/models/bill.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const BillRunModel = require('../../app/models/bill-run.model.js')
const BillHelper = require('../support/helpers/bill.helper.js')
const BillingAccountHelper = require('../support/helpers/billing-account.helper.js')
const BillingAccountModel = require('../../app/models/billing-account.model.js')
const BillRunModel = require('../../app/models/bill-run.model.js')
const BillLicenceHelper = require('../support/helpers/bill-licence.helper.js')
const BillLicenceModel = require('../../app/models/bill-licence.model.js')
const BillRunHelper = require('../support/helpers/bill-run.helper.js')
Expand Down Expand Up @@ -39,6 +41,36 @@ describe('Bill model', () => {
})

describe('Relationships', () => {
describe('when linking to billing account', () => {
let testBillingAccount

beforeEach(async () => {
testBillingAccount = await BillingAccountHelper.add()

const { id: billingAccountId } = testBillingAccount
testRecord = await BillHelper.add({ billingAccountId })
})

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

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

it('can eager load the billing account', async () => {
const result = await BillModel.query()
.findById(testRecord.id)
.withGraphFetched('billingAccount')

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

expect(result.billingAccount).to.be.an.instanceOf(BillingAccountModel)
expect(result.billingAccount).to.equal(testBillingAccount)
})
})

describe('when linking to bill run', () => {
let testBillRun

Expand Down
38 changes: 38 additions & 0 deletions test/models/billing-account.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const BillHelper = require('../support/helpers/bill.helper.js')
const BillModel = require('../../app/models/bill.model.js')
const BillingAccountAddressHelper = require('../support/helpers/billing-account-address.helper.js')
const BillingAccountAddressModel = require('../../app/models/billing-account-address.model.js')
const BillingAccountHelper = require('../support/helpers/billing-account.helper.js')
Expand Down Expand Up @@ -80,6 +82,42 @@ describe('Billing Account model', () => {
})
})

describe('when linking to bills', () => {
let testBills

beforeEach(async () => {
testRecord = await BillingAccountHelper.add()
const { id: billingAccountId } = testRecord

testBills = []
for (let i = 0; i < 2; i++) {
const bill = await BillHelper.add({ billingAccountId })
testBills.push(bill)
}
})

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

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

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

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

expect(result.bills).to.be.an.array()
expect(result.bills[0]).to.be.an.instanceOf(BillModel)
expect(result.bills).to.include(testBills[0])
expect(result.bills).to.include(testBills[1])
})
})

describe('when linking to charge versions', () => {
let testChargeVersions

Expand Down
Loading