add : tag db
This commit is contained in:
@@ -37,7 +37,7 @@ module.exports = {
|
||||
|
||||
createRoles: async (req, res, next) => {
|
||||
try {
|
||||
let { role_name, role_description, role_level,} = req.body;
|
||||
let { role_name, role_description, role_level, updated_by} = req.body;
|
||||
|
||||
if (!role_name || role_level === undefined || role_level === null) {
|
||||
return res.status(400).json(
|
||||
@@ -63,6 +63,7 @@ module.exports = {
|
||||
const dataToCreate = {
|
||||
role_name,
|
||||
role_description,
|
||||
updated_by,
|
||||
role_level: level,
|
||||
};
|
||||
|
||||
|
||||
105
db/tag.db.js
Normal file
105
db/tag.db.js
Normal file
@@ -0,0 +1,105 @@
|
||||
const { query, buildFilterQuery, buildDynamicUpdate } = require("../config");
|
||||
|
||||
// Get all tags
|
||||
const getAllTagsDb = async (searchParams = {}) => {
|
||||
const { whereConditions, queryParams } = buildFilterQuery([
|
||||
{ column: "mt.tag_name", param: searchParams.name, type: "string" },
|
||||
{ column: "mt.tag_code", param: searchParams.code, type: "string" },
|
||||
{ column: "md.device_name", param: searchParams.deviceName, type: "string" },
|
||||
{ column: "pss.sub_section_name", param: searchParams.subSectionName, type: "string" },
|
||||
]);
|
||||
|
||||
const whereClause = whereConditions.length
|
||||
? `AND ${whereConditions.join(" AND ")}`
|
||||
: "";
|
||||
|
||||
const queryText = `
|
||||
SELECT
|
||||
mt.tag_id, mt.device_id, mt.tag_code, mt.tag_name, mt.tag_number,
|
||||
mt.data_type, mt.unit, mt.is_active, mt.sub_section_id,
|
||||
mt.created_at, mt.updated_at, mt.deleted_at,
|
||||
md.device_name,
|
||||
pss.sub_section_code, pss.sub_section_name
|
||||
FROM m_tags mt
|
||||
LEFT JOIN m_device md ON mt.device_id = md.device_id
|
||||
LEFT JOIN plant_sub_section pss ON mt.sub_section_id = pss.sub_section_id
|
||||
WHERE mt.deleted_at IS NULL ${whereClause}
|
||||
ORDER BY mt.tag_id ASC
|
||||
`;
|
||||
const result = await query(queryText, queryParams);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
// Get tag by ID
|
||||
const getTagByIdDb = async (id) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
mt.tag_id, mt.device_id, mt.tag_code, mt.tag_name, mt.tag_number,
|
||||
mt.data_type, mt.unit, mt.is_active, mt.sub_section_id,
|
||||
mt.created_at, mt.updated_at, mt.deleted_at,
|
||||
md.device_name,
|
||||
pss.sub_section_code, pss.sub_section_name
|
||||
FROM m_tags mt
|
||||
LEFT JOIN m_device md ON mt.device_id = md.device_id
|
||||
LEFT JOIN plant_sub_section pss ON mt.sub_section_id = pss.sub_section_id
|
||||
WHERE mt.tag_id = $1 AND mt.deleted_at IS NULL
|
||||
`;
|
||||
const result = await query(queryText, [id]);
|
||||
return result.recordset[0];
|
||||
};
|
||||
|
||||
// Create tag
|
||||
const createTagDb = async (data) => {
|
||||
const queryText = `
|
||||
INSERT INTO m_tags
|
||||
(device_id, tag_code, tag_name, tag_number, data_type, unit, is_active, sub_section_id, created_by)
|
||||
VALUES
|
||||
($1,$2,$3,$4,$5,$6,$7,$8,$9);
|
||||
SELECT SCOPE_IDENTITY() as tag_id;
|
||||
`;
|
||||
|
||||
const values = [
|
||||
data.device_id,
|
||||
data.tag_code,
|
||||
data.tag_name,
|
||||
data.tag_number || null,
|
||||
data.data_type || 'A2',
|
||||
data.unit || null,
|
||||
data.is_active || 1, //default aktif
|
||||
data.sub_section_id || null,
|
||||
data.created_by || null,
|
||||
];
|
||||
|
||||
const result = await query(queryText, values);
|
||||
return result.recordset[0]?.tag_id || null;
|
||||
};
|
||||
|
||||
const updateTagDb = async (tagId, data) => {
|
||||
const { query: queryText, values } = buildDynamicUpdate("m_tags", data, { tag_id: tagId, updated_at: 'GETDATE()' });
|
||||
const finalQuery = queryText.replace("WHERE", "WHERE deleted_at IS NULL AND");
|
||||
await query(finalQuery, values);
|
||||
return true;
|
||||
};
|
||||
|
||||
const deleteTagDb = async (tagId, deletedBy) => {
|
||||
const queryText = `
|
||||
UPDATE m_tags
|
||||
SET
|
||||
deleted_at = GETDATE(),
|
||||
deleted_by = $1,
|
||||
is_active = 0
|
||||
WHERE tag_id = $2
|
||||
AND deleted_at IS NULL
|
||||
`;
|
||||
|
||||
await query(queryText, [deletedBy, tagId]);
|
||||
return true;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getAllTagsDb,
|
||||
getTagByIdDb,
|
||||
createTagDb,
|
||||
updateTagDb,
|
||||
deleteTagDb,
|
||||
};
|
||||
Reference in New Issue
Block a user