fix: brand api
This commit is contained in:
@@ -15,7 +15,6 @@ const {
|
||||
createErrorCodeDb,
|
||||
updateErrorCodeDb,
|
||||
deleteErrorCodeDb,
|
||||
checkErrorCodeExistsDb,
|
||||
} = require('../db/brand_code.db');
|
||||
|
||||
// Solution operations
|
||||
@@ -58,15 +57,31 @@ class BrandService {
|
||||
const solutionsWithFiles = await Promise.all(
|
||||
solutions.map(async (solution) => {
|
||||
let fileData = null;
|
||||
// console.log('Processing solution:', {
|
||||
// solution_id: solution.brand_code_solution_id,
|
||||
// path_solution: solution.path_solution,
|
||||
// type_solution: solution.type_solution
|
||||
// });
|
||||
|
||||
if (solution.path_solution && solution.type_solution !== 'text') {
|
||||
fileData = await getFileUploadByPathDb(solution.path_solution);
|
||||
console.log('File data found:', fileData);
|
||||
}
|
||||
|
||||
return {
|
||||
const enhancedSolution = {
|
||||
...solution,
|
||||
file_upload_name: fileData?.file_upload_name || null,
|
||||
path_document: fileData?.path_document || null
|
||||
};
|
||||
|
||||
// console.log('Enhanced solution:', {
|
||||
// solution_id: enhancedSolution.brand_code_solution_id,
|
||||
// original_path_solution: enhancedSolution.path_solution,
|
||||
// path_document: enhancedSolution.path_document,
|
||||
// file_upload_name: enhancedSolution.file_upload_name
|
||||
// });
|
||||
|
||||
return enhancedSolution;
|
||||
})
|
||||
);
|
||||
|
||||
@@ -125,7 +140,6 @@ class BrandService {
|
||||
const brandId = createdBrand.brand_id;
|
||||
|
||||
for (const errorCodeData of data.error_code) {
|
||||
// Use separate db function for error codes
|
||||
const errorId = await createErrorCodeDb(brandId, {
|
||||
error_code: errorCodeData.error_code,
|
||||
error_code_name: errorCodeData.error_code_name,
|
||||
@@ -141,7 +155,6 @@ class BrandService {
|
||||
// Create solutions for this error code
|
||||
if (errorCodeData.solution && Array.isArray(errorCodeData.solution)) {
|
||||
for (const solutionData of errorCodeData.solution) {
|
||||
// Use separate db function for solutions
|
||||
await createSolutionDb(errorId, {
|
||||
solution_name: solutionData.solution_name,
|
||||
type_solution: solutionData.type_solution,
|
||||
@@ -161,17 +174,16 @@ class BrandService {
|
||||
}
|
||||
}
|
||||
|
||||
// Soft delete brand by name (convert to ID for database operation)
|
||||
static async deleteBrand(brandName, userId) {
|
||||
// Soft delete brand by ID
|
||||
static async deleteBrand(id, userId) {
|
||||
try {
|
||||
// Get brand by name first to get ID
|
||||
const brandExist = await getBrandByNameDb(brandName);
|
||||
const brandExist = await getBrandByIdDb(id);
|
||||
|
||||
if (!brandExist) {
|
||||
throw new ErrorHandler(404, 'Brand not found');
|
||||
}
|
||||
|
||||
const result = await deleteBrandDb(brandName, userId);
|
||||
const result = await deleteBrandDb(id, userId);
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
@@ -185,7 +197,6 @@ class BrandService {
|
||||
const existingBrand = await getBrandByIdDb(id);
|
||||
if (!existingBrand) throw new ErrorHandler(404, 'Brand not found');
|
||||
|
||||
// Check if brand name already exists (excluding current brand)
|
||||
if (data.brand_name && data.brand_name !== existingBrand.brand_name) {
|
||||
const brandExists = await checkBrandNameExistsDb(data.brand_name, id);
|
||||
if (brandExists) {
|
||||
@@ -206,6 +217,7 @@ class BrandService {
|
||||
|
||||
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) {
|
||||
@@ -222,15 +234,41 @@ class BrandService {
|
||||
});
|
||||
|
||||
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) {
|
||||
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
|
||||
});
|
||||
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 {
|
||||
@@ -256,6 +294,12 @@ class BrandService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const existingEC of existingErrorCodes) {
|
||||
if (!incomingErrorCodes.includes(existingEC.error_code)) {
|
||||
await deleteErrorCodeDb(id, existingEC.error_code, data.updated_by);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return await this.getBrandById(id);
|
||||
|
||||
Reference in New Issue
Block a user