example for template master device
This commit is contained in:
103
db/device.db.js
103
db/device.db.js
@@ -2,67 +2,97 @@ const pool = require("../config");
|
||||
|
||||
// Get all devices
|
||||
const getAllDevicesDb = async (searchParams = {}) => {
|
||||
const { whereConditions, queryParams } = pool.buildFilterQuery([
|
||||
{ column: "d.device_name", param: searchParams.name, type: "string" },
|
||||
{ column: "d.device_code", param: searchParams.code, type: "string" },
|
||||
{ column: "d.device_location", param: searchParams.location, type: "string" },
|
||||
{ column: "b.brand_name", param: searchParams.brand, type: "string" },
|
||||
]);
|
||||
|
||||
const whereClause = whereConditions.length
|
||||
? `AND ${whereConditions.join(" AND ")}`
|
||||
: "";
|
||||
queryParams = []
|
||||
if (searchParams.limit) {
|
||||
const page = Number(searchParams.page ?? 1) - 1
|
||||
queryParams = [Number(searchParams.limit ?? 10), page];
|
||||
}
|
||||
|
||||
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike([
|
||||
"a.device_name",
|
||||
"a.device_code",
|
||||
"a.device_location",
|
||||
"a.ip_address",
|
||||
"b.brand_name"
|
||||
], searchParams.criteria, queryParams);
|
||||
|
||||
queryParams = whereParamOr ? whereParamOr : queryParams
|
||||
|
||||
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_location", param: searchParams.location, type: "string" },
|
||||
{ column: "b.brand_name", param: searchParams.brand, type: "string" },
|
||||
], queryParams);
|
||||
|
||||
queryParams = whereParamAnd ? whereParamAnd : queryParams
|
||||
|
||||
const queryText = `
|
||||
SELECT d.*, b.brand_name
|
||||
FROM m_device d
|
||||
LEFT JOIN m_brands b ON d.brand_id = b.brand_id
|
||||
WHERE d.deleted_at IS NULL ${whereClause}
|
||||
ORDER BY d.device_id ASC
|
||||
SELECT COUNT(*) OVER() AS total_data, a.*, b.brand_name
|
||||
FROM m_device a
|
||||
LEFT JOIN m_brands b ON a.brand_id = b.brand_id
|
||||
WHERE a.deleted_at IS NULL
|
||||
${whereConditions.length > 0 ? ` AND ${whereConditions.join(' AND ')}` : ''}
|
||||
${whereOrConditions ? whereOrConditions : ''}
|
||||
ORDER BY a.device_id ASC
|
||||
${searchParams.limit ? `OFFSET $2 ROWS FETCH NEXT $1 ROWS ONLY` : ''};
|
||||
`;
|
||||
const result = await pool.query(queryText, queryParams);
|
||||
return result.recordset;
|
||||
|
||||
// Menghitung total data.
|
||||
const total = result?.recordset.length > 0 ? parseInt(result.recordset[0].total_data, 10) : 0;
|
||||
|
||||
// Mengembalikan data dan total.
|
||||
return { data: result.recordset, total };
|
||||
};
|
||||
|
||||
const getDeviceByIdDb = async (id) => {
|
||||
const queryText = `
|
||||
SELECT d.*, b.brand_name
|
||||
FROM m_device d
|
||||
LEFT JOIN m_brands b ON d.brand_id = b.brand_id
|
||||
WHERE d.device_id = $1 AND d.deleted_at IS NULL
|
||||
SELECT a.*, b.brand_name
|
||||
FROM m_device a
|
||||
LEFT JOIN m_brands b ON a.brand_id = b.brand_id
|
||||
WHERE a.device_id = $1 AND a.deleted_at IS NULL
|
||||
`;
|
||||
const result = await pool.query(queryText, [id]);
|
||||
return result.recordset[0];
|
||||
};
|
||||
|
||||
const getDeviceByCodeDb = async (code) => {
|
||||
const queryText = `
|
||||
SELECT d.*, b.brand_name
|
||||
FROM m_device d
|
||||
LEFT JOIN m_brands b ON d.brand_id = b.brand_id
|
||||
WHERE d.device_code = $1 AND d.deleted_at IS NULL
|
||||
`;
|
||||
const result = await pool.query(queryText, [code]);
|
||||
return result.recordset[0];
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
const createDeviceDb = async (data) => {
|
||||
const { query: queryText, values } = pool.buildDynamicInsert("m_device", data);
|
||||
|
||||
const newCode = await pool.generateKode("DVC", "m_device", "device_code")
|
||||
|
||||
const store = {
|
||||
...data,
|
||||
device_code: newCode,
|
||||
}
|
||||
|
||||
const { query: queryText, values } = pool.buildDynamicInsert("m_device", store);
|
||||
const result = await pool.query(queryText, values);
|
||||
const insertedId = result.recordset[0]?.inserted_id;
|
||||
return insertedId ? await getDeviceByIdDb(insertedId) : null;
|
||||
};
|
||||
|
||||
const updateDeviceDb = async (id, data) => {
|
||||
const { query: queryText, values } = pool.buildDynamicUpdate("m_device", data, { device_id: id });
|
||||
|
||||
const store = {
|
||||
...data
|
||||
}
|
||||
|
||||
// Kondisi WHERE
|
||||
const whereData = {
|
||||
device_id: id
|
||||
};
|
||||
|
||||
const { query: queryText, values } = pool.buildDynamicUpdate("m_device", store, whereData);
|
||||
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
|
||||
return getDeviceByIdDb(id);
|
||||
};
|
||||
|
||||
const softDeleteDeviceDb = async (id, deletedBy) => {
|
||||
const deleteDeviceDb = async (id, deletedBy) => {
|
||||
const queryText = `
|
||||
UPDATE m_device
|
||||
SET deleted_at = GETDATE(), deleted_by = $1
|
||||
SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1
|
||||
WHERE device_id = $2 AND deleted_at IS NULL
|
||||
`;
|
||||
await pool.query(queryText, [deletedBy, id]);
|
||||
@@ -72,8 +102,7 @@ const softDeleteDeviceDb = async (id, deletedBy) => {
|
||||
module.exports = {
|
||||
getAllDevicesDb,
|
||||
getDeviceByIdDb,
|
||||
getDeviceByCodeDb,
|
||||
createDeviceDb,
|
||||
updateDeviceDb,
|
||||
softDeleteDeviceDb,
|
||||
deleteDeviceDb,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user