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

@@ -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);