not allow negative value

This commit is contained in:
unknown 2026-01-06 19:47:46 +05:30
parent bc810552e4
commit 4926de93d6

View File

@ -48,9 +48,54 @@ exports.createSubmission = async (req, res) => {
await QuarterlyWindowsConfiguration.update({responded: currentResponded + 1, not_responded: currentNotResponded - 1, updated_at: new Date() }, {where: { id: config.id }} );
}
const hasNegativeNumber = (obj, fields) => {
return fields.find((field) => {
const val = obj[field];
if (val === null || val === undefined || val === "") return false;
const num = Number(val);
return !isNaN(num) && num < 0;
});
};
if (products.length) {
// products.forEach((p) => (p.submission_id = submission.id));
// await SubmissionProduct.bulkCreate(products);
// await SubmissionProduct.bulkCreate(products);
const numericFields = [
"annual_installed_capacity",
"previous_quantity_period_one",
"previous_quantity_period_two",
"previous_quantity_period_three",
"previous_cost_period_one",
"previous_cost_period_two",
"previous_cost_period_three",
"current_quantity_period_one",
"current_quantity_period_two",
"current_quantity_period_three",
"current_cost_period_one",
"current_cost_period_two",
"current_cost_period_three",
"forecast_quantity_period_one",
"forecast_quantity_period_two",
"forecast_quantity_period_three",
"forecast_cost_period_one",
"forecast_cost_period_two",
"forecast_cost_period_three",
];
for (let i = 0; i < products.length; i++) {
const invalidField = hasNegativeNumber(products[i], numericFields);
if (invalidField) {
return res.status(400).json({
status: "failed",
message: `Product ${i + 1}: ${invalidField} cannot be negative`,
});
}
}
// Process products before insert
const processedProducts = products.map((p) => {