Unit/product master api changes

This commit is contained in:
Gowtham M 2026-05-14 16:50:22 +05:30
parent 050cecbee2
commit a723a851db
2 changed files with 32 additions and 11 deletions

View File

@ -181,7 +181,13 @@ exports.getAllProducts = async (req, res) => {
},
],
order: [
["product_name", "ASC"]
[
Sequelize.literal(
"GREATEST(products.created_at, COALESCE(products.updated_at, products.created_at))"
),
"DESC",
],
["id", "DESC"],
],
});

View File

@ -39,7 +39,7 @@ exports.createUnit = async (req, res) => {
message: "Description is required and must be a non-empty string.",
});
}
const cleaned = uom.replace(/[^A-Za-z]/g, "").toUpperCase();
const cleaned = uom.replace(/[^A-Za-z]/g, "");
if (!cleaned) {
return res.status(400).json({
@ -60,9 +60,10 @@ exports.createUnit = async (req, res) => {
}
const existingUOM = await UnitMaster.findOne({
where: {
uom: cleaned
}
where: Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("uom")),
cleaned.toLowerCase()
),
});
if (existingUOM) {
@ -96,7 +97,7 @@ exports.createUnit = async (req, res) => {
let uomShort =
cleaned.length <= 5
? cleaned
: (abbreviationMap[cleaned] || cleaned.substring(0, 3)) +
: (abbreviationMap[cleaned.toUpperCase()] || cleaned.substring(0, 3)) +
Math.random().toString(36).substring(2, 4).toUpperCase();
uomShort = uomShort.substring(0, 5);
@ -196,10 +197,21 @@ exports.updateUnit = async (req, res) => {
const unit = await UnitMaster.findByPk(req.params.id);
if (!unit) return res.status(404).json({ status: "error", message: "Unit not found" });
req.body.updated_by = req.body.updated_by || req.user.id;
req.body.updated_at = req.body.updated_at || new Date();
const payload = { ...req.body };
payload.updated_by = payload.updated_by || req.user.id;
payload.updated_at = payload.updated_at || new Date();
await unit.update(req.body);
if (payload.uom !== undefined) {
payload.uom = sanitizeStringValue(payload.uom);
}
if (payload.description !== undefined) {
payload.description = sanitizeStringValue(payload.description);
}
if (payload.factor !== undefined && typeof payload.factor === "string") {
payload.factor = sanitizeStringValue(payload.factor);
}
await unit.update(payload);
res.status(200).json({ status: "success" });
} catch (err) {
logger.error(err.message);
@ -454,7 +466,7 @@ exports.uploadUnitMasterFromCSV = async (req, res) => {
const seenInFile = new Set();
const generateShortName = (name) => {
const base = name.replace(/[^A-Z]/gi, "").toUpperCase().slice(0, 3);
const base = name.replace(/[^A-Za-z]/g, "").slice(0, 3);
const suffix = Math.random().toString(36).substring(2, 4).toUpperCase();
return `${base}${suffix}`;
};
@ -512,7 +524,10 @@ exports.uploadUnitMasterFromCSV = async (req, res) => {
const exists = await UnitMaster.findOne({
where: {
[Op.or]: [
{ uom: uom.toUpperCase() },
Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("uom")),
uom.toLowerCase()
),
{ uom_short_name: shortName },
],
},