wisdom #2

Merged
yogiedigital merged 126 commits from wisdom into main 2025-10-20 03:26:33 +00:00
Showing only changes of commit 45968832f0 - Show all commits

View File

@@ -1,90 +1,69 @@
const pool = require("../config");
// Get all devices with brand info
const getAllDevicesDb = async () => {
// 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 ")}`
: "";
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
WHERE d.deleted_at IS NULL ${whereClause}
ORDER BY d.device_id ASC
`;
const result = await pool.query(queryText);
const result = await pool.query(queryText, queryParams);
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 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
WHERE d.device_id = $1 AND d.deleted_at IS NULL
`;
const result = await pool.query(queryText, [id]);
return result.recordset[0];
};
// Get device by device_code
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
WHERE d.device_code = $1 AND d.deleted_at IS NULL
`;
const result = await pool.query(queryText, [code]);
return result.recordset[0];
};
// Create device
const createDeviceDb = async (data) => {
const { query: queryText, values } = pool.buildDynamicInsert("m_device", data);
const result = await pool.query(queryText, values);
const insertedId = result.recordset[0]?.inserted_id;
if (!insertedId) return null;
return getDeviceByIdDb(insertedId);
return insertedId ? await getDeviceByIdDb(insertedId) : null;
};
// Update device
const updateDeviceDb = async (id, data) => {
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);
};
// Soft delete device
const softDeleteDeviceDb = async (id, deletedBy) => {
const queryText = `
UPDATE m_device
SET deleted_at = GETDATE(),
deleted_by = $1
WHERE device_id = $2
AND deleted_at IS NULL
SET deleted_at = GETDATE(), deleted_by = $1
WHERE device_id = $2 AND deleted_at IS NULL
`;
await pool.query(queryText, [deletedBy, id]);
return true;
@@ -97,5 +76,4 @@ module.exports = {
createDeviceDb,
updateDeviceDb,
softDeleteDeviceDb,
searchDevicesDb,
};