From 63ce091c724b13a4f365eeccc3b32e78e503018d Mon Sep 17 00:00:00 2001 From: Rebecca Ransome Date: Wed, 14 Aug 2024 20:35:07 +0100 Subject: [PATCH 1/7] Add create licence supplementary years service https://eaflood.atlassian.net/jira/software/c/projects/WATER/boards/96?selectedIssue=WATER-4588 https://github.com/DEFRA/water-abstraction-system/pull/1236 The new two-part tariff supplementary bill run requires licences to be recorded in our new LicenceSupplementaryYears table, along with the years they affect. The first ticket we picked up for this new process was amending the existing annual two-part tariff review pages. A licence can be removed from the bill run there and this should be flagged. This was added to the existing review pages journey. Since starting work on the other flagging tickets it became clear that the process of adding the licence into our LicenceSupplementaryYears table is going to be repeated if we don't pull this out into its own service. So that is what this PR is for, making a service just for creating the licence record that can be shared amongst the various ways a licence can be flagged. From 476bad071fa9b5d1ee0e6b5275003f2ae2d5ce98 Mon Sep 17 00:00:00 2001 From: Rebecca Ransome Date: Wed, 14 Aug 2024 22:04:35 +0100 Subject: [PATCH 2/7] Add CreateLicenceSupplementaryYearsService and unit tests --- ...eate-licence-supplementary-year.service.js | 50 ++++++++++ ...licence-supplementary-year.service.test.js | 93 +++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 app/services/licences/supplementary/create-licence-supplementary-year.service.js create mode 100644 test/services/licences/supplementary/create-licence-supplementary-year.service.test.js diff --git a/app/services/licences/supplementary/create-licence-supplementary-year.service.js b/app/services/licences/supplementary/create-licence-supplementary-year.service.js new file mode 100644 index 0000000000..b5d5a6bbbf --- /dev/null +++ b/app/services/licences/supplementary/create-licence-supplementary-year.service.js @@ -0,0 +1,50 @@ +'use strict' + +/** + * Creates a licenceSupplementaryYears record based on the provided licence data + * @module CreateLicenceSupplementaryYearService + */ + +const LicenceSupplementaryYearModel = require('../../../models/licence-supplementary-year.model.js') + +/** + * Creates a licenceSupplementaryYears record based on the provided licence data + * + * @param {module:LicenceModel} licenceId - The UUID of the licence to be persisted + * @param {Object[]} years - An array of the years to be persisted as individual records + * @param {Boolean} twoPartTariff - If there are any two-part tariff indicators on the licence + */ +async function go (licenceId, years, twoPartTariff) { + for (const year of years) { + const existingLicenceSupplementaryYears = await _fetchExistingLicenceSupplementaryYears( + licenceId, year, twoPartTariff + ) + + // Create a new record only if no existing record matches all the provided properties, and where 'billRunId' is null + if (existingLicenceSupplementaryYears.length === 0) { + await _persistSupplementaryBillingYearsData(licenceId, year, twoPartTariff) + } + } +} + +async function _fetchExistingLicenceSupplementaryYears (licenceId, year, twoPartTariff) { + return LicenceSupplementaryYearModel.query() + .select('id') + .where('licenceId', licenceId) + .where('financialYearEnd', year) + .where('twoPartTariff', twoPartTariff) + .where('billRunId', null) +} + +async function _persistSupplementaryBillingYearsData (licenceId, year, twoPartTariff) { + return LicenceSupplementaryYearModel.query() + .insert({ + licenceId, + financialYearEnd: year, + twoPartTariff + }) +} + +module.exports = { + go +} diff --git a/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js b/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js new file mode 100644 index 0000000000..3f9f4fe3a4 --- /dev/null +++ b/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js @@ -0,0 +1,93 @@ +'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 { generateUUID } = require('../../../../app/lib/general.lib.js') +const LicenceSupplementaryYearHelper = require('../../../support/helpers/licence-supplementary-year.helper.js') +const LicenceSupplementaryYearModel = require('../../../../app/models/licence-supplementary-year.model.js') + +// Thing under test +const CreateLicenceSupplementaryYearService = require('../../../../app/services/licences/supplementary/create-licence-supplementary-year.service.js') + +describe('Create Licence Supplementary Years Service', () => { + let licenceId + let years + let twoPartTariff + + describe('when provided a licenceId, years and twoPartTariff data', () => { + beforeEach(async () => { + licenceId = generateUUID() + years = [2023] + twoPartTariff = true + }) + + describe('that does not already exist', () => { + it('persists the data', async () => { + await CreateLicenceSupplementaryYearService.go(licenceId, years, twoPartTariff) + + const result = await _fetchLicenceSupplementaryYears(licenceId) + + expect(result).to.have.length(1) + expect(result[0].licenceId).to.equal(licenceId) + }) + }) + + describe('that already exist', () => { + beforeEach(async () => { + await LicenceSupplementaryYearHelper.add({ licenceId, financialYearEnd: 2023, twoPartTariff: true }) + }) + + describe('without the billRunId', () => { + it('does not persist the data', async () => { + await CreateLicenceSupplementaryYearService.go(licenceId, years, twoPartTariff) + + const result = await _fetchLicenceSupplementaryYears(licenceId) + + expect(result).to.have.length(1) + expect(result[0].licenceId).to.equal(licenceId) + }) + }) + + describe('with the billRunId', () => { + let billRunId + + beforeEach(async () => { + billRunId = generateUUID() + + await LicenceSupplementaryYearModel.query() + .update({ billRunId }) + .where('licenceId', licenceId) + .orderBy('billRunId') + }) + + it('persist the data', async () => { + await CreateLicenceSupplementaryYearService.go(licenceId, years, twoPartTariff) + + const result = await _fetchLicenceSupplementaryYears(licenceId) + + expect(result).to.have.length(2) + expect(result[0].billRunId).to.equal(billRunId) + expect(result[1].billRunId).to.equal(null) + }) + }) + }) + }) +}) + +function _fetchLicenceSupplementaryYears (licenceId) { + return LicenceSupplementaryYearModel.query() + .select([ + 'id', + 'licenceId', + 'billRunId', + 'financialYearEnd', + 'twoPartTariff' + ]) + .where('licenceId', licenceId) +} From 4ca6336ec4f98a32922899f54aaedfe85e1c4fa0 Mon Sep 17 00:00:00 2001 From: Rebecca Ransome Date: Thu, 15 Aug 2024 09:21:56 +0100 Subject: [PATCH 3/7] Alter SubmitRemoveBillRunLicenceService to use new CreateLicenceSupplementaryYearsService --- .../submit-remove-bill-run-licence.service.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/app/services/bill-runs/two-part-tariff/submit-remove-bill-run-licence.service.js b/app/services/bill-runs/two-part-tariff/submit-remove-bill-run-licence.service.js index 8f98113ad2..deef185740 100644 --- a/app/services/bill-runs/two-part-tariff/submit-remove-bill-run-licence.service.js +++ b/app/services/bill-runs/two-part-tariff/submit-remove-bill-run-licence.service.js @@ -6,8 +6,8 @@ */ const BillRunModel = require('../../../models/bill-run.model.js') +const CreateLicenceSupplementaryYearService = require('../../licences/supplementary/create-licence-supplementary-year.service.js') const LicenceModel = require('../../../models/licence.model.js') -const LicenceSupplementaryYearModel = require('../../../models/licence-supplementary-year.model.js') const RemoveReviewDataService = require('./remove-review-data.service.js') const ReviewLicenceModel = require('../../../models/review-licence.model.js') @@ -57,15 +57,11 @@ async function _allLicencesRemoved (billRunId) { } async function _flagForSupplementaryBilling (licenceId, billRunId) { + const twoPartTariff = true const { toFinancialYearEnding } = await BillRunModel.query() .findById(billRunId) - return LicenceSupplementaryYearModel.query() - .insert({ - licenceId, - twoPartTariff: true, - financialYearEnd: toFinancialYearEnding - }) + return CreateLicenceSupplementaryYearService.go(licenceId, [toFinancialYearEnding], twoPartTariff) } module.exports = { From 63cb8505040108592fbaa98682a0503fa1571e9b Mon Sep 17 00:00:00 2001 From: Rebecca Ransome Date: Thu, 15 Aug 2024 09:27:58 +0100 Subject: [PATCH 4/7] Shuffle order of variables --- .../create-licence-supplementary-year.service.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js b/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js index 3f9f4fe3a4..25ec264c23 100644 --- a/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js +++ b/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js @@ -17,14 +17,14 @@ const CreateLicenceSupplementaryYearService = require('../../../../app/services/ describe('Create Licence Supplementary Years Service', () => { let licenceId - let years let twoPartTariff + let years describe('when provided a licenceId, years and twoPartTariff data', () => { beforeEach(async () => { licenceId = generateUUID() - years = [2023] twoPartTariff = true + years = [2023] }) describe('that does not already exist', () => { From c588e6a93bc1e20a065dbe2bd0115a9148bfb144 Mon Sep 17 00:00:00 2001 From: Rebecca Ransome Date: Thu, 15 Aug 2024 11:47:30 +0100 Subject: [PATCH 5/7] Update unit test --- .../create-licence-supplementary-year.service.test.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js b/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js index 25ec264c23..80ec18ddc4 100644 --- a/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js +++ b/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js @@ -34,7 +34,12 @@ describe('Create Licence Supplementary Years Service', () => { const result = await _fetchLicenceSupplementaryYears(licenceId) expect(result).to.have.length(1) - expect(result[0].licenceId).to.equal(licenceId) + expect(result[0]).to.equal({ + licenceId, + billRunId: null, + financialYearEnd: years[0], + twoPartTariff + }, { skip: ['id'] }) }) }) From 6a6fb24096211026eb2d8f99eee377c7670abfa3 Mon Sep 17 00:00:00 2001 From: Rebecca Ransome Date: Thu, 15 Aug 2024 11:56:27 +0100 Subject: [PATCH 6/7] Refactor code --- ...eate-licence-supplementary-year.service.js | 26 ++++++++++--------- ...licence-supplementary-year.service.test.js | 12 ++++----- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/app/services/licences/supplementary/create-licence-supplementary-year.service.js b/app/services/licences/supplementary/create-licence-supplementary-year.service.js index b5d5a6bbbf..1931b1d82f 100644 --- a/app/services/licences/supplementary/create-licence-supplementary-year.service.js +++ b/app/services/licences/supplementary/create-licence-supplementary-year.service.js @@ -11,36 +11,38 @@ const LicenceSupplementaryYearModel = require('../../../models/licence-supplemen * Creates a licenceSupplementaryYears record based on the provided licence data * * @param {module:LicenceModel} licenceId - The UUID of the licence to be persisted - * @param {Object[]} years - An array of the years to be persisted as individual records + * @param {Object[]} years - An array of the financial year ends to be persisted as individual records * @param {Boolean} twoPartTariff - If there are any two-part tariff indicators on the licence */ -async function go (licenceId, years, twoPartTariff) { - for (const year of years) { - const existingLicenceSupplementaryYears = await _fetchExistingLicenceSupplementaryYears( - licenceId, year, twoPartTariff - ) +async function go (licenceId, financialYearEnds, twoPartTariff) { + for (const financialYearEnd of financialYearEnds) { + const match = await _fetchExistingLicenceSupplementaryYears(licenceId, financialYearEnd, twoPartTariff) // Create a new record only if no existing record matches all the provided properties, and where 'billRunId' is null - if (existingLicenceSupplementaryYears.length === 0) { - await _persistSupplementaryBillingYearsData(licenceId, year, twoPartTariff) + if (match) { + continue } + + await _persistSupplementaryBillingYearsData(licenceId, financialYearEnd, twoPartTariff) } } -async function _fetchExistingLicenceSupplementaryYears (licenceId, year, twoPartTariff) { +async function _fetchExistingLicenceSupplementaryYears (licenceId, financialYearEnd, twoPartTariff) { return LicenceSupplementaryYearModel.query() .select('id') .where('licenceId', licenceId) - .where('financialYearEnd', year) + .where('financialYearEnd', financialYearEnd) .where('twoPartTariff', twoPartTariff) .where('billRunId', null) + .limit(1) + .first() } -async function _persistSupplementaryBillingYearsData (licenceId, year, twoPartTariff) { +async function _persistSupplementaryBillingYearsData (licenceId, financialYearEnd, twoPartTariff) { return LicenceSupplementaryYearModel.query() .insert({ licenceId, - financialYearEnd: year, + financialYearEnd, twoPartTariff }) } diff --git a/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js b/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js index 80ec18ddc4..7a1356e73a 100644 --- a/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js +++ b/test/services/licences/supplementary/create-licence-supplementary-year.service.test.js @@ -18,18 +18,18 @@ const CreateLicenceSupplementaryYearService = require('../../../../app/services/ describe('Create Licence Supplementary Years Service', () => { let licenceId let twoPartTariff - let years + let financialYearEnds describe('when provided a licenceId, years and twoPartTariff data', () => { beforeEach(async () => { licenceId = generateUUID() twoPartTariff = true - years = [2023] + financialYearEnds = [2023] }) describe('that does not already exist', () => { it('persists the data', async () => { - await CreateLicenceSupplementaryYearService.go(licenceId, years, twoPartTariff) + await CreateLicenceSupplementaryYearService.go(licenceId, financialYearEnds, twoPartTariff) const result = await _fetchLicenceSupplementaryYears(licenceId) @@ -37,7 +37,7 @@ describe('Create Licence Supplementary Years Service', () => { expect(result[0]).to.equal({ licenceId, billRunId: null, - financialYearEnd: years[0], + financialYearEnd: financialYearEnds[0], twoPartTariff }, { skip: ['id'] }) }) @@ -50,7 +50,7 @@ describe('Create Licence Supplementary Years Service', () => { describe('without the billRunId', () => { it('does not persist the data', async () => { - await CreateLicenceSupplementaryYearService.go(licenceId, years, twoPartTariff) + await CreateLicenceSupplementaryYearService.go(licenceId, financialYearEnds, twoPartTariff) const result = await _fetchLicenceSupplementaryYears(licenceId) @@ -72,7 +72,7 @@ describe('Create Licence Supplementary Years Service', () => { }) it('persist the data', async () => { - await CreateLicenceSupplementaryYearService.go(licenceId, years, twoPartTariff) + await CreateLicenceSupplementaryYearService.go(licenceId, financialYearEnds, twoPartTariff) const result = await _fetchLicenceSupplementaryYears(licenceId) From f2a1482d6d2786d4f6e76798245278d58e26c66b Mon Sep 17 00:00:00 2001 From: Rebecca Ransome Date: Thu, 15 Aug 2024 13:38:05 +0100 Subject: [PATCH 7/7] Update params --- .../supplementary/create-licence-supplementary-year.service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/licences/supplementary/create-licence-supplementary-year.service.js b/app/services/licences/supplementary/create-licence-supplementary-year.service.js index 1931b1d82f..6c5a8b40eb 100644 --- a/app/services/licences/supplementary/create-licence-supplementary-year.service.js +++ b/app/services/licences/supplementary/create-licence-supplementary-year.service.js @@ -11,7 +11,7 @@ const LicenceSupplementaryYearModel = require('../../../models/licence-supplemen * Creates a licenceSupplementaryYears record based on the provided licence data * * @param {module:LicenceModel} licenceId - The UUID of the licence to be persisted - * @param {Object[]} years - An array of the financial year ends to be persisted as individual records + * @param {Object[]} financialYearEnds - An array of the financial year ends to be persisted as individual records * @param {Boolean} twoPartTariff - If there are any two-part tariff indicators on the licence */ async function go (licenceId, financialYearEnds, twoPartTariff) {