Skip to content

Commit

Permalink
Merge pull request #4515 from airqo-platform/activity-date-validation
Browse files Browse the repository at this point in the history
Fix: Implement Date Range Validation for Activity Dates
  • Loading branch information
Baalmart authored Mar 3, 2025
2 parents 7179ab2 + cf25aa8 commit 3c01ed0
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions src/device-registry/validators/activities.validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,23 @@ const { oneOf, query, body, param } = require("express-validator");
const { ObjectId } = require("mongoose").Types;
const { isValidObjectId } = require("mongoose");
const constants = require("@config/constants");
const moment = require("moment");
const { validateNetwork, validateAdminLevels } = require("@validators/common");

const validateDateRange = (date) => {
const now = moment();
const oneMonthAgo = moment().subtract(1, "month");
const inputDate = moment(date);

if (inputDate.isAfter(now)) {
throw new Error("date cannot be in the future");
}
if (inputDate.isBefore(oneMonthAgo)) {
throw new Error("date cannot be more than one month in the past");
}
return true;
};

const commonValidations = {
tenant: [
query("tenant")
Expand Down Expand Up @@ -175,7 +190,21 @@ const commonValidations = {
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("date must be a valid datetime."),
.withMessage("date must be a valid datetime.")
.bail()
.custom(validateDateRange),
],
eachDate: [
body("*.date")
.exists()
.withMessage("date is missing")
.bail()
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("date must be a valid datetime.")
.bail()
.custom(validateDateRange),
],
description: [
body("description")
Expand Down Expand Up @@ -331,12 +360,7 @@ const activitiesValidations = {
.isIn(constants.RECALL_TYPES)
.withMessage("Invalid recallType"),
commonValidations.objectId("user_id", body),
body("date")
.optional()
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("Invalid date format"),
...commonValidations.date,
...commonValidations.firstName,
...commonValidations.lastName,
...commonValidations.userName,
Expand Down Expand Up @@ -380,13 +404,7 @@ const activitiesValidations = {
.toLowerCase()
.isIn(["pole", "wall", "faceboard", "rooftop", "suspended"])
.withMessage("Invalid mountType"),
body("date")
.exists()
.withMessage("date is required")
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("Invalid date format"),
...commonValidations.date,
//Optional fields validation if provided
body("isPrimaryInLocation")
.optional()
Expand Down Expand Up @@ -500,13 +518,7 @@ const activitiesValidations = {
.withMessage("network is required")
.trim()
.custom(validateNetwork),
body("*.date")
.exists()
.withMessage("date is required")
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("Invalid date format"),
...commonValidations.eachDate,
commonValidations.objectId("*.user_id", body),
commonValidations.objectId("*.host_id", body),
],
Expand Down

0 comments on commit 3c01ed0

Please sign in to comment.