wisdom #18

Merged
bragaz_rexita merged 11 commits from wisdom into main 2025-11-28 05:09:59 +00:00
2 changed files with 40 additions and 4 deletions
Showing only changes of commit 9926e2b181 - Show all commits

View File

@@ -9,6 +9,13 @@ const {
checkBrandNameExistsDb, checkBrandNameExistsDb,
} = require("../db/brand.db"); } = require("../db/brand.db");
const {
insertMultipleBrandSparepartsDb,
updateBrandSparepartsDb,
deleteAllBrandSparepartsDb,
getSparepartsByBrandIdDb,
} = require("../db/brand_sparepart.db");
// Error code operations // Error code operations
const { const {
getErrorCodesByBrandIdDb, getErrorCodesByBrandIdDb,
@@ -33,9 +40,21 @@ class BrandService {
try { try {
const results = await getAllBrandsDb(param); const results = await getAllBrandsDb(param);
results.data.map((element) => {}); // 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 results; return {
...results,
data: brandsWithSpareparts
};
} catch (error) { } catch (error) {
throw new ErrorHandler(error.statusCode, error.message); throw new ErrorHandler(error.statusCode, error.message);
} }
@@ -47,6 +66,9 @@ class BrandService {
const brand = await getBrandByIdDb(id); const brand = await getBrandByIdDb(id);
if (!brand) throw new ErrorHandler(404, "Brand not found"); 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 errorCodes = await getErrorCodesByBrandIdDb(brand.brand_id);
const errorCodesWithSolutions = await Promise.all( const errorCodesWithSolutions = await Promise.all(
@@ -95,6 +117,7 @@ class BrandService {
return { return {
...brand, ...brand,
spareparts: spareparts,
error_code: errorCodesWithSolutions, error_code: errorCodesWithSolutions,
}; };
} catch (error) { } catch (error) {
@@ -102,6 +125,7 @@ class BrandService {
} }
} }
// Create brand // Create brand
static async createBrandWithFullData(data) { static async createBrandWithFullData(data) {
try { try {
@@ -154,6 +178,10 @@ class BrandService {
const brandId = createdBrand.brand_id; 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) { for (const errorCodeData of data.error_code) {
const errorId = await createErrorCodeDb(brandId, { const errorId = await createErrorCodeDb(brandId, {
error_code: errorCodeData.error_code, error_code: errorCodeData.error_code,
@@ -184,7 +212,8 @@ class BrandService {
} }
} }
return await this.getBrandById(brandId); const createdBrandWithSpareparts = await this.getBrandById(brandId);
return createdBrandWithSpareparts;
} catch (error) { } catch (error) {
throw new ErrorHandler(500, `Bulk insert failed: ${error.message}`); throw new ErrorHandler(500, `Bulk insert failed: ${error.message}`);
} }
@@ -231,6 +260,10 @@ class BrandService {
await updateBrandDb(existingBrand.brand_name, brandData); 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)) { if (data.error_code && Array.isArray(data.error_code)) {
const existingErrorCodes = await getErrorCodesByBrandIdDb(id); const existingErrorCodes = await getErrorCodesByBrandIdDb(id);
const incomingErrorCodes = data.error_code.map((ec) => ec.error_code); const incomingErrorCodes = data.error_code.map((ec) => ec.error_code);
@@ -350,7 +383,8 @@ class BrandService {
} }
} }
return await this.getBrandById(id); const updatedBrandWithSpareparts = await this.getBrandById(id);
return updatedBrandWithSpareparts;
} catch (error) { } catch (error) {
throw new ErrorHandler(500, `Update failed: ${error.message}`); throw new ErrorHandler(500, `Update failed: ${error.message}`);
} }

View File

@@ -10,6 +10,7 @@ const insertBrandSchema = Joi.object({
brand_model: Joi.string().max(100).optional().allow(""), brand_model: Joi.string().max(100).optional().allow(""),
is_active: Joi.boolean().required(), is_active: Joi.boolean().required(),
description: Joi.string().max(255).optional().allow(""), description: Joi.string().max(255).optional().allow(""),
spareparts: Joi.array().items(Joi.number().integer()).optional(), // Array of sparepart_id
error_code: Joi.array() error_code: Joi.array()
.items( .items(
Joi.object({ Joi.object({
@@ -56,6 +57,7 @@ const updateBrandSchema = Joi.object({
brand_model: Joi.string().max(100).optional().allow(""), brand_model: Joi.string().max(100).optional().allow(""),
is_active: Joi.boolean().required(), is_active: Joi.boolean().required(),
description: Joi.string().max(255).optional().allow(""), description: Joi.string().max(255).optional().allow(""),
spareparts: Joi.array().items(Joi.number().integer()).optional(), // Array of sparepart_id
error_code: Joi.array() error_code: Joi.array()
.items( .items(
Joi.object({ Joi.object({