-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
2195037
commit b3467b5
Showing
4 changed files
with
127 additions
and
42 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
app/services/notifications/setup/determine-returns-period.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
test/services/notifications/setup/determine-returns-period.service.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters