Skip to content

Commit

Permalink
Refactor start of processing a supp. billing batch (#233)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-3977
https://eaflood.atlassian.net/browse/WATER-3984

Both to help resolve some issues found when cleaning up our existing process billing batch service and to support changes need for handling multi-year bill runs we need to make some changes.

The key one is `ProcessBillingBatchService` is no longer appropriate because it is actually focused on processing a single billing period. So, we renamed it to `ProcessBillingPeriodService` and have created a new `ProcessBillingBatchService` responsible for orchestrating the _whole_ process.

As the primary service now, we've updated the controller to call it instead of `InitiateBillingBatchService`. This resulted in us needing to make further changes in it and the services it uses.

Along the way we spotted opportunities to clean up some other things; passing in params only to find they are hard coded elsewhere, or having params named differently to how they are used within a service.
  • Loading branch information
Cruikshanks authored May 18, 2023
1 parent bcf6ead commit 3c7bee7
Show file tree
Hide file tree
Showing 10 changed files with 573 additions and 554 deletions.
6 changes: 4 additions & 2 deletions app/controllers/bill-runs/bill-runs.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const Boom = require('@hapi/boom')

const CreateBillRunValidator = require('../../validators/bill-runs/create-bill-run.validator.js')
const InitiateBillingBatchService = require('../../services/supplementary-billing/initiate-billing-batch.service.js')
const ProcessBillingBatchService = require('../../services/supplementary-billing/process-billing-batch.service.js')

async function create (request, h) {
const validatedData = CreateBillRunValidator.go(request.payload)
Expand All @@ -18,7 +18,9 @@ async function create (request, h) {
}

try {
const result = await InitiateBillingBatchService.go(validatedData.value)
const { region, user } = validatedData.value
const result = await ProcessBillingBatchService.go(region, user)

return h.response(result).code(200)
} catch (error) {
return _formattedInitiateBillingBatchError(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ const LIVE_STATUSES = ['processing', 'ready', 'review', 'queued']
* We define "live" as having the status `processing`, `ready`, `review` or `queued`
*
* @param {String} regionId The id of the region to be checked
* @param {Number} financialYear The financial year to be checked
* @param {Number} toFinancialYearEnding The financial year to be checked
*
* @returns {Boolean} Whether a "live" bill run exists
*/
async function go (regionId, financialYear) {
async function go (regionId, toFinancialYearEnding) {
const numberOfLiveBillRuns = await BillingBatchModel.query()
.select(1)
.where({
regionId,
toFinancialYearEnding: financialYear,
toFinancialYearEnding,
scheme: 'sroc',
batchType: 'supplementary'
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const BillingBatchModel = require('../../models/water/billing-batch.model.js')
* Create a new billing batch
*
* @param {Object} regionId The regionId for the selected region
* @param {Object} billingPeriod The billing period in the format { startDate: 2022-04-01, endDate: 2023-03-31 }
* @param {Object} financialYearEndings Object that contains the from and to financial year endings
* @param {Object} options Optional params to be overridden
* @param {String} [options.batchType=supplementary] The type of billing batch to create. Defaults to 'supplementary'
* @param {String} [options.scheme=sroc] The applicable charging scheme. Defaults to 'sroc'
Expand All @@ -23,14 +23,15 @@ const BillingBatchModel = require('../../models/water/billing-batch.model.js')
*
* @returns {module:BillingBatchModel} The newly created billing batch instance with the `.region` property populated
*/
async function go (regionId, billingPeriod, options) {
const optionsData = optionsDefaults(options)
async function go (regionId, financialYearEndings, options) {
const { fromFinancialYearEnding, toFinancialYearEnding } = financialYearEndings
const optionsData = _defaultOptions(options)

const billingBatch = await BillingBatchModel.query()
.insert({
regionId,
fromFinancialYearEnding: billingPeriod.endDate.getFullYear(),
toFinancialYearEnding: billingPeriod.endDate.getFullYear(),
fromFinancialYearEnding,
toFinancialYearEnding,
...optionsData
})
.returning('*')
Expand All @@ -39,7 +40,7 @@ async function go (regionId, billingPeriod, options) {
return billingBatch
}

function optionsDefaults (data) {
function _defaultOptions (option) {
const defaults = {
batchType: 'supplementary',
scheme: 'sroc',
Expand All @@ -51,7 +52,7 @@ function optionsDefaults (data) {

return {
...defaults,
...data
...option
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,44 @@
*/

const BillingBatchModel = require('../../models/water/billing-batch.model.js')
const BillingPeriodsService = require('./billing-periods.service.js')

const ChargingModuleCreateBillRunService = require('../charging-module/create-bill-run.service.js')
const CheckLiveBillRunService = require('./check-live-bill-run.service.js')
const CreateBillingBatchPresenter = require('../../presenters/supplementary-billing/create-billing-batch.presenter.js')
const CreateBillingBatchService = require('./create-billing-batch.service.js')
const CreateBillingBatchEventService = require('./create-billing-batch-event.service.js')
const ProcessBillingBatchService = require('./process-billing-batch.service.js')

/**
* Initiate a new billing batch
*
* Initiating a new billing batch means creating both the `billing_batch` and `event` record with the appropriate data,
* along with a bill run record in the SROC Charging Module API.
*
* @param {Object} billRunRequestData Validated version of the data sent in the request to create the new billing batch
* @param {String} regionId Id of the region the bill run is for
* @param {String} user Email address of the user who initiated the bill run
*
* @returns {Object} Details of the newly created billing batch record
* @returns {module:BillingBatchModel} The newly created billing batch instance
*/
async function go (billRunRequestData) {
// NOTE: It will be required in the future that we cater for a range of billing periods, as changes can be back dated
// up to 5 years. For now though, our delivery scope is only for the 2022-2023 billing period so the final record is
// extracted from the `billingPeriods` array which will currently always be for the 2022-2023 billing period.
const billingPeriods = BillingPeriodsService.go()
const billingPeriod = billingPeriods[billingPeriods.length - 1]

const { region, scheme, type, user } = billRunRequestData

const financialYear = billingPeriod.endDate.getFullYear()
const liveBillRunExists = await CheckLiveBillRunService.go(region, financialYear)
async function go (financialYearEndings, regionId, user) {
const liveBillRunExists = await CheckLiveBillRunService.go(regionId, financialYearEndings.toFinancialYearEnding)

if (liveBillRunExists) {
throw Error(`Batch already live for region ${region}`)
throw Error(`Batch already live for region ${regionId}`)
}

const chargingModuleResult = await ChargingModuleCreateBillRunService.go(region, 'sroc')
const chargingModuleResult = await ChargingModuleCreateBillRunService.go(regionId, 'sroc')

const billingBatchOptions = _billingBatchOptions(type, scheme, chargingModuleResult)
const billingBatch = await CreateBillingBatchService.go(region, billingPeriod, billingBatchOptions)
const billingBatchOptions = _billingBatchOptions(chargingModuleResult)
const billingBatch = await CreateBillingBatchService.go(regionId, financialYearEndings, billingBatchOptions)

await CreateBillingBatchEventService.go(billingBatch, user)

ProcessBillingBatchService.go(billingBatch, billingPeriod)

return _response(billingBatch)
return billingBatch
}

function _billingBatchOptions (type, scheme, chargingModuleResult) {
function _billingBatchOptions (chargingModuleResult) {
const options = {
scheme,
batchType: type
scheme: 'sroc',
batchType: 'supplementary'
}

if (chargingModuleResult.succeeded) {
Expand All @@ -71,10 +59,6 @@ function _billingBatchOptions (type, scheme, chargingModuleResult) {
return options
}

function _response (billingBatch) {
return CreateBillingBatchPresenter.go(billingBatch)
}

module.exports = {
go
}
Loading

0 comments on commit 3c7bee7

Please sign in to comment.