fix: brand device service
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Brand operations
|
||||
// BrandService.js
|
||||
const {
|
||||
getAllBrandsDb,
|
||||
getBrandByIdDb,
|
||||
@@ -9,7 +9,6 @@ const {
|
||||
checkBrandNameExistsDb,
|
||||
} = require('../db/brand.db');
|
||||
|
||||
// Error code operations
|
||||
const {
|
||||
getErrorCodesByBrandIdDb,
|
||||
createErrorCodeDb,
|
||||
@@ -17,7 +16,6 @@ const {
|
||||
deleteErrorCodeDb,
|
||||
} = require('../db/brand_code.db');
|
||||
|
||||
// Sparepart operations
|
||||
const {
|
||||
getSparePartnsByErrorCodeIdDb,
|
||||
createSparePartDb,
|
||||
@@ -25,7 +23,6 @@ const {
|
||||
deleteSparePartDb,
|
||||
} = require('../db/brand_sparepart.db');
|
||||
|
||||
// Solution operations
|
||||
const {
|
||||
getSolutionsByErrorCodeIdDb,
|
||||
createSolutionDb,
|
||||
@@ -37,13 +34,12 @@ const { getFileUploadByPathDb } = require('../db/file_uploads.db');
|
||||
const { ErrorHandler } = require('../helpers/error');
|
||||
|
||||
class BrandService {
|
||||
|
||||
static async getAllBrands(param) {
|
||||
try {
|
||||
const results = await getAllBrandsDb(param);
|
||||
return results;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
throw new ErrorHandler(error.statusCode || 500, error.message || 'Failed to get brands');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,20 +48,21 @@ class BrandService {
|
||||
const brand = await getBrandByIdDb(id);
|
||||
if (!brand) throw new ErrorHandler(404, 'Brand not found');
|
||||
|
||||
const errorCodes = await getErrorCodesByBrandIdDb(brand.brand_id);
|
||||
const errorCodes = await getErrorCodesByBrandIdDb(brand.brand_id) || [];
|
||||
|
||||
const errorCodesWithDetails = await Promise.all(
|
||||
errorCodes.map(async (errorCode) => {
|
||||
const solutions = await getSolutionsByErrorCodeIdDb(errorCode.error_code_id);
|
||||
|
||||
const solutions = (await getSolutionsByErrorCodeIdDb(errorCode.error_code_id)) || [];
|
||||
const solutionsWithFile = await Promise.all(
|
||||
solutions.map(async (s) => {
|
||||
let fileData = null;
|
||||
|
||||
if (s.path_solution && s.type_solution !== "text") {
|
||||
fileData = await getFileUploadByPathDb(s.path_solution);
|
||||
if (s.path_solution && s.type_solution && s.type_solution !== 'text') {
|
||||
try {
|
||||
fileData = await getFileUploadByPathDb(s.path_solution);
|
||||
} catch (e) {
|
||||
fileData = null;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...s,
|
||||
file_upload_name: fileData?.file_upload_name || null,
|
||||
@@ -74,7 +71,7 @@ class BrandService {
|
||||
})
|
||||
);
|
||||
|
||||
const spareparts = await getSparePartnsByErrorCodeIdDb(errorCode.error_code_id);
|
||||
const spareparts = (await getSparePartnsByErrorCodeIdDb(errorCode.error_code_id)) || [];
|
||||
|
||||
return {
|
||||
...errorCode,
|
||||
@@ -88,155 +85,173 @@ class BrandService {
|
||||
...brand,
|
||||
error_code: errorCodesWithDetails
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
throw new ErrorHandler(error.statusCode || 500, error.message || 'Failed to get brand detail');
|
||||
}
|
||||
}
|
||||
|
||||
static async createBrandWithFullData(data) {
|
||||
try {
|
||||
if (!data || typeof data !== "object") data = {};
|
||||
if (!data || typeof data !== 'object') data = {};
|
||||
|
||||
if (data.brand_name) {
|
||||
const exists = await checkBrandNameExistsDb(data.brand_name);
|
||||
if (exists) throw new ErrorHandler(400, "Brand name already exists");
|
||||
}
|
||||
if (!data.brand_name) throw new ErrorHandler(400, 'brand_name is required');
|
||||
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");
|
||||
if (!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 ec of data.error_code) {
|
||||
if (!ec.solution || ec.solution.length === 0) {
|
||||
if (!Array.isArray(ec.solution) || ec.solution.length === 0) {
|
||||
throw new ErrorHandler(400, `Error code ${ec.error_code} must have at least 1 solution`);
|
||||
}
|
||||
}
|
||||
|
||||
const createdBrand = await createBrandDb({
|
||||
const brandData = {
|
||||
brand_name: data.brand_name,
|
||||
brand_type: data.brand_type,
|
||||
brand_manufacture: data.brand_manufacture,
|
||||
brand_model: data.brand_model,
|
||||
brand_type: data.brand_type || null,
|
||||
brand_manufacture: data.brand_manufacture || null,
|
||||
brand_model: data.brand_model || null,
|
||||
is_active: data.is_active,
|
||||
created_by: data.created_by
|
||||
});
|
||||
created_by: data.created_by || null
|
||||
};
|
||||
|
||||
const createdBrand = await createBrandDb(brandData);
|
||||
if (!createdBrand || !createdBrand.brand_id) {
|
||||
throw new Error('Failed to create brand');
|
||||
}
|
||||
|
||||
const brandId = createdBrand.brand_id;
|
||||
for (const ec of data.error_code) {
|
||||
const errorId = await createErrorCodeDb(brandId, {
|
||||
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
|
||||
});
|
||||
|
||||
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
|
||||
});
|
||||
for (const ec of data.error_code) {
|
||||
const errorCodePayload = {
|
||||
error_code: ec.error_code,
|
||||
error_code_name: ec.error_code_name || null,
|
||||
error_code_description: ec.error_code_description || null,
|
||||
error_code_color: ec.error_code_color || null,
|
||||
path_icon: ec.path_icon || null,
|
||||
is_active: ec.is_active,
|
||||
created_by: data.created_by || null
|
||||
};
|
||||
|
||||
const errorId = await createErrorCodeDb(brandId, errorCodePayload);
|
||||
if (!errorId) {
|
||||
throw new Error('Failed to create error code');
|
||||
}
|
||||
|
||||
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
|
||||
});
|
||||
// solutions
|
||||
if (Array.isArray(ec.solution)) {
|
||||
for (const s of ec.solution) {
|
||||
const solPayload = {
|
||||
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 || null
|
||||
};
|
||||
await createSolutionDb(errorId, solPayload);
|
||||
}
|
||||
}
|
||||
|
||||
// spareparts
|
||||
if (Array.isArray(ec.sparepart)) {
|
||||
for (const sp of ec.sparepart) {
|
||||
const spPayload = {
|
||||
sparepart_name: sp.sparepart_name,
|
||||
brand_sparepart_description: sp.brand_sparepart_description || null,
|
||||
path_foto: sp.path_foto || null,
|
||||
is_active: sp.is_active,
|
||||
created_by: data.created_by || null
|
||||
};
|
||||
await createSparePartDb(errorId, spPayload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return await this.getBrandById(brandId);
|
||||
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(500, `Bulk insert failed: ${error.message}`);
|
||||
if (error instanceof ErrorHandler) throw error;
|
||||
throw new ErrorHandler(500, error.message || 'Create brand failed');
|
||||
}
|
||||
}
|
||||
|
||||
static async updateBrandWithFullData(id, data) {
|
||||
try {
|
||||
const existingBrand = await getBrandByIdDb(id);
|
||||
if (!existingBrand) throw new ErrorHandler(404, "Brand not found");
|
||||
if (!existingBrand) throw new ErrorHandler(404, 'Brand not found');
|
||||
|
||||
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.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');
|
||||
}
|
||||
|
||||
if (!data.error_code) return await this.getBrandById(id);
|
||||
const brandUpdatePayload = {
|
||||
brand_name: data.brand_name ?? existingBrand.brand_name,
|
||||
brand_type: data.brand_type ?? existingBrand.brand_type,
|
||||
brand_manufacture: data.brand_manufacture ?? existingBrand.brand_manufacture,
|
||||
brand_model: data.brand_model ?? existingBrand.brand_model,
|
||||
is_active: typeof data.is_active === 'boolean' ? data.is_active : existingBrand.is_active,
|
||||
updated_by: data.updated_by || null
|
||||
};
|
||||
await updateBrandDb(existingBrand.brand_name, brandUpdatePayload);
|
||||
|
||||
const existingErrorCodes = await getErrorCodesByBrandIdDb(id);
|
||||
if (!Array.isArray(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);
|
||||
|
||||
const existingEC = 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;
|
||||
if (existingEC) {
|
||||
const updatePayload = {
|
||||
error_code_name: ec.error_code_name ?? existingEC.error_code_name,
|
||||
error_code_description: ec.error_code_description ?? existingEC.error_code_description,
|
||||
error_code_color: ec.error_code_color ?? existingEC.error_code_color,
|
||||
path_icon: ec.path_icon ?? existingEC.path_icon,
|
||||
is_active: existingEC.is_active,
|
||||
updated_by: data.updated_by || null
|
||||
};
|
||||
await updateErrorCodeDb(existingEC.brand_id, existingEC.error_code, updatePayload);
|
||||
errorId = existingEC.error_code_id;
|
||||
} else {
|
||||
errorId = await createErrorCodeDb(id, {
|
||||
const createPayload = {
|
||||
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,
|
||||
error_code_name: ec.error_code_name || null,
|
||||
error_code_description: ec.error_code_description || null,
|
||||
error_code_color: ec.error_code_color || null,
|
||||
path_icon: ec.path_icon || null,
|
||||
is_active: ec.is_active,
|
||||
created_by: data.updated_by
|
||||
});
|
||||
created_by: data.updated_by || null
|
||||
};
|
||||
errorId = await createErrorCodeDb(id, createPayload);
|
||||
}
|
||||
|
||||
const existingSolutions = await getSolutionsByErrorCodeIdDb(errorId);
|
||||
const incomingSolutionNames = ec.solution?.map(s => s.solution_name) || [];
|
||||
if (!errorId) throw new Error(`Failed to create/update error code ${ec.error_code}`);
|
||||
|
||||
for (const s of ec.solution || []) {
|
||||
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);
|
||||
|
||||
const solPayload = {
|
||||
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 || null,
|
||||
created_by: data.updated_by || null
|
||||
};
|
||||
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
|
||||
});
|
||||
await updateSolutionDb(existsSolution.brand_code_solution_id, solPayload);
|
||||
} 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
|
||||
});
|
||||
await createSolutionDb(errorId, solPayload);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,28 +261,23 @@ class BrandService {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
const existingSpareparts = await getSparePartnsByErrorCodeIdDb(errorId) || [];
|
||||
const incomingSparepartNames = (ec.sparepart || []).map(sp => sp.sparepart_name);
|
||||
|
||||
for (const sp of (ec.sparepart || [])) {
|
||||
const existsSP = existingSpareparts.find(e => e.sparepart_name === sp.sparepart_name);
|
||||
const spPayload = {
|
||||
sparepart_name: sp.sparepart_name,
|
||||
brand_sparepart_description: sp.brand_sparepart_description || null,
|
||||
path_foto: sp.path_foto || null,
|
||||
is_active: sp.is_active,
|
||||
updated_by: data.updated_by || null,
|
||||
created_by: data.updated_by || null
|
||||
};
|
||||
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
|
||||
});
|
||||
await updateSparePartDb(existsSP.brand_sparepart_id, spPayload);
|
||||
} 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
|
||||
});
|
||||
await createSparePartDb(errorId, spPayload);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,7 +286,6 @@ class BrandService {
|
||||
await deleteSparePartDb(es.brand_sparepart_id, data.updated_by);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for (const ec of existingErrorCodes) {
|
||||
@@ -286,21 +295,20 @@ class BrandService {
|
||||
}
|
||||
|
||||
return await this.getBrandById(id);
|
||||
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(500, `Update failed: ${error.message}`);
|
||||
if (error instanceof ErrorHandler) throw error;
|
||||
throw new ErrorHandler(500, `Update failed: ${error.message || 'unknown error'}`);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
throw new ErrorHandler(error.statusCode || 500, error.message || 'Delete failed');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user