Added validation and automated short key to create unit master
This commit is contained in:
parent
50b2c8234c
commit
e87802d3db
@ -8,6 +8,88 @@ const csv = require("csv-parser");
|
|||||||
// Create new Unit
|
// Create new Unit
|
||||||
exports.createUnit = async (req, res) => {
|
exports.createUnit = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
const { uom } = req.body;
|
||||||
|
|
||||||
|
if (!uom || typeof uom !== "string" || !uom.trim()) {
|
||||||
|
return res.status(400).json({
|
||||||
|
status: "error",
|
||||||
|
message: "UOM (Unit Name) is required and must be a non-empty string.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const cleaned = uom.replace(/[^a-zA-Z]/g, "").toUpperCase();
|
||||||
|
if (!cleaned) {
|
||||||
|
return res.status(400).json({
|
||||||
|
status: "error",
|
||||||
|
message: "UOM must contain at least one letter (A-Z).",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const existingUOM = await UnitMaster.findOne({
|
||||||
|
where: Sequelize.where(
|
||||||
|
Sequelize.fn("LOWER", Sequelize.col("uom")),
|
||||||
|
uom.toLowerCase()
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingUOM)
|
||||||
|
return res.status(400).json({
|
||||||
|
status: "error",
|
||||||
|
message: `Unit '${uom}' already exists with short key '${existingUOM.uom_short_name}'.`,
|
||||||
|
});
|
||||||
|
|
||||||
|
//Generate short name (same rule as CSV)
|
||||||
|
const abbreviationMap = {
|
||||||
|
METER: "MT",
|
||||||
|
METRE: "MT",
|
||||||
|
KILOGRAM: "KG",
|
||||||
|
GRAM: "GM",
|
||||||
|
LITER: "LTR",
|
||||||
|
LITRE: "LTR",
|
||||||
|
CENTIMETER: "CM",
|
||||||
|
MILLIMETER: "MM",
|
||||||
|
SECOND: "SEC",
|
||||||
|
MINUTE: "MIN",
|
||||||
|
HOUR: "HR",
|
||||||
|
DAY: "DAY",
|
||||||
|
PIECE: "PC",
|
||||||
|
BOX: "BX",
|
||||||
|
USER: "USR",
|
||||||
|
ITEM: "ITM",
|
||||||
|
UNIT: "UNT",
|
||||||
|
};
|
||||||
|
|
||||||
|
let uomShort;
|
||||||
|
|
||||||
|
if (cleaned.length <= 5) {
|
||||||
|
uomShort = cleaned;
|
||||||
|
} else {
|
||||||
|
// If >5 letters, use abbreviation or auto-generate
|
||||||
|
const baseShort = abbreviationMap[cleaned] || cleaned.substring(0, 3);
|
||||||
|
const randomLetters = () =>
|
||||||
|
Array.from({ length: 2 }, () =>
|
||||||
|
String.fromCharCode(65 + Math.floor(Math.random() * 26))
|
||||||
|
).join("");
|
||||||
|
uomShort = `${baseShort}${randomLetters()}`.substring(0, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
let exists = await UnitMaster.findOne({
|
||||||
|
where: Sequelize.where(
|
||||||
|
Sequelize.fn("LOWER", Sequelize.col("uom_short_name")),
|
||||||
|
uomShort.toLowerCase()
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
while (exists) {
|
||||||
|
const randomSuffix = Math.random().toString(36).substring(2, 3).toUpperCase();
|
||||||
|
uomShort = (uomShort.substring(0, 4) + randomSuffix).substring(0, 5);
|
||||||
|
exists = await UnitMaster.findOne({
|
||||||
|
where: Sequelize.where(
|
||||||
|
Sequelize.fn("LOWER", Sequelize.col("uom_short_name")),
|
||||||
|
uomShort.toLowerCase()
|
||||||
|
),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
req.body.uom_short_name = uomShort;
|
||||||
|
|
||||||
const unit = await UnitMaster.create(req.body);
|
const unit = await UnitMaster.create(req.body);
|
||||||
res.status(201).json({ status: "success", data: unit });
|
res.status(201).json({ status: "success", data: unit });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user