106 lines
3.4 KiB
JavaScript
106 lines
3.4 KiB
JavaScript
// app/controllers/variationReasonMaster.controller.js
|
|
const db = require("../models");
|
|
const VariationReasonMaster = db.VariationReasonMaster;
|
|
const logger = require("../services/logger");
|
|
|
|
// Create
|
|
exports.createReason = async (req, res) => {
|
|
try {
|
|
let { reason, high_or_low } = req.body;
|
|
if (typeof reason !== "string" || reason.trim().length === 0) {
|
|
return res.status(400).json({
|
|
status: "failed",
|
|
message: "Reason is required"
|
|
});
|
|
}
|
|
reason = reason.trim();
|
|
|
|
if (high_or_low) {
|
|
high_or_low = high_or_low.toString().toLowerCase();
|
|
|
|
if (!["low", "high"].includes(high_or_low)) {
|
|
return res.status(400).json({
|
|
status: "failed",
|
|
message: "Invalid high_or_low value"
|
|
});
|
|
}
|
|
high_or_low = high_or_low === "low" ? "Low" : "High";
|
|
}
|
|
|
|
const newReason = await VariationReasonMaster.create({
|
|
reason,
|
|
high_or_low,
|
|
created_by: req.body.created_by || req.user.id
|
|
});
|
|
|
|
return res.status(201).json({
|
|
status: "success",
|
|
message: "Reason created successfully"
|
|
});
|
|
|
|
} catch (err) {
|
|
logger.error(err.message);
|
|
logger.error(`Stack trace: ${err.stack}`);
|
|
return res.status(500).json({
|
|
status: "failed",
|
|
message: "Internal server error"
|
|
});
|
|
}
|
|
};
|
|
|
|
// Get all
|
|
exports.getAllReasons = async (req, res) => {
|
|
try {
|
|
const data = await VariationReasonMaster.findAll();
|
|
res.status(200).send({ status: "success", message: "Fetched successfully", data });
|
|
} catch (err) {
|
|
logger.error(err.message);
|
|
logger.error(`Stack trace: ${err.stack}`);
|
|
res.status(500).send({ status: "failed", message: "Internal server error"});
|
|
}
|
|
};
|
|
|
|
// Get by ID
|
|
exports.getReasonById = async (req, res) => {
|
|
try {
|
|
const data = await VariationReasonMaster.findByPk(req.params.id);
|
|
if (!data) return res.status(404).send({ status: "failed", message: "Reason not found" });
|
|
res.status(200).send({ status: "success", message: "Fetched successfully", data });
|
|
} catch (err) {
|
|
logger.error(err.message);
|
|
logger.error(`Stack trace: ${err.stack}`);
|
|
res.status(500).send({ status: "failed", message: "Internal server error" });
|
|
}
|
|
};
|
|
|
|
// Update
|
|
exports.updateReason = async (req, res) => {
|
|
try {
|
|
const { reason, high_or_low, is_active } = req.body;
|
|
const updated = await VariationReasonMaster.update(
|
|
{ reason, high_or_low, is_active, updated_at: new Date(), updated_on: new Date(), updated_by: req.user.id },
|
|
{ where: { id: req.params.id } }
|
|
);
|
|
|
|
if (updated[0] === 0) return res.status(404).send({ status: "failed", message: "Reason not found" });
|
|
res.status(200).send({ status: "success", message: "Updated successfully" });
|
|
} catch (err) {
|
|
logger.error(err.message);
|
|
logger.error(`Stack trace: ${err.stack}`);
|
|
res.status(500).send({ status: "failed", message: "Internal server error" });
|
|
}
|
|
};
|
|
|
|
// Delete
|
|
exports.deleteReason = async (req, res) => {
|
|
try {
|
|
const deleted = await VariationReasonMaster.destroy({ where: { id: req.params.id } });
|
|
if (!deleted) return res.status(404).send({ status: "failed", message: "Reason not found" });
|
|
res.status(200).send({ status: "success", message: "Deleted successfully" });
|
|
} catch (err) {
|
|
logger.error(err.message);
|
|
logger.error(`Stack trace: ${err.stack}`);
|
|
res.status(500).send({ status: "failed", message:"Internal server error" });
|
|
}
|
|
};
|