add: error_code api
This commit is contained in:
@@ -64,10 +64,76 @@ const getErrorCodeByIdDb = async (error_code_id) => {
|
||||
return result.recordset[0];
|
||||
};
|
||||
|
||||
const getErrorCodeByBrandAndCodeDb = 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];
|
||||
};
|
||||
|
||||
// Get all error codes with pagination and search
|
||||
const getAllErrorCodesDb = async (searchParams = {}) => {
|
||||
let queryParams = [];
|
||||
|
||||
// Pagination
|
||||
if (searchParams.limit) {
|
||||
const page = Number(searchParams.page ?? 1) - 1;
|
||||
queryParams = [Number(searchParams.limit ?? 10), page];
|
||||
}
|
||||
|
||||
// Search across multiple columns
|
||||
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
|
||||
["a.error_code", "a.error_code_name", "a.error_code_description"],
|
||||
searchParams.criteria,
|
||||
queryParams
|
||||
);
|
||||
|
||||
queryParams = whereParamOr ? whereParamOr : queryParams;
|
||||
|
||||
// Filter conditions
|
||||
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
|
||||
[
|
||||
{ column: "a.is_active", param: searchParams.status, type: "string" },
|
||||
{ column: "a.brand_id", param: searchParams.brand_id, type: "number" },
|
||||
],
|
||||
queryParams
|
||||
);
|
||||
|
||||
queryParams = whereParamAnd ? whereParamAnd : queryParams;
|
||||
|
||||
const queryText = `
|
||||
SELECT
|
||||
COUNT(*) OVER() AS total_data,
|
||||
a.*,
|
||||
b.brand_name,
|
||||
b.brand_type,
|
||||
b.brand_manufacture,
|
||||
b.brand_model
|
||||
FROM brand_code a
|
||||
LEFT JOIN m_brands b ON a.brand_id = b.brand_id
|
||||
WHERE a.deleted_at IS NULL
|
||||
${whereConditions.length > 0 ? `AND ${whereConditions.join(' AND ')}` : ''}
|
||||
${whereOrConditions ? whereOrConditions : ''}
|
||||
ORDER BY a.error_code_id DESC
|
||||
${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ''}
|
||||
`;
|
||||
|
||||
const result = await pool.query(queryText, queryParams);
|
||||
const total = result?.recordset.length > 0 ? parseInt(result.recordset[0].total_data, 10) : 0;
|
||||
|
||||
return { data: result.recordset, total };
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getErrorCodesByBrandIdDb,
|
||||
getErrorCodeByIdDb,
|
||||
getErrorCodeByBrandAndCodeDb,
|
||||
createErrorCodeDb,
|
||||
updateErrorCodeDb,
|
||||
deleteErrorCodeDb,
|
||||
getAllErrorCodesDb,
|
||||
};
|
||||
Reference in New Issue
Block a user