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

Move and make generic some billing services #304

Merged
merged 14 commits into from
Jul 12, 2023
Merged
6 changes: 3 additions & 3 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 NewBillingBatchService = require('../../services/billing/supplementary/new-billing-batch.service.js')
const NewBillingBatchService = require('../../services/billing/new-billing-batch.service.js')

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

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

return h.response(result).code(200)
} catch (error) {
Expand Down
10 changes: 10 additions & 0 deletions app/lib/static-lookups.lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'

const billRunTypes = [
'supplementary',
'two_part_tariff'
]

module.exports = {
billRunTypes
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @module CheckLiveBillRunService
*/

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

const LIVE_STATUSES = ['processing', 'ready', 'review', 'queued']

Expand All @@ -16,17 +16,18 @@ const LIVE_STATUSES = ['processing', 'ready', 'review', 'queued']
*
* @param {String} regionId The id of the region to be checked
* @param {Number} toFinancialYearEnding The financial year to be checked
* @param {String} batchType The billing batch type to be checked
*
* @returns {Boolean} Whether a "live" bill run exists
*/
async function go (regionId, toFinancialYearEnding) {
async function go (regionId, toFinancialYearEnding, batchType) {
const numberOfLiveBillRuns = await BillingBatchModel.query()
.select(1)
.where({
regionId,
toFinancialYearEnding,
scheme: 'sroc',
batchType: 'supplementary'
batchType,
scheme: 'sroc'
})
.whereIn('status', LIVE_STATUSES)
.resultSize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* @module CreateBillingBatchEventService
*/

const CreateBillingBatchEventPresenter = require('../../../presenters/supplementary-billing/create-billing-batch-event.presenter.js')
const EventModel = require('../../../models/water/event.model.js')
const GeneralLib = require('../../../lib/general.lib.js')
const CreateBillingBatchEventPresenter = require('../../presenters/billing/create-billing-batch-event.presenter.js')
const EventModel = require('../../models/water/event.model.js')
const GeneralLib = require('../../lib/general.lib.js')

/**
* Create an event for when a new bill run is initialised
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @module CreateBillingBatchService
*/

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

/**
* Create a new billing batch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

/**
* Calculates billing periods needed when generating a SROC supplementary bill run
* @module BillingPeriodsService
* Determine the billing periods needed when generating an SROC supplementary bill run
* @module DetermineBillingPeriodsService
*/

/**
* Returns the billing periods needed when generating a SROC supplementary bill run
* Returns the billing periods needed when generating an SROC supplementary bill run
*
* Using the current date at the time the service is called, it calculates the billing periods to use. As we permit
* changes to charge versions to be retroactively applied for up to 5 years. This service will calculate the billing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @module FetchRegionService
*/

const RegionModel = require('../../../models/water/region.model.js')
const RegionModel = require('../../models/water/region.model.js')

/**
* Fetches the region with the matching NALD Region ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,47 @@
* @module InitiateBillingBatchService
*/

const BillingBatchModel = require('../../../models/water/billing-batch.model.js')
const ChargingModuleCreateBillRunService = require('../../charging-module/create-bill-run.service.js')
const BillingBatchModel = require('../../models/water/billing-batch.model.js')
const ChargingModuleCreateBillRunService = require('../charging-module/create-bill-run.service.js')
const CheckLiveBillRunService = require('./check-live-bill-run.service.js')
const CreateBillingBatchService = require('./create-billing-batch.service.js')
const CreateBillingBatchEventService = require('./create-billing-batch-event.service.js')
const ExpandedError = require('../../../errors/expanded.error.js')
const ExpandedError = require('../../errors/expanded.error.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} financialYearEndings Object that contains the from and to financial year endings
* @param {String} regionId Id of the region the bill run is for
* @param {String} batchType Type of bill run, for example, supplementary
* @param {String} userEmail Email address of the user who initiated the bill run
*
* @returns {module:BillingBatchModel} The newly created billing batch instance
*/
async function go (financialYearEndings, regionId, userEmail) {
const liveBillRunExists = await CheckLiveBillRunService.go(regionId, financialYearEndings.toFinancialYearEnding)
async function go (financialYearEndings, regionId, batchType, userEmail) {
const liveBillRunExists = await CheckLiveBillRunService.go(regionId, financialYearEndings.toFinancialYearEnding, batchType)

if (liveBillRunExists) {
throw new ExpandedError('Batch already live for region', { regionId })
}

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

const billingBatchOptions = _billingBatchOptions(chargingModuleResult)
const billingBatchOptions = _billingBatchOptions(chargingModuleResult, batchType)
const billingBatch = await CreateBillingBatchService.go(regionId, financialYearEndings, billingBatchOptions)

await CreateBillingBatchEventService.go(billingBatch, userEmail)

return billingBatch
}

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

if (chargingModuleResult.succeeded) {
Expand Down
59 changes: 59 additions & 0 deletions app/services/billing/new-billing-batch.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict'

/**
* Top level service for creating and processing a new billing batch
* @module NewBillingBatchService
*/

const DetermineBillingPeriodsService = require('./determine-billing-periods.service.js')
const CreateBillingBatchPresenter = require('../../presenters/billing/create-billing-batch.presenter.js')
const InitiateBillingBatchService = require('./initiate-billing-batch.service.js')
const SupplementaryProcessBillingBatchService = require('./supplementary/process-billing-batch.service.js')
const TwoPartTariffProcessBillingBatchService = require('./two-part-tariff/process-billing-batch.service.js')

/**
* Manages the creation of a new billing batch
*
* @param {String} regionId Id of the region the bill run is for
* @param {String} batchType Type of bill run, for example, supplementary
* @param {String} userEmail Email address of the user who initiated the bill run
*
* @returns {Object} Object that will be the JSON response returned to the client
*/
async function go (regionId, batchType, userEmail) {
const billingPeriods = DetermineBillingPeriodsService.go()
const financialYearEndings = _financialYearEndings(billingPeriods)

const billingBatch = await InitiateBillingBatchService.go(financialYearEndings, regionId, batchType, userEmail)

_processBillingBatch(billingBatch, billingPeriods)

return _response(billingBatch)
}

function _financialYearEndings (billingPeriods) {
return {
fromFinancialYearEnding: billingPeriods[billingPeriods.length - 1].endDate.getFullYear(),
toFinancialYearEnding: billingPeriods[0].endDate.getFullYear()
}
}

function _processBillingBatch (billingBatch, billingPeriods) {
// We do not `await` the billing batch being processed so we can leave it to run in the background while we return an immediate response
switch (billingBatch.batchType) {
case 'supplementary':
SupplementaryProcessBillingBatchService.go(billingBatch, billingPeriods)
break
case 'two_part_tariff':
TwoPartTariffProcessBillingBatchService.go(billingBatch, billingPeriods)
break
}
}

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

module.exports = {
go
}
38 changes: 0 additions & 38 deletions app/services/billing/supplementary/new-billing-batch.service.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

/**
* Process a given billing batch for the given billing periods
* Process a given supplementary billing batch for the given billing periods
* @module ProcessBillingBatchService
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

/**
* Process a given two-part tariff billing batch for the given billing periods
* @module ProcessBillingBatchService
*/

/**
* Functionality not yet implemented
*/
async function go (_billingBatch, _billingPeriods) {
throw new Error('Two Part Tariff is not yet implemented')
}

module.exports = {
go
}
2 changes: 1 addition & 1 deletion app/services/check/two-part.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @module SupplementaryDataService
*/

const FetchRegionService = require('../billing/supplementary/fetch-region.service.js')
const FetchRegionService = require('../billing/fetch-region.service.js')

async function go (naldRegionId) {
const region = await FetchRegionService.go(naldRegionId)
Expand Down
4 changes: 3 additions & 1 deletion app/validators/bill-runs/create-bill-run.validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

const Joi = require('joi')

const StaticLookupsLib = require('../../lib/static-lookups.lib.js')

/**
* Checks that the payload of a `create bill run` request is valid
*/
function go (data) {
const schema = Joi.object({
type: Joi.string().valid('supplementary').required(),
type: Joi.string().valid(...StaticLookupsLib.billRunTypes).required(),
scheme: Joi.string().valid('sroc').required(),
region: Joi.string().guid().required(),
user: Joi.string().email().required(),
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/bill-runs/bill-runs.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code

// Things we need to stub
const NewBillingBatchService = require('../../../app/services/billing/supplementary/new-billing-batch.service.js')
const NewBillingBatchService = require('../../../app/services/billing/new-billing-batch.service.js')
const Boom = require('@hapi/boom')

// For running our service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const DatabaseHelper = require('../../support/helpers/database.helper.js')
const RegionHelper = require('../../support/helpers/water/region.helper.js')

// Thing under test
const CreateBillingBatchEventPresenter = require('../../../app/presenters/supplementary-billing/create-billing-batch-event.presenter.js')
const CreateBillingBatchEventPresenter = require('../../../app/presenters/billing/create-billing-batch-event.presenter.js')

describe('Create Billing Batch Event presenter', () => {
beforeEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Thing under test
const CreateBillingBatchPresenter = require('../../../app/presenters/supplementary-billing/create-billing-batch.presenter.js')
const CreateBillingBatchPresenter = require('../../../app/presenters/billing/create-billing-batch.presenter.js')

describe('Create Billing Batch presenter', () => {
let data
Expand Down
Loading