Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create return cycle dates library #1436

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 0 additions & 116 deletions app/lib/dates.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* @module DatesLib
*/

const { returnCycleDates } = require('./static-lookups.lib.js')

const february = 2
const lastDayOfFebruary = 28
const lastDayOfFebruaryLeapYear = 29
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -226,12 +116,6 @@ function _isLeapYear (year) {
module.exports = {
formatDateObjectToISO,
formatStandardDateToISO,
cycleDueDate,
cycleDueDateAsISO,
cycleEndDate,
cycleEndDateAsISO,
cycleStartDate,
cycleStartDateAsISO,
isISODateFormat,
isValidDate
}
207 changes: 207 additions & 0 deletions app/lib/return-cycle-dates.lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
'use strict'

/**
* General helper methods
* @module DatesLib
Comment on lines +4 to +5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* General helper methods
* @module DatesLib
* Helper methods to deal with return cycle dates
* @module ReturnCycleDatesLib

*/

const { returnCycleDates } = require('./static-lookups.lib.js')
const { formatDateObjectToISO } = require('./dates.lib.js')
Comment on lines +8 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { returnCycleDates } = require('./static-lookups.lib.js')
const { formatDateObjectToISO } = require('./dates.lib.js')
const { formatDateObjectToISO } = require('./dates.lib.js')
const { returnCycleDates } = require('./static-lookups.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}`))
}
Comment on lines +54 to +71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This applies to a few of the methods in this file. Our linter is not picking this up but some of the lines have gone beyond 120 chars. I think we can resolve this and make things a bit clearer at the same time. My suggestion would be the following.

Suggested change
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}`))
}
function cycleDueDateByDate (date, summer) {
const year = date.getFullYear()
const month = date.getMonth()
let cycleDueYear
let cycleDueMonth
let cycleDueDay
if (summer) {
cycleDueDay = returnCycleDates.summer.dueDate.day
cycleDueMonth = returnCycleDates.summer.dueDate.month + 1
cycleDueYear = month > returnCycleDates.summer.endDate.month ? year + 1 : year
} else {
cycleDueDay = returnCycleDates.allYear.dueDate.day
cycleDueMonth = returnCycleDates.allYear.dueDate.month + 1
cycleDueYear = month > returnCycleDates.allYear.endDate.month ? year + 1 : year
}
const cycleDueDate = new Date(`${cycleDueYear}-${cycleDueMonth}-${cycleDueDay}`)
return formatDateObjectToISO(cycleDueDate)
}

But open to other ideas. I think the current pattern is based on my harping on about the 'guard clause' pattern. But I think this is a good example of an exception to that pattern.


/**
* 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Given an arbitary date and if it is summer or all-year return the end date of that cycle
* Given an arbitrary 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')

/**
Expand Down
6 changes: 3 additions & 3 deletions app/services/jobs/return-logs/generate-return-logs.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
Loading
Loading