diff --git a/app/services/return-logs/process-licence-return-logs.service.js b/app/services/return-logs/process-licence-return-logs.service.js index 49b45a5d8e..47d72aa917 100644 --- a/app/services/return-logs/process-licence-return-logs.service.js +++ b/app/services/return-logs/process-licence-return-logs.service.js @@ -68,7 +68,11 @@ async function _fetchReturnCycles(changeDate) { async function _processReturnCycle(returnCycle, returnRequirements, changeDate, licenceRef) { // Determine if we have any return requirements that match the cycle being processed const requirementsToProcess = returnRequirements.filter((returnRequirement) => { - return returnRequirement.summer === returnCycle.summer + return ( + returnRequirement.summer === returnCycle.summer && + (returnRequirement.returnVersion.endDate >= returnCycle.startDate || + returnRequirement.returnVersion.endDate === null) + ) }) if (requirementsToProcess.length === 0) { diff --git a/test/fixtures/return-logs.fixture.js b/test/fixtures/return-logs.fixture.js index 97988470f0..014fdaa0cb 100644 --- a/test/fixtures/return-logs.fixture.js +++ b/test/fixtures/return-logs.fixture.js @@ -71,7 +71,7 @@ function returnRequirements() { abstractionPeriodStartMonth: 5, externalId: '4:16999652', id: '3bc0e31a-4bfb-47ef-aa6e-8aca37d9aac2', - legacyId: 16999651, + legacyId: 16999652, reportingFrequency: 'day', returnVersionId: '5a077661-05fc-4fc4-a2c6-d84ec908f093', siteDescription: 'PUMP AT TINTAGEL', @@ -193,9 +193,148 @@ function returnRequirements() { ] } +/** + * Represents multiple results from either `FetchReturnRequirementsService` or + * `FetchLicenceReturnRequirementsService` that have different return versions + * + * @returns {object[]} + */ +function returnRequirementsAcrossReturnVersions() { + return [ + ...returnRequirements(), + { + abstractionPeriodEndDay: 31, + abstractionPeriodEndMonth: 10, + abstractionPeriodStartDay: 1, + abstractionPeriodStartMonth: 5, + externalId: '4:16999652', + id: '3bc0e31a-4bfb-47ef-aa6e-8aca37d9aac2', + legacyId: 16999652, + reportingFrequency: 'day', + returnVersionId: '5a077661-05fc-4fc4-a2c6-d84ec908f093', + siteDescription: 'PUMP AT TINTAGEL', + summer: true, + twoPartTariff: false, + upload: false, + returnVersion: { + endDate: new Date('2024-05-26'), + id: '5a077661-05fc-4fc4-a2c6-d84ec908f093', + reason: 'new-licence', + startDate: new Date('2022-04-01'), + licence: { + expiredDate: null, + id: '3acf7d80-cf74-4e86-8128-13ef687ea091', + lapsedDate: null, + licenceRef: '01/25/90/3242', + revokedDate: null, + areacode: 'SAAR', + region: { + id: 'eb57737f-b309-49c2-9ab6-f701e3a6fd96', + naldRegionId: 4 + } + } + }, + points: [ + { + description: 'Summer cycle - live licence - live return version - summer return requirement', + ngr1: 'TG 713 291', + ngr2: null, + ngr3: null, + ngr4: null + } + ], + returnRequirementPurposes: [ + { + alias: 'Purpose alias for testing', + id: '8a5164fd-1705-45bd-a01c-6b09d066e403', + primaryPurpose: { + description: 'Agriculture', + id: 'b6bb3b77-cfe8-4f22-8dc9-e92713ca3156', + legacyId: 'A' + }, + purpose: { + description: 'General Farming & Domestic', + id: '289d1644-5215-4a20-af9e-5664fa9a18c7', + legacyId: '140' + }, + secondaryPurpose: { + description: 'General Agriculture', + id: '2457bfeb-a120-4b57-802a-46494bd22f82', + legacyId: 'AGR' + } + } + ] + }, + { + abstractionPeriodEndDay: 31, + abstractionPeriodEndMonth: 3, + abstractionPeriodStartDay: 1, + abstractionPeriodStartMonth: 4, + externalId: '4:16999651', + id: '4bc1efa7-10af-4958-864e-32acae5c6fa4', + legacyId: 16999651, + reportingFrequency: 'day', + returnVersionId: '5a077661-05fc-4fc4-a2c6-d84ec908f093', + siteDescription: 'BOREHOLE AT AVALON', + summer: false, + twoPartTariff: false, + upload: false, + returnVersion: { + endDate: null, + id: '5a077661-05fc-4fc4-a2c6-d84ec908f094', + reason: 'new-licence', + startDate: new Date('2024-05-27'), + licence: { + expiredDate: null, + id: '3acf7d80-cf74-4e86-8128-13ef687ea091', + lapsedDate: null, + licenceRef: '01/25/90/3242', + revokedDate: null, + areacode: 'SAAR', + region: { + id: 'eb57737f-b309-49c2-9ab6-f701e3a6fd96', + naldRegionId: 4 + } + } + }, + points: [ + { + description: 'Winter cycle - live licence - live return version - winter return requirement', + ngr1: 'TG 713 291', + ngr2: null, + ngr3: null, + ngr4: null + } + ], + returnRequirementPurposes: [ + { + alias: 'Purpose alias for testing', + id: '06c4c2f2-3dff-4053-bbc8-e6f64cd39623', + primaryPurpose: { + description: 'Agriculture', + id: 'b6bb3b77-cfe8-4f22-8dc9-e92713ca3156', + legacyId: 'A' + }, + purpose: { + description: 'General Farming & Domestic', + id: '289d1644-5215-4a20-af9e-5664fa9a18c7', + legacyId: '140' + }, + secondaryPurpose: { + description: 'General Agriculture', + id: '2457bfeb-a120-4b57-802a-46494bd22f82', + legacyId: 'AGR' + } + } + ] + } + ] +} + module.exports = { returnCycle, returnCycles, returnRequirement, - returnRequirements + returnRequirements, + returnRequirementsAcrossReturnVersions } diff --git a/test/services/return-logs/process-licence-return-logs.service.test.js b/test/services/return-logs/process-licence-return-logs.service.test.js index e07753d961..9ffd79162d 100644 --- a/test/services/return-logs/process-licence-return-logs.service.test.js +++ b/test/services/return-logs/process-licence-return-logs.service.test.js @@ -13,7 +13,8 @@ const { returnCycle, returnCycles, returnRequirement, - returnRequirements + returnRequirements, + returnRequirementsAcrossReturnVersions } = require('../../fixtures/return-logs.fixture.js') // Things we need to stub @@ -104,6 +105,29 @@ describe('Process licence return logs service', () => { }) }) + describe('and the licence has both "summer" and "all-year" return requirements across multiple return versions', () => { + beforeEach(() => { + fetchReturnRequirementsStub.resolves(returnRequirementsAcrossReturnVersions()) + }) + + describe('and the change date means multiple return cycles need processing', () => { + beforeEach(() => { + returnCycleModelStub.resolves(returnCycles()) + + createReturnLogsStub.onCall(0).resolves(['v1:4:01/25/90/3242:16999651:2024-11-01:2025-10-31']) + createReturnLogsStub.onCall(1).resolves(['v1:4:01/25/90/3242:16999652:2024-04-01:2025-05-26']) + createReturnLogsStub.onCall(2).resolves(['v1:4:01/25/90/3242:16999652:2024-05-27:2025-03-31']) + }) + + it('processes all the return requirements for the licence', async () => { + await ProcessLicenceReturnLogsService.go(licenceId, changeDate) + + expect(createReturnLogsStub.callCount).to.equal(3) + expect(voidReturnLogsStub.callCount).to.equal(2) + }) + }) + }) + describe('and the licence has only a "summer" return requirement', () => { beforeEach(() => { fetchReturnRequirementsStub.resolves([returnRequirement(true)])