repair: tags db
This commit is contained in:
31
db/tag.db.js
31
db/tag.db.js
@@ -5,8 +5,16 @@ const getAllTagsDb = async (searchParams = {}) => {
|
|||||||
const { whereConditions, queryParams } = buildFilterQuery([
|
const { whereConditions, queryParams } = buildFilterQuery([
|
||||||
{ column: "mt.tag_name", param: searchParams.name, type: "string" },
|
{ column: "mt.tag_name", param: searchParams.name, type: "string" },
|
||||||
{ column: "mt.tag_code", param: searchParams.code, 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" },
|
column: "md.device_name",
|
||||||
|
param: searchParams.deviceName,
|
||||||
|
type: "string",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
column: "pss.sub_section_name",
|
||||||
|
param: searchParams.subSectionName,
|
||||||
|
type: "string",
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const whereClause = whereConditions.length
|
const whereClause = whereConditions.length
|
||||||
@@ -18,11 +26,11 @@ const getAllTagsDb = async (searchParams = {}) => {
|
|||||||
mt.tag_id, mt.device_id, mt.tag_code, mt.tag_name, mt.tag_number,
|
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.data_type, mt.unit, mt.is_active, mt.sub_section_id,
|
||||||
mt.created_at, mt.updated_at, mt.deleted_at,
|
mt.created_at, mt.updated_at, mt.deleted_at,
|
||||||
md.device_name,
|
md.device_name,md.ip_address,
|
||||||
pss.sub_section_code, pss.sub_section_name
|
pss.sub_section_code, pss.sub_section_name
|
||||||
FROM m_tags mt
|
FROM m_tags mt
|
||||||
LEFT JOIN m_device md ON mt.device_id = md.device_id
|
INNER 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
|
INNER JOIN plant_sub_section pss ON mt.sub_section_id = pss.sub_section_id
|
||||||
WHERE mt.deleted_at IS NULL ${whereClause}
|
WHERE mt.deleted_at IS NULL ${whereClause}
|
||||||
ORDER BY mt.tag_id ASC
|
ORDER BY mt.tag_id ASC
|
||||||
`;
|
`;
|
||||||
@@ -57,7 +65,7 @@ const createTagDb = async (data) => {
|
|||||||
($1,$2,$3,$4,$5,$6,$7,$8,$9);
|
($1,$2,$3,$4,$5,$6,$7,$8,$9);
|
||||||
SELECT SCOPE_IDENTITY() as tag_id;
|
SELECT SCOPE_IDENTITY() as tag_id;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const values = [
|
const values = [
|
||||||
data.device_id,
|
data.device_id,
|
||||||
data.tag_code,
|
data.tag_code,
|
||||||
@@ -67,7 +75,7 @@ const createTagDb = async (data) => {
|
|||||||
data.unit,
|
data.unit,
|
||||||
data.is_active || 1, //default aktif
|
data.is_active || 1, //default aktif
|
||||||
data.sub_section_id,
|
data.sub_section_id,
|
||||||
data.created_by,
|
data.created_by,
|
||||||
];
|
];
|
||||||
|
|
||||||
const result = await query(queryText, values);
|
const result = await query(queryText, values);
|
||||||
@@ -75,7 +83,10 @@ const createTagDb = async (data) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const updateTagDb = async (tagId, data) => {
|
const updateTagDb = async (tagId, data) => {
|
||||||
const { query: queryText, values } = buildDynamicUpdate("m_tags", data, { tag_id: tagId, updated_at: 'GETDATE()' });
|
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");
|
const finalQuery = queryText.replace("WHERE", "WHERE deleted_at IS NULL AND");
|
||||||
await query(finalQuery, values);
|
await query(finalQuery, values);
|
||||||
return true;
|
return true;
|
||||||
@@ -91,7 +102,7 @@ const deleteTagDb = async (tagId, deletedBy) => {
|
|||||||
WHERE tag_id = $2
|
WHERE tag_id = $2
|
||||||
AND deleted_at IS NULL
|
AND deleted_at IS NULL
|
||||||
`;
|
`;
|
||||||
|
|
||||||
await query(queryText, [deletedBy, tagId]);
|
await query(queryText, [deletedBy, tagId]);
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
@@ -102,4 +113,4 @@ module.exports = {
|
|||||||
createTagDb,
|
createTagDb,
|
||||||
updateTagDb,
|
updateTagDb,
|
||||||
deleteTagDb,
|
deleteTagDb,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user