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 - billing acc. <-> chg. version #750

Merged
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/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'
}
},
chargeVersions: {
relation: Model.HasManyRelation,
modelClass: 'charge-version.model',
join: {
from: 'billingAccounts.id',
to: 'chargeVersions.billingAccountId'
}
},
company: {
relation: Model.BelongsToOneRelation,
modelClass: 'company.model',
Expand Down
8 changes: 8 additions & 0 deletions app/models/charge-version.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ class ChargeVersionModel extends BaseModel {

static get relationMappings () {
return {
billingAccount: {
relation: Model.BelongsToOneRelation,
modelClass: 'billing-account.model',
join: {
from: 'chargeVersions.billingAccountId',
to: 'billingAccounts.id'
}
},
licence: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence.model',
Expand Down
86 changes: 62 additions & 24 deletions test/models/billing-account.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const { expect } = Code
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')
const ChargeVersionHelper = require('../support/helpers/charge-version.helper.js')
const ChargeVersionModel = require('../../app/models/charge-version.model.js')
const CompanyHelper = require('../support/helpers/company.helper.js')
const CompanyModel = require('../../app/models/company.model.js')
const DatabaseHelper = require('../support/helpers/database.helper.js')
Expand Down Expand Up @@ -39,70 +41,106 @@ describe('Billing Account model', () => {
})

describe('Relationships', () => {
describe('when linking to company', () => {
let testCompany
describe('when linking to billing account addresses', () => {
let testBillingAccountAddresses

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

testBillingAccountAddresses = []
for (let i = 0; i < 2; i++) {
// NOTE: A constraint in the invoice_account_addresses table means you cannot have 2 records with the same
// billingAccountId and start date
const startDate = i === 0 ? new Date(2023, 8, 4) : new Date(2023, 8, 3)
const billingAccountAddress = await BillingAccountAddressHelper.add({ startDate, billingAccountId })
testBillingAccountAddresses.push(billingAccountAddress)
}
})

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

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

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

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

expect(result.company).to.be.an.instanceOf(CompanyModel)
expect(result.company).to.equal(testCompany)
expect(result.billingAccountAddresses).to.be.an.array()
expect(result.billingAccountAddresses[0]).to.be.an.instanceOf(BillingAccountAddressModel)
expect(result.billingAccountAddresses).to.include(testBillingAccountAddresses[0])
expect(result.billingAccountAddresses).to.include(testBillingAccountAddresses[1])
})
})

describe('when linking to billing account addresses', () => {
let testBillingAccountAddresses
describe('when linking to charge versions', () => {
let testChargeVersions

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

testBillingAccountAddresses = []
testChargeVersions = []
for (let i = 0; i < 2; i++) {
// NOTE: A constraint in the invoice_account_addresses table means you cannot have 2 records with the same
// billingAccountId and start date
const startDate = i === 0 ? new Date(2023, 8, 4) : new Date(2023, 8, 3)
const billingAccountAddress = await BillingAccountAddressHelper.add({ startDate, billingAccountId })
testBillingAccountAddresses.push(billingAccountAddress)
const chargeVersion = await ChargeVersionHelper.add({ billingAccountId })
testChargeVersions.push(chargeVersion)
}
})

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

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

it('can eager load the billing account addresses', async () => {
it('can eager load the charge versions', async () => {
const result = await BillingAccountModel.query()
.findById(testRecord.id)
.withGraphFetched('billingAccountAddresses')
.withGraphFetched('chargeVersions')

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

expect(result.billingAccountAddresses).to.be.an.array()
expect(result.billingAccountAddresses[0]).to.be.an.instanceOf(BillingAccountAddressModel)
expect(result.billingAccountAddresses).to.include(testBillingAccountAddresses[0])
expect(result.billingAccountAddresses).to.include(testBillingAccountAddresses[1])
expect(result.chargeVersions).to.be.an.array()
expect(result.chargeVersions[0]).to.be.an.instanceOf(ChargeVersionModel)
expect(result.chargeVersions).to.include(testChargeVersions[0])
expect(result.chargeVersions).to.include(testChargeVersions[1])
})
})

describe('when linking to company', () => {
let testCompany

beforeEach(async () => {
testCompany = await CompanyHelper.add()
testRecord = await BillingAccountHelper.add({ companyId: testCompany.id })
})

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

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

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

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

expect(result.company).to.be.an.instanceOf(CompanyModel)
expect(result.company).to.equal(testCompany)
})
})
})
Expand Down
32 changes: 32 additions & 0 deletions test/models/charge-version.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 BillingAccountHelper = require('../support/helpers/billing-account.helper.js')
const BillingAccountModel = require('../../app/models/billing-account.model.js')
const ChangeReasonHelper = require('../support/helpers/change-reason.helper.js')
const ChangeReasonModel = require('../../app/models/change-reason.model.js')
const ChargeReferenceHelper = require('../support/helpers/charge-reference.helper.js')
Expand Down Expand Up @@ -39,6 +41,36 @@ describe('Charge Version model', () => {
})

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

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

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

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

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

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

expect(result).to.be.instanceOf(ChargeVersionModel)
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 licence', () => {
let testLicence

Expand Down
Loading