fix: brand api

This commit is contained in:
2025-10-26 18:26:22 +07:00
parent 2fa263e9e4
commit 409e2d3750
6 changed files with 83 additions and 112 deletions

View File

@@ -102,14 +102,14 @@ const updateBrandDb = async (brandName, data) => {
return getBrandByNameDb(brandName);
};
// Soft delete brand by name
const deleteBrandDb = async (brandName, deletedBy) => {
// Soft delete brand
const deleteBrandDb = async (id, deletedBy) => {
const queryText = `
UPDATE m_brands
SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1
WHERE brand_name = $2 AND deleted_at IS NULL
WHERE brand_id = $2 AND deleted_at IS NULL
`;
await pool.query(queryText, [deletedBy, brandName]);
await pool.query(queryText, [deletedBy, id]);
return true;
};
@@ -131,33 +131,6 @@ const checkBrandNameExistsDb = async (brandName, excludeId = null) => {
return result.recordset.length > 0;
};
// Get brand with error codes count
const getBrandsWithErrorCodeCountDb = async (searchParams = {}) => {
let queryParams = [];
const queryText = `
SELECT
a.brand_id,
a.brand_name,
a.brand_type,
a.brand_manufacture,
a.brand_model,
a.brand_code,
a.is_active,
a.created_at,
COUNT(bc.error_code_id) as error_code_count
FROM m_brands a
LEFT JOIN brand_code bc ON a.brand_id = bc.brand_id AND bc.deleted_at IS NULL
WHERE a.deleted_at IS NULL
GROUP BY
a.brand_id, a.brand_name, a.brand_type, a.brand_manufacture,
a.brand_model, a.brand_code, a.is_active, a.created_at
ORDER BY a.brand_name
`;
const result = await pool.query(queryText, queryParams);
return result.recordset;
};
module.exports = {
getAllBrandsDb,
@@ -167,5 +140,4 @@ module.exports = {
updateBrandDb,
deleteBrandDb,
checkBrandNameExistsDb,
getBrandsWithErrorCodeCountDb,
};