repair: sparepart per error code
This commit is contained in:
@@ -1,9 +1,128 @@
|
||||
const pool = require("../config");
|
||||
|
||||
// Get spareparts by brand_id
|
||||
const getSparepartsByBrandIdDb = async (brandId) => {
|
||||
// Get spareparts by error_code_id
|
||||
const getSparepartsByErrorCodeIdDb = async (errorCodeId) => {
|
||||
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,
|
||||
bs.created_at,
|
||||
bs.created_by
|
||||
FROM brand_spareparts bs
|
||||
JOIN m_sparepart s ON bs.sparepart_id = s.sparepart_id
|
||||
WHERE bs.error_code_id = $1
|
||||
AND s.deleted_at IS NULL
|
||||
ORDER BY s.sparepart_name
|
||||
`;
|
||||
const result = await pool.query(queryText, [errorCodeId]);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Get error codes by sparepart_id
|
||||
const getErrorCodesBySparepartIdDb = async (sparepartId) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
ec.error_code_id,
|
||||
ec.error_code,
|
||||
ec.error_code_name,
|
||||
ec.error_code_description,
|
||||
ec.error_code_color,
|
||||
ec.path_icon,
|
||||
ec.is_active,
|
||||
ec.created_at,
|
||||
ec.updated_at
|
||||
FROM brand_spareparts bs
|
||||
JOIN m_error_codes ec ON bs.error_code_id = ec.error_code_id
|
||||
WHERE bs.sparepart_id = $1
|
||||
AND ec.deleted_at IS NULL
|
||||
ORDER BY ec.error_code
|
||||
`;
|
||||
const result = await pool.query(queryText, [sparepartId]);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Insert error_code-spareparts relationship
|
||||
const insertErrorCodeSparepartDb = async (errorCodeId, sparepartId, createdBy) => {
|
||||
const queryText = `
|
||||
INSERT INTO brand_spareparts (error_code_id, sparepart_id, created_by, created_at)
|
||||
VALUES ($1, $2, $3, CURRENT_TIMESTAMP)
|
||||
`;
|
||||
const result = await pool.query(queryText, [errorCodeId, sparepartId, createdBy]);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Insert multiple error_code-spareparts relationships
|
||||
const insertMultipleErrorCodeSparepartsDb = async (errorCodeId, 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 (error_code_id, sparepart_id, created_by, created_at)
|
||||
VALUES ${values}
|
||||
`;
|
||||
const params = [errorCodeId, ...sparepartIds, createdBy];
|
||||
const result = await pool.query(queryText, params);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Delete specific error_code-sparepart relationship
|
||||
const deleteErrorCodeSparepartDb = async (errorCodeId, sparepartId) => {
|
||||
const queryText = `
|
||||
DELETE FROM brand_spareparts
|
||||
WHERE error_code_id = $1 AND sparepart_id = $2
|
||||
`;
|
||||
const result = await pool.query(queryText, [errorCodeId, sparepartId]);
|
||||
return result.rowsAffected > 0;
|
||||
};
|
||||
|
||||
// Delete all spareparts for an error_code
|
||||
const deleteAllErrorCodeSparepartsDb = async (errorCodeId) => {
|
||||
const queryText = `
|
||||
DELETE FROM brand_spareparts
|
||||
WHERE error_code_id = $1
|
||||
`;
|
||||
const result = await pool.query(queryText, [errorCodeId]);
|
||||
return result.rowsAffected > 0;
|
||||
};
|
||||
|
||||
// Update error_code-spareparts (replace all)
|
||||
const updateErrorCodeSparepartsDb = async (errorCodeId, sparepartIds, updatedBy) => {
|
||||
// Delete existing relationships
|
||||
await deleteAllErrorCodeSparepartsDb(errorCodeId);
|
||||
|
||||
// Insert new relationships
|
||||
if (sparepartIds && sparepartIds.length > 0) {
|
||||
return await insertMultipleErrorCodeSparepartsDb(errorCodeId, sparepartIds, updatedBy);
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
// Check if error_code-sparepart relationship exists
|
||||
const checkErrorCodeSparepartExistsDb = async (errorCodeId, sparepartId) => {
|
||||
const queryText = `
|
||||
SELECT 1
|
||||
FROM brand_spareparts
|
||||
WHERE error_code_id = $1 AND sparepart_id = $2
|
||||
`;
|
||||
const result = await pool.query(queryText, [errorCodeId, sparepartId]);
|
||||
return result.recordset.length > 0;
|
||||
};
|
||||
|
||||
// Legacy functions for backward compatibility (deprecated)
|
||||
// Get spareparts by brand_id (now using error_code_id mapping via error codes)
|
||||
const getSparepartsByBrandIdDb = async (brandId) => {
|
||||
const queryText = `
|
||||
SELECT DISTINCT
|
||||
s.sparepart_id,
|
||||
s.sparepart_name,
|
||||
s.sparepart_code,
|
||||
@@ -19,18 +138,20 @@ const getSparepartsByBrandIdDb = async (brandId) => {
|
||||
s.updated_at
|
||||
FROM brand_spareparts bs
|
||||
JOIN m_sparepart s ON bs.sparepart_id = s.sparepart_id
|
||||
WHERE bs.brand_id = $1
|
||||
JOIN m_error_codes ec ON bs.error_code_id = ec.error_code_id
|
||||
WHERE ec.brand_id = $1
|
||||
AND s.deleted_at IS NULL
|
||||
AND ec.deleted_at IS NULL
|
||||
ORDER BY s.sparepart_name
|
||||
`;
|
||||
const result = await pool.query(queryText, [brandId]);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Get brands by sparepart_id
|
||||
// Get brands by sparepart_id (now using error_code_id mapping)
|
||||
const getBrandsBySparepartIdDb = async (sparepartId) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
SELECT DISTINCT
|
||||
b.brand_id,
|
||||
b.brand_name,
|
||||
b.brand_type,
|
||||
@@ -41,8 +162,12 @@ const getBrandsBySparepartIdDb = async (sparepartId) => {
|
||||
b.created_at,
|
||||
b.updated_at
|
||||
FROM brand_spareparts bs
|
||||
JOIN m_brands b ON bs.brand_id = b.brand_id
|
||||
JOIN m_sparepart s ON bs.sparepart_id = s.sparepart_id
|
||||
JOIN m_error_codes ec ON bs.error_code_id = ec.error_code_id
|
||||
JOIN m_brands b ON ec.brand_id = b.brand_id
|
||||
WHERE bs.sparepart_id = $1
|
||||
AND s.deleted_at IS NULL
|
||||
AND ec.deleted_at IS NULL
|
||||
AND b.deleted_at IS NULL
|
||||
ORDER BY b.brand_name
|
||||
`;
|
||||
@@ -50,81 +175,16 @@ const getBrandsBySparepartIdDb = async (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;
|
||||
};
|
||||
// Deprecated functions removed - table structure changed to use error_code_id instead of brand_id
|
||||
|
||||
module.exports = {
|
||||
getSparepartsByBrandIdDb,
|
||||
getBrandsBySparepartIdDb,
|
||||
insertBrandSparepartDb,
|
||||
insertMultipleBrandSparepartsDb,
|
||||
deleteBrandSparepartDb,
|
||||
deleteAllBrandSparepartsDb,
|
||||
updateBrandSparepartsDb,
|
||||
checkBrandSparepartExistsDb,
|
||||
// New functions using error_code_id
|
||||
getSparepartsByErrorCodeIdDb,
|
||||
getErrorCodesBySparepartIdDb,
|
||||
insertErrorCodeSparepartDb,
|
||||
insertMultipleErrorCodeSparepartsDb,
|
||||
deleteErrorCodeSparepartDb,
|
||||
deleteAllErrorCodeSparepartsDb,
|
||||
updateErrorCodeSparepartsDb,
|
||||
checkErrorCodeSparepartExistsDb,
|
||||
};
|
||||
Reference in New Issue
Block a user