187 lines
5.6 KiB
JavaScript
187 lines
5.6 KiB
JavaScript
const pool = require("../config");
|
|
|
|
// 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_sparepart 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_sparepart 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_sparepart (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_sparepart (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_sparepart
|
|
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_sparepart
|
|
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;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
const getSparepartsByBrandIdDb = async (brandId) => {
|
|
const queryText = `
|
|
SELECT DISTINCT
|
|
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_sparepart bs
|
|
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
|
|
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 (now using error_code_id mapping)
|
|
const getBrandsBySparepartIdDb = async (sparepartId) => {
|
|
const queryText = `
|
|
SELECT DISTINCT
|
|
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_sparepart bs
|
|
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
|
|
`;
|
|
const result = await pool.query(queryText, [sparepartId]);
|
|
return result.recordset;
|
|
};
|
|
|
|
// Deprecated functions removed - table structure changed to use error_code_id instead of brand_id
|
|
|
|
module.exports = {
|
|
// New functions using error_code_id
|
|
getSparepartsByErrorCodeIdDb,
|
|
getErrorCodesBySparepartIdDb,
|
|
insertErrorCodeSparepartDb,
|
|
insertMultipleErrorCodeSparepartsDb,
|
|
deleteErrorCodeSparepartDb,
|
|
deleteAllErrorCodeSparepartsDb,
|
|
updateErrorCodeSparepartsDb,
|
|
checkErrorCodeSparepartExistsDb,
|
|
}; |