repair get error code by brandId
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ const getSparepartsByErrorCodeIdDb = async (errorCodeId) => {
|
||||
s.sparepart_stok,
|
||||
bs.created_at,
|
||||
bs.created_by
|
||||
FROM brand_spareparts bs
|
||||
FROM brand_sparepart bs
|
||||
JOIN m_sparepart s ON bs.sparepart_id = s.sparepart_id
|
||||
WHERE bs.error_code_id = $1
|
||||
AND s.deleted_at IS NULL
|
||||
@@ -40,7 +40,7 @@ const getErrorCodesBySparepartIdDb = async (sparepartId) => {
|
||||
ec.is_active,
|
||||
ec.created_at,
|
||||
ec.updated_at
|
||||
FROM brand_spareparts bs
|
||||
FROM brand_sparepart bs
|
||||
JOIN m_error_codes ec ON bs.error_code_id = ec.error_code_id
|
||||
WHERE bs.sparepart_id = $1
|
||||
AND ec.deleted_at IS NULL
|
||||
@@ -53,7 +53,7 @@ const getErrorCodesBySparepartIdDb = async (sparepartId) => {
|
||||
// Insert error_code-spareparts relationship
|
||||
const insertErrorCodeSparepartDb = async (errorCodeId, sparepartId, createdBy) => {
|
||||
const queryText = `
|
||||
INSERT INTO brand_spareparts (error_code_id, sparepart_id, created_by, created_at)
|
||||
INSERT INTO brand_sparepart (error_code_id, sparepart_id, created_by, created_at)
|
||||
VALUES ($1, $2, $3, CURRENT_TIMESTAMP)
|
||||
`;
|
||||
const result = await pool.query(queryText, [errorCodeId, sparepartId, createdBy]);
|
||||
@@ -66,7 +66,7 @@ const insertMultipleErrorCodeSparepartsDb = async (errorCodeId, sparepartIds, cr
|
||||
|
||||
const values = sparepartIds.map((_, index) => `($1, $${index + 2}, $${sparepartIds.length + 2}, CURRENT_TIMESTAMP)`).join(', ');
|
||||
const queryText = `
|
||||
INSERT INTO brand_spareparts (error_code_id, sparepart_id, created_by, created_at)
|
||||
INSERT INTO brand_sparepart (error_code_id, sparepart_id, created_by, created_at)
|
||||
VALUES ${values}
|
||||
`;
|
||||
const params = [errorCodeId, ...sparepartIds, createdBy];
|
||||
@@ -77,7 +77,7 @@ const insertMultipleErrorCodeSparepartsDb = async (errorCodeId, sparepartIds, cr
|
||||
// Delete specific error_code-sparepart relationship
|
||||
const deleteErrorCodeSparepartDb = async (errorCodeId, sparepartId) => {
|
||||
const queryText = `
|
||||
DELETE FROM brand_spareparts
|
||||
DELETE FROM brand_sparepart
|
||||
WHERE error_code_id = $1 AND sparepart_id = $2
|
||||
`;
|
||||
const result = await pool.query(queryText, [errorCodeId, sparepartId]);
|
||||
@@ -87,7 +87,7 @@ const deleteErrorCodeSparepartDb = async (errorCodeId, sparepartId) => {
|
||||
// Delete all spareparts for an error_code
|
||||
const deleteAllErrorCodeSparepartsDb = async (errorCodeId) => {
|
||||
const queryText = `
|
||||
DELETE FROM brand_spareparts
|
||||
DELETE FROM brand_sparepart
|
||||
WHERE error_code_id = $1
|
||||
`;
|
||||
const result = await pool.query(queryText, [errorCodeId]);
|
||||
@@ -107,7 +107,6 @@ const updateErrorCodeSparepartsDb = async (errorCodeId, sparepartIds, updatedBy)
|
||||
return true;
|
||||
};
|
||||
|
||||
// Check if error_code-sparepart relationship exists
|
||||
const checkErrorCodeSparepartExistsDb = async (errorCodeId, sparepartId) => {
|
||||
const queryText = `
|
||||
SELECT 1
|
||||
@@ -118,8 +117,6 @@ const checkErrorCodeSparepartExistsDb = async (errorCodeId, sparepartId) => {
|
||||
return result.recordset.length > 0;
|
||||
};
|
||||
|
||||
// Legacy functions for backward compatibility (deprecated)
|
||||
// Get spareparts by brand_id (now using error_code_id mapping via error codes)
|
||||
const getSparepartsByBrandIdDb = async (brandId) => {
|
||||
const queryText = `
|
||||
SELECT DISTINCT
|
||||
@@ -136,7 +133,7 @@ const getSparepartsByBrandIdDb = async (brandId) => {
|
||||
s.sparepart_stok,
|
||||
s.created_at,
|
||||
s.updated_at
|
||||
FROM brand_spareparts bs
|
||||
FROM brand_sparepart bs
|
||||
JOIN m_sparepart s ON bs.sparepart_id = s.sparepart_id
|
||||
JOIN m_error_codes ec ON bs.error_code_id = ec.error_code_id
|
||||
WHERE ec.brand_id = $1
|
||||
@@ -161,7 +158,7 @@ const getBrandsBySparepartIdDb = async (sparepartId) => {
|
||||
b.is_active,
|
||||
b.created_at,
|
||||
b.updated_at
|
||||
FROM brand_spareparts bs
|
||||
FROM brand_sparepart bs
|
||||
JOIN m_sparepart s ON bs.sparepart_id = s.sparepart_id
|
||||
JOIN m_error_codes ec ON bs.error_code_id = ec.error_code_id
|
||||
JOIN m_brands b ON ec.brand_id = b.brand_id
|
||||
|
||||
Reference in New Issue
Block a user