add: error_code api
This commit is contained in:
294
services/error_code.service.js
Normal file
294
services/error_code.service.js
Normal file
@@ -0,0 +1,294 @@
|
||||
const { ErrorHandler } = require("../helpers/error");
|
||||
const {
|
||||
getErrorCodesByBrandIdDb,
|
||||
getErrorCodeByIdDb,
|
||||
getErrorCodeByBrandAndCodeDb,
|
||||
createErrorCodeDb,
|
||||
updateErrorCodeDb,
|
||||
deleteErrorCodeDb,
|
||||
getAllErrorCodesDb,
|
||||
} = require("../db/brand_code.db");
|
||||
|
||||
const {
|
||||
getSolutionsByErrorCodeIdDb,
|
||||
createSolutionDb,
|
||||
updateSolutionDb,
|
||||
deleteSolutionDb,
|
||||
} = require("../db/brand_code_solution.db");
|
||||
|
||||
const {
|
||||
getSparepartsByErrorCodeIdDb,
|
||||
insertMultipleErrorCodeSparepartsDb,
|
||||
updateErrorCodeSparepartsDb,
|
||||
} = require("../db/brand_sparepart.db");
|
||||
|
||||
const { getFileUploadByPathDb } = require("../db/file_uploads.db");
|
||||
|
||||
class ErrorCodeService {
|
||||
// Get all error codes with pagination and search
|
||||
static async getAllErrorCodes(param) {
|
||||
try {
|
||||
const results = await getAllErrorCodesDb(param);
|
||||
|
||||
// Enhance with solutions and spareparts for each error code
|
||||
const errorCodesWithDetails = await Promise.all(
|
||||
results.data.map(async (errorCode) => {
|
||||
const solutions = await getSolutionsByErrorCodeIdDb(errorCode.error_code_id);
|
||||
const spareparts = await getSparepartsByErrorCodeIdDb(errorCode.error_code_id);
|
||||
|
||||
const solutionsWithFiles = await Promise.all(
|
||||
solutions.map(async (solution) => {
|
||||
let fileData = null;
|
||||
|
||||
if (solution.path_solution && solution.type_solution !== "text") {
|
||||
fileData = await getFileUploadByPathDb(solution.path_solution);
|
||||
}
|
||||
|
||||
return {
|
||||
...solution,
|
||||
file_upload_name: fileData?.file_upload_name || null,
|
||||
path_document: fileData?.path_document || null,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
...errorCode,
|
||||
solution: solutionsWithFiles,
|
||||
spareparts: spareparts,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
...results,
|
||||
data: errorCodesWithDetails,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Get error code by ID with complete data
|
||||
static async getErrorCodeById(id) {
|
||||
try {
|
||||
const errorCode = await getErrorCodeByIdDb(id);
|
||||
if (!errorCode) throw new ErrorHandler(404, "Error code not found");
|
||||
|
||||
const solutions = await getSolutionsByErrorCodeIdDb(errorCode.error_code_id);
|
||||
const spareparts = await getSparepartsByErrorCodeIdDb(errorCode.error_code_id);
|
||||
|
||||
const solutionsWithFiles = await Promise.all(
|
||||
solutions.map(async (solution) => {
|
||||
let fileData = null;
|
||||
|
||||
if (solution.path_solution && solution.type_solution !== "text") {
|
||||
fileData = await getFileUploadByPathDb(solution.path_solution);
|
||||
}
|
||||
|
||||
return {
|
||||
...solution,
|
||||
file_upload_name: fileData?.file_upload_name || null,
|
||||
path_document: fileData?.path_document || null,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
...errorCode,
|
||||
solution: solutionsWithFiles,
|
||||
spareparts: spareparts,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Get error codes by brand ID
|
||||
static async getErrorCodesByBrandId(brandId) {
|
||||
try {
|
||||
const errorCodes = await getErrorCodesByBrandIdDb(brandId);
|
||||
|
||||
const errorCodesWithDetails = await Promise.all(
|
||||
errorCodes.map(async (errorCode) => {
|
||||
const solutions = await getSolutionsByErrorCodeIdDb(errorCode.error_code_id);
|
||||
const spareparts = await getSparepartsByErrorCodeIdDb(errorCode.error_code_id);
|
||||
|
||||
const solutionsWithFiles = await Promise.all(
|
||||
solutions.map(async (solution) => {
|
||||
let fileData = null;
|
||||
|
||||
if (solution.path_solution && solution.type_solution !== "text") {
|
||||
fileData = await getFileUploadByPathDb(solution.path_solution);
|
||||
}
|
||||
|
||||
return {
|
||||
...solution,
|
||||
file_upload_name: fileData?.file_upload_name || null,
|
||||
path_document: fileData?.path_document || null,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
...errorCode,
|
||||
solution: solutionsWithFiles,
|
||||
spareparts: spareparts,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return errorCodesWithDetails;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Create error code with solutions and spareparts
|
||||
static async createErrorCodeWithFullData(brandId, data) {
|
||||
try {
|
||||
if (!data || typeof data !== "object") data = {};
|
||||
|
||||
if (
|
||||
!data.solution ||
|
||||
!Array.isArray(data.solution) ||
|
||||
data.solution.length === 0
|
||||
) {
|
||||
throw new ErrorHandler(
|
||||
400,
|
||||
"Error code must have at least 1 solution"
|
||||
);
|
||||
}
|
||||
|
||||
const errorId = await createErrorCodeDb(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,
|
||||
});
|
||||
|
||||
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, {
|
||||
solution_name: solutionData.solution_name,
|
||||
type_solution: solutionData.type_solution,
|
||||
text_solution: solutionData.text_solution || null,
|
||||
path_solution: solutionData.path_solution || null,
|
||||
is_active: solutionData.is_active,
|
||||
created_by: data.created_by,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const createdErrorCode = await this.getErrorCodeById(errorId);
|
||||
return createdErrorCode;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(500, `Create error code failed: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Update error code with solutions and spareparts
|
||||
static async updateErrorCodeWithFullData(brandId, errorCode, data) {
|
||||
try {
|
||||
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,
|
||||
error_code_color: data.error_code_color,
|
||||
path_icon: data.path_icon,
|
||||
is_active: data.is_active,
|
||||
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,
|
||||
{
|
||||
solution_name: solutionData.solution_name,
|
||||
type_solution: solutionData.type_solution,
|
||||
text_solution: solutionData.text_solution || null,
|
||||
path_solution: solutionData.path_solution || null,
|
||||
is_active: solutionData.is_active,
|
||||
updated_by: data.updated_by,
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// Create new solution
|
||||
await createSolutionDb(existingErrorCode.error_code_id, {
|
||||
solution_name: solutionData.solution_name,
|
||||
type_solution: solutionData.type_solution,
|
||||
text_solution: solutionData.text_solution || null,
|
||||
path_solution: solutionData.path_solution || null,
|
||||
is_active: solutionData.is_active,
|
||||
created_by: data.updated_by,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updatedErrorCode = await this.getErrorCodeById(existingErrorCode.error_code_id);
|
||||
return updatedErrorCode;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(500, `Update error code failed: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Soft delete error code
|
||||
static async deleteErrorCode(brandId, errorCode, deletedBy) {
|
||||
try {
|
||||
const errorCodeExist = await getErrorCodeByBrandAndCodeDb(brandId, errorCode);
|
||||
|
||||
if (!errorCodeExist) {
|
||||
throw new ErrorHandler(404, "Error code not found");
|
||||
}
|
||||
|
||||
const result = await deleteErrorCodeDb(brandId, errorCode, deletedBy);
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ErrorCodeService;
|
||||
Reference in New Issue
Block a user