Skip to content

Commit

Permalink
Add new bill run volume model
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4152

We are currently working on the SROC two part tariff bill. The first step of the process in the UI is to review how the returns have been matched. Our superstar design team are working on an improved design for the returns review screen but to do that they need data.

Enter our mock data service!

We previously [Create endpoint to generate fake data](#347) which was focused on a bill run. The design team would love it if we could do the same for the data the current returns review page uses.

The page relies on data in the `water.billing_volumes` table. So, first step in generating the mock data is to add that model.
  • Loading branch information
Cruikshanks committed Sep 29, 2023
1 parent b2a6bb1 commit d3f815a
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 1 deletion.
73 changes: 73 additions & 0 deletions app/models/water/bill-run-volume.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

/**
* Model for billing_batches
* @module BillRunModel
*/

const { Model } = require('objection')

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

class BillRunVolumeModel extends WaterBaseModel {
static get tableName () {
return 'billingVolumes'
}

static get idColumn () {
return 'billingVolumeId'
}

static get translations () {
return []
}

static get relationMappings () {
return {
billRun: {
relation: Model.BelongsToOneRelation,
modelClass: 'bill-run.model',
join: {
from: 'billingVolumes.billingBatchId',
to: 'billingBatches.billingBatchId'
}
},
chargeReference: {
relation: Model.BelongsToOneRelation,
modelClass: 'charge-reference.model',
join: {
from: 'billingVolumes.chargeElementId',
to: 'chargeElements.chargeElementId'
}
}
}
}

// NOTE: When we checked the live data the only statuses we could find in use were; 10, 40, 50, 60, 70, 90 and 100
static get twoPartTariffStatuses () {
return {
noReturnsSubmitted: 10,
underQuery: 20,
received: 30,
someReturnsDue: 40,
lateReturns: 50,
overAbstraction: 60,
noReturnsForMatching: 70,
notDueForBilling: 80,
returnLineOverlapsChargePeriod: 90,
noMatchingChargeElement: 100
}
}

$twoPartTariffStatus () {
Object.entries(BillRunVolumeModel.twoPartTariffStatuses).forEach(([key, value]) => {
if (value === this.twoPartTariffStatus) {
return key
}
})

return null
}
}

module.exports = BillRunVolumeModel
8 changes: 8 additions & 0 deletions app/models/water/bill-run.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ class BillRunModel extends WaterBaseModel {
from: 'billingBatches.billingBatchId',
to: 'billingInvoices.billingBatchId'
}
},
billRunVolumes: {
relation: Model.HasManyRelation,
modelClass: 'bill-run-volume.model',
join: {
from: 'billingBatches.billingBatchId',
to: 'billingVolumes.billingBatchId'
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions app/models/water/charge-reference.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ class ChargeReferenceModel extends WaterBaseModel {

static get relationMappings () {
return {
billRunVolumes: {
relation: Model.HasManyRelation,
modelClass: 'bill-run-volume.model',
join: {
from: 'chargeElements.chargeElementId',
to: 'billingVolumes.chargeElementId'
}
},
chargeVersion: {
relation: Model.BelongsToOneRelation,
modelClass: 'charge-version.model',
Expand Down
19 changes: 19 additions & 0 deletions app/services/data/mock/generate-returns-review.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

async function go (id) {
const mockedReturnReviewData = {
id: '83a67627-f523-4a0b-8875-24e990d1c89c',
name: 'boo',
otherId: id
}

return _response(mockedReturnReviewData)
}

function _response (mockedReturnReviewData) {
return mockedReturnReviewData
}

module.exports = {
go
}
8 changes: 7 additions & 1 deletion app/services/data/mock/mock.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

const ExpandedError = require('../../../errors/expanded.error.js')
const GenerateBillRunService = require('./generate-bill-run.service.js')
const GenerateReturnsReviewService = require('./generate-returns-review.service.js')

const types = {
'bill-run': _billRun
'bill-run': _billRun,
'returns-review': _returnsReview
}

async function go (type, id) {
Expand All @@ -34,6 +36,10 @@ async function _billRun (id) {
return GenerateBillRunService.go(id)
}

async function _returnsReview (id) {
return GenerateReturnsReviewService.go(id)
}

module.exports = {
go
}
33 changes: 33 additions & 0 deletions db/migrations/20230929085159_create-water-billing-volumes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const tableName = 'billing_volumes'

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

// Data
table.uuid('charge_element_id').notNullable()
table.integer('financial_year').notNullable()
table.boolean('is_summer').notNullable()
table.decimal('calculated_volume')
table.boolean('two_part_tariff_error').notNullable().defaultTo(false)
table.integer('two_part_tariff_status')
table.jsonb('two_part_tariff_review')
table.boolean('is_approved').notNullable().defaultTo(false)
table.uuid('billing_batch_id').notNullable()
table.decimal('volume')
table.timestamp('errored_on')
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
112 changes: 112 additions & 0 deletions test/models/water/bill-run-volume.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
'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 BillRunHelper = require('../../support/helpers/water/bill-run.helper.js')
const BillRunModel = require('../../../app/models/water/bill-run.model.js')
const BillRunVolumeHelper = require('../../support/helpers/water/bill-run-volume.helper.js')
const ChargeReferenceHelper = require('../../support/helpers/water/charge-reference.helper.js')
const ChargeReferenceModel = require('../../../app/models/water/charge-reference.model.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')

// Thing under test
const BillRunVolumeModel = require('../../../app/models/water/bill-run-volume.model.js')

describe('Bill Run Volume model', () => {
let testRecord

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

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

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

expect(result).to.be.an.instanceOf(BillRunVolumeModel)
expect(result.billingVolumeId).to.equal(testRecord.billingVolumeId)
})
})

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

beforeEach(async () => {
testBillRun = await BillRunHelper.add()

const { billingBatchId } = testBillRun
testRecord = await BillRunVolumeHelper.add({ billingBatchId })
})

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

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

it('can eager load the bill run', async () => {
const result = await BillRunVolumeModel.query()
.findById(testRecord.billingVolumeId)
.withGraphFetched('billRun')

expect(result).to.be.instanceOf(BillRunVolumeModel)
expect(result.billingVolumeId).to.equal(testRecord.billingVolumeId)

expect(result.billRun).to.be.an.instanceOf(BillRunModel)
expect(result.billRun).to.equal(testBillRun)
})
})

describe('when linking to charge reference', () => {
let testChargeReference

beforeEach(async () => {
testChargeReference = await ChargeReferenceHelper.add()

const { chargeElementId } = testChargeReference
testRecord = await BillRunVolumeHelper.add({ chargeElementId })
})

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

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

it('can eager load the charge reference', async () => {
const result = await BillRunVolumeModel.query()
.findById(testRecord.billingVolumeId)
.withGraphFetched('chargeReference')

expect(result).to.be.instanceOf(BillRunVolumeModel)
expect(result.billingVolumeId).to.equal(testRecord.billingVolumeId)

expect(result.chargeReference).to.be.an.instanceOf(ChargeReferenceModel)
expect(result.chargeReference).to.equal(testChargeReference)
})
})
})

describe('Static getters', () => {
describe('Two Part Tariff status codes', () => {
it('returns the requested status code', async () => {
const result = BillRunVolumeModel.twoPartTariffStatuses.noReturnsSubmitted

expect(result).to.equal(10)
})
})
})
})
38 changes: 38 additions & 0 deletions test/models/water/bill-run.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const { expect } = Code
const BillHelper = require('../../support/helpers/water/bill.helper.js')
const BillModel = require('../../../app/models/water/bill.model.js')
const BillRunHelper = require('../../support/helpers/water/bill-run.helper.js')
const BillRunVolumeHelper = require('../../support/helpers/water/bill-run-volume.helper.js')
const BillRunVolumeModel = require('../../../app/models/water/bill-run-volume.model.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const RegionHelper = require('../../support/helpers/water/region.helper.js')
const RegionModel = require('../../../app/models/water/region.model.js')
Expand Down Expand Up @@ -102,6 +104,42 @@ describe('Bill Run model', () => {
expect(result.bills).to.include(testBills[1])
})
})

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

