Skip to content

Commit

Permalink
Add billing_transaction model to water-abstraction-system (#135)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-3909

`billing_transaction` is linked to a billing invoice licence record. We need it to store the transaction detail for an invoice.

- add the model
- update relationships
- add unit tests for model
- update billing invoice licence model unit tests
- add test helper
  • Loading branch information
Jozzey authored Mar 2, 2023
1 parent 8fd331d commit 14d2c22
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/water/billing-invoice-licence.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ class BillingInvoiceLicenceModel extends WaterBaseModel {
to: 'billingInvoices.billingInvoiceId'
}
},
billingTransactions: {
relation: Model.HasManyRelation,
modelClass: 'billing-transaction.model',
join: {
from: 'billingInvoiceLicences.billingInvoiceLicenceId',
to: 'billingTransactions.billingInvoiceLicenceId'
}
},
licence: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence.model',
Expand Down
50 changes: 50 additions & 0 deletions app/models/water/billing-transaction.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

/**
* Model for billing_transactions
* @module BillingTransactionModel
*/

const { Model } = require('objection')

const WaterBaseModel = require('./water-base.model.js')

class BillingTransactionModel extends WaterBaseModel {
static get tableName () {
return 'billingTransactions'
}

static get idColumn () {
return 'billingTransactionId'
}

static get translations () {
return [
{ database: 'dateCreated', model: 'createdAt' },
{ database: 'dateUpdated', model: 'updatedAt' }
]
}

static get relationMappings () {
return {
chargeElement: {
relation: Model.BelongsToOneRelation,
modelClass: 'charge-element.model',
join: {
from: 'billingTransactions.chargeElementId',
to: 'chargeElements.chargeElementId'
}
},
billingInvoiceLicence: {
relation: Model.BelongsToOneRelation,
modelClass: 'billing-invoice-licence.model',
join: {
from: 'billingTransactions.billingInvoiceLicenceId',
to: 'billingInvoiceLicences.billingInvoiceLicenceId'
}
}
}
}
}

module.exports = BillingTransactionModel
8 changes: 8 additions & 0 deletions app/models/water/charge-element.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class ChargeElementModel extends WaterBaseModel {
from: 'chargeElements.chargeElementId',
to: 'chargePurposes.chargeElementId'
}
},
billingTransactions: {
relation: Model.HasManyRelation,
modelClass: 'billing-transaction.model',
join: {
from: 'chargeElements.chargeElementId',
to: 'billingTransactions.chargeElementId'
}
}
}
}
Expand Down
79 changes: 79 additions & 0 deletions db/migrations/20230302114546_create-water-billing-transactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict'

const tableName = 'billing_transactions'

