fix: device.db.js
This commit is contained in:
@@ -2,29 +2,36 @@ const pool = require("../config");
|
|||||||
|
|
||||||
// Get all devices
|
// Get all devices
|
||||||
const getAllDevicesDb = async (searchParams = {}) => {
|
const getAllDevicesDb = async (searchParams = {}) => {
|
||||||
queryParams = [];
|
let queryParams = [];
|
||||||
|
|
||||||
|
// Pagination
|
||||||
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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Search
|
||||||
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
|
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
|
||||||
["a.device_code", "a.device_location", "a.ip_address", "b.brand_name"],
|
[
|
||||||
|
"a.device_name",
|
||||||
|
"a.device_code",
|
||||||
|
"a.device_location",
|
||||||
|
"a.ip_address",
|
||||||
|
"b.brand_name",
|
||||||
|
],
|
||||||
searchParams.criteria,
|
searchParams.criteria,
|
||||||
queryParams
|
queryParams
|
||||||
);
|
);
|
||||||
|
|
||||||
queryParams = whereParamOr ? whereParamOr : queryParams;
|
queryParams = whereParamOr ? whereParamOr : queryParams;
|
||||||
|
|
||||||
|
// Filter
|
||||||
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
|
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
|
||||||
[
|
[
|
||||||
{ 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: "a.device_location",
|
|
||||||
param: searchParams.location,
|
|
||||||
type: "string",
|
|
||||||
},
|
|
||||||
{ column: "b.brand_name", param: searchParams.brand, type: "string" },
|
{ column: "b.brand_name", param: searchParams.brand, type: "string" },
|
||||||
|
{ column: "a.device_status", param: searchParams.status, type: "string" },
|
||||||
],
|
],
|
||||||
queryParams
|
queryParams
|
||||||
);
|
);
|
||||||
@@ -32,46 +39,48 @@ const getAllDevicesDb = async (searchParams = {}) => {
|
|||||||
queryParams = whereParamAnd ? whereParamAnd : queryParams;
|
queryParams = whereParamAnd ? whereParamAnd : queryParams;
|
||||||
|
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT COUNT(*) OVER() AS total_data,
|
SELECT
|
||||||
a.device_id,
|
COUNT(*) OVER() AS total_data,
|
||||||
a.device_status,
|
a.device_id,
|
||||||
a.device_location,
|
a.device_code,
|
||||||
a.device_description,
|
a.device_name,
|
||||||
a.ip_address,
|
a.device_status,
|
||||||
a.created_by,
|
a.device_location,
|
||||||
a.updated_by,
|
a.device_description,
|
||||||
a.deleted_by,
|
a.ip_address,
|
||||||
a.created_at,
|
a.created_by,
|
||||||
a.updated_at,
|
a.updated_by,
|
||||||
a.deleted_at,
|
a.deleted_by,
|
||||||
CONCAT(a.device_code, ' - ', a.device_name) AS device_code,
|
a.created_at,
|
||||||
b.brand_name
|
a.updated_at,
|
||||||
|
a.deleted_at,
|
||||||
|
b.brand_id,
|
||||||
|
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 ')}` : ''}
|
||||||
whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""
|
${whereOrConditions ? whereOrConditions : ''}
|
||||||
}
|
|
||||||
${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.
|
|
||||||
const total =
|
const total =
|
||||||
result?.recordset.length > 0
|
result?.recordset.length > 0
|
||||||
? parseInt(result.recordset[0].total_data, 10)
|
? parseInt(result.recordset[0].total_data, 10)
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
// Mengembalikan data dan total.
|
|
||||||
return { data: result.recordset, total };
|
return { data: result.recordset, total };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const getDeviceByIdDb = async (id) => {
|
const getDeviceByIdDb = async (id) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT
|
SELECT
|
||||||
a.device_id,
|
a.device_id,
|
||||||
|
a.device_name,
|
||||||
a.device_status,
|
a.device_status,
|
||||||
a.device_location,
|
a.device_location,
|
||||||
a.device_description,
|
a.device_description,
|
||||||
@@ -82,7 +91,6 @@ const getDeviceByIdDb = async (id) => {
|
|||||||
a.created_at,
|
a.created_at,
|
||||||
a.updated_at,
|
a.updated_at,
|
||||||
a.deleted_at,
|
a.deleted_at,
|
||||||
CONCAT(a.device_code, ' - ', a.device_name) AS device_code,
|
|
||||||
b.brand_name
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user