repair: sparepart per error code
This commit is contained in:
@@ -9,9 +9,9 @@ const {
|
||||
} = require("../db/brand.db");
|
||||
|
||||
const {
|
||||
insertMultipleBrandSparepartsDb,
|
||||
updateBrandSparepartsDb,
|
||||
getSparepartsByBrandIdDb,
|
||||
insertMultipleErrorCodeSparepartsDb,
|
||||
updateErrorCodeSparepartsDb,
|
||||
getSparepartsByErrorCodeIdDb,
|
||||
} = require("../db/brand_sparepart.db");
|
||||
|
||||
// Error code operations
|
||||
@@ -41,20 +41,10 @@ class BrandService {
|
||||
try {
|
||||
const results = await getAllBrandsDb(param);
|
||||
|
||||
// Add spareparts data for each brand
|
||||
const brandsWithSpareparts = await Promise.all(
|
||||
results.data.map(async (brand) => {
|
||||
const spareparts = await getSparepartsByBrandIdDb(brand.brand_id);
|
||||
return {
|
||||
...brand,
|
||||
spareparts: spareparts
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
// Return brands data - spareparts are now associated with error codes, not brands
|
||||
return {
|
||||
...results,
|
||||
data: brandsWithSpareparts
|
||||
data: results.data
|
||||
};
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
@@ -67,17 +57,17 @@ class BrandService {
|
||||
const brand = await getBrandByIdDb(id);
|
||||
if (!brand) throw new ErrorHandler(404, "Brand not found");
|
||||
|
||||
// Get spareparts for this brand
|
||||
const spareparts = await getSparepartsByBrandIdDb(brand.brand_id);
|
||||
|
||||
const errorCodes = await getErrorCodesByBrandIdDb(brand.brand_id);
|
||||
|
||||
const errorCodesWithSolutions = await Promise.all(
|
||||
const errorCodesWithSolutionsAndSpareparts = await Promise.all(
|
||||
errorCodes.map(async (errorCode) => {
|
||||
const solutions = await getSolutionsByErrorCodeIdDb(
|
||||
errorCode.error_code_id
|
||||
);
|
||||
|
||||
// Get spareparts for this error code
|
||||
const errorCodeSpareparts = await getSparepartsByErrorCodeIdDb(errorCode.error_code_id);
|
||||
|
||||
const solutionsWithFiles = await Promise.all(
|
||||
solutions.map(async (solution) => {
|
||||
let fileData = null;
|
||||
@@ -112,14 +102,14 @@ class BrandService {
|
||||
return {
|
||||
...errorCode,
|
||||
solution: solutionsWithFiles,
|
||||
spareparts: errorCodeSpareparts,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
...brand,
|
||||
spareparts: spareparts,
|
||||
error_code: errorCodesWithSolutions,
|
||||
error_code: errorCodesWithSolutionsAndSpareparts,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
@@ -179,10 +169,6 @@ class BrandService {
|
||||
|
||||
const brandId = createdBrand.brand_id;
|
||||
|
||||
if (data.spareparts && Array.isArray(data.spareparts) && data.spareparts.length > 0) {
|
||||
await insertMultipleBrandSparepartsDb(brandId, data.spareparts, data.created_by);
|
||||
}
|
||||
|
||||
for (const errorCodeData of data.error_code) {
|
||||
const errorId = await createErrorCodeDb(brandId, {
|
||||
error_code: errorCodeData.error_code,
|
||||
@@ -198,6 +184,11 @@ class BrandService {
|
||||
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) {
|
||||
@@ -237,33 +228,14 @@ class BrandService {
|
||||
}
|
||||
}
|
||||
|
||||
// Validate sparepart id
|
||||
static async validateSparepartIds(sparepartIds) {
|
||||
if (!sparepartIds || !Array.isArray(sparepartIds) || sparepartIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const existingSpareparts = await getSparepartsByIdsDb(sparepartIds);
|
||||
|
||||
if (existingSpareparts.length !== sparepartIds.length) {
|
||||
const existingIds = existingSpareparts.map(sp => sp.sparepart_id);
|
||||
const invalidIds = sparepartIds.filter(id => !existingIds.includes(id));
|
||||
throw new ErrorHandler(400, `Invalid sparepart IDs: ${invalidIds.join(', ')}`);
|
||||
}
|
||||
|
||||
return existingSpareparts;
|
||||
}
|
||||
|
||||
|
||||
// Update brand
|
||||
static async updateBrandWithFullData(id, data) {
|
||||
try {
|
||||
const existingBrand = await getBrandByIdDb(id);
|
||||
if (!existingBrand) throw new ErrorHandler(404, "Brand not found");
|
||||
|
||||
if (data.spareparts) {
|
||||
await this.validateSparepartIds(data.spareparts);
|
||||
}
|
||||
|
||||
|
||||
if (data.brand_name && data.brand_name !== existingBrand.brand_name) {
|
||||
const brandExists = await checkBrandNameExistsDb(data.brand_name, id);
|
||||
if (brandExists) {
|
||||
@@ -282,10 +254,6 @@ class BrandService {
|
||||
|
||||
await updateBrandDb(existingBrand.brand_name, brandData);
|
||||
|
||||
if (data.spareparts !== undefined) {
|
||||
await updateBrandSparepartsDb(existingBrand.brand_id, data.spareparts || [], data.updated_by);
|
||||
}
|
||||
|
||||
if (data.error_code && Array.isArray(data.error_code)) {
|
||||
const existingErrorCodes = await getErrorCodesByBrandIdDb(id);
|
||||
const incomingErrorCodes = data.error_code.map((ec) => ec.error_code);
|
||||
@@ -312,6 +280,10 @@ class BrandService {
|
||||
}
|
||||
);
|
||||
|
||||
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)
|
||||
@@ -380,6 +352,10 @@ class BrandService {
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user