wisdom #19
@@ -4,9 +4,10 @@ const { setResponse, setResponsePaging } = require('../helpers/utils');
|
|||||||
class ErrorCodeController {
|
class ErrorCodeController {
|
||||||
static async getByBrandId(req, res) {
|
static async getByBrandId(req, res) {
|
||||||
const { brandId } = req.params;
|
const { brandId } = req.params;
|
||||||
|
const queryParams = req.query;
|
||||||
|
|
||||||
const results = await ErrorCodeService.getErrorCodesByBrandId(brandId);
|
const results = await ErrorCodeService.getErrorCodesByBrandId(brandId, queryParams);
|
||||||
const response = setResponse(results, 'Error codes found');
|
const response = await setResponsePaging(queryParams, results, 'Error codes found');
|
||||||
|
|
||||||
res.status(response.statusCode).json(response);
|
res.status(response.statusCode).json(response);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,55 @@
|
|||||||
const pool = require("../config");
|
const pool = require("../config");
|
||||||
|
|
||||||
// Get error codes by brand ID
|
// 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 = `
|
const queryText = `
|
||||||
SELECT
|
SELECT
|
||||||
|
COUNT(*) OVER() AS total_data,
|
||||||
a.*
|
a.*
|
||||||
FROM brand_code a
|
FROM brand_code a
|
||||||
WHERE a.brand_id = $1 AND a.deleted_at IS NULL
|
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;
|
return result.recordset;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ const getSparepartsByErrorCodeIdDb = async (errorCodeId) => {
|
|||||||
s.sparepart_stok,
|
s.sparepart_stok,
|
||||||
bs.created_at,
|
bs.created_at,
|
||||||
bs.created_by
|
bs.created_by
|
||||||
FROM brand_spareparts bs
|
FROM brand_sparepart bs
|
||||||
JOIN m_sparepart s ON bs.sparepart_id = s.sparepart_id
|
JOIN m_sparepart s ON bs.sparepart_id = s.sparepart_id
|
||||||
WHERE bs.error_code_id = $1
|
WHERE bs.error_code_id = $1
|
||||||
AND s.deleted_at IS NULL
|
AND s.deleted_at IS NULL
|
||||||
@@ -40,7 +40,7 @@ const getErrorCodesBySparepartIdDb = async (sparepartId) => {
|
|||||||
ec.is_active,
|
ec.is_active,
|
||||||
ec.created_at,
|
ec.created_at,
|
||||||
ec.updated_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
|
JOIN m_error_codes ec ON bs.error_code_id = ec.error_code_id
|
||||||
WHERE bs.sparepart_id = $1
|
WHERE bs.sparepart_id = $1
|
||||||
AND ec.deleted_at IS NULL
|
AND ec.deleted_at IS NULL
|
||||||
@@ -53,7 +53,7 @@ const getErrorCodesBySparepartIdDb = async (sparepartId) => {
|
|||||||
// Insert error_code-spareparts relationship
|
// Insert error_code-spareparts relationship
|
||||||
const insertErrorCodeSparepartDb = async (errorCodeId, sparepartId, createdBy) => {
|
const insertErrorCodeSparepartDb = async (errorCodeId, sparepartId, createdBy) => {
|
||||||
const queryText = `
|
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)
|
VALUES ($1, $2, $3, CURRENT_TIMESTAMP)
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText, [errorCodeId, sparepartId, createdBy]);
|
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 values = sparepartIds.map((_, index) => `($1, $${index + 2}, $${sparepartIds.length + 2}, CURRENT_TIMESTAMP)`).join(', ');
|
||||||
const queryText = `
|
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}
|
VALUES ${values}
|
||||||
`;
|
`;
|
||||||
const params = [errorCodeId, ...sparepartIds, createdBy];
|
const params = [errorCodeId, ...sparepartIds, createdBy];
|
||||||
@@ -77,7 +77,7 @@ const insertMultipleErrorCodeSparepartsDb = async (errorCodeId, sparepartIds, cr
|
|||||||
// Delete specific error_code-sparepart relationship
|
// Delete specific error_code-sparepart relationship
|
||||||
const deleteErrorCodeSparepartDb = async (errorCodeId, sparepartId) => {
|
const deleteErrorCodeSparepartDb = async (errorCodeId, sparepartId) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
DELETE FROM brand_spareparts
|
DELETE FROM brand_sparepart
|
||||||
WHERE error_code_id = $1 AND sparepart_id = $2
|
WHERE error_code_id = $1 AND sparepart_id = $2
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText, [errorCodeId, sparepartId]);
|
const result = await pool.query(queryText, [errorCodeId, sparepartId]);
|
||||||
@@ -87,7 +87,7 @@ const deleteErrorCodeSparepartDb = async (errorCodeId, sparepartId) => {
|
|||||||
// Delete all spareparts for an error_code
|
// Delete all spareparts for an error_code
|
||||||
const deleteAllErrorCodeSparepartsDb = async (errorCodeId) => {
|
const deleteAllErrorCodeSparepartsDb = async (errorCodeId) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
DELETE FROM brand_spareparts
|
DELETE FROM brand_sparepart
|
||||||
WHERE error_code_id = $1
|
WHERE error_code_id = $1
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText, [errorCodeId]);
|
const result = await pool.query(queryText, [errorCodeId]);
|
||||||
@@ -107,7 +107,6 @@ const updateErrorCodeSparepartsDb = async (errorCodeId, sparepartIds, updatedBy)
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Check if error_code-sparepart relationship exists
|
|
||||||
const checkErrorCodeSparepartExistsDb = async (errorCodeId, sparepartId) => {
|
const checkErrorCodeSparepartExistsDb = async (errorCodeId, sparepartId) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT 1
|
SELECT 1
|
||||||
@@ -118,8 +117,6 @@ const checkErrorCodeSparepartExistsDb = async (errorCodeId, sparepartId) => {
|
|||||||
return result.recordset.length > 0;
|
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 getSparepartsByBrandIdDb = async (brandId) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT DISTINCT
|
SELECT DISTINCT
|
||||||
@@ -136,7 +133,7 @@ const getSparepartsByBrandIdDb = async (brandId) => {
|
|||||||
s.sparepart_stok,
|
s.sparepart_stok,
|
||||||
s.created_at,
|
s.created_at,
|
||||||
s.updated_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_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_error_codes ec ON bs.error_code_id = ec.error_code_id
|
||||||
WHERE ec.brand_id = $1
|
WHERE ec.brand_id = $1
|
||||||
@@ -161,7 +158,7 @@ const getBrandsBySparepartIdDb = async (sparepartId) => {
|
|||||||
b.is_active,
|
b.is_active,
|
||||||
b.created_at,
|
b.created_at,
|
||||||
b.updated_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_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_error_codes ec ON bs.error_code_id = ec.error_code_id
|
||||||
JOIN m_brands b ON ec.brand_id = b.brand_id
|
JOIN m_brands b ON ec.brand_id = b.brand_id
|
||||||
|
|||||||
@@ -72,12 +72,16 @@ class ErrorCodeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get error codes by brand ID
|
// Get error codes by brand ID
|
||||||
static async getErrorCodesByBrandId(brandId) {
|
static async getErrorCodesByBrandId(brandId, queryParams = {}) {
|
||||||
try {
|
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(
|
const errorCodesWithDetails = await Promise.all(
|
||||||
errorCodes.map(async (errorCode) => {
|
results.map(async (errorCode) => {
|
||||||
const solutions = await getSolutionsByErrorCodeIdDb(errorCode.error_code_id);
|
const solutions = await getSolutionsByErrorCodeIdDb(errorCode.error_code_id);
|
||||||
const spareparts = await getSparepartsByErrorCodeIdDb(errorCode.error_code_id);
|
const spareparts = await getSparepartsByErrorCodeIdDb(errorCode.error_code_id);
|
||||||
|
|
||||||
@@ -140,13 +144,10 @@ class ErrorCodeService {
|
|||||||
if (!errorId) {
|
if (!errorId) {
|
||||||
throw new Error("Failed to create error code");
|
throw new Error("Failed to create error code");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create sparepart relationships for this error code
|
|
||||||
if (data.spareparts && Array.isArray(data.spareparts)) {
|
if (data.spareparts && Array.isArray(data.spareparts)) {
|
||||||
await insertMultipleErrorCodeSparepartsDb(errorId, data.spareparts, data.created_by);
|
await insertMultipleErrorCodeSparepartsDb(errorId, data.spareparts, data.created_by);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create solutions for this error code
|
|
||||||
if (data.solution && Array.isArray(data.solution)) {
|
if (data.solution && Array.isArray(data.solution)) {
|
||||||
for (const solutionData of data.solution) {
|
for (const solutionData of data.solution) {
|
||||||
await createSolutionDb(errorId, {
|
await createSolutionDb(errorId, {
|
||||||
@@ -173,7 +174,6 @@ class ErrorCodeService {
|
|||||||
const existingErrorCode = await getErrorCodeByBrandAndCodeDb(brandId, errorCode);
|
const existingErrorCode = await getErrorCodeByBrandAndCodeDb(brandId, errorCode);
|
||||||
if (!existingErrorCode) throw new ErrorHandler(404, "Error code not found");
|
if (!existingErrorCode) throw new ErrorHandler(404, "Error code not found");
|
||||||
|
|
||||||
// Update error code
|
|
||||||
await updateErrorCodeDb(brandId, errorCode, {
|
await updateErrorCodeDb(brandId, errorCode, {
|
||||||
error_code_name: data.error_code_name,
|
error_code_name: data.error_code_name,
|
||||||
error_code_description: data.error_code_description,
|
error_code_description: data.error_code_description,
|
||||||
@@ -183,24 +183,20 @@ class ErrorCodeService {
|
|||||||
updated_by: data.updated_by,
|
updated_by: data.updated_by,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update spareparts if provided
|
|
||||||
if (data.spareparts && Array.isArray(data.spareparts)) {
|
if (data.spareparts && Array.isArray(data.spareparts)) {
|
||||||
await updateErrorCodeSparepartsDb(existingErrorCode.error_code_id, data.spareparts, data.updated_by);
|
await updateErrorCodeSparepartsDb(existingErrorCode.error_code_id, data.spareparts, data.updated_by);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update solutions if provided
|
|
||||||
if (data.solution && Array.isArray(data.solution)) {
|
if (data.solution && Array.isArray(data.solution)) {
|
||||||
const existingSolutions = await getSolutionsByErrorCodeIdDb(existingErrorCode.error_code_id);
|
const existingSolutions = await getSolutionsByErrorCodeIdDb(existingErrorCode.error_code_id);
|
||||||
const incomingSolutionNames = data.solution.map((s) => s.solution_name);
|
const incomingSolutionNames = data.solution.map((s) => s.solution_name);
|
||||||
|
|
||||||
// Update or create solutions
|
|
||||||
for (const solutionData of data.solution) {
|
for (const solutionData of data.solution) {
|
||||||
const existingSolution = existingSolutions.find(
|
const existingSolution = existingSolutions.find(
|
||||||
(s) => s.solution_name === solutionData.solution_name
|
(s) => s.solution_name === solutionData.solution_name
|
||||||
);
|
);
|
||||||
|
|
||||||
if (existingSolution) {
|
if (existingSolution) {
|
||||||
// Update existing solution
|
|
||||||
await updateSolutionDb(
|
await updateSolutionDb(
|
||||||
existingSolution.brand_code_solution_id,
|
existingSolution.brand_code_solution_id,
|
||||||
{
|
{
|
||||||
@@ -213,7 +209,6 @@ class ErrorCodeService {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// Create new solution
|
|
||||||
await createSolutionDb(existingErrorCode.error_code_id, {
|
await createSolutionDb(existingErrorCode.error_code_id, {
|
||||||
solution_name: solutionData.solution_name,
|
solution_name: solutionData.solution_name,
|
||||||
type_solution: solutionData.type_solution,
|
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) {
|
for (const existingSolution of existingSolutions) {
|
||||||
if (!incomingSolutionNames.includes(existingSolution.solution_name)) {
|
if (!incomingSolutionNames.includes(existingSolution.solution_name)) {
|
||||||
await deleteSolutionDb(existingSolution.brand_code_solution_id, data.updated_by);
|
await deleteSolutionDb(existingSolution.brand_code_solution_id, data.updated_by);
|
||||||
|
|||||||
Reference in New Issue
Block a user