const pool = require("../config"); // Get error codes by brand ID 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 ${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, 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; }; const createErrorCodeDb = async (brandId, data) => { const store = { brand_id: brandId, error_code: data.error_code, error_code_name: data.error_code_name, error_code_description: data.error_code_description, error_code_color: data.error_code_color, path_icon: data.path_icon, is_active: data.is_active, created_by: data.created_by }; const { query: queryText, values } = pool.buildDynamicInsert("brand_code", store); const result = await pool.query(queryText, values); const insertedId = result.recordset[0]?.inserted_id; return insertedId; }; const updateErrorCodeDb = async (brandId, errorCode, data) => { const store = { ...data }; const whereData = { brand_id: brandId, error_code: errorCode }; const { query: queryText, values } = pool.buildDynamicUpdate("brand_code", store, whereData); await pool.query(`${queryText} AND deleted_at IS NULL`, values); return true; }; const deleteErrorCodeDb = async (brandId, errorCode, deletedBy) => { const queryText = ` UPDATE brand_code SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1 WHERE brand_id = $2 AND error_code = $3 AND deleted_at IS NULL `; await pool.query(queryText, [deletedBy, brandId, errorCode]); return true; }; const getErrorCodeByIdDb = async (error_code_id) => { const queryText = ` SELECT a.* FROM brand_code a WHERE a.error_code_id = $1 AND a.deleted_at IS NULL `; const result = await pool.query(queryText, [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.* FROM brand_code a 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, };