From 2230587809afb67624528aa7312b3a046bd51d9a Mon Sep 17 00:00:00 2001 From: Neil Mills Date: Fri, 24 Jan 2025 19:41:37 +0000 Subject: [PATCH] MAN-167 refactor date range validation --- server/middleware/validation/activityLog.ts | 98 ++++++++++++--------- server/properties/errorMessages.ts | 6 +- 2 files changed, 58 insertions(+), 46 deletions(-) diff --git a/server/middleware/validation/activityLog.ts b/server/middleware/validation/activityLog.ts index 458d022b..5850a1cd 100644 --- a/server/middleware/validation/activityLog.ts +++ b/server/middleware/validation/activityLog.ts @@ -19,82 +19,92 @@ const activityLog: Route = (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', '')) diff --git a/server/properties/errorMessages.ts b/server/properties/errorMessages.ts index 243edb50..35f85e09 100644 --- a/server/properties/errorMessages.ts +++ b/server/properties/errorMessages.ts @@ -5,7 +5,8 @@ const ruleKeys = [ 'isNotReal', 'isIncomplete', 'isInFuture', - 'isAfterFrom', + 'isAfterTo', + 'isBeforeFrom', ] as const const appointmentsKeys = [ 'type', @@ -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': { @@ -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', }, }, },