feat: enhance sparepart database queries and sparepart number validation

This commit is contained in:
2026-04-27 17:22:21 +07:00
parent b7fa4b3d07
commit 51f4ea2bff
4 changed files with 20 additions and 16 deletions

View File

@@ -54,9 +54,8 @@ const getAllSparepartDb = async (searchParams = {}) => {
COALESCE(a.sparepart_code, '') + ' - ' + COALESCE(a.sparepart_name, '') AS sparepart_code_name
FROM m_sparepart a
WHERE a.deleted_at IS NULL
${
whereConditions.length > 0 ? `AND ${whereConditions.join(" AND ")}` : ""
}
${whereConditions.length > 0 ? `AND ${whereConditions.join(" AND ")}` : ""
}
${whereOrConditions ? whereOrConditions : ""}
ORDER BY a.sparepart_id ASC
${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ""}
@@ -84,15 +83,15 @@ const getSparepartByIdDb = async (id) => {
return result.recordset;
};
const checkSparepartNameExistsDb = async (sparePartName, excludeId = null) => {
const checkSparepartNameExistsDb = async (sparePartName, sparePartNumber, excludeId = null) => {
let queryText = `
SELECT sparepart_id
FROM m_sparepart
WHERE sparepart_name = $1 AND deleted_at IS NULL
WHERE sparepart_name = $1 AND sparepart_number = $2 AND deleted_at IS NULL
`;
let values = [sparePartName];
let values = [sparePartName, sparePartNumber];
if (excludeId) {
queryText += ` AND sparepart_id != $2`;
queryText += ` AND sparepart_id != $3`;
values.push(excludeId);
}
const result = await pool.query(queryText, values);