repair brand device: add brand sparepart in 2 step

This commit is contained in:
2025-11-17 16:01:29 +07:00
parent ff4176f8f9
commit 8176eb2bdd
8 changed files with 327 additions and 624 deletions

View File

@@ -17,6 +17,14 @@ const {
deleteErrorCodeDb,
} = require('../db/brand_code.db');
// Sparepart operations
const {
getSparePartnsByErrorCodeIdDb,
createSparePartDb,
updateSparePartDb,
deleteSparePartDb,
} = require('../db/brand_sparepart.db');
// Solution operations
const {
getSolutionsByErrorCodeIdDb,
@@ -24,25 +32,21 @@ const {
updateSolutionDb,
deleteSolutionDb,
} = require('../db/brand_code_solution.db');
const { getFileUploadByPathDb } = require('../db/file_uploads.db');
const { ErrorHandler } = require('../helpers/error');
class BrandService {
// Get all brands
static async getAllBrands(param) {
try {
const results = await getAllBrandsDb(param);
results.data.map(element => {
});
return results;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Get brand by ID with complete data
static async getBrandById(id) {
try {
const brand = await getBrandByIdDb(id);
@@ -50,123 +54,109 @@ class BrandService {
const errorCodes = await getErrorCodesByBrandIdDb(brand.brand_id);
const errorCodesWithSolutions = await Promise.all(
const errorCodesWithDetails = await Promise.all(
errorCodes.map(async (errorCode) => {
const solutions = await getSolutionsByErrorCodeIdDb(errorCode.error_code_id);
const solutionsWithFiles = await Promise.all(
solutions.map(async (solution) => {
const solutionsWithFile = await Promise.all(
solutions.map(async (s) => {
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);
if (s.path_solution && s.type_solution !== "text") {
fileData = await getFileUploadByPathDb(s.path_solution);
}
const enhancedSolution = {
...solution,
return {
...s,
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;
})
);
const spareparts = await getSparePartnsByErrorCodeIdDb(errorCode.error_code_id);
return {
...errorCode,
solution: solutionsWithFiles
solution: solutionsWithFile,
sparepart: spareparts
};
})
);
return {
...brand,
error_code: errorCodesWithSolutions
error_code: errorCodesWithDetails
};
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Create brand
static async createBrandWithFullData(data) {
try {
if (!data || typeof data !== 'object') data = {};
if (!data || typeof data !== "object") data = {};
if (data.brand_name) {
const brandExists = await checkBrandNameExistsDb(data.brand_name);
if (brandExists) {
throw new ErrorHandler(400, 'Brand name already exists');
const exists = await checkBrandNameExistsDb(data.brand_name);
if (exists) throw new ErrorHandler(400, "Brand name already exists");
}
if (!data.error_code || !Array.isArray(data.error_code)) {
throw new ErrorHandler(400, "Brand must have at least 1 error code");
}
for (const ec of data.error_code) {
if (!ec.solution || ec.solution.length === 0) {
throw new ErrorHandler(400, `Error code ${ec.error_code} must have at least 1 solution`);
}
}
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 = {
const createdBrand = await createBrandDb({
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,
created_by: data.created_by
};
const createdBrand = await createBrandDb(brandData);
if (!createdBrand) {
throw new Error('Failed to create brand');
}
});
const brandId = createdBrand.brand_id;
for (const errorCodeData of data.error_code) {
for (const ec 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,
error_code: ec.error_code,
error_code_name: ec.error_code_name,
error_code_description: ec.error_code_description,
error_code_color: ec.error_code_color,
path_icon: ec.path_icon,
is_active: ec.is_active,
created_by: data.created_by
});
if (!errorId) {
throw new Error('Failed to create error code');
for (const s of ec.solution) {
await createSolutionDb(errorId, {
solution_name: s.solution_name,
type_solution: s.type_solution,
text_solution: s.text_solution || null,
path_solution: s.path_solution || null,
is_active: s.is_active,
created_by: 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,
if (Array.isArray(ec.sparepart)) {
for (const sp of ec.sparepart) {
await createSparePartDb(errorId, {
sparepart_name: sp.sparepart_name,
brand_sparepart_description: sp.brand_sparepart_description,
path_foto: sp.path_foto || null,
is_active: sp.is_active,
created_by: data.created_by
});
}
}
}
return await this.getBrandById(brandId);
@@ -176,143 +166,143 @@ class BrandService {
}
}
// Soft delete brand by ID
static async deleteBrand(id, userId) {
try {
const brandExist = await getBrandByIdDb(id);
if (!brandExist) {
throw new ErrorHandler(404, 'Brand not found');
}
const result = await deleteBrandDb(id, userId);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Update brand
static async updateBrandWithFullData(id, data) {
try {
const existingBrand = await getBrandByIdDb(id);
if (!existingBrand) throw new ErrorHandler(404, 'Brand not found');
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) {
throw new ErrorHandler(400, 'Brand name already exists');
await updateBrandDb(existingBrand.brand_name, {
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,
updated_by: data.updated_by
});
if (!data.error_code) return await this.getBrandById(id);
const existingErrorCodes = await getErrorCodesByBrandIdDb(id);
const incomingErrorCodes = data.error_code.map(ec => ec.error_code);
for (const ec of data.error_code) {
const existsEC = existingErrorCodes.find(e => e.error_code === ec.error_code);
let errorId = null;
if (existsEC) {
await updateErrorCodeDb(existingBrand.brand_id, existsEC.error_code, {
error_code_name: ec.error_code_name,
error_code_description: ec.error_code_description,
error_code_color: ec.error_code_color,
path_icon: ec.path_icon,
is_active: ec.is_active,
updated_by: data.updated_by
});
errorId = existsEC.error_code_id;
} else {
errorId = await createErrorCodeDb(id, {
error_code: ec.error_code,
error_code_name: ec.error_code_name,
error_code_description: ec.error_code_description,
error_code_color: ec.error_code_color,
path_icon: ec.path_icon,
is_active: ec.is_active,
created_by: data.updated_by
});
}
const existingSolutions = await getSolutionsByErrorCodeIdDb(errorId);
const incomingSolutionNames = ec.solution?.map(s => s.solution_name) || [];
for (const s of ec.solution || []) {
const existsSolution = existingSolutions.find(es => es.solution_name === s.solution_name);
if (existsSolution) {
await updateSolutionDb(existsSolution.brand_code_solution_id, {
solution_name: s.solution_name,
type_solution: s.type_solution,
text_solution: s.text_solution || null,
path_solution: s.path_solution || null,
is_active: s.is_active,
updated_by: data.updated_by
});
} else {
await createSolutionDb(errorId, {
solution_name: s.solution_name,
type_solution: s.type_solution,
text_solution: s.text_solution || null,
path_solution: s.path_solution || null,
is_active: s.is_active,
created_by: data.updated_by
});
}
}
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,
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.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.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);
}
for (const es of existingSolutions) {
if (!incomingSolutionNames.includes(es.solution_name)) {
await deleteSolutionDb(es.brand_code_solution_id, data.updated_by);
}
}
const existingSpareparts = await getSparePartnsByErrorCodeIdDb(errorId);
const incomingSparepartNames = ec.sparepart?.map(sp => sp.sparepart_name) || [];
for (const sp of ec.sparepart || []) {
const existsSP = existingSpareparts.find(es => es.sparepart_name === sp.sparepart_name);
if (existsSP) {
await updateSparePartDb(existsSP.brand_sparepart_id, {
sparepart_name: sp.sparepart_name,
brand_sparepart_description: sp.brand_sparepart_description,
path_foto: sp.path_foto || null,
is_active: sp.is_active,
updated_by: data.updated_by
});
} else {
await createSparePartDb(errorId, {
sparepart_name: sp.sparepart_name,
brand_sparepart_description: sp.brand_sparepart_description,
path_foto: sp.path_foto || null,
is_active: sp.is_active,
created_by: data.updated_by
});
}
}
for (const es of existingSpareparts) {
if (!incomingSparepartNames.includes(es.sparepart_name)) {
await deleteSparePartDb(es.brand_sparepart_id, data.updated_by);
}
}
}
for (const ec of existingErrorCodes) {
if (!incomingErrorCodes.includes(ec.error_code)) {
await deleteErrorCodeDb(id, ec.error_code, data.updated_by);
}
}
return await this.getBrandById(id);
} catch (error) {
throw new ErrorHandler(500, `Update failed: ${error.message}`);
}
}
static async deleteBrand(id, userId) {
try {
const exist = await getBrandByIdDb(id);
if (!exist) throw new ErrorHandler(404, "Brand not found");
return await deleteBrandDb(id, userId);
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
}
module.exports = BrandService;
module.exports = BrandService;

View File

@@ -1,106 +0,0 @@
const {
getAllBrandSparepartsDb,
getBrandSparepartByIdDb,
createBrandSparepartDb,
updateBrandSparepartDb,
deleteBrandSparepartDb,
checkBrandSparepartNameExistsDb,
} = require('../db/brand_sparepart.db');
const { ErrorHandler } = require('../helpers/error');
class BrandSparepartService {
static async getAllBrandSparepart(param) {
try {
const results = await getAllBrandSparepartsDb(param);
results.data.map((item) => {});
return results;
} catch (error) {
throw new ErrorHandler(error.statusCode || 500, error.message);
}
}
static async getBrandSparepartById(id) {
try {
const brandSparepart = await getBrandSparepartByIdDb(id);
if (!brandSparepart) throw new ErrorHandler(404, 'Brand sparepart not found');
return brandSparepart;
} catch (error) {
throw new ErrorHandler(error.statusCode || 500, error.message);
}
}
static async createBrandSparepart(data) {
try {
if (!data || typeof data !== 'object') data = {};
if (data.sparepart_name) {
const exists = await checkBrandSparepartNameExistsDb(data.sparepart_name);
if (exists) throw new ErrorHandler(400, 'Brand sparepart name already exists');
}
const insertData = {
sparepart_name: data.sparepart_name,
brand_sparepart_description: data.brand_sparepart_description,
path_foto: data.path_foto,
error_code_id: data.error_code_id,
is_active: data.is_active,
created_by: data.created_by,
};
const created = await createBrandSparepartDb(insertData);
if (!created) throw new ErrorHandler(500, 'Failed to create brand sparepart');
return created;
} catch (error) {
throw new ErrorHandler(error.statusCode || 500, error.message);
}
}
static async updateBrandSparepart(id, data) {
try {
const existing = await getBrandSparepartByIdDb(id);
if (!existing) throw new ErrorHandler(404, 'Brand sparepart not found');
if (
data.sparepart_name &&
data.sparepart_name !== existing.sparepart_name
) {
const exists = await checkBrandSparepartNameExistsDb(data.sparepart_name, id);
if (exists) throw new ErrorHandler(400, 'Brand sparepart name already exists');
}
const updateData = {
sparepart_name: data.sparepart_name,
brand_sparepart_description: data.brand_sparepart_description,
path_foto: data.path_foto || existing.path_foto,
error_code_id: data.error_code_id,
is_active: data.is_active ?? existing.is_active,
updated_by: data.updated_by || null,
};
const updated = await updateBrandSparepartDb(id, updateData);
if (!updated) throw new ErrorHandler(500, 'Failed to update brand sparepart');
return await this.getBrandSparepartById(id);
} catch (error) {
throw new ErrorHandler(error.statusCode || 500, error.message);
}
}
static async deleteBrandSparepart(id, userId) {
try {
const existing = await getBrandSparepartByIdDb(id);
if (!existing) throw new ErrorHandler(404, 'Brand sparepart not found');
const deleted = await deleteBrandSparepartDb(id, userId);
if (!deleted) throw new ErrorHandler(500, 'Failed to delete brand sparepart');
return deleted;
} catch (error) {
throw new ErrorHandler(error.statusCode || 500, error.message);
}
}
}
module.exports = BrandSparepartService;