beforeEach(async () => {
testRecord = await BillRunHelper.add()
const { billingBatchId } = testRecord

testBillRunVolumes = []
for (let i = 0; i < 2; i++) {
const billRunVolume = await BillRunVolumeHelper.add({ billingBatchId })
testBillRunVolumes.push(billRunVolume)
}
})

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

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

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

expect(result).to.be.instanceOf(BillRunModel)
expect(result.billingBatchId).to.equal(testRecord.billingBatchId)

expect(result.billRunVolumes).to.be.an.array()
expect(result.billRunVolumes[0]).to.be.an.instanceOf(BillRunVolumeModel)
expect(result.billRunVolumes).to.include(testBillRunVolumes[0])
expect(result.billRunVolumes).to.include(testBillRunVolumes[1])
})
})
})

describe('Static getters', () => {
Expand Down
38 changes: 38 additions & 0 deletions test/models/water/charge-reference.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 BillRunVolumeHelper = require('../../support/helpers/water/bill-run-volume.helper.js')
const BillRunVolumeModel = require('../../../app/models/water/bill-run-volume.model.js')
const ChargeCategoryHelper = require('../../support/helpers/water/charge-category.helper.js')
const ChargeCategoryModel = require('../../../app/models/water/charge-category.model.js')
const ChargeElementHelper = require('../../support/helpers/water/charge-element.helper.js')
Expand Down Expand Up @@ -41,6 +43,42 @@ describe('Charge Reference model', () => {
})

describe('Relationships', () => {
describe('when linking to bill run volumes', () => {
let testBillRunVolumes

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

testBillRunVolumes = []
for (let i = 0; i < 2; i++) {
const billRunVolume = await BillRunVolumeHelper.add({ chargeElementId })
testBillRunVolumes.push(billRunVolume)
}
})

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

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

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

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

expect(result.billRunVolumes).to.be.an.array()
expect(result.billRunVolumes[0]).to.be.an.instanceOf(BillRunVolumeModel)
expect(result.billRunVolumes).to.include(testBillRunVolumes[0])
expect(result.billRunVolumes).to.include(testBillRunVolumes[1])
})
})

describe('when linking to charge category', () => {
let testChargeCategory

Expand Down
Loading

0 comments on commit d3f815a

Please sign in to comment.