repair: brand + errorcode
This commit is contained in:
@@ -7,32 +7,6 @@ const {
|
||||
deleteBrandDb,
|
||||
checkBrandNameExistsDb,
|
||||
} = require("../db/brand.db");
|
||||
|
||||
const {
|
||||
insertMultipleErrorCodeSparepartsDb,
|
||||
updateErrorCodeSparepartsDb,
|
||||
getSparepartsByErrorCodeIdDb,
|
||||
} = require("../db/brand_sparepart.db");
|
||||
|
||||
// Error code operations
|
||||
const {
|
||||
getErrorCodesByBrandIdDb,
|
||||
createErrorCodeDb,
|
||||
updateErrorCodeDb,
|
||||
deleteErrorCodeDb,
|
||||
} = require("../db/brand_code.db");
|
||||
|
||||
// Sparepart operations
|
||||
const { getSparepartsByIdsDb } = require("../db/sparepart.db");
|
||||
|
||||
// Solution operations
|
||||
const {
|
||||
getSolutionsByErrorCodeIdDb,
|
||||
createSolutionDb,
|
||||
updateSolutionDb,
|
||||
deleteSolutionDb,
|
||||
} = require("../db/brand_code_solution.db");
|
||||
const { getFileUploadByPathDb } = require("../db/file_uploads.db");
|
||||
const { ErrorHandler } = require("../helpers/error");
|
||||
|
||||
class BrandService {
|
||||
@@ -41,7 +15,6 @@ class BrandService {
|
||||
try {
|
||||
const results = await getAllBrandsDb(param);
|
||||
|
||||
// Return brands data - spareparts are now associated with error codes, not brands
|
||||
return {
|
||||
...results,
|
||||
data: results.data
|
||||
@@ -65,10 +38,8 @@ class BrandService {
|
||||
|
||||
|
||||
// Create brand
|
||||
static async createBrandWithFullData(data) {
|
||||
static async createBrand(data) {
|
||||
try {
|
||||
if (!data || typeof data !== "object") data = {};
|
||||
|
||||
if (data.brand_name) {
|
||||
const brandExists = await checkBrandNameExistsDb(data.brand_name);
|
||||
if (brandExists) {
|
||||
@@ -76,85 +47,23 @@ class BrandService {
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
!data.error_code ||
|
||||
!Array.isArray(data.error_code) ||
|
||||
data.error_code.length === 0
|
||||
) {
|
||||
throw new ErrorHandler(
|
||||
400,
|
||||
"Brand must have at least 1 error code with solution"
|
||||
);
|
||||
}
|
||||
|
||||
for (const errorCode of data.error_code) {
|
||||
if (
|
||||
!errorCode.solution ||
|
||||
!Array.isArray(errorCode.solution) ||
|
||||
errorCode.solution.length === 0
|
||||
) {
|
||||
throw new ErrorHandler(
|
||||
400,
|
||||
`Error code ${errorCode.error_code} must have at least 1 solution`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const brandData = {
|
||||
brand_name: data.brand_name,
|
||||
brand_type: data.brand_type,
|
||||
brand_manufacture: data.brand_manufacture,
|
||||
brand_model: data.brand_model,
|
||||
is_active: data.is_active,
|
||||
is_active: data.is_active !== undefined ? data.is_active : true,
|
||||
created_by: data.created_by,
|
||||
};
|
||||
|
||||
const createdBrand = await createBrandDb(brandData);
|
||||
if (!createdBrand) {
|
||||
throw new Error("Failed to create brand");
|
||||
throw new ErrorHandler(500, "Failed to create brand");
|
||||
}
|
||||
|
||||
const brandId = createdBrand.brand_id;
|
||||
|
||||
for (const errorCodeData of data.error_code) {
|
||||
const errorId = await createErrorCodeDb(brandId, {
|
||||
error_code: errorCodeData.error_code,
|
||||
error_code_name: errorCodeData.error_code_name,
|
||||
error_code_description: errorCodeData.error_code_description,
|
||||
error_code_color: errorCodeData.error_code_color,
|
||||
path_icon: errorCodeData.path_icon,
|
||||
is_active: errorCodeData.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 (errorCodeData.spareparts && Array.isArray(errorCodeData.spareparts)) {
|
||||
await insertMultipleErrorCodeSparepartsDb(errorId, errorCodeData.spareparts, data.created_by);
|
||||
}
|
||||
|
||||
// Create solutions for this error code
|
||||
if (errorCodeData.solution && Array.isArray(errorCodeData.solution)) {
|
||||
for (const solutionData of errorCodeData.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 createdBrandWithSpareparts = await this.getBrandById(brandId);
|
||||
return createdBrandWithSpareparts;
|
||||
return createdBrand;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(500, `Bulk insert failed: ${error.message}`);
|
||||
throw new ErrorHandler(error.statusCode || 500, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,12 +86,11 @@ class BrandService {
|
||||
|
||||
|
||||
// Update brand
|
||||
static async updateBrandWithFullData(id, data) {
|
||||
static async updateBrand(id, data) {
|
||||
try {
|
||||
const existingBrand = await getBrandByIdDb(id);
|
||||
if (!existingBrand) throw new ErrorHandler(404, "Brand not found");
|
||||
|
||||
|
||||
if (data.brand_name && data.brand_name !== existingBrand.brand_name) {
|
||||
const brandExists = await checkBrandNameExistsDb(data.brand_name, id);
|
||||
if (brandExists) {
|
||||
@@ -191,147 +99,22 @@ class BrandService {
|
||||
}
|
||||
|
||||
const brandData = {
|
||||
brand_name: data.brand_name,
|
||||
brand_type: data.brand_type,
|
||||
brand_manufacture: data.brand_manufacture,
|
||||
brand_model: data.brand_model,
|
||||
is_active: data.is_active,
|
||||
brand_name: data.brand_name || existingBrand.brand_name,
|
||||
brand_type: data.brand_type !== undefined ? data.brand_type : existingBrand.brand_type,
|
||||
brand_manufacture: data.brand_manufacture !== undefined ? data.brand_manufacture : existingBrand.brand_manufacture,
|
||||
brand_model: data.brand_model !== undefined ? data.brand_model : existingBrand.brand_model,
|
||||
is_active: data.is_active !== undefined ? data.is_active : existingBrand.is_active,
|
||||
updated_by: data.updated_by,
|
||||
};
|
||||
|
||||
await updateBrandDb(existingBrand.brand_name, brandData);
|
||||
|
||||
if (data.error_code && Array.isArray(data.error_code)) {
|
||||
const existingErrorCodes = await getErrorCodesByBrandIdDb(id);
|
||||
const incomingErrorCodes = data.error_code.map((ec) => ec.error_code);
|
||||
|
||||
// Create/update/delete error codes
|
||||
for (const errorCodeData of data.error_code) {
|
||||
// Check if error code already exists
|
||||
const existingEC = existingErrorCodes.find(
|
||||
(ec) => ec.error_code === errorCodeData.error_code
|
||||
);
|
||||
|
||||
if (existingEC) {
|
||||
// Update existing error code using separate db function
|
||||
await updateErrorCodeDb(
|
||||
existingEC.brand_id,
|
||||
existingEC.error_code,
|
||||
{
|
||||
error_code_name: errorCodeData.error_code_name,
|
||||
error_code_description: errorCodeData.error_code_description,
|
||||
error_code_color: errorCodeData.error_code_color,
|
||||
path_icon: errorCodeData.path_icon,
|
||||
is_active: errorCodeData.is_active,
|
||||
updated_by: data.updated_by,
|
||||
}
|
||||
);
|
||||
|
||||
if (errorCodeData.spareparts && Array.isArray(errorCodeData.spareparts)) {
|
||||
await updateErrorCodeSparepartsDb(existingEC.error_code_id, errorCodeData.spareparts, data.updated_by);
|
||||
}
|
||||
|
||||
if (
|
||||
errorCodeData.solution &&
|
||||
Array.isArray(errorCodeData.solution)
|
||||
) {
|
||||
const existingSolutions = await getSolutionsByErrorCodeIdDb(
|
||||
existingEC.error_code_id
|
||||
);
|
||||
const incomingSolutionNames = errorCodeData.solution.map(
|
||||
(s) => s.solution_name
|
||||
);
|
||||
|
||||
// Update or create solutions
|
||||
for (const solutionData of errorCodeData.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(existingEC.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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const errorId = await createErrorCodeDb(id, {
|
||||
error_code: errorCodeData.error_code,
|
||||
error_code_name: errorCodeData.error_code_name,
|
||||
error_code_description: errorCodeData.error_code_description,
|
||||
error_code_color: errorCodeData.error_code_color,
|
||||
path_icon: errorCodeData.path_icon,
|
||||
is_active: errorCodeData.is_active,
|
||||
created_by: data.updated_by,
|
||||
});
|
||||
|
||||
if (errorCodeData.spareparts && Array.isArray(errorCodeData.spareparts)) {
|
||||
await insertMultipleErrorCodeSparepartsDb(errorId, errorCodeData.spareparts, data.updated_by);
|
||||
}
|
||||
|
||||
if (
|
||||
errorCodeData.solution &&
|
||||
Array.isArray(errorCodeData.solution)
|
||||
) {
|
||||
for (const solutionData of errorCodeData.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.updated_by,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const existingEC of existingErrorCodes) {
|
||||
if (!incomingErrorCodes.includes(existingEC.error_code)) {
|
||||
await deleteErrorCodeDb(id, existingEC.error_code, data.updated_by);
|
||||
}
|
||||
}
|
||||
const updatedBrand = await updateBrandDb(existingBrand.brand_name, brandData);
|
||||
if (!updatedBrand) {
|
||||
throw new ErrorHandler(500, "Failed to update brand");
|
||||
}
|
||||
|
||||
const updatedBrandWithSpareparts = await this.getBrandById(id);
|
||||
return updatedBrandWithSpareparts;
|
||||
return updatedBrand;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(500, `Update failed: ${error.message}`);
|
||||
throw new ErrorHandler(error.statusCode || 500, error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user