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

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
robertparkinson marked this conversation as resolved.
Show resolved Hide resolved
*/

const { returnCycleDates } = require('./static-lookups.lib.js')
const { formatDateObjectToISO } = require('./dates.lib.js')
robertparkinson marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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}`))
}
robertparkinson marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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
robertparkinson marked this conversation as resolved.
Show resolved Hide resolved
*
* @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