From db75df3d89d628e056024be79f4f60ff57c16099 Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Fri, 28 Feb 2025 18:33:57 +0000 Subject: [PATCH 1/6] Move reusable two-part tariff billing services https://eaflood.atlassian.net/browse/WATER-4201 > Part of the work to support two-part tariff supplementary bill runs In [Move reusable supplementary billing services](https://github.com/DEFRA/water-abstraction-system/pull/1760) we shifted some existing supplementary billing services to make them resuable by the two-part tariff supplementary billing engine we're building. Our [spike](https://github.com/DEFRA/water-abstraction-system/pull/1412) has shown there are some two-part tariff annual billing services we can also make reusable. From f597c6e30c17b78efdd892b58b4064596c65301c Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Fri, 28 Feb 2025 18:42:56 +0000 Subject: [PATCH 2/6] Move GenerateTransaction out of 2PT annual --- ...=> generate-two-part-tariff-transaction.service.js} | 4 ++-- .../two-part-tariff/process-billing-period.service.js | 4 ++-- ...nerate-two-part-tariff-transaction.service.test.js} | 10 +++++----- .../process-billing-period.service.test.js | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) rename app/services/bill-runs/{two-part-tariff/generate-transaction.service.js => generate-two-part-tariff-transaction.service.js} (97%) rename test/services/bill-runs/{two-part-tariff/generate-transaction.service.test.js => generate-two-part-tariff-transaction.service.test.js} (92%) diff --git a/app/services/bill-runs/two-part-tariff/generate-transaction.service.js b/app/services/bill-runs/generate-two-part-tariff-transaction.service.js similarity index 97% rename from app/services/bill-runs/two-part-tariff/generate-transaction.service.js rename to app/services/bill-runs/generate-two-part-tariff-transaction.service.js index a2f25a7f8b..471a459778 100644 --- a/app/services/bill-runs/two-part-tariff/generate-transaction.service.js +++ b/app/services/bill-runs/generate-two-part-tariff-transaction.service.js @@ -2,10 +2,10 @@ /** * Generate a two-part tariff transaction data from the the charge reference and other information passed in - * @module GenerateTransactionService + * @module GenerateTwoPartTariffTransactionService */ -const { generateUUID } = require('../../../lib/general.lib.js') +const { generateUUID } = require('../../lib/general.lib.js') /** * Generate a two-part tariff transaction data from the the charge reference and other information passed in diff --git a/app/services/bill-runs/two-part-tariff/process-billing-period.service.js b/app/services/bill-runs/two-part-tariff/process-billing-period.service.js index b4a6fc20b7..e5162137c3 100644 --- a/app/services/bill-runs/two-part-tariff/process-billing-period.service.js +++ b/app/services/bill-runs/two-part-tariff/process-billing-period.service.js @@ -12,7 +12,7 @@ const BillLicenceModel = require('../../../models/bill-licence.model.js') const DetermineChargePeriodService = require('../determine-charge-period.service.js') const DetermineMinimumChargeService = require('../determine-minimum-charge.service.js') const { generateUUID } = require('../../../lib/general.lib.js') -const GenerateTransactionService = require('./generate-transaction.service.js') +const GenerateTwoPartTariffTransactionService = require('../generate-two-part-tariff-transaction.service.js') const SendTransactionsService = require('../send-transactions.service.js') const TransactionModel = require('../../../models/transaction.model.js') @@ -227,7 +227,7 @@ function _generateTransactionData(billLicenceId, chargePeriod, chargeVersion) { const transactions = [] chargeVersion.chargeReferences.forEach((chargeReference) => { - const transaction = GenerateTransactionService.go( + const transaction = GenerateTwoPartTariffTransactionService.go( billLicenceId, chargeReference, chargePeriod, diff --git a/test/services/bill-runs/two-part-tariff/generate-transaction.service.test.js b/test/services/bill-runs/generate-two-part-tariff-transaction.service.test.js similarity index 92% rename from test/services/bill-runs/two-part-tariff/generate-transaction.service.test.js rename to test/services/bill-runs/generate-two-part-tariff-transaction.service.test.js index 606ff87d04..e8c0ca859e 100644 --- a/test/services/bill-runs/two-part-tariff/generate-transaction.service.test.js +++ b/test/services/bill-runs/generate-two-part-tariff-transaction.service.test.js @@ -8,9 +8,9 @@ const { describe, it, beforeEach } = (exports.lab = Lab.script()) const { expect } = Code // Thing under test -const GenerateTransactionService = require('../../../../app/services/bill-runs/two-part-tariff/generate-transaction.service.js') +const GenerateTwoPartTariffTransactionService = require('../../../app/services/bill-runs/generate-two-part-tariff-transaction.service.js') -describe('Generate Transaction service', () => { +describe('Bill Runs - Generate Two Part Tariff Transaction service', () => { const billLicenceId = '5e2afb53-ca92-4515-ad71-36a7cefbcebb' let chargePeriod @@ -32,7 +32,7 @@ describe('Generate Transaction service', () => { describe('when called', () => { describe('with a charge reference that has volume to be billed', () => { it('returns a two-part tariff transaction ready to be persisted', () => { - const result = GenerateTransactionService.go( + const result = GenerateTwoPartTariffTransactionService.go( billLicenceId, chargeReference, chargePeriod, @@ -90,7 +90,7 @@ describe('Generate Transaction service', () => { }) it('returns the two-part tariff prefixed description', () => { - const result = GenerateTransactionService.go( + const result = GenerateTwoPartTariffTransactionService.go( billLicenceId, chargeReference, chargePeriod, @@ -112,7 +112,7 @@ describe('Generate Transaction service', () => { }) it('returns null', () => { - const result = GenerateTransactionService.go( + const result = GenerateTwoPartTariffTransactionService.go( billLicenceId, chargeReference, chargePeriod, diff --git a/test/services/bill-runs/two-part-tariff/process-billing-period.service.test.js b/test/services/bill-runs/two-part-tariff/process-billing-period.service.test.js index b8b3283d4c..7fb6f8d6ad 100644 --- a/test/services/bill-runs/two-part-tariff/process-billing-period.service.test.js +++ b/test/services/bill-runs/two-part-tariff/process-billing-period.service.test.js @@ -19,12 +19,12 @@ const RegionHelper = require('../../../support/helpers/region.helper.js') const BillRunError = require('../../../../app/errors/bill-run.error.js') const BillRunModel = require('../../../../app/models/bill-run.model.js') const ChargingModuleCreateTransactionRequest = require('../../../../app/requests/charging-module/create-transaction.request.js') -const GenerateTransactionService = require('../../../../app/services/bill-runs/two-part-tariff/generate-transaction.service.js') +const GenerateTwoPartTariffTransactionService = require('../../../../app/services/bill-runs/generate-two-part-tariff-transaction.service.js') // Thing under test const ProcessBillingPeriodService = require('../../../../app/services/bill-runs/two-part-tariff/process-billing-period.service.js') -describe('Two-part Tariff - Process Billing Period service', () => { +describe('Bill Runs - Two-part Tariff - Process Billing Period service', () => { const billingPeriod = { startDate: new Date('2022-04-01'), endDate: new Date('2023-03-31') @@ -198,7 +198,7 @@ describe('Two-part Tariff - Process Billing Period service', () => { describe('because generating the calculated transaction fails', () => { beforeEach(async () => { - Sinon.stub(GenerateTransactionService, 'go').throws() + Sinon.stub(GenerateTwoPartTariffTransactionService, 'go').throws() }) it('throws a BillRunError with the correct code', async () => { From b2a35d2d8c92a448c53426a986645439042a458e Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Fri, 28 Feb 2025 18:49:07 +0000 Subject: [PATCH 3/6] Move GenerateBillRunService --- app/controllers/bill-runs.controller.js | 4 +- ...erate-two-part-tariff-bill-run.service.js} | 24 ++++------ test/controllers/bill-runs.controller.test.js | 6 +-- ...-two-part-tariff-bill-run.service.test.js} | 46 +++++++++---------- 4 files changed, 38 insertions(+), 42 deletions(-) rename app/services/bill-runs/{two-part-tariff/generate-bill-run.service.js => generate-two-part-tariff-bill-run.service.js} (84%) rename test/services/bill-runs/{two-part-tariff/generate-bill-run.service.test.js => generate-two-part-tariff-bill-run.service.test.js} (80%) diff --git a/app/controllers/bill-runs.controller.js b/app/controllers/bill-runs.controller.js index a919c0006f..7a303bce80 100644 --- a/app/controllers/bill-runs.controller.js +++ b/app/controllers/bill-runs.controller.js @@ -7,7 +7,7 @@ const Boom = require('@hapi/boom') -const GenerateBillRunService = require('../services/bill-runs/two-part-tariff/generate-bill-run.service.js') +const GenerateTwoPartTariffBillRunService = require('../services/bill-runs/generate-two-part-tariff-bill-run.service.js') const IndexBillRunsService = require('../services/bill-runs/index-bill-runs.service.js') const SubmitCancelBillRunService = require('../services/bill-runs/cancel/submit-cancel-bill-run.service.js') const SubmitSendBillRunService = require('../services/bill-runs/send/submit-send-bill-run.service.js') @@ -66,7 +66,7 @@ async function twoPartTariff(request, h) { try { // NOTE: What we are awaiting here is for the GenerateBillRunService to update the status of the bill run to // `processing'. - await GenerateBillRunService.go(id) + await GenerateTwoPartTariffBillRunService.go(id) // Redirect to the bill runs page return h.redirect('/system/bill-runs') diff --git a/app/services/bill-runs/two-part-tariff/generate-bill-run.service.js b/app/services/bill-runs/generate-two-part-tariff-bill-run.service.js similarity index 84% rename from app/services/bill-runs/two-part-tariff/generate-bill-run.service.js rename to app/services/bill-runs/generate-two-part-tariff-bill-run.service.js index ffba9b6bc4..f0f00f959e 100644 --- a/app/services/bill-runs/two-part-tariff/generate-bill-run.service.js +++ b/app/services/bill-runs/generate-two-part-tariff-bill-run.service.js @@ -5,19 +5,15 @@ * @module GenerateBillRunService */ -const BillRunError = require('../../../errors/bill-run.error.js') -const BillRunModel = require('../../../models/bill-run.model.js') -const ChargingModuleGenerateBillRunRequest = require('../../../requests/charging-module/generate-bill-run.request.js') -const ExpandedError = require('../../../errors/expanded.error.js') -const { - calculateAndLogTimeTaken, - currentTimeInNanoseconds, - timestampForPostgres -} = require('../../../lib/general.lib.js') -const FetchTwoPartTariffBillingAccountsService = require('../fetch-two-part-tariff-billing-accounts.service.js') -const HandleErroredBillRunService = require('../handle-errored-bill-run.service.js') -const LegacyRefreshBillRunRequest = require('../../../requests/legacy/refresh-bill-run.request.js') -const ProcessBillingPeriodService = require('./process-billing-period.service.js') +const BillRunError = require('../../errors/bill-run.error.js') +const BillRunModel = require('../../models/bill-run.model.js') +const ChargingModuleGenerateBillRunRequest = require('../../requests/charging-module/generate-bill-run.request.js') +const ExpandedError = require('../../errors/expanded.error.js') +const { calculateAndLogTimeTaken, currentTimeInNanoseconds, timestampForPostgres } = require('../../lib/general.lib.js') +const FetchTwoPartTariffBillingAccountsService = require('./fetch-two-part-tariff-billing-accounts.service.js') +const HandleErroredBillRunService = require('./handle-errored-bill-run.service.js') +const LegacyRefreshBillRunRequest = require('../../requests/legacy/refresh-bill-run.request.js') +const ProcessAnnualBillingPeriodService = require('./two-part-tariff/process-billing-period.service.js') /** * Generates a two-part tariff bill run after the users have completed reviewing its match & allocate results @@ -136,7 +132,7 @@ async function _processBillingPeriod(billingPeriod, billRun) { const billingAccounts = await _fetchBillingAccounts(billRunId) - const billRunPopulated = await ProcessBillingPeriodService.go(billRun, billingPeriod, billingAccounts) + const billRunPopulated = await ProcessAnnualBillingPeriodService.go(billRun, billingPeriod, billingAccounts) await _finaliseBillRun(billRun, billRunPopulated) } diff --git a/test/controllers/bill-runs.controller.test.js b/test/controllers/bill-runs.controller.test.js index 2db3149ecd..fd41363617 100644 --- a/test/controllers/bill-runs.controller.test.js +++ b/test/controllers/bill-runs.controller.test.js @@ -13,7 +13,7 @@ const { postRequestOptions } = require('../support/general.js') // Things we need to stub const Boom = require('@hapi/boom') -const GenerateBillRunService = require('../../app/services/bill-runs/two-part-tariff/generate-bill-run.service.js') +const GenerateTwoPartTariffBillRunService = require('../../app/services/bill-runs/generate-two-part-tariff-bill-run.service.js') const IndexBillRunsService = require('../../app/services/bill-runs/index-bill-runs.service.js') const SubmitCancelBillRunService = require('../../app/services/bill-runs/cancel/submit-cancel-bill-run.service.js') const SubmitSendBillRunService = require('../../app/services/bill-runs/send/submit-send-bill-run.service.js') @@ -276,7 +276,7 @@ describe('Bill Runs controller', () => { describe('when a request is valid', () => { beforeEach(() => { - Sinon.stub(GenerateBillRunService, 'go').resolves('97db1a27-8308-4aba-b463-8a6af2558b28') + Sinon.stub(GenerateTwoPartTariffBillRunService, 'go').resolves('97db1a27-8308-4aba-b463-8a6af2558b28') }) it('redirects to the bill runs page', async () => { @@ -291,7 +291,7 @@ describe('Bill Runs controller', () => { describe('because the generate service threw an error', () => { beforeEach(async () => { Sinon.stub(Boom, 'badImplementation').returns(new Boom.Boom('Bang', { statusCode: 500 })) - Sinon.stub(GenerateBillRunService, 'go').rejects() + Sinon.stub(GenerateTwoPartTariffBillRunService, 'go').rejects() }) it('returns the error page', async () => { diff --git a/test/services/bill-runs/two-part-tariff/generate-bill-run.service.test.js b/test/services/bill-runs/generate-two-part-tariff-bill-run.service.test.js similarity index 80% rename from test/services/bill-runs/two-part-tariff/generate-bill-run.service.test.js rename to test/services/bill-runs/generate-two-part-tariff-bill-run.service.test.js index 722299dca4..20814505b3 100644 --- a/test/services/bill-runs/two-part-tariff/generate-bill-run.service.test.js +++ b/test/services/bill-runs/generate-two-part-tariff-bill-run.service.test.js @@ -11,19 +11,19 @@ const { expect } = Code // Test helpers const { setTimeout } = require('timers/promises') -const BillRunError = require('../../../../app/errors/bill-run.error.js') -const ExpandedErrorError = require('../../../../app/errors/expanded.error.js') +const BillRunError = require('../../../app/errors/bill-run.error.js') +const ExpandedErrorError = require('../../../app/errors/expanded.error.js') // Things we need to stub -const BillRunModel = require('../../../../app/models/bill-run.model.js') -const ChargingModuleGenerateRequest = require('../../../../app/requests/charging-module/generate-bill-run.request.js') -const FetchTwoPartTariffBillingAccountsService = require('../../../../app/services/bill-runs/fetch-two-part-tariff-billing-accounts.service.js') -const HandleErroredBillRunService = require('../../../../app/services/bill-runs/handle-errored-bill-run.service.js') -const LegacyRefreshBillRunRequest = require('../../../../app/requests/legacy/refresh-bill-run.request.js') -const ProcessBillingPeriodService = require('../../../../app/services/bill-runs/two-part-tariff/process-billing-period.service.js') +const BillRunModel = require('../../../app/models/bill-run.model.js') +const ChargingModuleGenerateRequest = require('../../../app/requests/charging-module/generate-bill-run.request.js') +const FetchTwoPartTariffBillingAccountsService = require('../../../app/services/bill-runs/fetch-two-part-tariff-billing-accounts.service.js') +const HandleErroredBillRunService = require('../../../app/services/bill-runs/handle-errored-bill-run.service.js') +const LegacyRefreshBillRunRequest = require('../../../app/requests/legacy/refresh-bill-run.request.js') +const ProcessAnnualBillingPeriodService = require('../../../app/services/bill-runs/two-part-tariff/process-billing-period.service.js') // Thing under test -const GenerateBillRunService = require('../../../../app/services/bill-runs/two-part-tariff/generate-bill-run.service.js') +const GenerateTwoPartTariffBillRunService = require('../../../app/services/bill-runs/generate-two-part-tariff-bill-run.service.js') describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { // NOTE: introducing a delay in the tests is not ideal. But the service is written such that the generating happens in @@ -79,7 +79,7 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { }) it('throws an error', async () => { - const error = await expect(GenerateBillRunService.go(billRunDetails.id)).to.reject() + const error = await expect(GenerateTwoPartTariffBillRunService.go(billRunDetails.id)).to.reject() expect(error).to.be.an.instanceOf(ExpandedErrorError) expect(error.message).to.equal('Cannot process a two-part tariff bill run that is not in review') @@ -93,7 +93,7 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { }) it('sets the bill run status first to "processing" and then to "empty"', async () => { - await GenerateBillRunService.go(billRunDetails.id) + await GenerateTwoPartTariffBillRunService.go(billRunDetails.id) await setTimeout(delay) @@ -113,18 +113,18 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { chargingModuleGenerateRequestStub = Sinon.stub(ChargingModuleGenerateRequest, 'send') legacyRefreshBillRunRequestStub = Sinon.stub(LegacyRefreshBillRunRequest, 'send') - Sinon.stub(ProcessBillingPeriodService, 'go').resolves(true) + Sinon.stub(ProcessAnnualBillingPeriodService, 'go').resolves(true) }) it('sets the bill run status to "processing"', async () => { - await GenerateBillRunService.go(billRunDetails.id) + await GenerateTwoPartTariffBillRunService.go(billRunDetails.id) expect(billRunPatchStub.calledOnce).to.be.true() expect(billRunPatchStub.firstCall.firstArg).to.equal({ status: 'processing' }, { skip: ['updatedAt'] }) }) it('tells the charging module API to "generate" the bill run', async () => { - await GenerateBillRunService.go(billRunDetails.id) + await GenerateTwoPartTariffBillRunService.go(billRunDetails.id) await setTimeout(delay) @@ -132,7 +132,7 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { }) it('tells the legacy service to start its refresh job', async () => { - await GenerateBillRunService.go(billRunDetails.id) + await GenerateTwoPartTariffBillRunService.go(billRunDetails.id) await setTimeout(delay) @@ -160,7 +160,7 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { }) it('calls HandleErroredBillRunService with appropriate error code', async () => { - await GenerateBillRunService.go(billRunDetails.id) + await GenerateTwoPartTariffBillRunService.go(billRunDetails.id) await setTimeout(delay) @@ -170,7 +170,7 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { }) it('logs the error', async () => { - await GenerateBillRunService.go(billRunDetails.id) + await GenerateTwoPartTariffBillRunService.go(billRunDetails.id) await setTimeout(delay) @@ -191,11 +191,11 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { thrownError = new BillRunError(new Error(), BillRunModel.errorCodes.failedToPrepareTransactions) Sinon.stub(FetchTwoPartTariffBillingAccountsService, 'go').resolves([]) - Sinon.stub(ProcessBillingPeriodService, 'go').rejects(thrownError) + Sinon.stub(ProcessAnnualBillingPeriodService, 'go').rejects(thrownError) }) it('calls HandleErroredBillRunService with appropriate error code', async () => { - await GenerateBillRunService.go(billRunDetails.id) + await GenerateTwoPartTariffBillRunService.go(billRunDetails.id) await setTimeout(delay) @@ -205,7 +205,7 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { }) it('logs the error', async () => { - await GenerateBillRunService.go(billRunDetails.id) + await GenerateTwoPartTariffBillRunService.go(billRunDetails.id) await setTimeout(delay) @@ -226,12 +226,12 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { thrownError = new Error('ERROR') Sinon.stub(FetchTwoPartTariffBillingAccountsService, 'go').resolves([]) - Sinon.stub(ProcessBillingPeriodService, 'go').resolves(true) + Sinon.stub(ProcessAnnualBillingPeriodService, 'go').resolves(true) Sinon.stub(ChargingModuleGenerateRequest, 'send').rejects(thrownError) }) it('calls HandleErroredBillRunService with appropriate error code', async () => { - await GenerateBillRunService.go(billRunDetails.id) + await GenerateTwoPartTariffBillRunService.go(billRunDetails.id) await setTimeout(delay) @@ -241,7 +241,7 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { }) it('logs the error', async () => { - await GenerateBillRunService.go(billRunDetails.id) + await GenerateTwoPartTariffBillRunService.go(billRunDetails.id) await setTimeout(delay) From 6986db16679a178d56eb236a08f497b447cc137f Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Fri, 28 Feb 2025 18:59:10 +0000 Subject: [PATCH 4/6] Add stub service for tpt supplementary This is where all the work of a billing engine happens. That change is out of scope for what we are doing in this change. So, we'll fill it out later. For now, this allows us to confirm that our changes to the generate service are working as expected, and it knows which process billing period service to call. --- .../process-billing-period.service.js | 23 +++++++++++++++++++ .../process-billing-period.service.test.js | 19 +++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 app/services/bill-runs/tpt-supplementary/process-billing-period.service.js create mode 100644 test/services/bill-runs/tpt-supplementary/process-billing-period.service.test.js diff --git a/app/services/bill-runs/tpt-supplementary/process-billing-period.service.js b/app/services/bill-runs/tpt-supplementary/process-billing-period.service.js new file mode 100644 index 0000000000..56657b3fde --- /dev/null +++ b/app/services/bill-runs/tpt-supplementary/process-billing-period.service.js @@ -0,0 +1,23 @@ +'use strict' + +/** + * Process the billing accounts for a given billing period and creates their supplementary two-part tariff bills + * @module ProcessBillingPeriodService + */ + +/** + * Process the billing accounts for a given billing period and creates their supplementary two-part tariff bills + * + * @param {module:BillRunModel} _billRun - The two-part tariff supplementary bill run we need to process + * @param {object} _billingPeriod - An object representing the financial year the bills will be for + * @param {module:BillingAccountModel[]} _billingAccounts - The billing accounts to create bills for + * + * @returns {Promise} true if the bill run is not empty (there are transactions to bill) else false + */ +async function go(_billRun, _billingPeriod, _billingAccounts) { + throw Error('Not implemented') +} + +module.exports = { + go +} diff --git a/test/services/bill-runs/tpt-supplementary/process-billing-period.service.test.js b/test/services/bill-runs/tpt-supplementary/process-billing-period.service.test.js new file mode 100644 index 0000000000..c29424d04e --- /dev/null +++ b/test/services/bill-runs/tpt-supplementary/process-billing-period.service.test.js @@ -0,0 +1,19 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') + +const { describe, it } = (exports.lab = Lab.script()) +const { expect } = Code + +// Thing under test +const ProcessBillingPeriodService = require('../../../../app/services/bill-runs/tpt-supplementary/process-billing-period.service.js') + +describe('Bill Runs - Two-part Tariff - Process Billing Period service', () => { + describe('when the service is called', () => { + it('throws an error', async () => { + await expect(ProcessBillingPeriodService.go()).to.reject() + }) + }) +}) From e11d93fe6161bd7d9cc43aa6579e1c22b3bce03b Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Fri, 28 Feb 2025 19:00:57 +0000 Subject: [PATCH 5/6] Housekeeping - highlight this is for annual --- .../bill-runs/two-part-tariff/process-billing-period.service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/bill-runs/two-part-tariff/process-billing-period.service.js b/app/services/bill-runs/two-part-tariff/process-billing-period.service.js index e5162137c3..1ca214c570 100644 --- a/app/services/bill-runs/two-part-tariff/process-billing-period.service.js +++ b/app/services/bill-runs/two-part-tariff/process-billing-period.service.js @@ -21,7 +21,7 @@ const BillingConfig = require('../../../../config/billing.config.js') /** * Process the billing accounts for a given billing period and creates their annual two-part tariff bills * - * @param {module:BillRunModel} billRun - The two-part tariff bill run we need to process + * @param {module:BillRunModel} billRun - The two-part tariff annual bill run we need to process * @param {object} billingPeriod - An object representing the financial year the bills will be for * @param {module:BillingAccountModel[]} billingAccounts - The billing accounts to create bills for * From 16c6ce28d03721147eba71503facfabee85d49cd Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Sat, 1 Mar 2025 13:45:54 +0000 Subject: [PATCH 6/6] Add logic to Generate service to select processor --- ...nerate-two-part-tariff-bill-run.service.js | 8 +++- ...e-two-part-tariff-bill-run.service.test.js | 47 +++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/app/services/bill-runs/generate-two-part-tariff-bill-run.service.js b/app/services/bill-runs/generate-two-part-tariff-bill-run.service.js index f0f00f959e..2944e89848 100644 --- a/app/services/bill-runs/generate-two-part-tariff-bill-run.service.js +++ b/app/services/bill-runs/generate-two-part-tariff-bill-run.service.js @@ -14,6 +14,7 @@ const FetchTwoPartTariffBillingAccountsService = require('./fetch-two-part-tarif const HandleErroredBillRunService = require('./handle-errored-bill-run.service.js') const LegacyRefreshBillRunRequest = require('../../requests/legacy/refresh-bill-run.request.js') const ProcessAnnualBillingPeriodService = require('./two-part-tariff/process-billing-period.service.js') +const ProcessSupplementaryBillingPeriodService = require('./tpt-supplementary/process-billing-period.service.js') /** * Generates a two-part tariff bill run after the users have completed reviewing its match & allocate results @@ -132,7 +133,12 @@ async function _processBillingPeriod(billingPeriod, billRun) { const billingAccounts = await _fetchBillingAccounts(billRunId) - const billRunPopulated = await ProcessAnnualBillingPeriodService.go(billRun, billingPeriod, billingAccounts) + let billRunPopulated = false + if (billRun.batchType === 'two_part_tariff') { + billRunPopulated = await ProcessAnnualBillingPeriodService.go(billRun, billingPeriod, billingAccounts) + } else { + billRunPopulated = await ProcessSupplementaryBillingPeriodService.go(billRun, billingPeriod, billingAccounts) + } await _finaliseBillRun(billRun, billRunPopulated) } diff --git a/test/services/bill-runs/generate-two-part-tariff-bill-run.service.test.js b/test/services/bill-runs/generate-two-part-tariff-bill-run.service.test.js index 20814505b3..b57f209c11 100644 --- a/test/services/bill-runs/generate-two-part-tariff-bill-run.service.test.js +++ b/test/services/bill-runs/generate-two-part-tariff-bill-run.service.test.js @@ -21,6 +21,7 @@ const FetchTwoPartTariffBillingAccountsService = require('../../../app/services/ const HandleErroredBillRunService = require('../../../app/services/bill-runs/handle-errored-bill-run.service.js') const LegacyRefreshBillRunRequest = require('../../../app/requests/legacy/refresh-bill-run.request.js') const ProcessAnnualBillingPeriodService = require('../../../app/services/bill-runs/two-part-tariff/process-billing-period.service.js') +const ProcessSupplementaryBillingPeriodService = require('../../../app/services/bill-runs/tpt-supplementary/process-billing-period.service.js') // Thing under test const GenerateTwoPartTariffBillRunService = require('../../../app/services/bill-runs/generate-two-part-tariff-bill-run.service.js') @@ -41,6 +42,8 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { let billRunPatchStub let billRunSelectStub + let processAnnualStub + let processSupplementaryStub let notifierStub beforeEach(async () => { @@ -53,6 +56,9 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { select: billRunSelectStub }) + processAnnualStub = Sinon.stub(ProcessAnnualBillingPeriodService, 'go') + processSupplementaryStub = Sinon.stub(ProcessSupplementaryBillingPeriodService, 'go') + // BaseRequest depends on the GlobalNotifier to have been set. This happens in app/plugins/global-notifier.plugin.js // when the app starts up and the plugin is registered. As we're not creating an instance of Hapi server in this // test we recreate the condition by setting it directly with our own stub @@ -87,9 +93,44 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { }) describe('and the bill run is in review', () => { + describe('and is a "two part tariff annual"', () => { + beforeEach(async () => { + billRunSelectStub.resolves({ ...billRunDetails }) + processAnnualStub.resolves(false) + processSupplementaryStub.resolves(false) + }) + + it('triggers the "process annual" service', async () => { + await GenerateTwoPartTariffBillRunService.go(billRunDetails.id) + + await setTimeout(delay) + + expect(processAnnualStub.calledOnce).to.be.true() + expect(processSupplementaryStub.called).to.be.false() + }) + }) + + describe('and is a "two part tariff annual"', () => { + beforeEach(async () => { + billRunSelectStub.resolves({ ...billRunDetails, batchType: 'two_part_supplementary' }) + processAnnualStub.resolves(false) + processSupplementaryStub.resolves(false) + }) + + it('triggers the "process supplementary" service', async () => { + await GenerateTwoPartTariffBillRunService.go(billRunDetails.id) + + await setTimeout(delay) + + expect(processSupplementaryStub.calledOnce).to.be.true() + expect(processAnnualStub.called).to.be.false() + }) + }) + describe('but there is nothing to bill', () => { beforeEach(async () => { billRunSelectStub.resolves({ ...billRunDetails }) + processAnnualStub.resolves(false) }) it('sets the bill run status first to "processing" and then to "empty"', async () => { @@ -113,7 +154,7 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { chargingModuleGenerateRequestStub = Sinon.stub(ChargingModuleGenerateRequest, 'send') legacyRefreshBillRunRequestStub = Sinon.stub(LegacyRefreshBillRunRequest, 'send') - Sinon.stub(ProcessAnnualBillingPeriodService, 'go').resolves(true) + processAnnualStub.resolves(true) }) it('sets the bill run status to "processing"', async () => { @@ -191,7 +232,7 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { thrownError = new BillRunError(new Error(), BillRunModel.errorCodes.failedToPrepareTransactions) Sinon.stub(FetchTwoPartTariffBillingAccountsService, 'go').resolves([]) - Sinon.stub(ProcessAnnualBillingPeriodService, 'go').rejects(thrownError) + processAnnualStub.rejects(thrownError) }) it('calls HandleErroredBillRunService with appropriate error code', async () => { @@ -226,7 +267,7 @@ describe('Bill Runs - Two Part Tariff - Generate Bill Run Service', () => { thrownError = new Error('ERROR') Sinon.stub(FetchTwoPartTariffBillingAccountsService, 'go').resolves([]) - Sinon.stub(ProcessAnnualBillingPeriodService, 'go').resolves(true) + processAnnualStub.resolves(true) Sinon.stub(ChargingModuleGenerateRequest, 'send').rejects(thrownError) })