diff --git a/app/services/bill-runs/check-live-bill-run.service.js b/app/services/bill-runs/check-live-bill-run.service.js deleted file mode 100644 index 438aacd9a5..0000000000 --- a/app/services/bill-runs/check-live-bill-run.service.js +++ /dev/null @@ -1,53 +0,0 @@ -'use strict' - -/** - * Checks whether a "live" bill run exists for the specified region, scheme, type and financial year - * @module CheckLiveBillRunService - */ - -const BillRunModel = require('../../models/bill-run.model.js') - -const LIVE_STATUSES = ['processing', 'ready', 'review', 'queued'] - -/** - * Check whether a "live" bill run exists for the specified region, scheme, type and financial year - * - * We define "live" as having the status `processing`, `ready`, `review` or `queued`. In the case of annual this also - * includes `sent` and `sending`. This is because there should only ever be one annual bill run per region and - * financial year. - * - * The process is that an annual bill run is generated at the start of the financial year then multiple supplementary - * bill runs to deal with any changes after the annual bill run has been processed. - * - * @param {String} regionId - The UUID of the region to be checked - * @param {Number} toFinancialYearEnding - The financial year to be checked - * @param {String} batchType - The bill run type to be checked - * - * @returns {Promise} true if a live bill run is found else false - */ -async function go (regionId, toFinancialYearEnding, batchType) { - const statuses = [...LIVE_STATUSES] - - // Only one annual bill run per region and financial year is allowed. So, we include sent and sending in the statues - // to check for - if (batchType === 'annual') { - statuses.push('sent', 'sending') - } - - const numberOfLiveBillRuns = await BillRunModel.query() - .select(1) - .where({ - regionId, - toFinancialYearEnding, - batchType, - scheme: 'sroc' - }) - .whereIn('status', statuses) - .resultSize() - - return numberOfLiveBillRuns !== 0 -} - -module.exports = { - go -} diff --git a/app/services/bill-runs/setup/determine-blocking-bill-run.service.js b/app/services/bill-runs/determine-blocking-bill-run.service.js similarity index 99% rename from app/services/bill-runs/setup/determine-blocking-bill-run.service.js rename to app/services/bill-runs/determine-blocking-bill-run.service.js index 756d8dc62b..83d4ed2a95 100644 --- a/app/services/bill-runs/setup/determine-blocking-bill-run.service.js +++ b/app/services/bill-runs/determine-blocking-bill-run.service.js @@ -2,7 +2,7 @@ /** * Determines if an existing bill run will block the one a user is trying to setup - * @module ExistsService + * @module DetermineBlockingBillRunService */ const FetchLiveBillRunsService = require('./fetch-live-bill-runs.service.js') diff --git a/app/services/bill-runs/setup/fetch-live-bill-runs.service.js b/app/services/bill-runs/fetch-live-bill-runs.service.js similarity index 98% rename from app/services/bill-runs/setup/fetch-live-bill-runs.service.js rename to app/services/bill-runs/fetch-live-bill-runs.service.js index f0cf4da3de..384f89a1af 100644 --- a/app/services/bill-runs/setup/fetch-live-bill-runs.service.js +++ b/app/services/bill-runs/fetch-live-bill-runs.service.js @@ -5,7 +5,7 @@ * @module FetchBillRunsForRegionService */ -const BillRunModel = require('../../../models/bill-run.model.js') +const BillRunModel = require('../../models/bill-run.model.js') const LAST_PRESROC_YEAR = 2022 diff --git a/app/services/bill-runs/setup/fetch-matching-bill-run.service.js b/app/services/bill-runs/fetch-matching-bill-run.service.js similarity index 98% rename from app/services/bill-runs/setup/fetch-matching-bill-run.service.js rename to app/services/bill-runs/fetch-matching-bill-run.service.js index 7f3b65b014..d9c698bf1e 100644 --- a/app/services/bill-runs/setup/fetch-matching-bill-run.service.js +++ b/app/services/bill-runs/fetch-matching-bill-run.service.js @@ -5,7 +5,7 @@ * @module ExistsService */ -const BillRunModel = require('../../../models/bill-run.model.js') +const BillRunModel = require('../../models/bill-run.model.js') /** * Fetch bill run(s) that match the options provided diff --git a/app/services/bill-runs/initiate-bill-run.service.js b/app/services/bill-runs/initiate-bill-run.service.js index 8ca7fe1d7a..a4a8e52e65 100644 --- a/app/services/bill-runs/initiate-bill-run.service.js +++ b/app/services/bill-runs/initiate-bill-run.service.js @@ -7,9 +7,9 @@ const BillRunModel = require('../../models/bill-run.model.js') const ChargingModuleCreateBillRunRequest = require('../../requests/charging-module/create-bill-run.request.js') -const CheckLiveBillRunService = require('./check-live-bill-run.service.js') const CreateBillRunService = require('./create-bill-run.service.js') const CreateBillRunEventService = require('./create-bill-run-event.service.js') +const DetermineBlockingBillRunService = require('./determine-blocking-bill-run.service.js') const ExpandedError = require('../../errors/expanded.error.js') /** @@ -26,11 +26,7 @@ const ExpandedError = require('../../errors/expanded.error.js') * @returns {Promise} The newly created bill run instance */ 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 }) - } + await _billRunBlocked(regionId, batchType, financialYearEndings.toFinancialYearEnding) const chargingModuleResult = await ChargingModuleCreateBillRunRequest.send(regionId, 'sroc') @@ -61,6 +57,23 @@ function _billRunOptions (chargingModuleResult, batchType) { return options } +async function _billRunBlocked (regionId, batchType, financialEndYear) { + const matchResults = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) + + // No matches so we can create the bill run + if (matchResults.length === 0) { + return + } + + // You can only have one SROC and PRESROC supplementary being processed at any time. If less than 2 then we can create + // a bill run + if (batchType === 'supplementary' && matchResults.length < 2) { + return + } + + throw new ExpandedError('Batch already live for region', { billRunId: matchResults[0].id }) +} + module.exports = { go } diff --git a/app/services/bill-runs/setup/exists.service.js b/app/services/bill-runs/setup/exists.service.js index c7bb6a0e94..bf7cecd7c5 100644 --- a/app/services/bill-runs/setup/exists.service.js +++ b/app/services/bill-runs/setup/exists.service.js @@ -6,7 +6,7 @@ */ const CreatePresenter = require('../../../presenters/bill-runs/setup/create.presenter.js') -const DetermineBlockingBillRunService = require('./determine-blocking-bill-run.service.js') +const DetermineBlockingBillRunService = require('../determine-blocking-bill-run.service.js') const SessionModel = require('../../../models/session.model.js') const { determineCurrentFinancialYear } = require('../../../lib/general.lib.js') diff --git a/test/services/bill-runs/check-live-bill-run.service.test.js b/test/services/bill-runs/check-live-bill-run.service.test.js deleted file mode 100644 index bfa1516c3a..0000000000 --- a/test/services/bill-runs/check-live-bill-run.service.test.js +++ /dev/null @@ -1,114 +0,0 @@ -'use strict' - -// Test framework dependencies -const Lab = require('@hapi/lab') -const Code = require('@hapi/code') - -const { describe, it, beforeEach } = exports.lab = Lab.script() -const { expect } = Code - -// Test helpers -const BillRunHelper = require('../../support/helpers/bill-run.helper.js') -const DatabaseSupport = require('../../support/database.js') -const { determineCurrentFinancialYear } = require('../../../app/lib/general.lib.js') - -// Thing under test -const CheckLiveBillRunService = require('../../../app/services/bill-runs/check-live-bill-run.service.js') - -describe('Check Live Bill Run service', () => { - const billingPeriod = determineCurrentFinancialYear() - const regionId = '6ec2f8b5-70e2-4abf-8ba9-026971d9de52' - const toFinancialYearEnding = billingPeriod.endDate.getFullYear() - - beforeEach(async () => { - await DatabaseSupport.clean() - }) - - describe('when an sroc supplementary bill run exists for this region and financial year', () => { - const batchType = 'supplementary' - - describe('with a status considered to be "live" (queued)', () => { - beforeEach(async () => { - await BillRunHelper.add({ batchType, regionId, toFinancialYearEnding, status: 'queued' }) - }) - - it('returns `true`', async () => { - const result = await CheckLiveBillRunService.go(regionId, toFinancialYearEnding, batchType) - - expect(result).to.be.true() - }) - }) - - describe('with a status not considered to be "live" (sent)', () => { - beforeEach(async () => { - await BillRunHelper.add({ batchType, regionId, toFinancialYearEnding, status: 'sent' }) - }) - - it('returns `false`', async () => { - const result = await CheckLiveBillRunService.go(regionId, toFinancialYearEnding, batchType) - - expect(result).to.be.false() - }) - }) - }) - - describe('when an sroc annual bill run exists for this region and financial year', () => { - const batchType = 'annual' - - describe('with a status considered to be "live" (sent)', () => { - beforeEach(async () => { - await BillRunHelper.add({ batchType, regionId, toFinancialYearEnding, status: 'sent' }) - }) - - it('returns `true`', async () => { - const result = await CheckLiveBillRunService.go(regionId, toFinancialYearEnding, batchType) - - expect(result).to.be.true() - }) - }) - - describe('with a status not considered to be "live" (empty)', () => { - beforeEach(async () => { - await BillRunHelper.add({ batchType, regionId, toFinancialYearEnding, status: 'empty' }) - }) - - it('returns `false`', async () => { - const result = await CheckLiveBillRunService.go(regionId, toFinancialYearEnding, batchType) - - expect(result).to.be.false() - }) - }) - }) - - describe('when an sroc bill run does not exist', () => { - const batchType = 'annual' - - beforeEach(async () => { - await BillRunHelper.add({ batchType, regionId, toFinancialYearEnding }) - }) - - describe('for this region', () => { - it('returns `false`', async () => { - const result = await CheckLiveBillRunService.go('4f142d8d-ca04-48bc-8d03-31dcd05c7070', toFinancialYearEnding, batchType) - - expect(result).to.be.false() - }) - }) - - describe('for this financialYear', () => { - it('returns `false`', async () => { - const result = await CheckLiveBillRunService.go(regionId, 2023, batchType) - - expect(result).to.be.false() - }) - }) - - describe('for this batch type', () => { - it('returns `false`', async () => { - const result = await CheckLiveBillRunService.go(regionId, toFinancialYearEnding, 'supplementary') - - expect(result).to.be.false() - }) - }) - }) -}) diff --git a/test/services/bill-runs/setup/determine-blocking-bill-run.service.test.js b/test/services/bill-runs/determine-blocking-bill-run.service.test.js similarity index 96% rename from test/services/bill-runs/setup/determine-blocking-bill-run.service.test.js rename to test/services/bill-runs/determine-blocking-bill-run.service.test.js index 09908b7718..9c8053d386 100644 --- a/test/services/bill-runs/setup/determine-blocking-bill-run.service.test.js +++ b/test/services/bill-runs/determine-blocking-bill-run.service.test.js @@ -8,15 +8,15 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const BillRunHelper = require('../../../support/helpers/bill-run.helper.js') -const DatabaseSupport = require('../../../support/database.js') -const { determineCurrentFinancialYear } = require('../../../../app/lib/general.lib.js') -const RegionHelper = require('../../../support/helpers/region.helper.js') +const BillRunHelper = require('../../support/helpers/bill-run.helper.js') +const DatabaseSupport = require('../../support/database.js') +const { determineCurrentFinancialYear } = require('../../../app/lib/general.lib.js') +const RegionHelper = require('../../support/helpers/region.helper.js') // Thing under test -const DetermineBlockingBillRunService = require('../../../../app/services/bill-runs/setup/determine-blocking-bill-run.service.js') +const DetermineBlockingBillRunService = require('../../../app/services/bill-runs/determine-blocking-bill-run.service.js') -describe('Bill Runs Setup Determine Blocking Bill Run service', () => { +describe('Determine Blocking Bill Run service', () => { let batchType let financialEndYear let regionId diff --git a/test/services/bill-runs/setup/fetch-live-bill-runs.service.test.js b/test/services/bill-runs/fetch-live-bill-runs.service.test.js similarity index 92% rename from test/services/bill-runs/setup/fetch-live-bill-runs.service.test.js rename to test/services/bill-runs/fetch-live-bill-runs.service.test.js index 1ad73b1cd3..5b7270b09e 100644 --- a/test/services/bill-runs/setup/fetch-live-bill-runs.service.test.js +++ b/test/services/bill-runs/fetch-live-bill-runs.service.test.js @@ -8,14 +8,14 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const BillRunHelper = require('../../../support/helpers/bill-run.helper.js') -const DatabaseSupport = require('../../../support/database.js') -const RegionHelper = require('../../../support/helpers/region.helper.js') +const BillRunHelper = require('../../support/helpers/bill-run.helper.js') +const DatabaseSupport = require('../../support/database.js') +const RegionHelper = require('../../support/helpers/region.helper.js') // Thing under test -const FetchLiveBillRunsService = require('../../../../app/services/bill-runs/setup/fetch-live-bill-runs.service.js') +const FetchLiveBillRunsService = require('../../../app/services/bill-runs/fetch-live-bill-runs.service.js') -describe('Bill Runs Setup Fetch Live Bill Runs service', () => { +describe('Fetch Live Bill Runs service', () => { let financialYearEnding let regionId diff --git a/test/services/bill-runs/setup/fetch-matching-bill-run.service.test.js b/test/services/bill-runs/fetch-matching-bill-run.service.test.js similarity index 93% rename from test/services/bill-runs/setup/fetch-matching-bill-run.service.test.js rename to test/services/bill-runs/fetch-matching-bill-run.service.test.js index 95ab968a9f..447277bde2 100644 --- a/test/services/bill-runs/setup/fetch-matching-bill-run.service.test.js +++ b/test/services/bill-runs/fetch-matching-bill-run.service.test.js @@ -8,14 +8,14 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const BillRunHelper = require('../../../support/helpers/bill-run.helper.js') -const DatabaseSupport = require('../../../support/database.js') -const RegionHelper = require('../../../support/helpers/region.helper.js') +const BillRunHelper = require('../../support/helpers/bill-run.helper.js') +const DatabaseSupport = require('../../support/database.js') +const RegionHelper = require('../../support/helpers/region.helper.js') // Thing under test -const FetchMatchingBillRunService = require('../../../../app/services/bill-runs/setup/fetch-matching-bill-run.service.js') +const FetchMatchingBillRunService = require('../../../app/services/bill-runs/fetch-matching-bill-run.service.js') -describe('Bill Runs Setup Fetch Matching Bill Run service', () => { +describe('Fetch Matching Bill Run service', () => { let matchingBillRunId let regionId diff --git a/test/services/bill-runs/initiate-bill-run.service.test.js b/test/services/bill-runs/initiate-bill-run.service.test.js index 34376fecd4..966d7321ab 100644 --- a/test/services/bill-runs/initiate-bill-run.service.test.js +++ b/test/services/bill-runs/initiate-bill-run.service.test.js @@ -16,16 +16,17 @@ const RegionHelper = require('../../support/helpers/region.helper.js') // Things we need to stub const ChargingModuleCreateBillRunRequest = require('../../../app/requests/charging-module/create-bill-run.request.js') -const CheckLiveBillRunService = require('../../../app/services/bill-runs/check-live-bill-run.service.js') +const DetermineBlockingBillRunService = require('../../../app/services/bill-runs/determine-blocking-bill-run.service.js') const SupplementaryProcessBillRunService = require('../../../app/services/bill-runs/supplementary/process-bill-run.service.js') // Thing under test const InitiateBillRunService = require('../../../app/services/bill-runs/initiate-bill-run.service.js') describe('Initiate Bill Run service', () => { - const batchType = 'supplementary' const financialYearEndings = { fromFinancialYearEnding: 2023, toFinancialYearEnding: 2024 } const user = 'test.user@defra.gov.uk' + + let batchType let regionId beforeEach(async () => { @@ -34,8 +35,6 @@ describe('Initiate Bill Run service', () => { const region = await RegionHelper.add() regionId = region.id - Sinon.stub(CheckLiveBillRunService, 'go').resolves(false) - // The InitiateBillRun service does not await the call to the ProcessBillRunService. It is intended to // kick of the process and then move on. This is why we simply stub it in the tests. Sinon.stub(SupplementaryProcessBillRunService, 'go') @@ -54,6 +53,10 @@ describe('Initiate Bill Run service', () => { } beforeEach(() => { + batchType = 'supplementary' + + Sinon.stub(DetermineBlockingBillRunService, 'go').resolves([]) + Sinon.stub(ChargingModuleCreateBillRunRequest, 'send').resolves({ succeeded: true, response: { @@ -101,6 +104,8 @@ describe('Initiate Bill Run service', () => { describe('when initiating a bill run fails', () => { describe('because a bill run could not be created in the Charging Module', () => { beforeEach(() => { + Sinon.stub(DetermineBlockingBillRunService, 'go').resolves([]) + Sinon.stub(ChargingModuleCreateBillRunRequest, 'send').resolves({ succeeded: false, response: { @@ -132,9 +137,11 @@ describe('Initiate Bill Run service', () => { }) }) - describe('because a bill run already exists for this region, financial year and type', () => { + describe('because a live bill run already exists for this region, financial year and type', () => { beforeEach(() => { - CheckLiveBillRunService.go.resolves(true) + batchType = 'annual' + + Sinon.stub(DetermineBlockingBillRunService, 'go').resolves([{ id: 'becf430d-f6dd-45a3-b943-42683f7bb889' }]) }) it('rejects with an appropriate error', async () => { @@ -142,7 +149,7 @@ describe('Initiate Bill Run service', () => { expect(err).to.be.an.error() expect(err.message).to.equal('Batch already live for region') - expect(err.regionId).to.equal(regionId) + expect(err.billRunId).to.equal('becf430d-f6dd-45a3-b943-42683f7bb889') }) }) }) diff --git a/test/services/bill-runs/setup/exists.service.test.js b/test/services/bill-runs/setup/exists.service.test.js index 13dfaf23c4..1f287fc486 100644 --- a/test/services/bill-runs/setup/exists.service.test.js +++ b/test/services/bill-runs/setup/exists.service.test.js @@ -14,7 +14,7 @@ const { determineCurrentFinancialYear } = require('../../../../app/lib/general.l const SessionHelper = require('../../../support/helpers/session.helper.js') // Things we need to stub -const DetermineBlockingBillRunService = require('../../../../app/services/bill-runs/setup/determine-blocking-bill-run.service.js') +const DetermineBlockingBillRunService = require('../../../../app/services/bill-runs/determine-blocking-bill-run.service.js') // Thing under test const ExistsService = require('../../../../app/services/bill-runs/setup/exists.service.js')