Skip to content
Open
Changes from all 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
38 changes: 32 additions & 6 deletions backend/controllers/batchController.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,26 @@ exports.getBatches = async (req, res) => {
if (start || end) {
query.createdAt = {};
if (start) {
query.createdAt.$gte = new Date(start);
const startDateObj = new Date(start);
if (Number.isNaN(startDateObj.getTime())) {
return res
.status(400)
.json(apiResponse.errorResponse("Invalid 'start' date. Use ISO 8601 format (e.g. 2024-01-01)."));
}
query.createdAt.$gte = startDateObj;
}
if (end) {
query.createdAt.$lte = new Date(end);
const endDateObj = new Date(end);
if (Number.isNaN(endDateObj.getTime())) {
return res
.status(400)
.json(apiResponse.errorResponse("Invalid 'end' date. Use ISO 8601 format (e.g. 2024-01-01)."));
}
query.createdAt.$lte = endDateObj;
}
// Drop an empty object if both ends were absent/invalid-but-silenced.
if (!query.createdAt.$gte && !query.createdAt.$lte) {
delete query.createdAt;
}
}

Expand All @@ -367,8 +383,21 @@ exports.getBatches = async (req, res) => {
);
const skip = (pageNumber - 1) * limitNumber;

// Allow-list sort keys so a client cannot inject arbitrary/malformed
// sort fields into the Mongoose query.
const ALLOWED_SORT_FIELDS = new Set([
"createdAt",
"updatedAt",
"batchId",
"cropType",
"quantity",
"farmerName",
"status",
"currentStage",
]);
const safeSortBy = ALLOWED_SORT_FIELDS.has(sortBy) ? sortBy : "createdAt";
const sort = {};
sort[sortBy] = sortOrder.toLowerCase() === "asc" ? 1 : -1;
sort[safeSortBy] = sortOrder.toLowerCase() === "asc" ? 1 : -1;

// Use lean() for read-only queries to skip Mongoose document hydration
const batches = await Batch.find(query)
Expand Down Expand Up @@ -712,6 +741,3 @@ exports.getIoTData = async (req, res) => {
);
}
};

.catch(err => console.error("Promise.all failed:", err));
};
Loading