diff --git a/app/lib/dates.lib.js b/app/lib/dates.lib.js index 6670e8c6ea..2dd9df83c7 100644 --- a/app/lib/dates.lib.js +++ b/app/lib/dates.lib.js @@ -5,8 +5,6 @@ * @module DatesLib */ -const { returnCycleDates } = require('./static-lookups.lib.js') - const february = 2 const lastDayOfFebruary = 28 const lastDayOfFebruaryLeapYear = 29 @@ -47,114 +45,6 @@ function formatDateObjectToISO (date) { return date.toISOString().split('T')[0] } -/** - * Get the due date of next provided cycle, either summer or winter and all year, formatted as YYYY-MM-DD - * - * @param {boolean} summer - true for summer, false for winter and all year. - * @returns {string} - the due date of the next cycle as an ISO string. - */ -function cycleDueDateAsISO (summer) { - return formatDateObjectToISO(cycleDueDate(summer)) -} - -/** - * Get the due date of next provided cycle, either summer or winter and all year - * - * @param {boolean} summer - true for summer, false for winter and all year. - * @returns {Date} - the due date of the next cycle. - */ -function cycleDueDate (summer) { - const today = new Date() - const year = today.getFullYear() - const month = today.getMonth() - - if (summer) { - if (month > returnCycleDates.summer.endDate.month) { - return new Date(year + 1, returnCycleDates.summer.dueDate.month, returnCycleDates.summer.dueDate.day) - } - - return new Date(year, returnCycleDates.summer.dueDate.month, returnCycleDates.summer.dueDate.day) - } - - if (month > returnCycleDates.allYear.endDate.month) { - return new Date(year + 1, returnCycleDates.allYear.dueDate.month, returnCycleDates.allYear.dueDate.day) - } - - return new Date(year, returnCycleDates.allYear.dueDate.month, returnCycleDates.allYear.dueDate.day) -} - -/** - * Get the end date of next provided cycle, either summer or winter and all year, formatted as YYYY-MM-DD - * - * @param {boolean} summer - true for summer, false for winter and all year. - * @returns {string} - the end date of the next cycle as an ISO string. - */ -function cycleEndDateAsISO (summer) { - return formatDateObjectToISO(cycleEndDate(summer)) -} - -/** - * Get the end date of next provided cycle, either summer and winter or all year - * - * @param {boolean} summer - true for summer, false for winter and all year. - * @returns {Date} - the end date of the next cycle. - */ -function cycleEndDate (summer) { - const today = new Date() - const year = today.getFullYear() - const month = today.getMonth() - - if (summer) { - if (month > returnCycleDates.summer.endDate.month) { - return new Date(year + 1, returnCycleDates.summer.endDate.month, returnCycleDates.summer.endDate.day) - } - - return new Date(year, returnCycleDates.summer.endDate.month, returnCycleDates.summer.endDate.day) - } - - if (month > returnCycleDates.allYear.endDate.month) { - return new Date(year + 1, returnCycleDates.allYear.endDate.month, returnCycleDates.allYear.endDate.day) - } - - return new Date(year, returnCycleDates.allYear.endDate.month, returnCycleDates.allYear.endDate.day) -} - -/** - * Get the start date of next provided cycle, either summer and winter and all year, formatted as YYYY-MM-DD - * - * @param {boolean} summer - true for summer, false for winter and all year. - * @returns {string} - the start date of the next cycle as an ISO string. - */ -function cycleStartDateAsISO (summer) { - return formatDateObjectToISO(cycleStartDate(summer)) -} - -/** - * Get the start date of next provided cycle, either summer or winter and all year - * - * @param {boolean} summer - true for summer, false for winter and all year. - * @returns {Date} - the start date of the next cycle. - */ -function cycleStartDate (summer) { - const today = new Date() - const year = today.getFullYear() - const month = today.getMonth() - - if (summer) { - if (month < returnCycleDates.summer.startDate.month) { - return new Date(year - 1, returnCycleDates.summer.startDate.month, returnCycleDates.summer.startDate.day) - } - - return new Date(year, returnCycleDates.summer.startDate.month, returnCycleDates.summer.startDate.day) - } - - if (month < returnCycleDates.allYear.startDate.month) { - return new Date(year - 1, returnCycleDates.allYear.startDate.month, returnCycleDates.allYear.startDate.day) - } - - return new Date(year, returnCycleDates.allYear.startDate.month, returnCycleDates.allYear.startDate.day) -} - /** * Check if a date is valid or not by creating a date and checking it gives the time * @@ -226,12 +116,6 @@ function _isLeapYear (year) { module.exports = { formatDateObjectToISO, formatStandardDateToISO, - cycleDueDate, - cycleDueDateAsISO, - cycleEndDate, - cycleEndDateAsISO, - cycleStartDate, - cycleStartDateAsISO, isISODateFormat, isValidDate } diff --git a/app/lib/return-cycle-dates.lib.js b/app/lib/return-cycle-dates.lib.js new file mode 100644 index 0000000000..73cd4e9c77 --- /dev/null +++ b/app/lib/return-cycle-dates.lib.js @@ -0,0 +1,207 @@ +'use strict' + +/** + * General helper methods + * @module DatesLib + */ + +const { returnCycleDates } = require('./static-lookups.lib.js') +const { formatDateObjectToISO } = require('./dates.lib.js') + +/** + * Get the due date of next provided cycle, either summer or winter and all year, formatted as YYYY-MM-DD + * + * @param {boolean} summer - true for summer, false for winter and all year. + * @returns {string} - the due date of the next cycle as an ISO string. + */ +function cycleDueDateAsISO (summer) { + return formatDateObjectToISO(cycleDueDate(summer)) +} + +/** + * Get the due date of next provided cycle, either summer or winter and all year + * + * @param {boolean} summer - true for summer, false for winter and all year. + * @returns {Date} - the due date of the next cycle. + */ +function cycleDueDate (summer) { + const today = new Date() + const year = today.getFullYear() + const month = today.getMonth() + + if (summer) { + if (month > returnCycleDates.summer.endDate.month) { + return new Date(year + 1, returnCycleDates.summer.dueDate.month, returnCycleDates.summer.dueDate.day) + } + + return new Date(year, returnCycleDates.summer.dueDate.month, returnCycleDates.summer.dueDate.day) + } + + if (month > returnCycleDates.allYear.endDate.month) { + return new Date(year + 1, returnCycleDates.allYear.dueDate.month, returnCycleDates.allYear.dueDate.day) + } + + return new Date(year, returnCycleDates.allYear.dueDate.month, returnCycleDates.allYear.dueDate.day) +} + +/** + * Given an arbitary date and if it is summer or all-year return the due date of that cycle + * + * @param {Date} date - the date whose due date you want to find. + * @param {boolean} summer - true for summer, false for winter and all year. + * @returns {string} - the due date of the next cycle. + */ +function cycleDueDateByDate (date, summer) { + const year = date.getFullYear() + const month = date.getMonth() + + if (summer) { + if (month > returnCycleDates.summer.endDate.month) { + return formatDateObjectToISO(new Date(`${year + 1}-${returnCycleDates.summer.dueDate.month + 1}-${returnCycleDates.summer.dueDate.day}`)) + } + + return formatDateObjectToISO(new Date(`${year}-${returnCycleDates.summer.dueDate.month + 1}-${returnCycleDates.summer.dueDate.day}`)) + } + + if (month > returnCycleDates.allYear.endDate.month) { + return formatDateObjectToISO(new Date(`${year + 1}-${returnCycleDates.allYear.dueDate.month + 1}-${returnCycleDates.allYear.dueDate.day}`)) + } + + return formatDateObjectToISO(new Date(`${year}-${returnCycleDates.allYear.dueDate.month + 1}-${returnCycleDates.allYear.dueDate.day}`)) +} + +/** + * Get the end date of next provided cycle, either summer or winter and all year, formatted as YYYY-MM-DD + * + * @param {boolean} summer - true for summer, false for winter and all year. + * @returns {string} - the end date of the next cycle as an ISO string. + */ +function cycleEndDateAsISO (summer) { + return formatDateObjectToISO(cycleEndDate(summer)) +} + +/** + * Get the end date of next provided cycle, either summer and winter or all year + * + * @param {boolean} summer - true for summer, false for winter and all year. + * @returns {Date} - the end date of the next cycle. + */ +function cycleEndDate (summer) { + const today = new Date() + const year = today.getFullYear() + const month = today.getMonth() + + if (summer) { + if (month > returnCycleDates.summer.endDate.month) { + return new Date(year + 1, returnCycleDates.summer.endDate.month, returnCycleDates.summer.endDate.day) + } + + return new Date(year, returnCycleDates.summer.endDate.month, returnCycleDates.summer.endDate.day) + } + + if (month > returnCycleDates.allYear.endDate.month) { + return new Date(year + 1, returnCycleDates.allYear.endDate.month, returnCycleDates.allYear.endDate.day) + } + + return new Date(year, returnCycleDates.allYear.endDate.month, returnCycleDates.allYear.endDate.day) +} + +/** + * Given an arbitary date and if it is summer or all-year return the end date of that cycle + * + * @param {Date} date - the date whose start date you want to find. + * @param {boolean} summer - true for summer, false for winter and all year. + * @returns {Date} - the start date of the next cycle. + */ +function cycleEndDateByDate (date, summer) { + const year = date.getFullYear() + const month = date.getMonth() + + if (summer) { + if (month > returnCycleDates.summer.endDate.month) { + return formatDateObjectToISO(new Date(`${year + 1}-${returnCycleDates.summer.endDate.month + 1}-${returnCycleDates.summer.endDate.day}`)) + } + + return formatDateObjectToISO(new Date(`${year}-${returnCycleDates.summer.endDate.month + 1}-${returnCycleDates.summer.endDate.day}`)) + } + + if (month > returnCycleDates.allYear.endDate.month) { + return formatDateObjectToISO(new Date(`${year + 1}-${returnCycleDates.allYear.endDate.month + 1}-${returnCycleDates.allYear.endDate.day}`)) + } + + return formatDateObjectToISO(new Date(`${year}-${returnCycleDates.allYear.endDate.month + 1}-${returnCycleDates.allYear.endDate.day}`)) +} + +/** + * Get the start date of next provided cycle, either summer and winter and all year, formatted as YYYY-MM-DD + * + * @param {boolean} summer - true for summer, false for winter and all year. + * @returns {string} - the start date of the next cycle as an ISO string. + */ +function cycleStartDateAsISO (summer) { + return formatDateObjectToISO(cycleStartDate(summer)) +} + +/** + * Get the start date of next provided cycle, either summer or winter and all year + * + * @param {boolean} summer - true for summer, false for winter and all year. + * @returns {Date} - the start date of the next cycle. + */ +function cycleStartDate (summer) { + const today = new Date() + const year = today.getFullYear() + const month = today.getMonth() + + if (summer) { + if (month < returnCycleDates.summer.startDate.month) { + return new Date(year - 1, returnCycleDates.summer.startDate.month, returnCycleDates.summer.startDate.day) + } + + return new Date(year, returnCycleDates.summer.startDate.month, returnCycleDates.summer.startDate.day) + } + + if (month < returnCycleDates.allYear.startDate.month) { + return new Date(year - 1, returnCycleDates.allYear.startDate.month, returnCycleDates.allYear.startDate.day) + } + + return new Date(year, returnCycleDates.allYear.startDate.month, returnCycleDates.allYear.startDate.day) +} + +/** + * Given an arbitary date and if it is summer or all-year return the start date of that cycle + * + * @param {Date} date - the date whose start date you want to find. + * @param {boolean} summer - true for summer, false for winter and all year. + * @returns {Date} - the start date of the next cycle. + */ +function cycleStartDateByDate (date, summer) { + const year = date.getFullYear() + const month = date.getMonth() + + if (summer) { + if (month < returnCycleDates.summer.startDate.month) { + return formatDateObjectToISO(new Date(`${year - 1}-${returnCycleDates.summer.startDate.month + 1}-${returnCycleDates.summer.startDate.day}`)) + } + + return formatDateObjectToISO(new Date(`${year}-${returnCycleDates.summer.startDate.month + 1}-${returnCycleDates.summer.startDate.day}`)) + } + + if (month < returnCycleDates.allYear.startDate.month) { + return formatDateObjectToISO(new Date(`${year - 1}-${returnCycleDates.allYear.startDate.month + 1}-${returnCycleDates.allYear.startDate.day}`)) + } + + return formatDateObjectToISO(new Date(`${year}-${returnCycleDates.allYear.startDate.month + 1}-${returnCycleDates.allYear.startDate.day}`)) +} + +module.exports = { + cycleDueDate, + cycleDueDateByDate, + cycleDueDateAsISO, + cycleEndDate, + cycleEndDateByDate, + cycleEndDateAsISO, + cycleStartDate, + cycleStartDateByDate, + cycleStartDateAsISO +} diff --git a/app/services/jobs/return-logs/fetch-licence-return-requirements.service.js b/app/services/jobs/return-logs/fetch-licence-return-requirements.service.js index 8e11a55309..8dfacd256f 100644 --- a/app/services/jobs/return-logs/fetch-licence-return-requirements.service.js +++ b/app/services/jobs/return-logs/fetch-licence-return-requirements.service.js @@ -10,7 +10,7 @@ const ReturnRequirementModel = require('../../../models/return-requirement.model const ReturnVersionModel = require('../../../models/return-version.model.js') const { db } = require('../../../../db/db.js') -const { cycleEndDateAsISO, cycleStartDateAsISO, cycleStartDate } = require('../../../lib/dates.lib.js') +const { cycleEndDateAsISO, cycleStartDateAsISO, cycleStartDate } = require('../../../lib/return-cycle-dates.lib.js') /** * Given the licence reference this service returns the return requirements to be turned into return logs. 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 bb724fddd7..490c18131c 100644 --- a/app/services/jobs/return-logs/fetch-return-requirements.service.js +++ b/app/services/jobs/return-logs/fetch-return-requirements.service.js @@ -10,7 +10,7 @@ const ReturnRequirementModel = require('../../../models/return-requirement.model const ReturnVersionModel = require('../../../models/return-version.model.js') const { db } = require('../../../../db/db.js') -const { cycleEndDateAsISO, cycleStartDateAsISO } = require('../../../lib/dates.lib.js') +const { cycleEndDateAsISO, cycleStartDateAsISO } = require('../../../lib/return-cycle-dates.lib.js') /** * Fetch all return requirements that need return logs created. diff --git a/app/services/jobs/return-logs/generate-return-cycle.service.js b/app/services/jobs/return-logs/generate-return-cycle.service.js index 32f47c5754..02d7bc7fb1 100644 --- a/app/services/jobs/return-logs/generate-return-cycle.service.js +++ b/app/services/jobs/return-logs/generate-return-cycle.service.js @@ -5,7 +5,7 @@ * @module GenerateReturnCycleService */ -const { cycleDueDateAsISO, cycleEndDateAsISO, cycleStartDateAsISO } = require('../../../lib/dates.lib.js') +const { cycleDueDateAsISO, cycleEndDateAsISO, cycleStartDateAsISO } = require('../../../lib/return-cycle-dates.lib.js') const ReturnCycleModel = require('../../../models/return-cycle.model.js') /** diff --git a/app/services/jobs/return-logs/generate-return-logs.service.js b/app/services/jobs/return-logs/generate-return-logs.service.js index c0eb95c9a9..1de78d4a7c 100644 --- a/app/services/jobs/return-logs/generate-return-logs.service.js +++ b/app/services/jobs/return-logs/generate-return-logs.service.js @@ -10,9 +10,9 @@ const { cycleEndDate, cycleEndDateAsISO, cycleStartDateAsISO, - cycleStartDate, - formatDateObjectToISO -} = require('../../../lib/dates.lib.js') + cycleStartDate +} = require('../../../lib/return-cycle-dates.lib.js') +const { formatDateObjectToISO } = require('../../../lib/dates.lib.js') const FetchReturnCycleService = require('./fetch-return-cycle.service.js') const GenerateReturnCycleService = require('./generate-return-cycle.service.js') diff --git a/test/lib/dates.lib.test.js b/test/lib/dates.lib.test.js index 1638a292ff..2694d7a7b1 100644 --- a/test/lib/dates.lib.test.js +++ b/test/lib/dates.lib.test.js @@ -4,21 +4,13 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it } = exports.lab = Lab.script() const { expect } = Code -const { returnCycleDates } = require('../../app/lib/static-lookups.lib.js') - // Thing under test const DateLib = require('../../app/lib/dates.lib.js') describe('Dates lib', () => { - const today = new Date() - const month = today.getMonth() - - let expectedDate - let summer - describe('formatStandardDateToISO', () => { it('returns null if the date is null ', () => { const result = DateLib.formatStandardDateToISO(null) @@ -94,234 +86,4 @@ describe('Dates lib', () => { expect(result).to.be.true() }) }) - - describe('cycleDueDate', () => { - describe('when the requested cycle is "summer"', () => { - beforeEach(() => { - summer = true - - if (month > returnCycleDates.summer.endDate.month) { - expectedDate = new Date(new Date().getFullYear() + 1, 10, 28) - } else { - expectedDate = new Date(new Date().getFullYear(), 10, 28) - } - }) - - it('should return the due date for the current summer cycle', () => { - const result = DateLib.cycleDueDate(summer) - - expect(result).to.equal(expectedDate) - }) - }) - - describe('when the requested cycle is "winter and all year"', () => { - beforeEach(() => { - summer = false - - if (month > returnCycleDates.allYear.endDate.month) { - expectedDate = new Date(new Date().getFullYear() + 1, 3, 28) - } else { - expectedDate = new Date(new Date().getFullYear(), 3, 28) - } - }) - - it('should return the due date of the current winter and all year cycle', () => { - const result = DateLib.cycleDueDate(summer) - - expect(result).to.equal(expectedDate) - }) - }) - }) - - describe('cycleDueDateAsISO', () => { - describe('when the requested cycle is "summer"', () => { - beforeEach(() => { - summer = true - - if (month > returnCycleDates.summer.endDate.month) { - expectedDate = new Date(new Date().getFullYear() + 1, 10, 28).toISOString().split('T')[0] - } else { - expectedDate = new Date(new Date().getFullYear(), 10, 28).toISOString().split('T')[0] - } - }) - - it('should return the due date for the current summer cycle formatted as YYYY-MM-DD', () => { - const result = DateLib.cycleDueDateAsISO(summer) - - expect(result).to.equal(expectedDate) - }) - }) - - describe('when the requested cycle is "winter and all year"', () => { - beforeEach(() => { - summer = false - - if (month > returnCycleDates.allYear.endDate.month) { - expectedDate = new Date(new Date().getFullYear() + 1, 3, 28).toISOString().split('T')[0] - } else { - expectedDate = new Date(new Date().getFullYear(), 3, 28).toISOString().split('T')[0] - } - }) - - it('should return the due date of the current winter and all year cycle formatted as YYYY-MM-DD', () => { - const result = DateLib.cycleDueDateAsISO(summer) - - expect(result).to.equal(expectedDate) - }) - }) - }) - - describe('cycleEndDate', () => { - describe('when the requested cycle is "summer"', () => { - beforeEach(() => { - summer = true - - if (month > returnCycleDates.summer.endDate.month) { - expectedDate = new Date(new Date().getFullYear() + 1, 9, 31) - } else { - expectedDate = new Date(new Date().getFullYear(), 9, 31) - } - }) - - it('should return the due date for the current summer cycle', () => { - const result = DateLib.cycleEndDate(summer) - - expect(result).to.equal(expectedDate) - }) - }) - - describe('when the requested cycle is "winter and all year"', () => { - beforeEach(() => { - summer = false - - if (month > returnCycleDates.allYear.endDate.month) { - expectedDate = new Date(new Date().getFullYear() + 1, 2, 31) - } else { - expectedDate = new Date(new Date().getFullYear(), 2, 31) - } - }) - - it('should return the due date of the current winter and all year cycle', () => { - const result = DateLib.cycleEndDate(summer) - - expect(result).to.equal(expectedDate) - }) - }) - }) - - describe('cycleEndDateAsISO', () => { - describe('when the requested cycle is "summer"', () => { - beforeEach(() => { - summer = true - - if (month > returnCycleDates.summer.endDate.month) { - expectedDate = new Date(new Date().getFullYear() + 1, 9, 31).toISOString().split('T')[0] - } else { - expectedDate = new Date(new Date().getFullYear(), 9, 31).toISOString().split('T')[0] - } - }) - - it('should return the due date for the current summer cycle formatted as YYYY-MM-DD', () => { - const result = DateLib.cycleEndDateAsISO(summer) - - expect(result).to.equal(expectedDate) - }) - }) - - describe('when the requested cycle is "winter and all year"', () => { - beforeEach(() => { - summer = false - - if (month > returnCycleDates.allYear.endDate.month) { - expectedDate = new Date(new Date().getFullYear() + 1, 2, 31).toISOString().split('T')[0] - } else { - expectedDate = new Date(new Date().getFullYear(), 2, 31).toISOString().split('T')[0] - } - }) - - it('should return the due date of the current winter and all year cycle formatted as YYYY-MM-DD', () => { - const result = DateLib.cycleEndDateAsISO(summer) - - expect(result).to.equal(expectedDate) - }) - }) - }) - - describe('cycleStartDate', () => { - describe('when the requested cycle is "summer"', () => { - beforeEach(() => { - summer = true - - if (month < returnCycleDates.summer.startDate.month) { - expectedDate = new Date(new Date().getFullYear() - 1, 10, 1) - } else { - expectedDate = new Date(new Date().getFullYear(), 10, 1) - } - }) - - it('should return the start date for the current summer cycle', () => { - const result = DateLib.cycleStartDate(summer) - - expect(result).to.equal(expectedDate) - }) - }) - - describe('when the requested cycle is "winter and all year"', () => { - beforeEach(() => { - summer = false - - if (month < returnCycleDates.allYear.startDate.month) { - expectedDate = new Date(new Date().getFullYear() - 1, 3, 1) - } else { - expectedDate = new Date(new Date().getFullYear(), 3, 1) - } - }) - - it('should return the due date of the current winter and all year cycle', () => { - const result = DateLib.cycleStartDate(summer) - - expect(result).to.equal(expectedDate) - }) - }) - }) - - describe('cycleStartDateAsISO', () => { - describe('when the requested cycle is "summer"', () => { - beforeEach(() => { - summer = true - - if (month < returnCycleDates.summer.startDate.month) { - expectedDate = new Date(new Date().getFullYear() - 1, 10, 1).toISOString().split('T')[0] - } else { - expectedDate = new Date(new Date().getFullYear(), 10, 1).toISOString().split('T')[0] - } - }) - - it('should return the start date for the current summer cycle formatted as YYYY-MM-DD', () => { - const result = DateLib.cycleStartDateAsISO(summer) - - expect(result).to.equal(expectedDate) - }) - }) - - describe('when the requested cycle is "winter and all year"', () => { - beforeEach(() => { - summer = false - - if (month < returnCycleDates.allYear.startDate.month) { - expectedDate = new Date(new Date().getFullYear() - 1, 3, 1).toISOString().split('T')[0] - } else { - expectedDate = new Date(new Date().getFullYear(), 3, 1).toISOString().split('T')[0] - } - - expectedDate = new Date(new Date().getFullYear(), 3, 1).toISOString().split('T')[0] - }) - - it('should return the due date of the current winter and all year cycle formatted as YYYY-MM-DD', () => { - const result = DateLib.cycleStartDateAsISO(summer) - - expect(result).to.equal(expectedDate) - }) - }) - }) }) diff --git a/test/lib/return-cycle-dates.lib.test.js b/test/lib/return-cycle-dates.lib.test.js new file mode 100644 index 0000000000..7770db110c --- /dev/null +++ b/test/lib/return-cycle-dates.lib.test.js @@ -0,0 +1,562 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') + +const { describe, it, before, beforeEach } = exports.lab = Lab.script() +const { expect } = Code + +const { returnCycleDates } = require('../../app/lib/static-lookups.lib.js') + +// Thing under test +const ReturnCycleDatesLib = require('../../app/lib/return-cycle-dates.lib.js') + +describe('Return Cycle Dates lib', () => { + const today = new Date() + const year = today.getFullYear() + const month = today.getMonth() + + let expectedDate + let summer + + describe('cycleDueDate', () => { + describe('when the requested cycle is "summer"', () => { + beforeEach(() => { + summer = true + + if (month > returnCycleDates.summer.endDate.month) { + expectedDate = new Date( + new Date().getFullYear() + 1, + returnCycleDates.summer.dueDate.month, + returnCycleDates.summer.dueDate.day + ) + } else { + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.summer.dueDate.month, + returnCycleDates.summer.dueDate.day + ) + } + }) + + it('should return the due date for the current summer cycle', () => { + const result = ReturnCycleDatesLib.cycleDueDate(summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when the requested cycle is "winter and all year"', () => { + beforeEach(() => { + summer = false + + if (month > returnCycleDates.allYear.endDate.month) { + expectedDate = new Date( + new Date().getFullYear() + 1, + returnCycleDates.allYear.dueDate.month, + returnCycleDates.allYear.dueDate.day + ) + } else { + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.allYear.dueDate.month, + returnCycleDates.allYear.dueDate.day + ) + } + }) + + it('should return the due date of the current winter and all year cycle', () => { + const result = ReturnCycleDatesLib.cycleDueDate(summer) + + expect(result).to.equal(expectedDate) + }) + }) + }) + + describe('cycleDueDateAsISO', () => { + describe('when the requested cycle is "summer"', () => { + beforeEach(() => { + summer = true + + if (month > returnCycleDates.summer.endDate.month) { + expectedDate = new Date( + new Date().getFullYear() + 1, + returnCycleDates.summer.dueDate.month, + returnCycleDates.summer.dueDate.day + ).toISOString().split('T')[0] + } else { + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.summer.dueDate.month, + returnCycleDates.summer.dueDate.day + ).toISOString().split('T')[0] + } + }) + + describe('when the requested cycle is "winter and all year"', () => { + beforeEach(() => { + summer = false + + if (month > returnCycleDates.allYear.endDate.month) { + expectedDate = new Date( + new Date().getFullYear() + 1, + returnCycleDates.allYear.dueDate.month, + returnCycleDates.allYear.dueDate.day + ).toISOString().split('T')[0] + } else { + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.allYear.dueDate.month, + returnCycleDates.allYear.dueDate.day + ).toISOString().split('T')[0] + } + }) + + it('should return the due date of the current winter and all year cycle formatted as YYYY-MM-DD', () => { + const result = ReturnCycleDatesLib.cycleDueDateAsISO(summer) + + expect(result).to.equal(expectedDate) + }) + }) + }) + }) + + describe('cycleEndDate', () => { + describe('when the requested cycle is "summer"', () => { + beforeEach(() => { + summer = true + + if (month > returnCycleDates.summer.endDate.month) { + expectedDate = new Date( + new Date().getFullYear() + 1, + returnCycleDates.summer.endDate.month, + returnCycleDates.summer.endDate.day + ) + } else { + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.summer.endDate.month, + returnCycleDates.summer.endDate.day + ) + } + }) + + it('should return the due date for the current summer cycle', () => { + const result = ReturnCycleDatesLib.cycleEndDate(summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when the requested cycle is "winter and all year"', () => { + beforeEach(() => { + summer = false + + if (month > returnCycleDates.allYear.endDate.month) { + expectedDate = new Date( + new Date().getFullYear() + 1, + returnCycleDates.allYear.endDate.month, + returnCycleDates.allYear.endDate.day + ) + } else { + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.allYear.endDate.month, + returnCycleDates.allYear.endDate.day + ) + } + }) + + it('should return the due date of the current winter and all year cycle', () => { + const result = ReturnCycleDatesLib.cycleEndDate(summer) + + expect(result).to.equal(expectedDate) + }) + }) + }) + + describe('cycleEndDateAsISO', () => { + describe('when the requested cycle is "summer"', () => { + beforeEach(() => { + summer = true + + if (month > returnCycleDates.summer.endDate.month) { + expectedDate = new Date( + new Date().getFullYear() + 1, + returnCycleDates.summer.endDate.month, + returnCycleDates.summer.endDate.day + ).toISOString().split('T')[0] + } else { + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.summer.endDate.month, + returnCycleDates.summer.endDate.day + ).toISOString().split('T')[0] + } + }) + + it('should return the due date for the current summer cycle formatted as YYYY-MM-DD', () => { + const result = ReturnCycleDatesLib.cycleEndDateAsISO(summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when the requested cycle is "winter and all year"', () => { + beforeEach(() => { + summer = false + + if (month > returnCycleDates.allYear.endDate.month) { + expectedDate = new Date( + new Date().getFullYear() + 1, + returnCycleDates.allYear.endDate.month, + returnCycleDates.allYear.endDate.day + ).toISOString().split('T')[0] + } else { + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.allYear.endDate.month, + returnCycleDates.allYear.endDate.day + ).toISOString().split('T')[0] + } + }) + + it('should return the due date of the current winter and all year cycle formatted as YYYY-MM-DD', () => { + const result = ReturnCycleDatesLib.cycleEndDateAsISO(summer) + + expect(result).to.equal(expectedDate) + }) + }) + }) + + describe('cycleStartDate', () => { + describe('when the requested cycle is "summer"', () => { + beforeEach(() => { + summer = true + + if (month < returnCycleDates.summer.startDate.month) { + expectedDate = new Date( + new Date().getFullYear() - 1, + returnCycleDates.summer.startDate.month, + returnCycleDates.summer.startDate.day + ) + } else { + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.summer.startDate.month, + returnCycleDates.summer.startDate.day + ) + } + }) + + it('should return the start date for the current summer cycle', () => { + const result = ReturnCycleDatesLib.cycleStartDate(summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when the requested cycle is "winter and all year"', () => { + beforeEach(() => { + summer = false + + if (month < returnCycleDates.allYear.startDate.month) { + expectedDate = new Date( + new Date().getFullYear() - 1, + returnCycleDates.allYear.startDate.month, + returnCycleDates.allYear.startDate.day + ) + } else { + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.allYear.startDate.month, + returnCycleDates.allYear.startDate.day + ) + } + }) + + it('should return the due date of the current winter and all year cycle', () => { + const result = ReturnCycleDatesLib.cycleStartDate(summer) + + expect(result).to.equal(expectedDate) + }) + }) + }) + + describe('cycleStartDateAsISO', () => { + describe('when the requested cycle is "summer"', () => { + beforeEach(() => { + summer = true + + if (month < returnCycleDates.summer.startDate.month) { + expectedDate = new Date( + new Date().getFullYear() - 1, + returnCycleDates.summer.startDate.month, + returnCycleDates.summer.startDate.day + ).toISOString().split('T')[0] + } else { + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.summer.startDate.month, + returnCycleDates.summer.startDate.day + ).toISOString().split('T')[0] + } + }) + + it('should return the start date for the current summer cycle formatted as YYYY-MM-DD', () => { + const result = ReturnCycleDatesLib.cycleStartDateAsISO(summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when the requested cycle is "winter and all year"', () => { + beforeEach(() => { + summer = false + + if (month < returnCycleDates.allYear.startDate.month) { + expectedDate = new Date( + new Date().getFullYear() - 1, + returnCycleDates.allYear.startDate.month, + returnCycleDates.allYear.startDate.day + ).toISOString().split('T')[0] + } else { + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.allYear.startDate.month, + returnCycleDates.allYear.startDate.day + ).toISOString().split('T')[0] + } + }) + + it('should return the due date of the current winter and all year cycle formatted as YYYY-MM-DD', () => { + const result = ReturnCycleDatesLib.cycleStartDateAsISO(summer) + + expect(result).to.equal(expectedDate) + }) + }) + }) + + describe('cycleDueDateByDate', () => { + describe('when summer is true and given a date in december last year', () => { + before(() => { + summer = true + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.summer.dueDate.month, + returnCycleDates.summer.dueDate.day + ).toISOString().split('T')[0] + }) + + it('should return the correct due date for the summer cycle formatted as YYYY-MM-DD', () => { + const testDate = new Date(`${year - 1}-12-01`) + const result = ReturnCycleDatesLib.cycleDueDateByDate(testDate, summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when summer is true and given a date in september last year', () => { + before(() => { + summer = true + expectedDate = new Date( + new Date().getFullYear() - 1, + returnCycleDates.summer.dueDate.month, + returnCycleDates.summer.dueDate.day + ).toISOString().split('T')[0] + }) + + it('should return the correct due date for the summer cycle formatted as YYYY-MM-DD', () => { + const testDate = new Date(`${year - 1}-09-01`) + const result = ReturnCycleDatesLib.cycleDueDateByDate(testDate, summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when summer is false and given a date in may last year', () => { + before(() => { + summer = false + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.allYear.dueDate.month, + returnCycleDates.allYear.dueDate.day + ).toISOString().split('T')[0] + }) + + it('should return the correct due date for the all year cycle formatted as YYYY-MM-DD', () => { + const testDate = new Date(`${year - 1}-05-01`) + const result = ReturnCycleDatesLib.cycleDueDateByDate(testDate, summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when summer is false and given a date in march last year', () => { + before(() => { + summer = false + expectedDate = new Date( + new Date().getFullYear() - 1, + returnCycleDates.allYear.dueDate.month, + returnCycleDates.allYear.dueDate.day + ).toISOString().split('T')[0] + }) + + it('should return the correct due date for the all year cycle formatted as YYYY-MM-DD', () => { + const testDate = new Date(`${year - 1}-03-01`) + const result = ReturnCycleDatesLib.cycleDueDateByDate(testDate, summer) + + expect(result).to.equal(expectedDate) + }) + }) + }) + + describe('cycleEndDateByDate', () => { + describe('when summer is true and given a date in december last year', () => { + before(() => { + summer = true + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.summer.endDate.month, + returnCycleDates.summer.endDate.day + ).toISOString().split('T')[0] + }) + + it('should return the correct end date for the summer cycle formatted as YYYY-MM-DD', () => { + const testDate = new Date(`${year - 1}-12-01`) + const result = ReturnCycleDatesLib.cycleEndDateByDate(testDate, summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when summer is true and given a date in september last year', () => { + before(() => { + summer = true + expectedDate = new Date( + new Date().getFullYear() - 1, + returnCycleDates.summer.endDate.month, + returnCycleDates.summer.endDate.day + ).toISOString().split('T')[0] + }) + + it('should return the correct end date for the summer cycle formatted as YYYY-MM-DD', () => { + const testDate = new Date(`${year - 1}-09-01`) + const result = ReturnCycleDatesLib.cycleEndDateByDate(testDate, summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when summer is false and given a date in may last year', () => { + before(() => { + summer = false + expectedDate = new Date( + new Date().getFullYear(), + returnCycleDates.allYear.endDate.month, + returnCycleDates.allYear.endDate.day + ).toISOString().split('T')[0] + }) + + it('should return the correct end date for the all year cycle formatted as YYYY-MM-DD', () => { + const testDate = new Date(`${year - 1}-05-01`) + const result = ReturnCycleDatesLib.cycleEndDateByDate(testDate, summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when summer is false and given a date in march last year', () => { + before(() => { + summer = false + expectedDate = new Date( + new Date().getFullYear() - 1, + returnCycleDates.allYear.endDate.month, + returnCycleDates.allYear.endDate.day + ).toISOString().split('T')[0] + }) + + it('should return the correct end date for the all year cycle formatted as YYYY-MM-DD', () => { + const testDate = new Date(`${year - 1}-03-01`) + const result = ReturnCycleDatesLib.cycleEndDateByDate(testDate, summer) + + expect(result).to.equal(expectedDate) + }) + }) + }) + + describe('cycleStartDateByDate', () => { + describe('when summer is true and given a date in december last year', () => { + before(() => { + summer = true + expectedDate = new Date( + new Date().getFullYear() - 1, + returnCycleDates.summer.startDate.month, + returnCycleDates.summer.startDate.day + ).toISOString().split('T')[0] + }) + + it('should return the correct end date for the summer cycle formatted as YYYY-MM-DD', () => { + const testDate = new Date(`${year - 1}-12-01`) + const result = ReturnCycleDatesLib.cycleStartDateByDate(testDate, summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when summer is true and given a date in september last year', () => { + before(() => { + summer = true + expectedDate = new Date( + new Date().getFullYear() - 2, + returnCycleDates.summer.startDate.month, + returnCycleDates.summer.startDate.day + ).toISOString().split('T')[0] + }) + + it('should return the correct end date for the summer cycle formatted as YYYY-MM-DD', () => { + const testDate = new Date(`${year - 1}-09-01`) + const result = ReturnCycleDatesLib.cycleStartDateByDate(testDate, summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when summer is false and given a date in may last year', () => { + before(() => { + summer = false + expectedDate = new Date( + new Date().getFullYear() - 1, + returnCycleDates.allYear.startDate.month, + returnCycleDates.allYear.startDate.day + ).toISOString().split('T')[0] + }) + + it('should return the correct end date for the all year cycle formatted as YYYY-MM-DD', () => { + const testDate = new Date(`${year - 1}-05-01`) + const result = ReturnCycleDatesLib.cycleStartDateByDate(testDate, summer) + + expect(result).to.equal(expectedDate) + }) + }) + + describe('when summer is false and given a date in march last year', () => { + before(() => { + summer = false + expectedDate = new Date( + new Date().getFullYear() - 2, + returnCycleDates.allYear.startDate.month, + returnCycleDates.allYear.startDate.day + ).toISOString().split('T')[0] + }) + + it('should return the correct end date for the all year cycle formatted as YYYY-MM-DD', () => { + const testDate = new Date(`${year - 1}-03-01`) + const result = ReturnCycleDatesLib.cycleStartDateByDate(testDate, summer) + + expect(result).to.equal(expectedDate) + }) + }) + }) +}) diff --git a/test/services/jobs/return-logs/generate-return-cycle.service.test.js b/test/services/jobs/return-logs/generate-return-cycle.service.test.js index 5069956faa..6f7973632d 100644 --- a/test/services/jobs/return-logs/generate-return-cycle.service.test.js +++ b/test/services/jobs/return-logs/generate-return-cycle.service.test.js @@ -8,7 +8,7 @@ const { describe, it } = exports.lab = Lab.script() const { expect } = Code // Test Helpers -const { cycleDueDateAsISO, cycleEndDateAsISO, cycleStartDateAsISO } = require('../../../../app/lib/dates.lib.js') +const { cycleDueDateAsISO, cycleEndDateAsISO, cycleStartDateAsISO } = require('../../../../app/lib/return-cycle-dates.lib.js') const ReturnCycleModel = require('../../../../app/models/return-cycle.model.js') // Thing under test