exports.up = async function (knex) {
await knex
.schema
.withSchema('water')
.createTable(tableName, table => {
// Primary Key
table.uuid('billing_transaction_id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.uuid('billing_invoice_licence_id').notNullable()
table.uuid('charge_element_id')
table.date('start_date')
table.date('end_date')
table.jsonb('abstraction_period')
table.string('source')
table.string('season')
table.string('loss')
table.decimal('net_amount')
table.boolean('is_credit')
table.string('charge_type')
table.decimal('authorised_quantity')
table.decimal('billable_quantity')
table.integer('authorised_days')
table.integer('billable_days')
table.string('status')
table.string('description').notNullable()
table.uuid('external_id')
table.decimal('volume')
table.decimal('section_126_factor').defaultTo(1)
table.boolean('section_127_agreement').notNullable().defaultTo(false)
table.string('section_130_agreement')
table.boolean('is_new_licence').notNullable().defaultTo(false)
table.boolean('is_de_minimis').notNullable().defaultTo(false)
table.string('legacy_id')
table.jsonb('metadata')
table.uuid('source_transaction_id')
table.decimal('calc_source_factor')
table.decimal('calc_season_factor')
table.decimal('calc_loss_factor')
table.decimal('calc_suc_factor')
table.decimal('calc_s_126_factor')
table.decimal('calc_s_127_factor')
table.decimal('calc_eiuc_factor')
table.decimal('calc_eiuc_source_factor')
table.boolean('is_credited_back').defaultTo(false)
table.boolean('is_two_part_second_part_charge').notNullable().defaultTo(false)
table.string('scheme').notNullable().defaultTo('alcs')
table.decimal('aggregate_factor')
table.decimal('adjustment_factor')
table.string('charge_category_code')
table.string('charge_category_description')
table.boolean('is_supported_source').defaultTo(false)
table.string('supported_source_name')
table.boolean('is_water_company_charge').defaultTo(false)
table.boolean('is_winter_only').defaultTo(false)
table.boolean('is_water_undertaker').defaultTo(false)
table.jsonb('purposes')
table.jsonb('gross_values_calculated')
table.decimal('winter_discount_factor')
table.decimal('calc_adjustment_factor')
table.decimal('calc_winter_discount_factor')
table.decimal('calc_s_130_factor')

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now())
table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now())
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
38 changes: 38 additions & 0 deletions test/models/water/billing-invoice-licence.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const { expect } = Code
const BillingInvoiceHelper = require('../../support/helpers/water/billing-invoice.helper.js')
const BillingInvoiceModel = require('../../../app/models/water/billing-invoice.model.js')
const BillingInvoiceLicenceHelper = require('../../support/helpers/water/billing-invoice-licence.helper.js')
const BillingTransactionHelper = require('../../support/helpers/water/billing-transaction.helper.js')
const BillingTransactionModel = require('../../../app/models/water/billing-transaction.model.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const LicenceHelper = require('../../support/helpers/water/licence.helper.js')
const LicenceModel = require('../../../app/models/water/licence.model.js')
Expand Down Expand Up @@ -67,6 +69,42 @@ describe('Billing Invoice Licence model', () => {
})
})

describe('when linking to billing transactions', () => {
let testBillingTransactions

beforeEach(async () => {
testRecord = await BillingInvoiceLicenceHelper.add()
const { billingInvoiceLicenceId } = testRecord

testBillingTransactions = []
for (let i = 0; i < 2; i++) {
const billingTransaction = await BillingTransactionHelper.add({ billingInvoiceLicenceId })
testBillingTransactions.push(billingTransaction)
}
})

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

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

it('can eager load the billing transactions', async () => {
const result = await BillingInvoiceLicenceModel.query()
.findById(testRecord.billingInvoiceLicenceId)
.withGraphFetched('billingTransactions')

expect(result).to.be.instanceOf(BillingInvoiceLicenceModel)
expect(result.billingInvoiceLicenceId).to.equal(testRecord.billingInvoiceLicenceId)

expect(result.billingTransactions).to.be.an.array()
expect(result.billingTransactions[0]).to.be.an.instanceOf(BillingTransactionModel)
expect(result.billingTransactions).to.include(testBillingTransactions[0])
expect(result.billingTransactions).to.include(testBillingTransactions[1])
})
})

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

Expand Down
100 changes: 100 additions & 0 deletions test/models/water/billing-transaction.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const BillingInvoiceLicenceModel = require('../../../app/models/water/billing-invoice-licence.model.js')
const BillingInvoiceLicenceHelper = require('../../support/helpers/water/billing-invoice-licence.helper.js')
const BillingTransactionHelper = require('../../support/helpers/water/billing-transaction.helper.js')
const ChargeElementHelper = require('../../support/helpers/water/charge-element.helper.js')
const ChargeElementModel = require('../../../app/models/water/charge-element.model.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')

// Thing under test
const BillingTransactionModel = require('../../../app/models/water/billing-transaction.model.js')

describe('Billing Transaction model', () => {
let testRecord

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

testRecord = await BillingTransactionHelper.add()
})

describe('Basic query', () => {
it('can successfully run a basic query', async () => {
const result = await BillingTransactionModel.query().findById(testRecord.billingTransactionId)

expect(result).to.be.an.instanceOf(BillingTransactionModel)
expect(result.billingTransactionId).to.equal(testRecord.billingTransactionId)
})
})

describe('Relationships', () => {
describe('when linking to billing invoice licence', () => {
let testBillingInvoiceLicence

beforeEach(async () => {
testBillingInvoiceLicence = await BillingInvoiceLicenceHelper.add()

const { billingInvoiceLicenceId } = testBillingInvoiceLicence
testRecord = await BillingTransactionHelper.add({ billingInvoiceLicenceId })
})

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

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

it('can eager load the billing invoice licence', async () => {
const result = await BillingTransactionModel.query()
.findById(testRecord.billingTransactionId)
.withGraphFetched('billingInvoiceLicence')

expect(result).to.be.instanceOf(BillingTransactionModel)
expect(result.billingTransactionId).to.equal(testRecord.billingTransactionId)

expect(result.billingInvoiceLicence).to.be.an.instanceOf(BillingInvoiceLicenceModel)
expect(result.billingInvoiceLicence).to.equal(testBillingInvoiceLicence)
})
})

describe('when linking to charge element', () => {
let testChargeElement

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

const { chargeElementId } = testChargeElement
testRecord = await BillingTransactionHelper.add({ chargeElementId })
})

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

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

it('can eager load the charge element', async () => {
const result = await BillingTransactionModel.query()
.findById(testRecord.billingTransactionId)
.withGraphFetched('chargeElement')

expect(result).to.be.instanceOf(BillingTransactionModel)
expect(result.billingTransactionId).to.equal(testRecord.billingTransactionId)

expect(result.chargeElement).to.be.an.instanceOf(ChargeElementModel)
expect(result.chargeElement).to.equal(testChargeElement)
})
})
})
})
37 changes: 37 additions & 0 deletions test/models/water/charge-element.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const { expect } = Code
// Test helpers
const BillingChargeCategoryHelper = require('../../support/helpers/water/billing-charge-category.helper.js')
const BillingChargeCategoryModel = require('../../../app/models/water/billing-charge-category.model.js')
const BillingTransactionHelper = require('../../support/helpers/water/billing-transaction.helper.js')
const BillingTransactionModel = require('../../../app/models/water/billing-transaction.model.js')
const ChargeElementHelper = require('../../support/helpers/water/charge-element.helper.js')
const ChargePurposeHelper = require('../../support/helpers/water/charge-purpose.helper.js')
const ChargePurposeModel = require('../../../app/models/water/charge-purpose.model.js')
Expand Down Expand Up @@ -104,6 +106,41 @@ describe('Charge Element model', () => {
})
})

describe('when linking to billing transactions', () => {
let testBillingTransactions

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

testBillingTransactions = []
for (let i = 0; i < 2; i++) {
const billingTransaction = await BillingTransactionHelper.add({ description: `TEST BILLING TRANSACTION ${i}`, chargeElementId })
testBillingTransactions.push(billingTransaction)
}
})

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

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

it('can eager load the billing transactions', async () => {
const result = await ChargeElementModel.query()
.findById(testRecord.chargeElementId)
.withGraphFetched('billingTransactions')

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

expect(result.billingTransactions).to.be.an.array()
expect(result.billingTransactions[0]).to.be.an.instanceOf(BillingTransactionModel)
expect(result.billingTransactions).to.include(testBillingTransactions[0])
expect(result.billingTransactions).to.include(testBillingTransactions[1])
})
})

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

Expand Down
Loading

0 comments on commit 14d2c22

Please sign in to comment.