repair device db: device code - device name

This commit is contained in:
Muhammad Afif
2025-10-15 15:50:56 +07:00
parent 7aaf35f7e9
commit 32d25cef6a

View File

@@ -2,46 +2,66 @@ const pool = require("../config");
// Get all devices // Get all devices
const getAllDevicesDb = async (searchParams = {}) => { const getAllDevicesDb = async (searchParams = {}) => {
queryParams = [];
queryParams = []
if (searchParams.limit) { if (searchParams.limit) {
const page = Number(searchParams.page ?? 1) - 1 const page = Number(searchParams.page ?? 1) - 1;
queryParams = [Number(searchParams.limit ?? 10), page]; queryParams = [Number(searchParams.limit ?? 10), page];
} }
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike([ const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
"a.device_name", ["a.device_code", "a.device_location", "a.ip_address", "b.brand_name"],
"a.device_code", searchParams.criteria,
"a.device_location", queryParams
"a.ip_address", );
"b.brand_name"
], searchParams.criteria, queryParams);
queryParams = whereParamOr ? whereParamOr : queryParams queryParams = whereParamOr ? whereParamOr : queryParams;
const { whereConditions, whereParamAnd } = pool.buildFilterQuery([ const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
{ column: "a.device_name", param: searchParams.name, type: "string" }, [
{ column: "a.device_code", param: searchParams.code, type: "string" }, { column: "a.device_code", param: searchParams.code, type: "string" },
{ column: "a.device_location", param: searchParams.location, type: "string" }, {
{ column: "b.brand_name", param: searchParams.brand, type: "string" }, column: "a.device_location",
], queryParams); param: searchParams.location,
type: "string",
},
{ column: "b.brand_name", param: searchParams.brand, type: "string" },
],
queryParams
);
queryParams = whereParamAnd ? whereParamAnd : queryParams queryParams = whereParamAnd ? whereParamAnd : queryParams;
const queryText = ` const queryText = `
SELECT COUNT(*) OVER() AS total_data, a.*, b.brand_name SELECT COUNT(*) OVER() AS total_data,
a.device_status,
a.device_location,
a.device_description,
a.ip_address,
a.created_by,
a.updated_by,
a.deleted_by,
a.created_at,
a.updated_at,
a.deleted_at,
CONCAT(a.device_code, ' - ', a.device_name) AS device_code,
b.brand_name
FROM m_device a FROM m_device a
LEFT JOIN m_brands b ON a.brand_id = b.brand_id LEFT JOIN m_brands b ON a.brand_id = b.brand_id
WHERE a.deleted_at IS NULL WHERE a.deleted_at IS NULL
${whereConditions.length > 0 ? ` AND ${whereConditions.join(' AND ')}` : ''} ${
${whereOrConditions ? whereOrConditions : ''} whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""
}
${whereOrConditions ? whereOrConditions : ""}
ORDER BY a.device_id ASC ORDER BY a.device_id ASC
${searchParams.limit ? `OFFSET $2 ROWS FETCH NEXT $1 ROWS ONLY` : ''}; ${searchParams.limit ? `OFFSET $2 ROWS FETCH NEXT $1 ROWS ONLY` : ""};
`; `;
const result = await pool.query(queryText, queryParams); const result = await pool.query(queryText, queryParams);
// Menghitung total data. // Menghitung total data.
const total = result?.recordset.length > 0 ? parseInt(result.recordset[0].total_data, 10) : 0; const total =
result?.recordset.length > 0
? parseInt(result.recordset[0].total_data, 10)
: 0;
// Mengembalikan data dan total. // Mengembalikan data dan total.
return { data: result.recordset, total }; return { data: result.recordset, total };
@@ -49,7 +69,19 @@ const getAllDevicesDb = async (searchParams = {}) => {
const getDeviceByIdDb = async (id) => { const getDeviceByIdDb = async (id) => {
const queryText = ` const queryText = `
SELECT a.*, b.brand_name SELECT
a.device_status,
a.device_location,
a.device_description,
a.ip_address,
a.created_by,
a.updated_by,
a.deleted_by,
a.created_at,
a.updated_at,
a.deleted_at,
CONCAT(a.device_code, ' - ', a.device_name) AS device_code,
b.brand_name
FROM m_device a FROM m_device a
LEFT JOIN m_brands b ON a.brand_id = b.brand_id LEFT JOIN m_brands b ON a.brand_id = b.brand_id
WHERE a.device_id = $1 AND a.deleted_at IS NULL WHERE a.device_id = $1 AND a.deleted_at IS NULL
@@ -59,32 +91,37 @@ const getDeviceByIdDb = async (id) => {
}; };
const createDeviceDb = async (data) => { const createDeviceDb = async (data) => {
const newCode = await pool.generateKode("DVC", "m_device", "device_code");
const newCode = await pool.generateKode("DVC", "m_device", "device_code")
const store = { const store = {
...data, ...data,
device_code: newCode, device_code: newCode,
} };
const { query: queryText, values } = pool.buildDynamicInsert("m_device", store); const { query: queryText, values } = pool.buildDynamicInsert(
"m_device",
store
);
const result = await pool.query(queryText, values); const result = await pool.query(queryText, values);
const insertedId = result.recordset[0]?.inserted_id; const insertedId = result.recordset[0]?.inserted_id;
return insertedId ? await getDeviceByIdDb(insertedId) : null; return insertedId ? await getDeviceByIdDb(insertedId) : null;
}; };
const updateDeviceDb = async (id, data) => { const updateDeviceDb = async (id, data) => {
const store = { const store = {
...data ...data,
} };
// Kondisi WHERE // Kondisi WHERE
const whereData = { const whereData = {
device_id: id device_id: id,
}; };
const { query: queryText, values } = pool.buildDynamicUpdate("m_device", store, whereData); const { query: queryText, values } = pool.buildDynamicUpdate(
"m_device",
store,
whereData
);
await pool.query(`${queryText} AND deleted_at IS NULL`, values); await pool.query(`${queryText} AND deleted_at IS NULL`, values);
return getDeviceByIdDb(id); return getDeviceByIdDb(id);
}; };