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

Fix send bill run unflagging licences #884

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/services/bill-runs/submit-send-bill-run.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* Orchestrates the sending of a bill run
* @module SubmitCancelBillRunService
* @module SubmitSendBillRunService
*/

const BillModel = require('../../models/bill.model.js')
Expand Down Expand Up @@ -63,6 +63,7 @@ async function _fetchBillRun (id) {
.findById(id)
.select([
'id',
'batchType',
'createdAt',
'externalId',
'regionId',
Expand Down Expand Up @@ -94,7 +95,9 @@ async function _sendBillRun (billRun) {

await _updateBillRunData(billRun, externalBillRun)

await UnflagBilledLicencesService.go(billRun)
if (billRun.batchType === 'supplementary') {
await UnflagBilledLicencesService.go(billRun)
}

calculateAndLogTimeTaken(startTime, 'Send bill run complete', { billRunId })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* Unflag all licences in a bill run that resulted in a billing invoice (they are billed)
* @module UnflagUnbilledLicencesService
* @module UnflagBilledLicencesService
*/

const LicenceModel = require('../../../models/licence.model.js')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ const ExpandedError = require('../../../app/errors/expanded.error.js')
// Things we need to stub
const ChargingModuleSendBillRunRequest = require('../../../app/requests/charging-module/send-bill-run.request.js')
const ChargingModuleViewBillRunRequest = require('../../../app/requests/charging-module/view-bill-run.request.js')
const UnflagBilledLicencesService = require('../../../app/services/bill-runs/supplementary/unflag-billed-licences.service.js')

// Thing under test
const SubmitSendBillBunService = require('../../../app/services/bill-runs/submit-send-bill-run.service.js')

describe('Submit Cancel Bill Run service', () => {
describe('Submit Send Bill Run service', () => {
// NOTE: introducing a delay in the tests is not ideal. But the service is written such that the send happens in the
// background and is not awaited. We want to confirm things like the records have been updated. But the only way to do
// so is to give the background process time to complete.
Expand All @@ -32,12 +33,14 @@ describe('Submit Cancel Bill Run service', () => {
let chargingModuleSendBillRunRequestStub
let chargingModuleViewBillRunRequestStub
let notifierStub
let unflagBilledLicencesServiceStub

beforeEach(async () => {
await DatabaseSupport.clean()

chargingModuleSendBillRunRequestStub = Sinon.stub(ChargingModuleSendBillRunRequest, 'send').resolves()
chargingModuleViewBillRunRequestStub = Sinon.stub(ChargingModuleViewBillRunRequest, 'send')
unflagBilledLicencesServiceStub = Sinon.stub(UnflagBilledLicencesService, 'go').resolves()

// The service depends on 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
Expand Down Expand Up @@ -123,6 +126,34 @@ describe('Submit Cancel Bill Run service', () => {
expect(logDataArg.timeTakenSs).to.exist()
expect(logDataArg.billRunId).to.exist()
})

describe("when the bill run's batch type is 'supplementary'", () => {
it('removes the SROC supplementary billing flag from those licences billed', async () => {
await SubmitSendBillBunService.go(billRun.id)

await setTimeout(delay)

expect(unflagBilledLicencesServiceStub.called).to.be.true()
})
})

describe("when the bill run's batch type is not 'supplementary'", () => {
let annualBillRun

beforeEach(async () => {
annualBillRun = await BillRunHelper.add({
batchType: 'annual', externalId: '76ed78bd-c104-4ad7-8842-4b660df02331', status: 'ready'
})
})

it('leaves the SROC supplementary billing flag for those licences billed', async () => {
await SubmitSendBillBunService.go(annualBillRun.id)

await setTimeout(delay)

expect(unflagBilledLicencesServiceStub.called).to.be.false()
})
})
})

describe('but the request to view the Charging Module bill run fails', () => {
Expand Down
Loading