fix: brand api
This commit is contained in:
@@ -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,
|
||||
};
|
||||
@@ -13,17 +13,6 @@ const getErrorCodesByBrandIdDb = async (brandId) => {
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Get error code by brand ID and error code
|
||||
const getErrorCodeByBrandIdAndCodeDb = async (brandId, errorCode) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
a.*
|
||||
FROM brand_code a
|
||||
WHERE a.brand_id = $1 AND a.error_code = $2 AND a.deleted_at IS NULL
|
||||
`;
|
||||
const result = await pool.query(queryText, [brandId, errorCode]);
|
||||
return result.recordset[0];
|
||||
};
|
||||
|
||||
// Create error code for brand
|
||||
const createErrorCodeDb = async (brandId, data) => {
|
||||
@@ -66,29 +55,10 @@ const deleteErrorCodeDb = async (brandId, errorCode, deletedBy) => {
|
||||
return true;
|
||||
};
|
||||
|
||||
// Check if error code exists for brand
|
||||
const checkErrorCodeExistsDb = async (brandId, errorCode, excludeId = null) => {
|
||||
let queryText = `
|
||||
SELECT error_code_id
|
||||
FROM brand_code
|
||||
WHERE brand_id = $1 AND error_code = $2 AND deleted_at IS NULL
|
||||
`;
|
||||
let values = [brandId, errorCode];
|
||||
|
||||
if (excludeId) {
|
||||
queryText += ` AND error_code_id != $3`;
|
||||
values.push(excludeId);
|
||||
}
|
||||
|
||||
const result = await pool.query(queryText, values);
|
||||
return result.recordset.length > 0;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getErrorCodesByBrandIdDb,
|
||||
getErrorCodeByBrandIdAndCodeDb,
|
||||
createErrorCodeDb,
|
||||
updateErrorCodeDb,
|
||||
deleteErrorCodeDb,
|
||||
checkErrorCodeExistsDb,
|
||||
};
|
||||
@@ -52,22 +52,9 @@ const deleteSolutionDb = async (solutionId, deletedBy) => {
|
||||
return true;
|
||||
};
|
||||
|
||||
// Get solution by ID
|
||||
const getSolutionByIdDb = async (solutionId) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
a.*
|
||||
FROM brand_code_solution a
|
||||
WHERE a.brand_code_solution_id = $1 AND a.deleted_at IS NULL
|
||||
`;
|
||||
const result = await pool.query(queryText, [solutionId]);
|
||||
return result.recordset[0];
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getSolutionsByErrorCodeIdDb,
|
||||
createSolutionDb,
|
||||
updateSolutionDb,
|
||||
deleteSolutionDb,
|
||||
getSolutionByIdDb,
|
||||
};
|
||||
Reference in New Issue
Block a user