repair get error code by brandId

This commit is contained in:
2025-12-04 15:38:19 +07:00
parent 096fe9461d
commit a8eb785a5b
4 changed files with 61 additions and 29 deletions

View File

@@ -1,15 +1,55 @@
const pool = require("../config");
// Get error codes by brand ID
const getErrorCodesByBrandIdDb = async (brandId) => {
const getErrorCodesByBrandIdDb = async (brandId, searchParams = {}) => {
let queryParams = [brandId];
// Pagination
if (searchParams.limit) {
const page = Number(searchParams.page ?? 1) - 1;
queryParams = [brandId, 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" },
],
queryParams
);
queryParams = whereParamAnd ? whereParamAnd : queryParams;
const queryText = `
SELECT
COUNT(*) OVER() AS total_data,
a.*
FROM brand_code a
WHERE a.brand_id = $1 AND a.deleted_at IS NULL
ORDER BY a.error_code_id
${whereConditions.length > 0 ? `AND ${whereConditions.join(' AND ')}` : ''}
${whereOrConditions ? whereOrConditions : ''}
ORDER BY a.error_code_id DESC
${searchParams.limit ? `OFFSET $3 * $2 ROWS FETCH NEXT $2 ROWS ONLY` : ''}
`;
const result = await pool.query(queryText, [brandId]);
const result = await pool.query(queryText, queryParams);
// Return paginated format if limit is provided
if (searchParams.limit) {
const total = result?.recordset.length > 0 ? parseInt(result.recordset[0].total_data, 10) : 0;
return { data: result.recordset, total };
}
// Return simple array for backward compatibility
return result.recordset;
};