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

Update fetch charge versions service in water-abstraction-system #136

Merged
merged 4 commits into from
Mar 3, 2023
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
59 changes: 45 additions & 14 deletions app/services/supplementary-billing/fetch-charge-versions.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { ref } = require('objection')
/**
* Fetches SROC charge versions that might be included in a supplementary bill run
* @module FetchChargeVersionsService
Expand Down Expand Up @@ -27,7 +28,13 @@ async function go (regionId, billingPeriod) {

async function _fetch (regionId, billingPeriod) {
const chargeVersions = await ChargeVersion.query()
.select('chargeVersionId', 'scheme', 'chargeVersions.startDate', 'chargeVersions.endDate', 'invoiceAccountId')
.select([
'chargeVersionId',
'scheme',
'chargeVersions.startDate',
'chargeVersions.endDate',
'invoiceAccountId'
])
.innerJoinRelated('licence')
.where('scheme', 'sroc')
.where('includeInSupplementaryBilling', 'yes')
Expand All @@ -36,31 +43,55 @@ async function _fetch (regionId, billingPeriod) {
.where('chargeVersions.startDate', '<=', billingPeriod.endDate)
.withGraphFetched('licence')
.modifyGraph('licence', builder => {
builder.select(
builder.select([
'licenceId',
'licenceRef'
)
'licenceRef',
'isWaterUndertaker',
ref('licences.regions:historicalAreaCode').castText().as('historicalAreaCode'),
ref('licences.regions:regionalChargeArea').castText().as('regionalChargeArea')
])
})
.withGraphFetched('licence.region')
.modifyGraph('licence.region', builder => {
builder.select([
'regionId',
'chargeRegionId'
])
})
.withGraphFetched('changeReason')
.modifyGraph('changeReason', builder => {
builder.select([
'triggersMinimumCharge'
])
})
.withGraphFetched('chargeElements')
.modifyGraph('chargeElements', builder => {
builder.select([
'chargeElementId',
'source',
'loss',
'volume',
'adjustments',
'additionalCharges',
'description'
])
})
.withGraphFetched('chargeElements.billingChargeCategory')
.modifyGraph('chargeElements.billingChargeCategory', builder => {
builder.select(
'reference'
)
builder.select([
'reference',
'shortDescription'
])
})
.withGraphFetched('chargeElements.chargePurposes')
.modifyGraph('chargeElements', builder => {
builder.select(
'chargeElementId'
)
})
.modifyGraph('chargeElements.chargePurposes', builder => {
builder.select(
builder.select([
'chargePurposeId',
'abstractionPeriodStartDay',
'abstractionPeriodStartMonth',
'abstractionPeriodEndDay',
'abstractionPeriodEndMonth'
)
])
})

return chargeVersions
Expand Down
35 changes: 35 additions & 0 deletions db/migrations/20230302143311_alter-water-licences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const tableName = 'licences'

exports.up = async function (knex) {
await knex
.schema
.withSchema('water')
.alterTable(tableName, table => {
table.boolean('is_water_undertaker')
table.jsonb('regions')
table.date('start_date')
table.date('expired_date')
table.date('lapsed_date')
table.date('revoked_date')
table.boolean('suspend_from_billing')
})
}

exports.down = async function (knex) {
return knex
.schema
.withSchema('water')
.alterTable(tableName, table => {
table.dropColumns(
'is_water_undertaker',
'regions',
'start_date',
'expired_date',
'lapsed_date',
'revoked_date',
'suspend_from_billing'
)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,54 @@ const { expect } = Code
const BillingChargeCategoryHelper = require('../../support/helpers/water/billing-charge-category.helper.js')
const ChargeElementHelper = require('../../support/helpers/water/charge-element.helper.js')
const ChargePurposeHelper = require('../../support/helpers/water/charge-purpose.helper.js')
const ChangeReasonHelper = require('../../support/helpers/water/change-reason.helper.js')
const ChargeVersionHelper = require('../../support/helpers/water/charge-version.helper.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const LicenceHelper = require('../../support/helpers/water/licence.helper.js')
const RegionHelper = require('../../support/helpers/water/region.helper.js')

// Thing under test
const FetchChargeVersionsService = require('../../../app/services/supplementary-billing/fetch-charge-versions.service.js')

describe('Fetch Charge Versions service', () => {
const { regionId } = LicenceHelper.defaults()
const licenceDefaults = LicenceHelper.defaults()

let testRecords
let billingPeriod
let region
let regionId

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

region = await RegionHelper.add()
regionId = region.regionId
})

describe('when there are licences to be included in supplementary billing', () => {
let billingChargeCategory
let chargeElement
let chargePurpose
let changeReason

beforeEach(async () => {
billingPeriod = {
startDate: new Date('2022-04-01'),
endDate: new Date('2023-03-31')
}

changeReason = await ChangeReasonHelper.add({ triggersMinimumCharge: true })

// This creates an SROC charge version linked to a licence marked for supplementary billing
const srocChargeVersion = await ChargeVersionHelper.add(
{},
{ includeInSupplementaryBilling: 'yes' }
{ changeReasonId: changeReason.changeReasonId },
{ regionId, isWaterUndertaker: true, includeInSupplementaryBilling: 'yes' }
)

// This creates an ALCS (presroc) charge version linked to a licence marked for supplementary billing
const alcsChargeVersion = await ChargeVersionHelper.add(
{ scheme: 'alcs' },
{ includeInSupplementaryBilling: 'yes' }
{ regionId, includeInSupplementaryBilling: 'yes' }
)

testRecords = [srocChargeVersion, alcsChargeVersion]
Expand All @@ -71,13 +82,37 @@ describe('Fetch Charge Versions service', () => {
expect(result[0].chargeVersionId).to.equal(testRecords[0].chargeVersionId)
})

it('includes related licence and region', async () => {
const result = await FetchChargeVersionsService.go(regionId, billingPeriod)

expect(result[0].licence.licenceRef).to.equal(licenceDefaults.licenceRef)
expect(result[0].licence.isWaterUndertaker).to.equal(true)
expect(result[0].licence.historicalAreaCode).to.equal(licenceDefaults.regions.historicalAreaCode)
expect(result[0].licence.regionalChargeArea).to.equal(licenceDefaults.regions.regionalChargeArea)
expect(result[0].licence.region.regionId).to.equal(regionId)
expect(result[0].licence.region.chargeRegionId).to.equal(region.chargeRegionId)
})

it('includes related change reason', async () => {
const result = await FetchChargeVersionsService.go(regionId, billingPeriod)

expect(result[0].changeReason.triggersMinimumCharge).to.equal(changeReason.triggersMinimumCharge)
})

it('includes related charge elements, billing charge category and charge purposes', async () => {
const result = await FetchChargeVersionsService.go(regionId, billingPeriod)

const expectedResult = {
chargeElementId: chargeElement.chargeElementId,
source: chargeElement.source,
loss: chargeElement.loss,
volume: chargeElement.volume,
adjustments: chargeElement.adjustments,
additionalCharges: chargeElement.additionalCharges,
description: chargeElement.description,
billingChargeCategory: {
reference: billingChargeCategory.reference
reference: billingChargeCategory.reference,
shortDescription: billingChargeCategory.shortDescription
},
chargePurposes: [{
chargePurposeId: chargePurpose.chargePurposeId,
Expand Down
3 changes: 2 additions & 1 deletion test/support/helpers/water/licence.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ async function add (data = {}) {
function defaults (data = {}) {
const defaults = {
licenceRef: '01/123',
regionId: 'bd114474-790f-4470-8ba4-7b0cc9c225d7'
regionId: 'bd114474-790f-4470-8ba4-7b0cc9c225d7',
regions: { historicalAreaCode: 'SAAR', regionalChargeArea: 'Southern' }
}

return {
Expand Down