From b3467b5e66738e37f355fdc658984b75416e1306 Mon Sep 17 00:00:00 2001 From: Jonathan Goulding <58443816+jonathangoulding@users.noreply.github.com> Date: Tue, 4 Feb 2025 08:56:47 +0000 Subject: [PATCH] Refactor Determine Returns Period (#1676) * Refactor Determine Returns Period https://eaflood.atlassian.net/browse/WATER-4776 As part of the work to implement notifications in the system repo we have found some logic that will need to be duplicated in upcoming work. This change lifts the determine returns periods logic into a shared service. This can / will be used in multiple places and is complex enough to justify lifting into it own service and associated tests. --- .../setup/determine-returns-period.service.js | 58 +++++++++++++++++ .../notifications/setup/review.service.js | 20 +----- .../determine-returns-period.service.test.js | 65 +++++++++++++++++++ .../setup/review.service.test.js | 26 +------- 4 files changed, 127 insertions(+), 42 deletions(-) create mode 100644 app/services/notifications/setup/determine-returns-period.service.js create mode 100644 test/services/notifications/setup/determine-returns-period.service.test.js diff --git a/app/services/notifications/setup/determine-returns-period.service.js b/app/services/notifications/setup/determine-returns-period.service.js new file mode 100644 index 0000000000..33b34c67e2 --- /dev/null +++ b/app/services/notifications/setup/determine-returns-period.service.js @@ -0,0 +1,58 @@ +'use strict' + +/** + * Determines the returns period data for the provided returns period + * @module DetermineReturnsPeriod + */ + +const { determineUpcomingReturnPeriods } = require('../../../lib/return-periods.lib.js') + +/** + * Determines the returns period data for the provided returns period + * + * All the returns periods will be calculated from the upcoming return periods. + * + * The `returnsPeriod` will be found and returned + * + * @param {string} returnsPeriod + * + * @returns {object} - the returns period and the if the period is `summer` + */ +function go(returnsPeriod) { + return { + returnsPeriod: _returnsPeriod(returnsPeriod), + summer: _summer(returnsPeriod) + } +} + +/** + * Finds the provided returns period from the determined upcoming return periods. + * + * @private + */ +function _returnsPeriod(returnsPeriod) { + const periods = determineUpcomingReturnPeriods() + + return periods.find((period) => { + return period.name === returnsPeriod + }) +} + +/** + * When the returns period is summer then need return 'true' + * + * The string value is used when querying crm which expects a string and not a boolean + * + * @param {string} returnsPeriod + * + * @returns {string} - CRM expects a string of the boolean value ("true" | "false") + * + * @private + */ +function _summer(returnsPeriod) { + return returnsPeriod === 'summer' ? 'true' : 'false' +} + +module.exports = { + go +} diff --git a/app/services/notifications/setup/review.service.js b/app/services/notifications/setup/review.service.js index 0698caf3b6..756fbce25c 100644 --- a/app/services/notifications/setup/review.service.js +++ b/app/services/notifications/setup/review.service.js @@ -6,11 +6,11 @@ */ const DetermineRecipientsService = require('./determine-recipients.service.js') +const DetermineReturnsPeriodService = require('./determine-returns-period.service.js') const PaginatorPresenter = require('../../../presenters/paginator.presenter.js') const RecipientsService = require('./fetch-recipients.service.js') const ReviewPresenter = require('../../../presenters/notifications/setup/review.presenter.js') const SessionModel = require('../../../models/session.model.js') -const { determineUpcomingReturnPeriods } = require('../../../lib/return-periods.lib.js') /** * Orchestrates fetching and presenting the data needed for the notifications setup review page @@ -23,11 +23,9 @@ const { determineUpcomingReturnPeriods } = require('../../../lib/return-periods. async function go(sessionId, page = 1) { const session = await SessionModel.query().findById(sessionId) - const { returnsPeriod } = session - const selectedReturnsPeriod = _extractReturnPeriod(returnsPeriod) - const summer = _summer(returnsPeriod) + const { returnsPeriod, summer } = DetermineReturnsPeriodService.go(session.returnsPeriod) - const recipientsData = await RecipientsService.go(selectedReturnsPeriod.dueDate, summer) + const recipientsData = await RecipientsService.go(returnsPeriod.dueDate, summer) const recipients = DetermineRecipientsService.go(recipientsData) @@ -47,18 +45,6 @@ async function go(sessionId, page = 1) { } } -function _extractReturnPeriod(returnsPeriod) { - const periods = determineUpcomingReturnPeriods() - - return periods.find((period) => { - return period.name === returnsPeriod - }) -} - -function _summer(returnsPeriod) { - return returnsPeriod === 'summer' ? 'true' : 'false' -} - module.exports = { go } diff --git a/test/services/notifications/setup/determine-returns-period.service.test.js b/test/services/notifications/setup/determine-returns-period.service.test.js new file mode 100644 index 0000000000..f4534b2ee6 --- /dev/null +++ b/test/services/notifications/setup/determine-returns-period.service.test.js @@ -0,0 +1,65 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') +const Sinon = require('sinon') + +const { describe, it, afterEach, before, beforeEach } = (exports.lab = Lab.script()) +const { expect } = Code + +// Thing under test +const DetermineReturnsPeriodService = require('../../../../app/services/notifications/setup/determine-returns-period.service.js') + +describe('Notifications Setup - Determine returns period service', () => { + const year = 2025 + + let clock + let returnsPeriod + + before(async () => { + clock = Sinon.useFakeTimers(new Date(`${year}-01-01`)) + + returnsPeriod = 'quarterFour' + }) + + afterEach(() => { + clock.restore() + }) + + describe('when the returns period is not for summer', () => { + it('should return the returns period and summer "false"', () => { + const result = DetermineReturnsPeriodService.go(returnsPeriod) + + expect(result).to.equal({ + returnsPeriod: { + dueDate: new Date('2025-04-28'), + endDate: new Date('2025-03-31'), + name: 'quarterFour', + startDate: new Date('2025-01-01') + }, + summer: 'false' + }) + }) + }) + + describe('when the returns period is for summer', () => { + beforeEach(async () => { + returnsPeriod = 'summer' + }) + + it('should return the returns period and summer "true"', () => { + const result = DetermineReturnsPeriodService.go(returnsPeriod) + + expect(result).to.equal({ + returnsPeriod: { + dueDate: new Date('2025-11-28'), + endDate: new Date('2025-10-31'), + name: 'summer', + startDate: new Date('2024-11-01') + }, + summer: 'true' + }) + }) + }) +}) diff --git a/test/services/notifications/setup/review.service.test.js b/test/services/notifications/setup/review.service.test.js index ecd9e067f3..30a30e8610 100644 --- a/test/services/notifications/setup/review.service.test.js +++ b/test/services/notifications/setup/review.service.test.js @@ -5,7 +5,7 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') const Sinon = require('sinon') -const { describe, it, afterEach, before, beforeEach } = (exports.lab = Lab.script()) +const { describe, it, afterEach, before } = (exports.lab = Lab.script()) const { expect } = Code // Test helpers @@ -58,28 +58,4 @@ describe('Notifications Setup - Review service', () => { recipientsAmount: 1 }) }) - - describe('when the returns period is not for summer', () => { - beforeEach(async () => { - session = await SessionHelper.add({ data: { returnsPeriod: 'quarterFour' } }) - }) - - it('should call the "ReviewService" with the returns period due date and summer "false"', async () => { - await ReviewService.go(session.id) - - expect(RecipientsService.go.calledWith(new Date(`${year}-04-28`), 'false')).to.be.true() - }) - }) - - describe('when the returns period is for summer', () => { - beforeEach(async () => { - session = await SessionHelper.add({ data: { returnsPeriod: 'summer' } }) - }) - - it('should call the "ReviewService" with the returns period due date and summer "false"', async () => { - await ReviewService.go(session.id) - - expect(RecipientsService.go.calledWith(new Date(`${year}-11-28`), 'true')).to.be.true() - }) - }) })