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

Implement abstraction period query using Objection #66

Merged
merged 17 commits into from
Dec 23, 2022
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
11 changes: 1 addition & 10 deletions app/presenters/check/supplementary-data.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@ function go (data) {
licenceRef: licence.licenceRef
}
})
const chargeVersions = data.chargeVersions.map((chargeVersion) => {
return {
chargeVersionId: chargeVersion.chargeVersionId,
licenceRef: chargeVersion.licenceRef,
licenceId: chargeVersion.licenceId,
scheme: chargeVersion.scheme,
startDate: chargeVersion.startDate,
endDate: chargeVersion.endDate
}
})
const chargeVersions = data.chargeVersions

return {
billingPeriods: data.billingPeriods,
Expand Down
47 changes: 36 additions & 11 deletions app/services/supplementary-billing/fetch-charge-versions.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @module FetchChargeVersionsService
*/

const { db } = require('../../../db/db.js')
const ChargeVersion = require('../../models/charge-version.model.js')

/**
* Fetch all SROC charge versions linked to licences flagged for supplementary billing that are in the period being
Expand All @@ -26,17 +26,42 @@ async function go (regionId, billingPeriod) {
}

async function _fetch (regionId, billingPeriod) {
const chargeVersions = db
.select('chv.chargeVersionId', 'chv.scheme', 'chv.startDate', 'chv.endDate', 'lic.licenceId', 'lic.licenceRef')
.from({ chv: 'water.charge_versions' })
.innerJoin({ lic: 'water.licences' }, 'chv.licence_id', 'lic.licence_id')
.where({
scheme: 'sroc',
'lic.include_in_supplementary_billing': 'yes',
'lic.region_id': regionId
const chargeVersions = await ChargeVersion.query()
.select('chargeVersionId', 'scheme', 'chargeVersions.startDate', 'chargeVersions.endDate')
.innerJoinRelated('licence')
.where('scheme', 'sroc')
.where('includeInSupplementaryBilling', 'yes')
.where('regionId', regionId)
.where('chargeVersions.startDate', '>=', billingPeriod.startDate)
.where('chargeVersions.startDate', '<=', billingPeriod.endDate)
.withGraphFetched('licence')
.modifyGraph('licence', builder => {
builder.select(
'licenceId',
'licenceRef'
)
})
.withGraphFetched('chargeElements.billingChargeCategory')
.modifyGraph('chargeElements.billingChargeCategory', builder => {
builder.select(
'reference'
)
})
.withGraphFetched('chargeElements.chargePurposes')
.modifyGraph('chargeElements', builder => {
builder.select(
'chargeElementId'
)
})
.modifyGraph('chargeElements.chargePurposes', builder => {
builder.select(
'chargePurposeId',
'abstractionPeriodStartDay',
'abstractionPeriodStartMonth',
'abstractionPeriodEndDay',
'abstractionPeriodEndMonth'
)
})
.andWhere('chv.start_date', '>=', billingPeriod.startDate)
.andWhere('chv.start_date', '<=', billingPeriod.endDate)

return chargeVersions
}
Expand Down
51 changes: 34 additions & 17 deletions test/presenters/check/supplementary-data.presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,44 @@ describe('Supplementary presenter', () => {
beforeEach(() => {
data = {
billingPeriods: [
{ startDate: new Date(2022, 3, 1), endDate: new Date(2023, 2, 31) }
{
startDate: new Date(2022, 3, 1), // 2022-04-01 - Months are zero indexed
endDate: new Date(2023, 2, 31)
}
],
licences: [
{ licenceId: '0000579f-0f8f-4e21-b63a-063384ad32c8', licenceRef: 'AT/SROC/SUPB/01' }
{
licenceId: 'f1288f6c-8503-4dc1-b114-75c408a14bd0',
licenceRef: 'AT/SROC/SUPB/01'
}
],
chargeVersions: [
{
chargeVersionId: '4b5cbe04-a0e2-468c-909e-1e2d93810ba8',
scheme: 'sroc',
startDate: new Date('2022-04-01'),
endDate: null,
licenceRef: 'AT/SROC/SUPB/01',
licenceId: '0000579f-0f8f-4e21-b63a-063384ad32c8'
},
{
chargeVersionId: '732fde85-fd3b-44e8-811f-8e6f4eb8cf6f',
chargeVersionId: '6a472535-145c-4170-ab59-f555783fa6e7',
scheme: 'sroc',
startDate: new Date('2022-04-01'),
startDate: new Date(2022, 4, 1),
endDate: null,
licenceRef: 'AT/SROC/SUPB/01',
licenceId: '0000579f-0f8f-4e21-b63a-063384ad32c8'
licence: {
licenceId: 'f1288f6c-8503-4dc1-b114-75c408a14bd0',
licenceRef: 'AT/SROC/SUPB/01'
},
chargeElements: [
{
chargeElementId: '0382824f-2b17-4294-aa57-c5fe5749960f',
chargePurposes: [
{
chargePurposeId: 'ffcb7d57-6148-4ee7-bc95-9de23c0cdc39',
abstractionPeriodStartDay: 1,
abstractionPeriodStartMonth: 4,
abstractionPeriodEndDay: 31,
abstractionPeriodEndMonth: 3
}
],
billingChargeCategory: {
reference: '4.2.1'
}
}
]
}
]
}
Expand All @@ -51,11 +68,11 @@ describe('Supplementary presenter', () => {

expect(result.licences).to.have.length(1)
expect(result.licences[0]).to.equal({
licenceId: data.chargeVersions[0].licenceId,
licenceRef: data.chargeVersions[0].licenceRef
licenceId: data.chargeVersions[0].licence.licenceId,
licenceRef: data.chargeVersions[0].licence.licenceRef
})

expect(result.chargeVersions).to.have.length(2)
expect(result.chargeVersions).to.have.length(1)
expect(result.chargeVersions[0]).to.equal(data.chargeVersions[0])
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const BillingChargeCategoryHelper = require('../../support/helpers/billing-charge-category.helper.js')
const ChargeElementHelper = require('../../support/helpers/charge-element.helper.js')
const ChargePurposeHelper = require('../../support/helpers/charge-purpose.helper.js')
const ChargeVersionHelper = require('../../support/helpers/charge-version.helper.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const LicenceHelper = require('../../support/helpers/licence.helper.js')
Expand All @@ -25,6 +28,10 @@ describe('Fetch Charge Versions service', () => {
})

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

beforeEach(async () => {
billingPeriod = {
startDate: new Date('2022-04-01'),
Expand All @@ -44,6 +51,17 @@ describe('Fetch Charge Versions service', () => {
)

testRecords = [srocChargeVersion, alcsChargeVersion]

billingChargeCategory = await BillingChargeCategoryHelper.add()

chargeElement = await ChargeElementHelper.add({
chargeVersionId: srocChargeVersion.chargeVersionId,
billingChargeCategoryId: billingChargeCategory.billingChargeCategoryId
})

chargePurpose = await ChargePurposeHelper.add({
chargeElementId: chargeElement.chargeElementId
})
})

it('returns only the current SROC charge versions that are applicable', async () => {
Expand All @@ -52,6 +70,28 @@ describe('Fetch Charge Versions service', () => {
expect(result.length).to.equal(1)
expect(result[0].chargeVersionId).to.equal(testRecords[0].chargeVersionId)
})

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

const expectedResult = {
chargeElementId: chargeElement.chargeElementId,
billingChargeCategory: {
reference: billingChargeCategory.reference
},
chargePurposes: [{
chargePurposeId: chargePurpose.chargePurposeId,
abstractionPeriodStartDay: chargePurpose.abstractionPeriodStartDay,
abstractionPeriodStartMonth: chargePurpose.abstractionPeriodStartMonth,
abstractionPeriodEndDay: chargePurpose.abstractionPeriodEndDay,
abstractionPeriodEndMonth: chargePurpose.abstractionPeriodEndMonth
}]
}

expect(result.length).to.equal(1)
expect(result[0].chargeVersionId).to.equal(testRecords[0].chargeVersionId)
expect(result[0].chargeElements[0]).to.equal(expectedResult)
})
})

describe('when there are no licences to be included in supplementary billing', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/support/helpers/billing-charge-category.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function add (data = {}) {
const insertData = defaults(data)

return BillingChargeCategoryModel.query()
.insertData({ ...insertData })
.insert({ ...insertData })
.returning('*')
}

Expand Down