Compare commits
2 Commits
fdb560985c
...
462cf6e94b
| Author | SHA1 | Date | |
|---|---|---|---|
| 462cf6e94b | |||
| 9926e2b181 |
130
db/brand_sparepart.db.js
Normal file
130
db/brand_sparepart.db.js
Normal file
@@ -0,0 +1,130 @@
|
||||
const pool = require("../config");
|
||||
|
||||
// Get spareparts by brand_id
|
||||
const getSparepartsByBrandIdDb = async (brandId) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
s.sparepart_id,
|
||||
s.sparepart_name,
|
||||
s.sparepart_code,
|
||||
s.sparepart_description,
|
||||
s.sparepart_model,
|
||||
s.sparepart_foto,
|
||||
s.sparepart_item_type,
|
||||
s.sparepart_qty,
|
||||
s.sparepart_unit,
|
||||
s.sparepart_merk,
|
||||
s.sparepart_stok,
|
||||
s.created_at,
|
||||
s.updated_at
|
||||
FROM brand_spareparts bs
|
||||
JOIN m_sparepart s ON bs.sparepart_id = s.sparepart_id
|
||||
WHERE bs.brand_id = $1
|
||||
AND s.deleted_at IS NULL
|
||||
ORDER BY s.sparepart_name
|
||||
`;
|
||||
const result = await pool.query(queryText, [brandId]);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Get brands by sparepart_id
|
||||
const getBrandsBySparepartIdDb = async (sparepartId) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
b.brand_id,
|
||||
b.brand_name,
|
||||
b.brand_type,
|
||||
b.brand_manufacture,
|
||||
b.brand_model,
|
||||
b.brand_code,
|
||||
b.is_active,
|
||||
b.created_at,
|
||||
b.updated_at
|
||||
FROM brand_spareparts bs
|
||||
JOIN m_brands b ON bs.brand_id = b.brand_id
|
||||
WHERE bs.sparepart_id = $1
|
||||
AND b.deleted_at IS NULL
|
||||
ORDER BY b.brand_name
|
||||
`;
|
||||
const result = await pool.query(queryText, [sparepartId]);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Insert brand-spareparts relationship
|
||||
const insertBrandSparepartDb = async (brandId, sparepartId, createdBy) => {
|
||||
const queryText = `
|
||||
INSERT INTO brand_spareparts (brand_id, sparepart_id, created_by, created_at)
|
||||
VALUES ($1, $2, $3, CURRENT_TIMESTAMP)
|
||||
`;
|
||||
const result = await pool.query(queryText, [brandId, sparepartId, createdBy]);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Insert multiple brand-spareparts relationships
|
||||
const insertMultipleBrandSparepartsDb = async (brandId, sparepartIds, createdBy) => {
|
||||
if (!sparepartIds || sparepartIds.length === 0) return [];
|
||||
|
||||
const values = sparepartIds.map((_, index) => `($1, $${index + 2}, $${sparepartIds.length + 2}, CURRENT_TIMESTAMP)`).join(', ');
|
||||
const queryText = `
|
||||
INSERT INTO brand_spareparts (brand_id, sparepart_id, created_by, created_at)
|
||||
VALUES ${values}
|
||||
`;
|
||||
const params = [brandId, ...sparepartIds, createdBy];
|
||||
const result = await pool.query(queryText, params);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Delete specific brand-sparepart relationship
|
||||
const deleteBrandSparepartDb = async (brandId, sparepartId) => {
|
||||
const queryText = `
|
||||
DELETE FROM brand_spareparts
|
||||
WHERE brand_id = $1 AND sparepart_id = $2
|
||||
`;
|
||||
const result = await pool.query(queryText, [brandId, sparepartId]);
|
||||
return result.rowsAffected > 0;
|
||||
};
|
||||
|
||||
// Delete all spareparts for a brand
|
||||
const deleteAllBrandSparepartsDb = async (brandId) => {
|
||||
const queryText = `
|
||||
DELETE FROM brand_spareparts
|
||||
WHERE brand_id = $1
|
||||
`;
|
||||
const result = await pool.query(queryText, [brandId]);
|
||||
return result.rowsAffected > 0;
|
||||
};
|
||||
|
||||
// Update brand-spareparts (replace all)
|
||||
const updateBrandSparepartsDb = async (brandId, sparepartIds, updatedBy) => {
|
||||
// Delete existing relationships
|
||||
await deleteAllBrandSparepartsDb(brandId);
|
||||
|
||||
// Insert new relationships
|
||||
if (sparepartIds && sparepartIds.length > 0) {
|
||||
return await insertMultipleBrandSparepartsDb(brandId, sparepartIds, updatedBy);
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
// Check if brand-sparepart relationship exists
|
||||
const checkBrandSparepartExistsDb = async (brandId, sparepartId) => {
|
||||
const queryText = `
|
||||
SELECT 1
|
||||
FROM brand_spareparts
|
||||
WHERE brand_id = $1 AND sparepart_id = $2
|
||||
`;
|
||||
const result = await pool.query(queryText, [brandId, sparepartId]);
|
||||
return result.recordset.length > 0;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getSparepartsByBrandIdDb,
|
||||
getBrandsBySparepartIdDb,
|
||||
insertBrandSparepartDb,
|
||||
insertMultipleBrandSparepartsDb,
|
||||
deleteBrandSparepartDb,
|
||||
deleteAllBrandSparepartsDb,
|
||||
updateBrandSparepartsDb,
|
||||
checkBrandSparepartExistsDb,
|
||||
};
|
||||
@@ -9,6 +9,13 @@ const {
|
||||
checkBrandNameExistsDb,
|
||||
} = require("../db/brand.db");
|
||||
|
||||
const {
|
||||
insertMultipleBrandSparepartsDb,
|
||||
updateBrandSparepartsDb,
|
||||
deleteAllBrandSparepartsDb,
|
||||
getSparepartsByBrandIdDb,
|
||||
} = require("../db/brand_sparepart.db");
|
||||
|
||||
// Error code operations
|
||||
const {
|
||||
getErrorCodesByBrandIdDb,
|
||||
@@ -33,9 +40,21 @@ class BrandService {
|
||||
try {
|
||||
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) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
}
|
||||
@@ -47,6 +66,9 @@ 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(
|
||||
@@ -95,6 +117,7 @@ class BrandService {
|
||||
|
||||
return {
|
||||
...brand,
|
||||
spareparts: spareparts,
|
||||
error_code: errorCodesWithSolutions,
|
||||
};
|
||||
} catch (error) {
|
||||
@@ -102,6 +125,7 @@ class BrandService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Create brand
|
||||
static async createBrandWithFullData(data) {
|
||||
try {
|
||||
@@ -154,6 +178,10 @@ 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,
|
||||
@@ -184,7 +212,8 @@ class BrandService {
|
||||
}
|
||||
}
|
||||
|
||||
return await this.getBrandById(brandId);
|
||||
const createdBrandWithSpareparts = await this.getBrandById(brandId);
|
||||
return createdBrandWithSpareparts;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(500, `Bulk insert failed: ${error.message}`);
|
||||
}
|
||||
@@ -231,6 +260,10 @@ 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);
|
||||
@@ -350,7 +383,8 @@ class BrandService {
|
||||
}
|
||||
}
|
||||
|
||||
return await this.getBrandById(id);
|
||||
const updatedBrandWithSpareparts = await this.getBrandById(id);
|
||||
return updatedBrandWithSpareparts;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(500, `Update failed: ${error.message}`);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ const insertBrandSchema = Joi.object({
|
||||
brand_model: Joi.string().max(100).optional().allow(""),
|
||||
is_active: Joi.boolean().required(),
|
||||
description: Joi.string().max(255).optional().allow(""),
|
||||
spareparts: Joi.array().items(Joi.number().integer()).optional(), // Array of sparepart_id
|
||||
error_code: Joi.array()
|
||||
.items(
|
||||
Joi.object({
|
||||
@@ -56,6 +57,7 @@ const updateBrandSchema = Joi.object({
|
||||
brand_model: Joi.string().max(100).optional().allow(""),
|
||||
is_active: Joi.boolean().required(),
|
||||
description: Joi.string().max(255).optional().allow(""),
|
||||
spareparts: Joi.array().items(Joi.number().integer()).optional(), // Array of sparepart_id
|
||||
error_code: Joi.array()
|
||||
.items(
|
||||
Joi.object({
|
||||
|
||||
Reference in New Issue
Block a user