diff --git a/app/errors/no-billing-periods.error.js b/app/errors/no-billing-periods.error.js new file mode 100644 index 0000000000..6f085d88e1 --- /dev/null +++ b/app/errors/no-billing-periods.error.js @@ -0,0 +1,13 @@ +'use strict' + +class NoBillingPeriodsError extends Error { + constructor (year = null) { + super( + "No billing periods could be determined. Perhaps you are trying to create a supplementary in an environment where the last annual doesn't exist or was in a PRESROC year." + ) + + this.year = year + } +} + +module.exports = NoBillingPeriodsError diff --git a/app/services/bill-runs/start-bill-run-process.service.js b/app/services/bill-runs/start-bill-run-process.service.js index 31dd04b842..7d4701330f 100644 --- a/app/services/bill-runs/start-bill-run-process.service.js +++ b/app/services/bill-runs/start-bill-run-process.service.js @@ -9,6 +9,7 @@ const AnnualProcessBillRunService = require('./annual/process-bill-run.service.j const DetermineBillingPeriodsService = require('./determine-billing-periods.service.js') const CreateBillRunPresenter = require('../../presenters/bill-runs/create-bill-run.presenter.js') const InitiateBillRunService = require('./initiate-bill-run.service.js') +const NoBillingPeriodsError = require('../../errors/no-billing-periods.error.js') const SupplementaryProcessBillRunService = require('./supplementary/process-bill-run.service.js') const TwoPartTariffProcessBillRunService = require('./two-part-tariff/process-bill-run.service.js') @@ -24,8 +25,12 @@ const TwoPartTariffProcessBillRunService = require('./two-part-tariff/process-bi */ async function go (regionId, batchType, userEmail, financialYearEnding) { const billingPeriods = DetermineBillingPeriodsService.go(batchType, financialYearEnding) - const financialYearEndings = _financialYearEndings(billingPeriods) + if (billingPeriods.length === 0) { + throw new NoBillingPeriodsError(financialYearEnding) + } + + const financialYearEndings = _financialYearEndings(billingPeriods) const billRun = await InitiateBillRunService.go(financialYearEndings, regionId, batchType, userEmail) _processBillRun(billRun, billingPeriods) diff --git a/test/services/bill-runs/start-bill-run-process.service.test.js b/test/services/bill-runs/start-bill-run-process.service.test.js index a6bcb04da2..18fc9afe28 100644 --- a/test/services/bill-runs/start-bill-run-process.service.test.js +++ b/test/services/bill-runs/start-bill-run-process.service.test.js @@ -8,6 +8,9 @@ const Sinon = require('sinon') const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() const { expect } = Code +// Test helpers +const NoBillingPeriodsError = require('../../../app/errors/no-billing-periods.error.js') + // Things we need to stub const AnnualProcessBillRunService = require('../../../app/services/bill-runs/annual/process-bill-run.service.js') const DetermineBillingPeriodsService = require('../../../app/services/bill-runs/determine-billing-periods.service.js') @@ -179,12 +182,26 @@ describe('Start Bill Run Process service', () => { }) describe('when calling the service fails', () => { - beforeEach(() => { - Sinon.stub(DetermineBillingPeriodsService, 'go').throws() + describe('because of an unexpected error', () => { + beforeEach(() => { + Sinon.stub(DetermineBillingPeriodsService, 'go').throws() + }) + + it('throws an error', async () => { + await expect(StartBillRunProcessService.go(regionId, userEmail)).to.reject() + }) }) - it('throws an error', async () => { - await expect(StartBillRunProcessService.go(regionId, userEmail)).to.reject() + describe('because no billing periods could be determined', () => { + beforeEach(() => { + Sinon.stub(DetermineBillingPeriodsService, 'go').returns([]) + }) + + it('throws a NoBillingPeriodsError', async () => { + const result = await expect(StartBillRunProcessService.go(regionId, userEmail)).to.reject() + + expect(result).to.be.instanceOf(NoBillingPeriodsError) + }) }) }) })