diff --git a/app/lib/return-periods.lib.js b/app/lib/return-periods.lib.js index dde3ab116c..d4738304ad 100644 --- a/app/lib/return-periods.lib.js +++ b/app/lib/return-periods.lib.js @@ -7,6 +7,46 @@ const { returnPeriodDates } = require('./static-lookups.lib.js') +/** + * Determine return periods for a given return cycle. + * + * This function calculates the upcoming return periods for the provided return cycle. + * + * The result includes the start date, end date, and due date for each period as well as the return cycle id. + * + * @param {object} returnCycle - The return cycle to be broken into periods. + * + * @returns {Array} An object containing calculated dates for all return periods + */ +function determineReturnsPeriods(returnCycle) { + return [ + { + startDate: _startDate(returnCycle.startDate, returnPeriodDates.quarterOne), + endDate: _endDate(returnCycle.startDate, returnPeriodDates.quarterOne), + id: returnCycle.id, + dueDate: _dueDate(returnCycle.startDate, returnPeriodDates.quarterOne) + }, + { + startDate: _startDate(returnCycle.startDate, returnPeriodDates.quarterTwo), + endDate: _endDate(returnCycle.startDate, returnPeriodDates.quarterTwo), + id: returnCycle.id, + dueDate: _dueDate(returnCycle.startDate, returnPeriodDates.quarterTwo) + }, + { + startDate: _startDateQuarterThree(returnCycle.startDate, returnPeriodDates.quarterThree), + endDate: _endDateQuarterThree(returnCycle.startDate, returnPeriodDates.quarterThree), + id: returnCycle.id, + dueDate: _dueQuarterThree(returnCycle.startDate, returnPeriodDates.quarterThree) + }, + { + startDate: _startDate(returnCycle.endDate, returnPeriodDates.quarterFour), + endDate: _endDate(returnCycle.endDate, returnPeriodDates.quarterFour), + id: returnCycle.id, + dueDate: _dueDate(returnCycle.endDate, returnPeriodDates.quarterFour) + } + ] +} + /** * Determine return periods. * @@ -20,7 +60,7 @@ const { returnPeriodDates } = require('./static-lookups.lib.js') * * @returns {object} An object containing calculated dates for all return periods */ -function determineReturnsPeriods(determinationDate = new Date()) { +function determineUpcomingReturnsPeriods(determinationDate = new Date()) { return { allYear: { startDate: _cycleStartDate(determinationDate, returnPeriodDates.allYear), @@ -43,14 +83,14 @@ function determineReturnsPeriods(determinationDate = new Date()) { dueDate: _dueDate(determinationDate, returnPeriodDates.quarterTwo) }, quarterThree: { - startDate: _startDate(determinationDate, returnPeriodDates.quarterThree), - endDate: _endDate(determinationDate, returnPeriodDates.quarterThree), - dueDate: _dueDate(determinationDate, returnPeriodDates.quarterThree) + startDate: _startDateQuarterThree(determinationDate, returnPeriodDates.quarterThree), + endDate: _endDateQuarterThree(determinationDate, returnPeriodDates.quarterThree), + dueDate: _dueQuarterThree(determinationDate, returnPeriodDates.quarterThree) }, quarterFour: { - startDate: _startDateQuarterFour(determinationDate, returnPeriodDates.quarterFour), - endDate: _endDateQuarterFour(determinationDate, returnPeriodDates.quarterFour), - dueDate: _dueQuarterFour(determinationDate, returnPeriodDates.quarterFour) + startDate: _startDate(determinationDate, returnPeriodDates.quarterFour), + endDate: _endDate(determinationDate, returnPeriodDates.quarterFour), + dueDate: _dueDate(determinationDate, returnPeriodDates.quarterFour) } } } @@ -145,29 +185,28 @@ function _isDue(determinationDate, period) { } /** - * Calculates the start date for Quarter Four based on the determination date. + * Calculates the start date for Quarter Three based on the determination date. * * The start date is determined using the period's start day and month. The year for the start date * is always set to the year of the provided determination date, regardless of its specific value. * - * * @param {Date} determinationDate - The base date used to calculate the start date. * @param {object} period - The period object containing the start date information. * - * @returns {Date} A `Date` object representing the start date of Quarter Four. + * @returns {Date} A `Date` object representing the start date of Quarter Three. * @private */ -function _startDateQuarterFour(determinationDate, period) { +function _startDateQuarterThree(determinationDate, period) { const periodStartDay = period.startDate.day const periodStartMonth = period.startDate.month + 1 - const year = _newYearElapsedQuarterFour(determinationDate, period) + const year = _newYearElapsedQuarterThree(determinationDate, period) return new Date(`${year}-${periodStartMonth}-${periodStartDay}`) } /** - * Calculates the end date for Quarter Four based on the determination date. + * Calculates the end date for Quarter Three based on the determination date. * * The end date is determined using the period's end day and month. If the determination date * falls on or before December 31 of the current year, the end year is set to the current year. @@ -177,20 +216,20 @@ function _startDateQuarterFour(determinationDate, period) { * @param {Date} determinationDate - The base date used to calculate the end date. * @param {Object} period - The period object containing the end date information. * - * @returns {Date} A `Date` object representing the end date of Quarter Four. + * @returns {Date} A `Date` object representing the end date of Quarter Three. * @private */ -function _endDateQuarterFour(determinationDate, period) { +function _endDateQuarterThree(determinationDate, period) { const periodStartDay = period.endDate.day const periodStartMonth = period.endDate.month + 1 - const year = _newYearElapsedQuarterFour(determinationDate, period) + const year = _newYearElapsedQuarterThree(determinationDate, period) return new Date(`${year}-${periodStartMonth}-${periodStartDay}`) } /** - * Calculates the due date for Quarter Four, which spans into the next year. + * Calculates the due date for Quarter Three, which spans into the next year. * * The due date is always set in the following year relative to the provided determination date. * For example, if the determination date is `2024-01-01`, the due date will be in `2025`, @@ -199,34 +238,34 @@ function _endDateQuarterFour(determinationDate, period) { * @param {Date} determinationDate - The base date used to calculate the due date. * @param {object} period - The period object containing the due date information. * - * @returns {Date} A `Date` object representing the due date of Quarter Four, always in the following year. + * @returns {Date} A `Date` object representing the due date of Quarter Three, always in the following year. * @private */ -function _dueQuarterFour(determinationDate, period) { +function _dueQuarterThree(determinationDate, period) { const periodDueDay = period.dueDate.day const periodDueMonth = period.dueDate.month + 1 - const year = _newYearElapsedQuarterFourDueDate(determinationDate, period) + const year = _newYearElapsedQuarterThreeDueDate(determinationDate, period) return new Date(`${year}-${periodDueMonth}-${periodDueDay}`) } /** - * Determines whether the new year has elapsed for Quarter Four based on the determination date. + * Determines whether the new year has elapsed for Quarter Three based on the determination date. * - * This function calculates the year associated with Quarter Four by comparing the determination + * This function calculates the year associated with Quarter Three by comparing the determination * date with the due date specified in the period. If the determination date is earlier than or equal - * to the due date for Quarter Four in the current year, the previous year is returned (as the quarter is still Due). + * to the due date for Quarter Three in the current year, the previous year is returned (as the quarter is still Due). * Otherwise, the current year is returned. * - * @param {Date} determinationDate - The base date used to determine the year for Quarter Four. + * @param {Date} determinationDate - The base date used to determine the year for Quarter Three. * @param {object} period - The period object containing the due date information. * - * @returns {number} The year associated with Quarter Four: the previous year if the determination date + * @returns {number} The year associated with Quarter Three: the previous year if the determination date * is before or on the due date, otherwise the current year. * @private */ -function _newYearElapsedQuarterFour(determinationDate, period) { +function _newYearElapsedQuarterThree(determinationDate, period) { const year = determinationDate.getFullYear() const lastYear = year - 1 @@ -241,21 +280,21 @@ function _newYearElapsedQuarterFour(determinationDate, period) { } /** - * Determines the year associated with Quarter Four's due date based on the determination date. + * Determines the year associated with Quarter Three's due date based on the determination date. * - * This function calculates the year for Quarter Four's due date by comparing the determination date + * This function calculates the year for Quarter Three's due date by comparing the determination date * with the due date specified in the period. If the determination date is earlier than or equal to * the due date in the current year, the current year is returned. Otherwise, the next year is returned * (as the quarter is non longer due in the current year). * - * @param {Date} determinationDate - The base date used to determine the year for Quarter Four's due date. + * @param {Date} determinationDate - The base date used to determine the year for Quarter Three's due date. * @param {object} period - The period object containing the due date information. * - * @returns {number} The year associated with Quarter Four's due date: the current year if the determination + * @returns {number} The year associated with Quarter Three's due date: the current year if the determination * date is before or on the due date, otherwise the next year. * @private */ -function _newYearElapsedQuarterFourDueDate(determinationDate, period) { +function _newYearElapsedQuarterThreeDueDate(determinationDate, period) { const year = determinationDate.getFullYear() const nextYear = year + 1 @@ -279,7 +318,7 @@ function _newYearElapsedQuarterFourDueDate(determinationDate, period) { * @returns {object[]} - An array of return periods */ function determineUpcomingReturnPeriods(determinationDate = new Date()) { - const returnPeriods = determineReturnsPeriods(determinationDate) + const returnPeriods = determineUpcomingReturnsPeriods(determinationDate) const mappedReturnPeriods = _mapReturnsPeriods(returnPeriods) return _sortByDueDate(mappedReturnPeriods) } @@ -352,5 +391,6 @@ function _cycleStartDate(determinationDate, period) { module.exports = { determineReturnsPeriods, + determineUpcomingReturnsPeriods, determineUpcomingReturnPeriods } diff --git a/app/lib/static-lookups.lib.js b/app/lib/static-lookups.lib.js index 5436229d9d..f39bc2c5fd 100644 --- a/app/lib/static-lookups.lib.js +++ b/app/lib/static-lookups.lib.js @@ -26,6 +26,9 @@ const naldRegions = { const organisationTypes = ['individual', 'limitedCompany', 'limitedLiabilityPartnership', 'publicLimitedCompany'] +/** + * The start, end and due dates for each return cycle + */ const returnCycleDates = { allYear: { dueDate: { day: 28, month: 3 }, @@ -39,6 +42,32 @@ const returnCycleDates = { } } +/** + * The start, end and due dates for each quarterly return period + */ +const quarterlyReturnPeriods = { + quarterOne: { + dueDate: { day: 28, month: 6 }, + endDate: { day: 30, month: 5 }, + startDate: { day: 1, month: 3 } + }, + quarterTwo: { + dueDate: { day: 28, month: 9 }, + endDate: { day: 30, month: 8 }, + startDate: { day: 1, month: 6 } + }, + quarterThree: { + dueDate: { day: 28, month: 0 }, + endDate: { day: 31, month: 11 }, + startDate: { day: 1, month: 9 } + }, + quarterFour: { + dueDate: { day: 28, month: 3 }, + endDate: { day: 31, month: 2 }, + startDate: { day: 1, month: 0 } + } +} + /** * An object defining the return periods / cycles with their respective start dates, end dates, and due dates. * @@ -61,26 +90,7 @@ const returnCycleDates = { */ const returnPeriodDates = { ...returnCycleDates, - quarterOne: { - dueDate: { day: 28, month: 3 }, - endDate: { day: 31, month: 2 }, - startDate: { day: 1, month: 0 } - }, - quarterTwo: { - dueDate: { day: 28, month: 6 }, - endDate: { day: 30, month: 5 }, - startDate: { day: 1, month: 3 } - }, - quarterThree: { - dueDate: { day: 28, month: 9 }, - endDate: { day: 30, month: 8 }, - startDate: { day: 1, month: 6 } - }, - quarterFour: { - dueDate: { day: 28, month: 0 }, - endDate: { day: 31, month: 11 }, - startDate: { day: 1, month: 9 } - } + ...quarterlyReturnPeriods } const returnRequirementFrequencies = { @@ -142,5 +152,6 @@ module.exports = { returnRequirementFrequencies, returnRequirementReasons, sources, - twoPartTariffReviewIssues + twoPartTariffReviewIssues, + quarterlyReturnPeriods } diff --git a/app/services/jobs/return-logs/fetch-return-requirements.service.js b/app/services/jobs/return-logs/fetch-return-requirements.service.js index 48ca4f18d8..27f00f9e99 100644 --- a/app/services/jobs/return-logs/fetch-return-requirements.service.js +++ b/app/services/jobs/return-logs/fetch-return-requirements.service.js @@ -74,7 +74,7 @@ async function _fetch(returnCycle) { .withGraphFetched('returnVersion') .modifyGraph('returnVersion', (returnVersionBuilder) => { returnVersionBuilder - .select(['endDate', 'id', 'reason', 'startDate']) + .select(['endDate', 'id', 'reason', 'startDate', 'quarterlyReturns']) .withGraphFetched('licence') .modifyGraph('licence', (licenceBuilder) => { licenceBuilder diff --git a/app/services/return-logs/create-return-logs.service.js b/app/services/return-logs/create-return-logs.service.js index b19cb6d70e..10f274b9b8 100644 --- a/app/services/return-logs/create-return-logs.service.js +++ b/app/services/return-logs/create-return-logs.service.js @@ -6,6 +6,7 @@ */ const { timestampForPostgres } = require('../../lib/general.lib.js') +const { determineReturnsPeriods } = require('../../lib/return-periods.lib.js') const GenerateReturnLogService = require('./generate-return-log.service.js') const ReturnLogModel = require('../../models/return-log.model.js') @@ -42,9 +43,20 @@ async function go(returnRequirement, returnCycle) { * @private */ function _generateReturnLogs(returnRequirement, returnCycle) { - const returnLog = GenerateReturnLogService.go(returnRequirement, returnCycle) + const isQuarterlyReturn = returnRequirement.returnVersion.quarterlyReturns + const returnLogs = [] - return [returnLog] + if (isQuarterlyReturn && returnCycle.startDate >= new Date('2025-04-01')) { + const quarterlyReturnPeriods = determineReturnsPeriods(returnCycle) + + for (const quarterlyReturnPeriod of quarterlyReturnPeriods) { + returnLogs.push(GenerateReturnLogService.go(returnRequirement, quarterlyReturnPeriod)) + } + } else { + returnLogs.push(GenerateReturnLogService.go(returnRequirement, returnCycle)) + } + + return returnLogs } async function _persistReturnLogs(returnLogs) { diff --git a/app/services/return-logs/fetch-licence-return-requirements.service.js b/app/services/return-logs/fetch-licence-return-requirements.service.js index b423aba627..21a3a190ac 100644 --- a/app/services/return-logs/fetch-licence-return-requirements.service.js +++ b/app/services/return-logs/fetch-licence-return-requirements.service.js @@ -60,7 +60,7 @@ async function _fetch(licenceId, changeDate) { .withGraphFetched('returnVersion') .modifyGraph('returnVersion', (returnVersionBuilder) => { returnVersionBuilder - .select(['endDate', 'id', 'reason', 'startDate']) + .select(['endDate', 'id', 'reason', 'startDate', 'quarterlyReturns']) .withGraphFetched('licence') .modifyGraph('licence', (licenceBuilder) => { licenceBuilder diff --git a/test/fixtures/return-logs.fixture.js b/test/fixtures/return-logs.fixture.js index 1cc73814fc..c01060399a 100644 --- a/test/fixtures/return-logs.fixture.js +++ b/test/fixtures/return-logs.fixture.js @@ -23,6 +23,22 @@ function returnCycle(summer = false) { */ function returnCycles(numberOfCycles = 2) { const cycles = [ + { + id: '4c5ff4dc-dfe0-4693-9cb5-acdebf6f76b8', + startDate: new Date('2025-11-01'), + endDate: new Date('2026-10-31'), + dueDate: new Date('2026-11-28'), + summer: true, + submittedInWrls: true + }, + { + id: '6889b98d-964f-4966-b6d6-bf511d6526a9', + startDate: new Date('2025-04-01'), + endDate: new Date('2026-03-31'), + dueDate: new Date('2026-04-28'), + summer: false, + submittedInWrls: true + }, { id: '4c5ff4dc-dfe0-4693-9cb5-acdebf6f76b4', startDate: new Date('2024-11-01'), @@ -176,7 +192,8 @@ function returnRequirements() { id: 'eb57737f-b309-49c2-9ab6-f701e3a6fd96', naldRegionId: 4 } - } + }, + quarterlyReturns: true }, points: [ { diff --git a/test/lib/return-periods.lib.test.js b/test/lib/return-periods.lib.test.js index 4c531d1283..0cde45cef8 100644 --- a/test/lib/return-periods.lib.test.js +++ b/test/lib/return-periods.lib.test.js @@ -5,10 +5,11 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') const Sinon = require('sinon') -const { afterEach, describe, it, beforeEach } = (exports.lab = Lab.script()) +const { afterEach, describe, it, before, beforeEach } = (exports.lab = Lab.script()) const { expect } = Code const { returnPeriodDates } = require('../../app/lib/static-lookups.lib.js') +const ReturnCycleHelper = require('../support/helpers/return-cycle.helper.js') // Thing under test const ReturnPeriodLib = require('../../app/lib/return-periods.lib.js') @@ -32,13 +33,56 @@ describe('Return Period lib', () => { }) describe('determineReturnsPeriods', () => { + describe('when provided a return cycle', () => { + let allYearReturnCycle + let testYear + + before(async () => { + allYearReturnCycle = await ReturnCycleHelper.select(0, false) + testYear = allYearReturnCycle.startDate.getFullYear() + }) + + it('should return the return periods for that cycle', () => { + const results = ReturnPeriodLib.determineReturnsPeriods(allYearReturnCycle) + + expect(results).to.equal([ + { + dueDate: new Date(`${testYear}-07-28`), + endDate: new Date(`${testYear}-06-30`), + id: allYearReturnCycle.id, + startDate: new Date(`${testYear}-04-01`) + }, + { + dueDate: new Date(`${testYear}-10-28`), + endDate: new Date(`${testYear}-09-30`), + id: allYearReturnCycle.id, + startDate: new Date(`${testYear}-07-01`) + }, + { + dueDate: new Date(`${testYear + 1}-01-28`), + endDate: new Date(`${testYear}-12-31`), + id: allYearReturnCycle.id, + startDate: new Date(`${testYear}-10-01`) + }, + { + dueDate: new Date(`${testYear + 1}-04-28`), + endDate: new Date(`${testYear + 1}-03-31`), + id: allYearReturnCycle.id, + startDate: new Date(`${testYear + 1}-01-01`) + } + ]) + }) + }) + }) + + describe('determineUpcomingReturnsPeriods', () => { describe('"allYear"', () => { beforeEach(() => { dates = _getAllYearDates(year, lastYear) }) it('should return the "allYear" period', () => { - const result = ReturnPeriodLib.determineReturnsPeriods() + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods() expect(result.allYear).to.equal({ dueDate: dates.dueDate, @@ -54,7 +98,7 @@ describe('Return Period lib', () => { }) it('should return the "summer" period', () => { - const result = ReturnPeriodLib.determineReturnsPeriods() + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods() expect(result.summer).to.equal({ dueDate: dates.dueDate, @@ -64,18 +108,18 @@ describe('Return Period lib', () => { }) }) - describe('"quarterOne": 1 January - 31 March (Due date 28 April) ', () => { - describe('when it is currently "quarterOne"', () => { + describe('"quarterFour": 1 January - 31 March (Due date 28 April) ', () => { + describe('when it is currently "quarterFour"', () => { beforeEach(() => { - dates = _getPeriodDates(returnPeriodDates.quarterOne, year) + dates = _getPeriodDates(returnPeriodDates.quarterFour, year) testDate = new Date(`${year}-01-01`) }) - it('should return the "quarterOne" period', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterFour" period', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - expect(result.quarterOne).to.equal({ + expect(result.quarterFour).to.equal({ dueDate: dates.dueDate, endDate: dates.endDate, startDate: dates.startDate @@ -83,17 +127,17 @@ describe('Return Period lib', () => { }) }) - describe('when it is before "quarterOne" (in the previous year)', () => { + describe('when it is before "quarterFour" (in the previous year)', () => { beforeEach(() => { - dates = _getPeriodDates(returnPeriodDates.quarterOne, nextYear) + dates = _getPeriodDates(returnPeriodDates.quarterFour, nextYear) testDate = new Date(`${year}-12-31`) }) - it('should return the "quarterOne" period in the upcoming year', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterFour" period in the upcoming year', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - expect(result.quarterOne).to.equal({ + expect(result.quarterFour).to.equal({ dueDate: dates.dueDate, endDate: dates.endDate, startDate: dates.startDate @@ -101,17 +145,17 @@ describe('Return Period lib', () => { }) }) - describe('when it is after "quarterOne"', () => { + describe('when it is after "quarterFour"', () => { beforeEach(() => { - dates = _getPeriodDates(returnPeriodDates.quarterOne, nextYear) + dates = _getPeriodDates(returnPeriodDates.quarterFour, nextYear) testDate = new Date(`${year}-04-30`) }) - it('should return the "quarterOne" period for next year', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterFour" period for next year', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - expect(result.quarterOne).to.equal({ + expect(result.quarterFour).to.equal({ dueDate: dates.dueDate, endDate: dates.endDate, startDate: dates.startDate @@ -120,18 +164,18 @@ describe('Return Period lib', () => { }) }) - describe('"quarterTwo": 1 April - 30 June (Due date 28 July) ', () => { - describe('when it is currently "quarterTwo"', () => { + describe('"quarterOne": 1 April - 30 June (Due date 28 July) ', () => { + describe('when it is currently "quarterOne"', () => { beforeEach(() => { - dates = _getPeriodDates(returnPeriodDates.quarterTwo, year) + dates = _getPeriodDates(returnPeriodDates.quarterOne, year) testDate = new Date(`${year}-04-29`) }) - it('should return the "quarterTwo" period', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterOne" period', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - expect(result.quarterTwo).to.equal({ + expect(result.quarterOne).to.equal({ dueDate: dates.dueDate, endDate: dates.endDate, startDate: dates.startDate @@ -139,17 +183,17 @@ describe('Return Period lib', () => { }) }) - describe('when it is before "quarterTwo" (in the previous year)', () => { + describe('when it is before "quarterOne" (in the previous year)', () => { beforeEach(() => { - dates = _getPeriodDates(returnPeriodDates.quarterTwo, nextYear) + dates = _getPeriodDates(returnPeriodDates.quarterOne, nextYear) testDate = new Date(`${year}-12-31`) }) - it('should return the "quarterTwo" period in the upcoming year', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterOne" period in the upcoming year', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - expect(result.quarterTwo).to.equal({ + expect(result.quarterOne).to.equal({ dueDate: dates.dueDate, endDate: dates.endDate, startDate: dates.startDate @@ -157,17 +201,17 @@ describe('Return Period lib', () => { }) }) - describe('when it is after "quarterTwo"', () => { + describe('when it is after "quarterOne"', () => { beforeEach(() => { - dates = _getPeriodDates(returnPeriodDates.quarterTwo, nextYear) + dates = _getPeriodDates(returnPeriodDates.quarterOne, nextYear) testDate = new Date(`${year}-07-29`) }) - it('should return the "quarterTwo" period for next year', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterOne" period for next year', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - expect(result.quarterTwo).to.equal({ + expect(result.quarterOne).to.equal({ dueDate: dates.dueDate, endDate: dates.endDate, startDate: dates.startDate @@ -176,18 +220,18 @@ describe('Return Period lib', () => { }) }) - describe('"quarterThree": 1 July - 30 September (Due date 28 October) ', () => { + describe('"quarterTwo": 1 July - 30 September (Due date 28 October) ', () => { describe('when it is currently "quarterThree"', () => { beforeEach(() => { - dates = _getPeriodDates(returnPeriodDates.quarterThree, year) + dates = _getPeriodDates(returnPeriodDates.quarterTwo, year) testDate = new Date(`${year}-07-01`) }) - it('should return the "quarterThree" period', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterTwo" period', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - expect(result.quarterThree).to.equal({ + expect(result.quarterTwo).to.equal({ dueDate: dates.dueDate, endDate: dates.endDate, startDate: dates.startDate @@ -195,17 +239,17 @@ describe('Return Period lib', () => { }) }) - describe('when it is before "quarterThree" (in the previous year)', () => { + describe('when it is before "quarterTwo" (in the previous year)', () => { beforeEach(() => { - dates = _getPeriodDates(returnPeriodDates.quarterThree, nextYear) + dates = _getPeriodDates(returnPeriodDates.quarterTwo, nextYear) testDate = new Date(`${year}-12-31`) }) - it('should return the "quarterThree" period in the upcoming year', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterTwo" period in the upcoming year', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - expect(result.quarterThree).to.equal({ + expect(result.quarterTwo).to.equal({ dueDate: dates.dueDate, endDate: dates.endDate, startDate: dates.startDate @@ -213,17 +257,17 @@ describe('Return Period lib', () => { }) }) - describe('when it is after "quarterThree"', () => { + describe('when it is after "quarterTwo"', () => { beforeEach(() => { - dates = _getPeriodDates(returnPeriodDates.quarterThree, nextYear) + dates = _getPeriodDates(returnPeriodDates.quarterTwo, nextYear) testDate = new Date(`${year}-10-29`) }) - it('should return the "quarterThree" period for next year', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterTwo" period for next year', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - expect(result.quarterThree).to.equal({ + expect(result.quarterTwo).to.equal({ dueDate: dates.dueDate, endDate: dates.endDate, startDate: dates.startDate @@ -232,10 +276,10 @@ describe('Return Period lib', () => { }) }) - describe('"quarterFour": 1 October - 31 December (Due date 28 January) ', () => { - describe('when it is currently "quarterFour" (Between October and December)', () => { + describe('"quarterThree": 1 October - 31 December (Due date 28 January) ', () => { + describe('when it is currently "quarterThree" (Between October and December)', () => { beforeEach(() => { - dates = _getPeriodDates(returnPeriodDates.quarterFour, year) + dates = _getPeriodDates(returnPeriodDates.quarterThree, year) dueDateNextYear = new Date(dates.dueDate) dueDateNextYear.setFullYear(dueDateNextYear.getFullYear() + 1) @@ -243,10 +287,10 @@ describe('Return Period lib', () => { testDate = new Date(`${year}-10-01`) }) - it('should return the "quarterFour" period', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterThree" period', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - expect(result.quarterFour).to.equal({ + expect(result.quarterThree).to.equal({ dueDate: dueDateNextYear, endDate: dates.endDate, startDate: dates.startDate @@ -254,15 +298,15 @@ describe('Return Period lib', () => { }) }) - describe('when it is currently "quarterFour" (In January before the Due date)', () => { + describe('when it is currently "quarterThree" (In January before the Due date)', () => { beforeEach(() => { testDate = new Date(`${year}-01-01`) }) - it('should return the "quarterFour" period - with the Due date in the current year and the start and end in the previous year', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterThree" period - with the Due date in the current year and the start and end in the previous year', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - dates = _getPeriodDates(returnPeriodDates.quarterFour, year) + dates = _getPeriodDates(returnPeriodDates.quarterThree, year) const startDate = new Date(dates.startDate) startDate.setFullYear(startDate.getFullYear() - 1) @@ -270,7 +314,7 @@ describe('Return Period lib', () => { const endDate = new Date(dates.endDate) endDate.setFullYear(endDate.getFullYear() - 1) - expect(result.quarterFour).to.equal({ + expect(result.quarterThree).to.equal({ dueDate: dates.dueDate, endDate, startDate @@ -278,9 +322,9 @@ describe('Return Period lib', () => { }) }) - describe('when it is before "quarterFour"', () => { + describe('when it is before "quarterThree"', () => { beforeEach(() => { - dates = _getPeriodDates(returnPeriodDates.quarterFour, year) + dates = _getPeriodDates(returnPeriodDates.quarterThree, year) dueDateNextYear = new Date(dates.dueDate) dueDateNextYear.setFullYear(dueDateNextYear.getFullYear() + 1) @@ -288,10 +332,10 @@ describe('Return Period lib', () => { testDate = new Date(`${year}-08-01`) }) - it('should return the "quarterFour" period in the upcoming year', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterThree" period in the upcoming year', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - expect(result.quarterFour).to.equal({ + expect(result.quarterThree).to.equal({ dueDate: dueDateNextYear, endDate: dates.endDate, startDate: dates.startDate @@ -299,9 +343,9 @@ describe('Return Period lib', () => { }) }) - describe('when it is after "quarterFour"', () => { + describe('when it is after "quarterThree"', () => { beforeEach(() => { - dates = _getPeriodDates(returnPeriodDates.quarterFour, nextYear) + dates = _getPeriodDates(returnPeriodDates.quarterThree, nextYear) dueDateNextYear = new Date(dates.dueDate) dueDateNextYear.setFullYear(dueDateNextYear.getFullYear() + 1) @@ -309,10 +353,10 @@ describe('Return Period lib', () => { testDate = new Date(`${nextYear}-01-29`) }) - it('should return the "quarterFour" period for next year', () => { - const result = ReturnPeriodLib.determineReturnsPeriods(testDate) + it('should return the "quarterThree" period for next year', () => { + const result = ReturnPeriodLib.determineUpcomingReturnsPeriods(testDate) - expect(result.quarterFour).to.equal({ + expect(result.quarterThree).to.equal({ dueDate: dueDateNextYear, endDate: dates.endDate, startDate: dates.startDate @@ -323,7 +367,7 @@ describe('Return Period lib', () => { }) describe('determineUpcomingReturnPeriods', () => { - describe('When the period is due for "quarterOne"', () => { + describe('When the period is due for "quarterFour"', () => { describe('and the current date is between 29 January - 28 April', () => { beforeEach(() => { dates = testDate = new Date(`${year}-01-29`) @@ -334,35 +378,35 @@ describe('Return Period lib', () => { expect(result).to.equal([ { - ..._getPeriodDates(returnPeriodDates.quarterOne, year), - name: 'quarterOne' + ..._getPeriodDates(returnPeriodDates.quarterFour, year), + name: 'quarterFour' }, { ..._getAllYearDates(year, lastYear), name: 'allYear' }, { - ..._getPeriodDates(returnPeriodDates.quarterTwo, year), - name: 'quarterTwo' + ..._getPeriodDates(returnPeriodDates.quarterOne, year), + name: 'quarterOne' }, { - ..._getPeriodDates(returnPeriodDates.quarterThree, year), - name: 'quarterThree' + ..._getPeriodDates(returnPeriodDates.quarterTwo, year), + name: 'quarterTwo' }, { ..._getSummerDates(year, lastYear), name: 'summer' }, { - ..._getPeriodDatesQuarterFour(year), - name: 'quarterFour' + ..._getPeriodDatesQuarterThree(year), + name: 'quarterThree' } ]) }) }) }) - describe('When the period is due for "quarterTwo"', () => { + describe('When the period is due for "quarterOne"', () => { describe('and the current date is between 29 April - 28 July', () => { beforeEach(() => { testDate = new Date(`${year}-04-29`) @@ -373,24 +417,24 @@ describe('Return Period lib', () => { expect(result).to.equal([ { - ..._getPeriodDates(returnPeriodDates.quarterTwo, year), - name: 'quarterTwo' + ..._getPeriodDates(returnPeriodDates.quarterOne, year), + name: 'quarterOne' }, { - ..._getPeriodDates(returnPeriodDates.quarterThree, year), - name: 'quarterThree' + ..._getPeriodDates(returnPeriodDates.quarterTwo, year), + name: 'quarterTwo' }, { ..._getSummerDates(year, lastYear), name: 'summer' }, { - ..._getPeriodDatesQuarterFour(year), - name: 'quarterFour' + ..._getPeriodDatesQuarterThree(year), + name: 'quarterThree' }, { - ..._getPeriodDates(returnPeriodDates.quarterOne, nextYear), - name: 'quarterOne' + ..._getPeriodDates(returnPeriodDates.quarterFour, nextYear), + name: 'quarterFour' }, { ..._getAllYearDates(nextYear, year), @@ -401,7 +445,7 @@ describe('Return Period lib', () => { }) }) - describe('When the current period is due for "quarterThree"', () => { + describe('When the current period is due for "quarterTwo"', () => { describe('and the current date is between 29 July - 28 October', () => { beforeEach(() => { testDate = new Date(`${year}-10-01`) @@ -412,28 +456,28 @@ describe('Return Period lib', () => { expect(result).to.equal([ { - ..._getPeriodDates(returnPeriodDates.quarterThree, year), - name: 'quarterThree' + ..._getPeriodDates(returnPeriodDates.quarterTwo, year), + name: 'quarterTwo' }, { ..._getSummerDates(year, lastYear), name: 'summer' }, { - ..._getPeriodDatesQuarterFour(year), - name: 'quarterFour' + ..._getPeriodDatesQuarterThree(year), + name: 'quarterThree' }, { - ..._getPeriodDates(returnPeriodDates.quarterOne, nextYear), - name: 'quarterOne' + ..._getPeriodDates(returnPeriodDates.quarterFour, nextYear), + name: 'quarterFour' }, { ..._getAllYearDates(nextYear, year), name: 'allYear' }, { - ..._getPeriodDates(returnPeriodDates.quarterTwo, nextYear), - name: 'quarterTwo' + ..._getPeriodDates(returnPeriodDates.quarterOne, nextYear), + name: 'quarterOne' } ]) }) @@ -455,31 +499,31 @@ describe('Return Period lib', () => { name: 'summer' }, { - ..._getPeriodDatesQuarterFour(year), - name: 'quarterFour' + ..._getPeriodDatesQuarterThree(year), + name: 'quarterThree' }, { - ..._getPeriodDates(returnPeriodDates.quarterOne, nextYear), - name: 'quarterOne' + ..._getPeriodDates(returnPeriodDates.quarterFour, nextYear), + name: 'quarterFour' }, { ..._getAllYearDates(nextYear, year), name: 'allYear' }, { - ..._getPeriodDates(returnPeriodDates.quarterTwo, nextYear), - name: 'quarterTwo' + ..._getPeriodDates(returnPeriodDates.quarterOne, nextYear), + name: 'quarterOne' }, { - ..._getPeriodDates(returnPeriodDates.quarterThree, nextYear), - name: 'quarterThree' + ..._getPeriodDates(returnPeriodDates.quarterTwo, nextYear), + name: 'quarterTwo' } ]) }) }) }) - describe('When the current period is due for "quarterFour"', () => { + describe('When the current period is due for "quarterThree"', () => { describe('and the current date is between 29 November - 31 December', () => { beforeEach(() => { testDate = new Date(`${year}-11-29`) @@ -490,24 +534,24 @@ describe('Return Period lib', () => { expect(result).to.equal([ { - ..._getPeriodDatesQuarterFour(year), - name: 'quarterFour' + ..._getPeriodDatesQuarterThree(year), + name: 'quarterThree' }, { - ..._getPeriodDates(returnPeriodDates.quarterOne, nextYear), - name: 'quarterOne' + ..._getPeriodDates(returnPeriodDates.quarterFour, nextYear), + name: 'quarterFour' }, { ..._getAllYearDates(nextYear, year), name: 'allYear' }, { - ..._getPeriodDates(returnPeriodDates.quarterTwo, nextYear), - name: 'quarterTwo' + ..._getPeriodDates(returnPeriodDates.quarterOne, nextYear), + name: 'quarterOne' }, { - ..._getPeriodDates(returnPeriodDates.quarterThree, nextYear), - name: 'quarterThree' + ..._getPeriodDates(returnPeriodDates.quarterTwo, nextYear), + name: 'quarterTwo' }, { ..._getSummerDates(nextYear, year), @@ -527,24 +571,24 @@ describe('Return Period lib', () => { expect(result).to.equal([ { - ..._getPeriodDatesQuarterFour(lastYear), - name: 'quarterFour' + ..._getPeriodDatesQuarterThree(lastYear), + name: 'quarterThree' }, { - ..._getPeriodDates(returnPeriodDates.quarterOne, year), - name: 'quarterOne' + ..._getPeriodDates(returnPeriodDates.quarterFour, year), + name: 'quarterFour' }, { ..._getAllYearDates(year, lastYear), name: 'allYear' }, { - ..._getPeriodDates(returnPeriodDates.quarterTwo, year), - name: 'quarterTwo' + ..._getPeriodDates(returnPeriodDates.quarterOne, year), + name: 'quarterOne' }, { - ..._getPeriodDates(returnPeriodDates.quarterThree, year), - name: 'quarterThree' + ..._getPeriodDates(returnPeriodDates.quarterTwo, year), + name: 'quarterTwo' }, { ..._getSummerDates(year, lastYear), @@ -565,8 +609,8 @@ function _getPeriodDates(period, year) { } } -function _getPeriodDatesQuarterFour(year) { - const period = returnPeriodDates.quarterFour +function _getPeriodDatesQuarterThree(year) { + const period = returnPeriodDates.quarterThree return { startDate: new Date(`${year}-${period.startDate.month + 1}-${period.startDate.day}`), endDate: new Date(`${year}-${period.endDate.month + 1}-${period.endDate.day}`), diff --git a/test/presenters/notifications/setup/returns-period.presenter.test.js b/test/presenters/notifications/setup/returns-period.presenter.test.js index 975190e2e2..72771bd286 100644 --- a/test/presenters/notifications/setup/returns-period.presenter.test.js +++ b/test/presenters/notifications/setup/returns-period.presenter.test.js @@ -42,7 +42,7 @@ describe('Notifications Setup - Returns Period presenter', () => { beforeEach(() => { session = { returnsPeriod: 'quarterOne' } - testDate = new Date(`${currentYear}-01-29`) + testDate = new Date(`${currentYear}-04-29`) clock = Sinon.useFakeTimers(testDate) }) @@ -54,26 +54,26 @@ describe('Notifications Setup - Returns Period presenter', () => { expect(currentReturnPeriod).to.equal({ checked: true, value: 'quarterOne', - text: `Quarterly 1 January ${currentYear} to 31 March ${currentYear}`, - hint: { text: `Due date 28 April ${currentYear}` } + text: `Quarterly 1 April ${currentYear} to 30 June ${currentYear}`, + hint: { text: `Due date 28 July ${currentYear}` } }) }) }) - describe('When the current period is due for "quarterOne"', () => { + describe('When the current period is due for "quarterFour"', () => { describe('and the current date is between 29 January - 28 April', () => { beforeEach(() => { testDate = new Date(`${currentYear}-01-29`) clock = Sinon.useFakeTimers(testDate) }) - it('returns the current return period as "quarterOne"', () => { + it('returns the current return period as "quarterFour"', () => { const { returnsPeriod: [currentReturnPeriod] } = ReturnsPeriodPresenter.go(session) expect(currentReturnPeriod).to.equal({ checked: false, - value: 'quarterOne', + value: 'quarterFour', text: `Quarterly 1 January ${currentYear} to 31 March ${currentYear}`, hint: { text: `Due date 28 April ${currentYear}` } }) @@ -94,34 +94,34 @@ describe('Notifications Setup - Returns Period presenter', () => { }) }) - describe('When the current period is due for "quarterTwo"', () => { + describe('When the current period is due for "quarterOne"', () => { describe('and the current date is between 29 April - 28 July', () => { beforeEach(() => { testDate = new Date(`${currentYear}-04-29`) clock = Sinon.useFakeTimers(testDate) }) - it('returns the current return period as "quarterTwo"', () => { + it('returns the current return period as "quarterOne"', () => { const { returnsPeriod: [currentReturnPeriod] } = ReturnsPeriodPresenter.go(session) expect(currentReturnPeriod).to.equal({ checked: false, - value: 'quarterTwo', + value: 'quarterOne', text: `Quarterly 1 April ${currentYear} to 30 June ${currentYear}`, hint: { text: `Due date 28 July ${currentYear}` } }) }) - it('returns the next return period as "quarterThree"', () => { + it('returns the next return period as "quarterTwo"', () => { const { returnsPeriod: [, nextReturnPeriod] } = ReturnsPeriodPresenter.go(session) expect(nextReturnPeriod).to.equal({ checked: false, - value: 'quarterThree', + value: 'quarterTwo', text: `Quarterly 1 July ${currentYear} to 30 September ${currentYear}`, hint: { text: `Due date 28 October ${currentYear}` } }) @@ -129,21 +129,21 @@ describe('Notifications Setup - Returns Period presenter', () => { }) }) - describe('When the current period is due for "quarterThree"', () => { + describe('When the current period is due for "quarterTwo"', () => { describe('and the current date is between 29 July - 28 October', () => { beforeEach(() => { testDate = new Date(`${currentYear}-07-29`) clock = Sinon.useFakeTimers(testDate) }) - it('returns the current return period as "quarterThree"', () => { + it('returns the current return period as "quarterTwo"', () => { const { returnsPeriod: [currentReturnPeriod] } = ReturnsPeriodPresenter.go(session) expect(currentReturnPeriod).to.equal({ checked: false, - value: 'quarterThree', + value: 'quarterTwo', text: `Quarterly 1 July ${currentYear} to 30 September ${currentYear}`, hint: { text: `Due date 28 October ${currentYear}` } }) @@ -184,14 +184,14 @@ describe('Notifications Setup - Returns Period presenter', () => { }) }) - it('returns the next return period as "quarterFour"', () => { + it('returns the next return period as "quarterThree"', () => { const { returnsPeriod: [, nextReturnPeriod] } = ReturnsPeriodPresenter.go(session) expect(nextReturnPeriod).to.equal({ checked: false, - value: 'quarterFour', + value: 'quarterThree', text: `Quarterly 1 October ${currentYear} to 31 December ${currentYear}`, hint: { text: `Due date 28 January ${nextYear}` } }) @@ -199,34 +199,34 @@ describe('Notifications Setup - Returns Period presenter', () => { }) }) - describe('When the current period is due for "quarterFour"', () => { + describe('When the current period is due for "quarterThree"', () => { describe('and the current date is between 29 November - 31 December', () => { beforeEach(() => { testDate = new Date(`${currentYear}-11-29`) clock = Sinon.useFakeTimers(testDate) }) - it('returns the current return period as "quarterFour"', () => { + it('returns the current return period as "quarterThree"', () => { const { returnsPeriod: [currentReturnPeriod] } = ReturnsPeriodPresenter.go(session) expect(currentReturnPeriod).to.equal({ checked: false, - value: 'quarterFour', + value: 'quarterThree', text: `Quarterly 1 October ${currentYear} to 31 December ${currentYear}`, hint: { text: `Due date 28 January ${nextYear}` } }) }) - it('returns the next return period as "quarterOne"', () => { + it('returns the next return period as "quarterFour"', () => { const { returnsPeriod: [, nextReturnPeriod] } = ReturnsPeriodPresenter.go(session) expect(nextReturnPeriod).to.equal({ checked: false, - value: 'quarterOne', + value: 'quarterFour', text: `Quarterly 1 January ${nextYear} to 31 March ${nextYear}`, hint: { text: `Due date 28 April ${nextYear}` } }) @@ -239,14 +239,14 @@ describe('Notifications Setup - Returns Period presenter', () => { clock = Sinon.useFakeTimers(testDate) }) - it('returns the current return period as "quarterFour" - with the start and end date in the previous year', () => { + it('returns the current return period as "quarterThree" - with the start and end date in the previous year', () => { const { returnsPeriod: [currentReturnPeriod] } = ReturnsPeriodPresenter.go(session) expect(currentReturnPeriod).to.equal({ checked: false, - value: 'quarterFour', + value: 'quarterThree', text: `Quarterly 1 October ${previousYear} to 31 December ${previousYear}`, hint: { text: `Due date 28 January ${currentYear}` } }) @@ -259,7 +259,7 @@ describe('Notifications Setup - Returns Period presenter', () => { expect(nextReturnPeriod).to.equal({ checked: false, - value: 'quarterOne', + value: 'quarterFour', text: `Quarterly 1 January ${currentYear} to 31 March ${currentYear}`, hint: { text: `Due date 28 April ${currentYear}` } }) diff --git a/test/services/jobs/return-logs/fetch-return-requirements.service.test.js b/test/services/jobs/return-logs/fetch-return-requirements.service.test.js index 5af22af92a..1ff0827a5a 100644 --- a/test/services/jobs/return-logs/fetch-return-requirements.service.test.js +++ b/test/services/jobs/return-logs/fetch-return-requirements.service.test.js @@ -531,7 +531,8 @@ function _expectedResult() { id: region.id, naldRegionId: region.naldRegionId } - } + }, + quarterlyReturns: returnVersion.quarterlyReturns }, points: [ { diff --git a/test/services/notifications/setup/returns-period.services.test.js b/test/services/notifications/setup/returns-period.services.test.js index b27beda64d..163be7700a 100644 --- a/test/services/notifications/setup/returns-period.services.test.js +++ b/test/services/notifications/setup/returns-period.services.test.js @@ -45,7 +45,7 @@ describe('Notifications Setup - Returns Period service', () => { text: 'Due date 28 January 2025' }, text: 'Quarterly 1 October 2024 to 31 December 2024', - value: 'quarterFour' + value: 'quarterThree' }, { checked: false, @@ -53,7 +53,7 @@ describe('Notifications Setup - Returns Period service', () => { text: 'Due date 28 April 2025' }, text: 'Quarterly 1 January 2025 to 31 March 2025', - value: 'quarterOne' + value: 'quarterFour' } ] }) diff --git a/test/services/notifications/setup/submit-returns-period.services.test.js b/test/services/notifications/setup/submit-returns-period.services.test.js index f8b6db8aa5..37376a3dbb 100644 --- a/test/services/notifications/setup/submit-returns-period.services.test.js +++ b/test/services/notifications/setup/submit-returns-period.services.test.js @@ -75,7 +75,7 @@ describe('Notifications Setup - Submit Returns Period service', () => { text: 'Due date 28 January 2025' }, text: 'Quarterly 1 October 2024 to 31 December 2024', - value: 'quarterFour' + value: 'quarterThree' }, { checked: false, @@ -83,7 +83,7 @@ describe('Notifications Setup - Submit Returns Period service', () => { text: 'Due date 28 April 2025' }, text: 'Quarterly 1 January 2025 to 31 March 2025', - value: 'quarterOne' + value: 'quarterFour' } ] }) diff --git a/test/services/return-logs/create-return-logs.service.test.js b/test/services/return-logs/create-return-logs.service.test.js index 3e23ba74c2..d80e6ea8c3 100644 --- a/test/services/return-logs/create-return-logs.service.test.js +++ b/test/services/return-logs/create-return-logs.service.test.js @@ -9,7 +9,7 @@ const { describe, it, beforeEach, afterEach } = (exports.lab = Lab.script()) const { expect } = Code // Test helpers -const { returnCycle, returnRequirement } = require('../../fixtures/return-logs.fixture.js') +const { returnCycle, returnCycles, returnRequirement } = require('../../fixtures/return-logs.fixture.js') // Things we need to stub const GenerateReturnLogService = require('../../../app/services/return-logs/generate-return-log.service.js') @@ -50,6 +50,57 @@ describe('Return Logs - Create Return Logs service', () => { }) describe('when called', () => { + beforeEach(() => { + // NOTE: GenerateReturnLogService's results will depend on what the current date is, hence we control it + clock = Sinon.useFakeTimers(new Date(`${year - 1}-12-01`)) + + testReturnCycle = returnCycle(true) + testReturnRequirement = returnRequirement(true) + }) + + it('will persist the return logs generated from the return requirement and cycle passed in', async () => { + await CreateReturnLogsService.go(testReturnRequirement, testReturnCycle) + + expect(insertStub.callCount).to.equal(1) + + // Check we create the return log as expected + const [insertObject] = insertStub.args[0] + + // NOTE: We don't assert every property of the object passed in because we know it is coming from + // GenerateReturnLogService and that has its own suite of tests. We do however confirm that the createdAt and + // UpdatedAt properties are set because those only get set in the service + expect(insertObject.id).to.equal('v1:4:01/25/90/3242:16999652:2025-11-01:2026-10-31') + expect(insertObject.createdAt).to.exist() + expect(insertObject.updatedAt).to.exist() + }) + + it('returns the return log IDs it generated', async () => { + const results = await CreateReturnLogsService.go(testReturnRequirement, testReturnCycle) + + expect(results).to.equal(['v1:4:01/25/90/3242:16999652:2025-11-01:2026-10-31']) + }) + + describe('and an error occurs when creating the return logs', () => { + beforeEach(() => { + // NOTE: We stub the generate service to throw purely because it is easier to structure our tests on that basis. + // But if the actual insert were to throw the expected behaviour would be the same. + Sinon.stub(GenerateReturnLogService, 'go').throws() + }) + + it('handles the error', async () => { + await CreateReturnLogsService.go(testReturnRequirement, testReturnCycle) + + const args = notifierStub.omfg.firstCall.args + + expect(args[0]).to.equal('Return logs creation errored') + expect(args[1].returnRequirement.id).to.equal('3bc0e31a-4bfb-47ef-aa6e-8aca37d9aac2') + expect(args[1].returnCycle.id).to.equal('4c5ff4dc-dfe0-4693-9cb5-acdebf6f76b8') + expect(args[2]).to.be.an.error() + }) + }) + }) + + describe('when called with a quarterly return version and a return cycle after 01-04-2025', () => { beforeEach(() => { // NOTE: GenerateReturnLogService's results will depend on what the current date is, hence we control it clock = Sinon.useFakeTimers(new Date(`${year - 1}-12-01`)) @@ -61,7 +112,7 @@ describe('Return Logs - Create Return Logs service', () => { it('will persist the return logs generated from the return requirement and cycle passed in', async () => { await CreateReturnLogsService.go(testReturnRequirement, testReturnCycle) - expect(insertStub.callCount).to.equal(1) + expect(insertStub.callCount).to.equal(4) // Check we create the return log as expected const [insertObject] = insertStub.args[0] @@ -69,7 +120,10 @@ describe('Return Logs - Create Return Logs service', () => { // NOTE: We don't assert every property of the object passed in because we know it is coming from // GenerateReturnLogService and that has its own suite of tests. We do however confirm that the createdAt and // UpdatedAt properties are set because those only get set in the service - expect(insertObject.id).to.equal('v1:4:01/25/90/3242:16999651:2024-04-01:2025-03-31') + expect(insertStub.args[0][0].id).to.equal('v1:4:01/25/90/3242:16999651:2025-04-01:2025-06-30') + expect(insertStub.args[1][0].id).to.equal('v1:4:01/25/90/3242:16999651:2025-07-01:2025-09-30') + expect(insertStub.args[2][0].id).to.equal('v1:4:01/25/90/3242:16999651:2025-10-01:2025-12-31') + expect(insertStub.args[3][0].id).to.equal('v1:4:01/25/90/3242:16999651:2026-01-01:2026-03-31') expect(insertObject.createdAt).to.exist() expect(insertObject.updatedAt).to.exist() }) @@ -77,7 +131,12 @@ describe('Return Logs - Create Return Logs service', () => { it('returns the return log IDs it generated', async () => { const results = await CreateReturnLogsService.go(testReturnRequirement, testReturnCycle) - expect(results).to.equal(['v1:4:01/25/90/3242:16999651:2024-04-01:2025-03-31']) + expect(results).to.equal([ + 'v1:4:01/25/90/3242:16999651:2025-04-01:2025-06-30', + 'v1:4:01/25/90/3242:16999651:2025-07-01:2025-09-30', + 'v1:4:01/25/90/3242:16999651:2025-10-01:2025-12-31', + 'v1:4:01/25/90/3242:16999651:2026-01-01:2026-03-31' + ]) }) describe('and an error occurs when creating the return logs', () => { @@ -94,9 +153,25 @@ describe('Return Logs - Create Return Logs service', () => { expect(args[0]).to.equal('Return logs creation errored') expect(args[1].returnRequirement.id).to.equal('4bc1efa7-10af-4958-864e-32acae5c6fa4') - expect(args[1].returnCycle.id).to.equal('6889b98d-964f-4966-b6d6-bf511d6526a1') + expect(args[1].returnCycle.id).to.equal('6889b98d-964f-4966-b6d6-bf511d6526a9') expect(args[2]).to.be.an.error() }) }) }) + + describe('when called with a quarterly return version and a return cycle before 01-04-2025', () => { + beforeEach(() => { + // NOTE: GenerateReturnLogService's results will depend on what the current date is, hence we control it + clock = Sinon.useFakeTimers(new Date(`${year - 1}-12-01`)) + + testReturnCycle = returnCycles(6)[5] + testReturnRequirement = returnRequirement() + }) + + it('returns only one return log', async () => { + const results = await CreateReturnLogsService.go(testReturnRequirement, testReturnCycle) + + expect(results).to.equal(['v1:4:01/25/90/3242:16999651:2023-04-01:2024-03-31']) + }) + }) }) diff --git a/test/services/return-logs/fetch-licence-return-requirements.service.test.js b/test/services/return-logs/fetch-licence-return-requirements.service.test.js index 789e164221..022c8908f9 100644 --- a/test/services/return-logs/fetch-licence-return-requirements.service.test.js +++ b/test/services/return-logs/fetch-licence-return-requirements.service.test.js @@ -308,7 +308,8 @@ function _expectedResult( id: region.id, naldRegionId: region.naldRegionId } - } + }, + quarterlyReturns: _returnVersion.quarterlyReturns }, points: [ { diff --git a/test/services/return-logs/generate-return-log.service.test.js b/test/services/return-logs/generate-return-log.service.test.js index e76630f4cb..008bed56c4 100644 --- a/test/services/return-logs/generate-return-log.service.test.js +++ b/test/services/return-logs/generate-return-log.service.test.js @@ -40,9 +40,9 @@ describe('Return Logs - Generate Return Log service', () => { const result = GenerateReturnLogService.go(testReturnRequirement, testReturnCycle) expect(result).to.equal({ - dueDate: new Date('2025-04-28'), - endDate: new Date('2025-03-31'), - id: 'v1:4:01/25/90/3242:16999651:2024-04-01:2025-03-31', + dueDate: new Date('2026-04-28'), + endDate: new Date('2026-03-31'), + id: 'v1:4:01/25/90/3242:16999651:2025-04-01:2026-03-31', licenceRef: '01/25/90/3242', metadata: { description: 'BOREHOLE AT AVALON', @@ -79,11 +79,11 @@ describe('Return Logs - Generate Return Log service', () => { ], version: 1 }, - returnCycleId: '6889b98d-964f-4966-b6d6-bf511d6526a1', + returnCycleId: '6889b98d-964f-4966-b6d6-bf511d6526a9', returnReference: '16999651', returnsFrequency: 'day', source: 'WRLS', - startDate: new Date('2024-04-01'), + startDate: new Date('2025-04-01'), status: 'due' }) }) @@ -106,7 +106,7 @@ describe('Return Logs - Generate Return Log service', () => { it('returns a unique identifier built from the region code, licence reference, legacy ID, start and end date', () => { const result = GenerateReturnLogService.go(testReturnRequirement, testReturnCycle) - expect(result.id).to.equal(`v1:4:01/25/90/3242:16999651:2024-04-01:2025-03-31`) + expect(result.id).to.equal(`v1:4:01/25/90/3242:16999651:2025-04-01:2026-03-31`) }) })