repair get error code by brandId
This commit is contained in:
@@ -4,9 +4,10 @@ const { setResponse, setResponsePaging } = require('../helpers/utils');
|
||||
class ErrorCodeController {
|
||||
static async getByBrandId(req, res) {
|
||||
const { brandId } = req.params;
|
||||
const queryParams = req.query;
|
||||
|
||||
const results = await ErrorCodeService.getErrorCodesByBrandId(brandId);
|
||||
const response = setResponse(results, 'Error codes found');
|
||||
const results = await ErrorCodeService.getErrorCodesByBrandId(brandId, queryParams);
|
||||
const response = await setResponsePaging(queryParams, results, 'Error codes found');
|
||||
|
||||
res.status(response.statusCode).json(response);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -72,12 +72,16 @@ class ErrorCodeService {
|
||||
}
|
||||
|
||||
// Get error codes by brand ID
|
||||
static async getErrorCodesByBrandId(brandId) {
|
||||
static async getErrorCodesByBrandId(brandId, queryParams = {}) {
|
||||
try {
|
||||
const errorCodes = await getErrorCodesByBrandIdDb(brandId);
|
||||
const results = await getErrorCodesByBrandIdDb(brandId, queryParams);
|
||||
|
||||
if (results.data && results.total !== undefined) {
|
||||
return results;
|
||||
}
|
||||
|
||||
const errorCodesWithDetails = await Promise.all(
|
||||
errorCodes.map(async (errorCode) => {
|
||||
results.map(async (errorCode) => {
|
||||
const solutions = await getSolutionsByErrorCodeIdDb(errorCode.error_code_id);
|
||||
const spareparts = await getSparepartsByErrorCodeIdDb(errorCode.error_code_id);
|
||||
|
||||
@@ -140,13 +144,10 @@ class ErrorCodeService {
|
||||
if (!errorId) {
|
||||
throw new Error("Failed to create error code");
|
||||
}
|
||||
|
||||
// Create sparepart relationships for this error code
|
||||
if (data.spareparts && Array.isArray(data.spareparts)) {
|
||||
await insertMultipleErrorCodeSparepartsDb(errorId, data.spareparts, data.created_by);
|
||||
}
|
||||
|
||||
// Create solutions for this error code
|
||||
if (data.solution && Array.isArray(data.solution)) {
|
||||
for (const solutionData of data.solution) {
|
||||
await createSolutionDb(errorId, {
|
||||
@@ -173,7 +174,6 @@ class ErrorCodeService {
|
||||
const existingErrorCode = await getErrorCodeByBrandAndCodeDb(brandId, errorCode);
|
||||
if (!existingErrorCode) throw new ErrorHandler(404, "Error code not found");
|
||||
|
||||
// Update error code
|
||||
await updateErrorCodeDb(brandId, errorCode, {
|
||||
error_code_name: data.error_code_name,
|
||||
error_code_description: data.error_code_description,
|
||||
@@ -183,24 +183,20 @@ class ErrorCodeService {
|
||||
updated_by: data.updated_by,
|
||||
});
|
||||
|
||||
// Update spareparts if provided
|
||||
if (data.spareparts && Array.isArray(data.spareparts)) {
|
||||
await updateErrorCodeSparepartsDb(existingErrorCode.error_code_id, data.spareparts, data.updated_by);
|
||||
}
|
||||
|
||||
// Update solutions if provided
|
||||
if (data.solution && Array.isArray(data.solution)) {
|
||||
const existingSolutions = await getSolutionsByErrorCodeIdDb(existingErrorCode.error_code_id);
|
||||
const incomingSolutionNames = data.solution.map((s) => s.solution_name);
|
||||
|
||||
// Update or create solutions
|
||||
for (const solutionData of data.solution) {
|
||||
const existingSolution = existingSolutions.find(
|
||||
(s) => s.solution_name === solutionData.solution_name
|
||||
);
|
||||
|
||||
if (existingSolution) {
|
||||
// Update existing solution
|
||||
await updateSolutionDb(
|
||||
existingSolution.brand_code_solution_id,
|
||||
{
|
||||
@@ -213,7 +209,6 @@ class ErrorCodeService {
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// Create new solution
|
||||
await createSolutionDb(existingErrorCode.error_code_id, {
|
||||
solution_name: solutionData.solution_name,
|
||||
type_solution: solutionData.type_solution,
|
||||
@@ -225,7 +220,6 @@ class ErrorCodeService {
|
||||
}
|
||||
}
|
||||
|
||||
// Delete solutions that are not in the incoming request
|
||||
for (const existingSolution of existingSolutions) {
|
||||
if (!incomingSolutionNames.includes(existingSolution.solution_name)) {
|
||||
await deleteSolutionDb(existingSolution.brand_code_solution_id, data.updated_by);
|
||||
|
||||
Reference in New Issue
Block a user