From 01a63e75c604d255cb189c7f4d435a8e8d60c1eb Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Tue, 14 May 2024 14:40:56 +0100 Subject: [PATCH] Fix unsent bills displaying in licence bills tab (#1015) https://eaflood.atlassian.net/browse/WATER-4316 In [Add View Licence Bills page](https://github.com/DEFRA/water-abstraction-system/pull/986) we added support for displaying the bills linked to a licence in the view licence page's 'Bills' tab. Only we didn't spot that it should only be 'sent' bills. This change updates the relevant fetch service to ensure the bills we get back are only those with a status of 'sent'. --- .../licences/fetch-licence-bills.service.js | 6 +- .../fetch-licence-bills.service.test.js | 134 +++++++++++++----- 2 files changed, 99 insertions(+), 41 deletions(-) diff --git a/app/services/licences/fetch-licence-bills.service.js b/app/services/licences/fetch-licence-bills.service.js index 1564df56ec..f9d815a090 100644 --- a/app/services/licences/fetch-licence-bills.service.js +++ b/app/services/licences/fetch-licence-bills.service.js @@ -5,9 +5,9 @@ * @module FetchLicenceBillsService */ -const BillModel = require('../../models/bill.model') +const BillModel = require('../../models/bill.model.js') -const DatabaseConfig = require('../../../config/database.config') +const DatabaseConfig = require('../../../config/database.config.js') /** * Fetches all bills for a licence which is needed for the view '/licences/{id}/bills` page @@ -37,7 +37,9 @@ async function _fetch (licenceId, page) { 'bills.netAmount' ]) .innerJoinRelated('billLicences') + .innerJoin('billRuns', 'billRuns.id', 'bills.billRunId') .where('billLicences.licence_id', licenceId) + .where('billRuns.status', 'sent') .withGraphFetched('billRun') .modifyGraph('billRun', (builder) => { builder.select(['batchType']) diff --git a/test/services/licences/fetch-licence-bills.service.test.js b/test/services/licences/fetch-licence-bills.service.test.js index bd3a495a41..5c28244150 100644 --- a/test/services/licences/fetch-licence-bills.service.test.js +++ b/test/services/licences/fetch-licence-bills.service.test.js @@ -9,68 +9,124 @@ const { expect } = Code // Test helpers const DatabaseSupport = require('../../support/database.js') -const BillLicenceHelper = require('../../support/helpers/bill-licence.helper') -const BillHelper = require('../../support/helpers/bill.helper') -const BillRunHelper = require('../../support/helpers/bill-run.helper') +const BillLicenceHelper = require('../../support/helpers/bill-licence.helper.js') +const BillHelper = require('../../support/helpers/bill.helper.js') +const BillRunHelper = require('../../support/helpers/bill-run.helper.js') // Thing under test -const FetchLicenceBillService = require('../../../app/services/licences/fetch-licence-bills.service') +const FetchLicenceBillService = require('../../../app/services/licences/fetch-licence-bills.service.js') + +describe('Fetch Licence Bills service', () => { + const billId = '72988ec1-9fb2-4b87-b0a0-3c0be628a72c' + const billingAccountId = '0ba3b707-72ee-4296-b177-a19afff10688' + const billRunId = 'd40004d5-a99b-40a7-adf2-22d36d5b20b5' + const createdDate = new Date('2022-01-01') + const licenceId = '96d97293-1a62-4ad0-bcb6-24f68a203e6b' -describe('Fetch licence bills service', () => { beforeEach(async () => { await DatabaseSupport.clean() }) describe('when the licence has bills', () => { - const createdDate = new Date('2022-01-01') - - const licenceId = '96d97293-1a62-4ad0-bcb6-24f68a203e6b' - const billId = '72988ec1-9fb2-4b87-b0a0-3c0be628a72c' - const billingAccountId = '0ba3b707-72ee-4296-b177-a19afff10688' - beforeEach(async () => { await BillLicenceHelper.add({ licenceId, billId }) + }) + + describe("and they are linked to a 'sent' bill run", () => { + beforeEach(async () => { + await BillRunHelper.add({ id: billRunId, status: 'sent' }) + + await BillHelper.add( + { + id: billId, + billRunId, + invoiceNumber: '123', + accountNumber: 'T21404193A', + netAmount: 12345, + billingAccountId, + createdAt: createdDate + }) + + // Add an extra bill linked to the same bill run to test only bills for the licence are retrieved + await BillHelper.add({ billRunId }) + }) + + it('returns results', async () => { + const result = await FetchLicenceBillService.go(licenceId, 1) + + expect(result.pagination).to.equal({ + total: 1 + }) + + expect(result.bills).to.equal( + [{ + accountNumber: 'T21404193A', + billRun: { + batchType: 'supplementary' + }, + billingAccountId, + createdAt: createdDate, + credit: null, + deminimis: false, + financialYearEnding: 2023, + id: billId, + invoiceNumber: '123', + legacyId: null, + netAmount: 12345 + }] + ) + }) + }) + + describe("but they are not linked to a 'sent' bill run", () => { + beforeEach(async () => { + await BillRunHelper.add({ id: billRunId }) + + await BillHelper.add( + { + id: billId, + billRunId, + invoiceNumber: '123', + accountNumber: 'T21404193A', + netAmount: 12345, + billingAccountId, + createdAt: createdDate + }) + + // Add an extra bill linked to the same bill run to test only bills for the licence are retrieved + await BillHelper.add({ billRunId }) + }) - await BillRunHelper.add({ batchType: 'annual' }) + it('returns no results', async () => { + const result = await FetchLicenceBillService.go(licenceId, 1) - await BillHelper.add( - { - id: billId, - invoiceNumber: '123', - accountNumber: 'T21404193A', - netAmount: 12345, - billingAccountId, - createdAt: createdDate + expect(result.pagination).to.equal({ + total: 0 }) - // Add an extra record to ensure the bill is retrieved by the license id - await BillHelper.add() + + expect(result.bills).to.be.empty() + }) + }) + }) + + describe('when the licence has no bills', () => { + beforeEach(async () => { + await BillRunHelper.add({ id: billRunId, status: 'sent' }) + await BillHelper.add({ id: billId, billRunId }) + await BillLicenceHelper.add({ billId }) }) - it('returns results', async () => { + it('returns no results', async () => { const result = await FetchLicenceBillService.go(licenceId, 1) expect(result.pagination).to.equal({ - total: 1 + total: 0 }) - expect(result.bills).to.equal( - [{ - accountNumber: 'T21404193A', - billRun: null, - billingAccountId, - createdAt: createdDate, - credit: null, - deminimis: false, - financialYearEnding: 2023, - id: billId, - invoiceNumber: '123', - legacyId: null, - netAmount: 12345 - }] - ) + expect(result.bills).to.be.empty() }) }) })