-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add billing_transaction model to water-abstraction-system (#135)
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
Showing
8 changed files
with
372 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
db/migrations/20230302114546_create-water-billing-transactions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.