wisdom #2
@@ -1,90 +1,69 @@
|
|||||||
const pool = require("../config");
|
const pool = require("../config");
|
||||||
|
|
||||||
// Get all devices with brand info
|
// Get all devices
|
||||||
const getAllDevicesDb = async () => {
|
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 ")}`
|
||||||
|
: "";
|
||||||
|
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT d.*, b.brand_name
|
SELECT d.*, b.brand_name
|
||||||
FROM m_device d
|
FROM m_device d
|
||||||
LEFT JOIN m_brands b ON d.brand_id = b.brand_id
|
LEFT JOIN m_brands b ON d.brand_id = b.brand_id
|
||||||
WHERE d.deleted_at IS NULL
|
WHERE d.deleted_at IS NULL ${whereClause}
|
||||||
ORDER BY d.device_id ASC
|
ORDER BY d.device_id ASC
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText);
|
const result = await pool.query(queryText, queryParams);
|
||||||
return result.recordset;
|
return result.recordset;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Search devices by keyword
|
|
||||||
const searchDevicesDb = async (keyword) => {
|
|
||||||
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
|
|
||||||
AND (
|
|
||||||
d.device_name LIKE '%' + $1 + '%'
|
|
||||||
OR d.device_code LIKE '%' + $1 + '%'
|
|
||||||
OR d.device_location LIKE '%' + $1 + '%'
|
|
||||||
OR d.ip_address LIKE '%' + $1 + '%'
|
|
||||||
OR d.device_description LIKE '%' + $1 + '%'
|
|
||||||
OR b.brand_name LIKE '%' + $1 + '%'
|
|
||||||
)
|
|
||||||
ORDER BY d.device_id ASC
|
|
||||||
`;
|
|
||||||
const result = await pool.query(queryText, [keyword]);
|
|
||||||
return result.recordset;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Get device by ID
|
|
||||||
const getDeviceByIdDb = async (id) => {
|
const getDeviceByIdDb = async (id) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT d.*, b.brand_name
|
SELECT d.*, b.brand_name
|
||||||
FROM m_device d
|
FROM m_device d
|
||||||
LEFT JOIN m_brands b ON d.brand_id = b.brand_id
|
LEFT JOIN m_brands b ON d.brand_id = b.brand_id
|
||||||
WHERE d.device_id = $1
|
WHERE d.device_id = $1 AND d.deleted_at IS NULL
|
||||||
AND d.deleted_at IS NULL
|
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText, [id]);
|
const result = await pool.query(queryText, [id]);
|
||||||
return result.recordset[0];
|
return result.recordset[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get device by device_code
|
|
||||||
const getDeviceByCodeDb = async (code) => {
|
const getDeviceByCodeDb = async (code) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT d.*, b.brand_name
|
SELECT d.*, b.brand_name
|
||||||
FROM m_device d
|
FROM m_device d
|
||||||
LEFT JOIN m_brands b ON d.brand_id = b.brand_id
|
LEFT JOIN m_brands b ON d.brand_id = b.brand_id
|
||||||
WHERE d.device_code = $1
|
WHERE d.device_code = $1 AND d.deleted_at IS NULL
|
||||||
AND d.deleted_at IS NULL
|
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText, [code]);
|
const result = await pool.query(queryText, [code]);
|
||||||
return result.recordset[0];
|
return result.recordset[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create device
|
|
||||||
const createDeviceDb = async (data) => {
|
const createDeviceDb = async (data) => {
|
||||||
const { query: queryText, values } = pool.buildDynamicInsert("m_device", data);
|
const { query: queryText, values } = pool.buildDynamicInsert("m_device", data);
|
||||||
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;
|
||||||
if (!insertedId) return null;
|
return insertedId ? await getDeviceByIdDb(insertedId) : null;
|
||||||
|
|
||||||
return getDeviceByIdDb(insertedId);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Update device
|
|
||||||
const updateDeviceDb = async (id, data) => {
|
const updateDeviceDb = async (id, data) => {
|
||||||
const { query: queryText, values } = pool.buildDynamicUpdate("m_device", data, { device_id: id });
|
const { query: queryText, values } = pool.buildDynamicUpdate("m_device", data, { device_id: id });
|
||||||
await pool.query(queryText, values);
|
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
|
||||||
return getDeviceByIdDb(id);
|
return getDeviceByIdDb(id);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Soft delete device
|
|
||||||
const softDeleteDeviceDb = async (id, deletedBy) => {
|
const softDeleteDeviceDb = async (id, deletedBy) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
UPDATE m_device
|
UPDATE m_device
|
||||||
SET deleted_at = GETDATE(),
|
SET deleted_at = GETDATE(), deleted_by = $1
|
||||||
deleted_by = $1
|
WHERE device_id = $2 AND deleted_at IS NULL
|
||||||
WHERE device_id = $2
|
|
||||||
AND deleted_at IS NULL
|
|
||||||
`;
|
`;
|
||||||
await pool.query(queryText, [deletedBy, id]);
|
await pool.query(queryText, [deletedBy, id]);
|
||||||
return true;
|
return true;
|
||||||
@@ -97,5 +76,4 @@ module.exports = {
|
|||||||
createDeviceDb,
|
createDeviceDb,
|
||||||
updateDeviceDb,
|
updateDeviceDb,
|
||||||
softDeleteDeviceDb,
|
softDeleteDeviceDb,
|
||||||
searchDevicesDb,
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user