Skip to content

Commit

Permalink
MAN-167 refactor date range validation
Browse files Browse the repository at this point in the history
  • Loading branch information
neil-mills committed Jan 24, 2025
1 parent 297b37b commit 2230587
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 46 deletions.
98 changes: 54 additions & 44 deletions server/middleware/validation/activityLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,82 +19,92 @@ const activityLog: Route<void> = (req, res, next) => {
return DateTime.fromISO(DateTime.local(parseInt(year, 10), parseInt(month, 10), parseInt(day, 10)).toISODate())
}

const validateDateFrom = () => {
let isValid = true
const anchor = 'dateFrom'
if (!dateFrom && dateTo) {
logger.info(properties.errorMessages['activity-log']['date-from'].log)
const text = properties.errorMessages['activity-log']['date-from'].errors.isEmpty
errors = utils.addError(errors, { text, anchor })
isValid = false
}
if (isValid && dateFrom && !isValidFormat(dateFrom as string)) {
const validateDateRanges = () => {
let dateFromIsValid = true
let dateToIsValid = true
if (dateFrom && !isValidFormat(dateFrom as string)) {
const text = properties.errorMessages['activity-log']['date-from'].errors.isInvalid
errors = utils.addError(errors, { text, anchor })
isValid = false
errors = utils.addError(errors, { text, anchor: 'dateFrom' })
dateFromIsValid = false
}
if (isValid && dateFrom) {
if (dateFromIsValid && dateFrom) {
const dateFromIso = getIsoDate(dateFrom as string)
if (!dateFromIso.isValid) {
const text = properties.errorMessages['activity-log']['date-from'].errors.isNotReal
errors = utils.addError(errors, { text, anchor })
isValid = false
errors = utils.addError(errors, { text, anchor: 'dateFrom' })
dateFromIsValid = false
}
}
if (isValid && dateFrom) {
if (dateFromIsValid && dateFrom) {
const dateFromIso = getIsoDate(dateFrom as string)
const today = DateTime.now()
if (dateFromIso > today) {
const text = properties.errorMessages['activity-log']['date-from'].errors.isInFuture
errors = utils.addError(errors, { text, anchor })
isValid = false
errors = utils.addError(errors, { text, anchor: 'dateFrom' })
dateFromIsValid = false
}
}
}

const validateDateTo = () => {
let isValid = true
const anchor = 'dateTo'
if (!dateTo && dateFrom) {
logger.info(properties.errorMessages['activity-log']['date-to'].log)
const text = properties.errorMessages['activity-log']['date-to'].errors.isEmpty
errors = utils.addError(errors, { text, anchor })
isValid = false
if (dateTo && !isValidFormat(dateTo as string)) {
const text = properties.errorMessages['activity-log']['date-to'].errors.isInvalid
errors = utils.addError(errors, { text, anchor: 'dateTo' })
dateToIsValid = false
}
if (isValid && dateTo && !isValidFormat(dateTo as string)) {
if (isValid && !isValidFormat(dateTo as string)) {
const text = properties.errorMessages['activity-log']['date-to'].errors.isInvalid
errors = utils.addError(errors, { text, anchor })
isValid = false
}
}
if (isValid && dateTo) {
if (dateToIsValid && dateTo) {
const dateToIso = getIsoDate(dateTo as string)
if (!dateToIso.isValid) {
const text = properties.errorMessages['activity-log']['date-to'].errors.isNotReal
errors = utils.addError(errors, { text, anchor })
isValid = false
errors = utils.addError(errors, { text, anchor: 'dateTo' })
dateToIsValid = false
}
}
if (isValid && dateTo) {
if (dateToIsValid && dateTo) {
const dateToIso = getIsoDate(dateTo as string)
const today = DateTime.now()
if (dateToIso > today) {
const text = properties.errorMessages['activity-log']['date-to'].errors.isInFuture
errors = utils.addError(errors, { text, anchor })
isValid = false
errors = utils.addError(errors, { text, anchor: 'dateTo' })
dateToIsValid = false
}
}
if (!dateFrom && dateTo && dateToIsValid) {
logger.info(properties.errorMessages['activity-log']['date-from'].log)
const text = properties.errorMessages['activity-log']['date-from'].errors.isEmpty
errors = utils.addError(errors, { text, anchor: 'dateFrom' })
dateFromIsValid = false
}
if (!dateTo && dateFrom && dateFromIsValid) {
logger.info(properties.errorMessages['activity-log']['date-to'].log)
const text = properties.errorMessages['activity-log']['date-to'].errors.isEmpty
errors = utils.addError(errors, { text, anchor: 'dateTo' })
dateToIsValid = false
}
if (dateFrom && dateFromIsValid && dateTo && dateToIsValid) {
const dateFromIso = getIsoDate(dateFrom as string)
const dateToIso = getIsoDate(dateTo as string)
if (dateFromIso > dateToIso) {
const text = properties.errorMessages['activity-log']['date-from'].errors.isAfterTo
errors = utils.addError(errors, { text, anchor: 'dateFrom' })
dateFromIsValid = false
}
}
if (dateTo && dateToIsValid && dateFrom && dateFromIsValid) {
const dateFromIso = getIsoDate(dateFrom as string)
const dateToIso = getIsoDate(dateTo as string)
if (dateToIso < dateFromIso) {
const text = properties.errorMessages['activity-log']['date-to'].errors.isBeforeFrom
errors = utils.addError(errors, { text, anchor: 'dateTo' })
dateToIsValid = false
}
}
}

let errors: Errors = null
if (submit) {
if (req?.session?.errors) {
delete req.session.errors
}

validateDateFrom()
validateDateTo()

validateDateRanges()
if (errors) {
req.session.errors = errors
return res.redirect(url.replace('&submit=true', ''))
Expand Down
6 changes: 4 additions & 2 deletions server/properties/errorMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const ruleKeys = [
'isNotReal',
'isIncomplete',
'isInFuture',
'isAfterFrom',
'isAfterTo',
'isBeforeFrom',
] as const
const appointmentsKeys = [
'type',
Expand Down Expand Up @@ -126,7 +127,7 @@ const errorMessages: ErrorMessages = {
isNotReal: 'Enter a real date',
isIncomplete: 'Enter a full date, for example 17/5/2024',
isInFuture: 'The from date must be today or in the past',
isAfterFrom: 'The from date must be on or before the to date',
isAfterTo: 'The from date must be on or before the to date',
},
},
'date-to': {
Expand All @@ -137,6 +138,7 @@ const errorMessages: ErrorMessages = {
isNotReal: 'Enter a real date',
isIncomplete: 'Enter a full date, for example 17/5/2024',
isInFuture: 'The to date must be today or in the past',
isBeforeFrom: 'The to date must be on or after the from date',
},
},
},
Expand Down

0 comments on commit 2230587

Please sign in to comment.