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 6 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
}
243 changes: 243 additions & 0 deletions app/lib/return-cycle-dates.lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
'use strict'

/**
* Helper methods to deal with return cycle dates
* @module ReturnCycleDatesLib
*/

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()

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 cycleDueDate
}

/**
* 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()

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)
}

/**
* 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()

let cycleEndYear
let cycleEndMonth
let cycleEndDay

if (summer) {
cycleEndDay = returnCycleDates.summer.endDate.day
cycleEndMonth = returnCycleDates.summer.endDate.month + 1

cycleEndYear = month > returnCycleDates.summer.endDate.month ? year + 1 : year
} else {
cycleEndDay = returnCycleDates.allYear.endDate.day
cycleEndMonth = returnCycleDates.allYear.endDate.month + 1

cycleEndYear = month > returnCycleDates.allYear.endDate.month ? year + 1 : year
}

const cycleEndDate = new Date(`${cycleEndYear}-${cycleEndMonth}-${cycleEndDay}`)

return cycleEndDate
}

/**
* 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()

let cycleEndYear
let cycleEndMonth
let cycleEndDay

if (summer) {
cycleEndDay = returnCycleDates.summer.endDate.day
cycleEndMonth = returnCycleDates.summer.endDate.month + 1

cycleEndYear = month > returnCycleDates.summer.endDate.month ? year + 1 : year
} else {
cycleEndDay = returnCycleDates.allYear.endDate.day
cycleEndMonth = returnCycleDates.allYear.endDate.month + 1

cycleEndYear = month > returnCycleDates.allYear.endDate.month ? year + 1 : year
}

const cycleEndDate = new Date(`${cycleEndYear}-${cycleEndMonth}-${cycleEndDay}`)

return formatDateObjectToISO(cycleEndDate)
}

/**
* 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()

let cycleStartYear
let cycleStartMonth
let cycleStartDay

if (summer) {
cycleStartDay = returnCycleDates.summer.startDate.day
cycleStartMonth = returnCycleDates.summer.startDate.month + 1

cycleStartYear = month < returnCycleDates.summer.startDate.month ? year - 1 : year
} else {
cycleStartDay = returnCycleDates.allYear.startDate.day
cycleStartMonth = returnCycleDates.allYear.startDate.month + 1

cycleStartYear = month < returnCycleDates.allYear.startDate.month ? year - 1 : year
}

const cycleEndDate = new Date(`${cycleStartYear}-${cycleStartMonth}-${cycleStartDay}`)

return cycleEndDate
}

/**
* 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()

let cycleStartYear
let cycleStartMonth
let cycleStartDay

if (summer) {
cycleStartDay = returnCycleDates.summer.startDate.day
cycleStartMonth = returnCycleDates.summer.startDate.month + 1

cycleStartYear = month < returnCycleDates.summer.startDate.month ? year - 1 : year
} else {
cycleStartDay = returnCycleDates.allYear.startDate.day
cycleStartMonth = returnCycleDates.allYear.startDate.month + 1

cycleStartYear = month < returnCycleDates.allYear.startDate.month ? year - 1 : year
}

const cycleEndDate = new Date(`${cycleStartYear}-${cycleStartMonth}-${cycleStartDay}`)

return formatDateObjectToISO(cycleEndDate)
}

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
Loading
Loading