diff --git a/controllers/history_value.controller.js b/controllers/history_value.controller.js new file mode 100644 index 0000000..2a6751a --- /dev/null +++ b/controllers/history_value.controller.js @@ -0,0 +1,16 @@ +const HistoryValue = require('../services/history_value.service'); +const { setResponsePaging } = require('../helpers/utils'); + +class HistoryValueController { + // Get all units + static async getAllHistoryAlarm(req, res) { + const queryParams = req.query; + + const results = await HistoryValue.getAllHistoryAlarm(queryParams); + const response = await setResponsePaging(queryParams, results, 'Unit found'); + + res.status(response.statusCode).json(response); + } +} + +module.exports = HistoryValueController; diff --git a/db/history_value.db.js b/db/history_value.db.js new file mode 100644 index 0000000..8f12d7b --- /dev/null +++ b/db/history_value.db.js @@ -0,0 +1,66 @@ +const pool = require("../config"); + +// Get all tags +const getHistoryAlarmDb = async (searchParams = {}) => { + let queryParams = []; + + if (searchParams.limit) { + const page = Number(searchParams.page ?? 1) - 1; + queryParams = [Number(searchParams.limit ?? 10), page]; + } + + const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike( + [ + "b.tag_name", + "a.tagnum" + ], + searchParams.criteria, + queryParams + ); + + if (whereParamOr) queryParams = whereParamOr; + + const { whereConditions, whereParamAnd } = pool.buildFilterQuery( + [ + { column: "b.tag_name", param: searchParams.name, type: "string" }, + { column: "b.tag_number", param: searchParams.name, type: "number" }, + ], + queryParams + ); + + if (whereParamAnd) queryParams = whereParamAnd; + + const queryText = ` + SELECT + COUNT(*) OVER() AS total_data, + a.*, + b.tag_name, + b.tag_number, + b.lim_low_crash, + b.lim_low, + b.lim_high, + b.lim_high_crash, + c.status_color + FROM alarm_history a + LEFT JOIN m_tags b ON a.tagnum = b.tag_number AND b.deleted_at IS NULL + LEFT JOIN m_status c ON a.status = c.status_number AND c.deleted_at IS NULL + WHERE a.datetime IS NOT NULL + ${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""} + ${whereOrConditions ? ` ${whereOrConditions}` : ""} + ORDER BY a.datetime DESC + ${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ''} + `; + + const result = await pool.query(queryText, queryParams); + + const total = + result?.recordset?.length > 0 + ? parseInt(result.recordset[0].total_data, 10) + : 0; + + return { data: result.recordset, total }; +}; + +module.exports = { + getHistoryAlarmDb, +}; diff --git a/routes/history_value.route.js b/routes/history_value.route.js new file mode 100644 index 0000000..277acce --- /dev/null +++ b/routes/history_value.route.js @@ -0,0 +1,17 @@ +const express = require('express'); +const HistoryValueController = require('../controllers/history_value.controller'); +const verifyToken = require('../middleware/verifyToken'); +const verifyAccess = require('../middleware/verifyAccess'); + +const router = express.Router(); + +router.route('/alarm') + .get(verifyToken.verifyAccessToken, HistoryValueController.getAllHistoryAlarm) +router.route('/event') + .get(verifyToken.verifyAccessToken, HistoryValueController.getAllHistoryAlarm) +router.route('/value-table') + .get(verifyToken.verifyAccessToken, HistoryValueController.getAllHistoryAlarm) +router.route('/value-trending') + .get(verifyToken.verifyAccessToken, HistoryValueController.getAllHistoryAlarm) + +module.exports = router; diff --git a/routes/index.js b/routes/index.js index 8bbd80e..5437936 100644 --- a/routes/index.js +++ b/routes/index.js @@ -12,6 +12,7 @@ const schedule = require("./schedule.route"); const status = require("./status.route"); const unit = require("./unit.route") const UserSchedule = require("./user_schedule.route") +const historyValue = require("./history_value.route") router.use("/auth", auth); router.use("/user", users); @@ -26,6 +27,7 @@ router.use("/schedule", schedule); router.use("/status", status); router.use("/unit", unit); router.use("/user-schedule", UserSchedule) +router.use("/history", historyValue) module.exports = router; diff --git a/services/history_value.service.js b/services/history_value.service.js new file mode 100644 index 0000000..0fe1216 --- /dev/null +++ b/services/history_value.service.js @@ -0,0 +1,20 @@ +const { getHistoryAlarmDb } = require('../db/history_value.db'); +const { ErrorHandler } = require('../helpers/error'); + +class HistoryValue { + // Get all devices + static async getAllHistoryAlarm(param) { + try { + const results = await getHistoryAlarmDb(param); + + results.data.map(element => { + }); + + return results + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } + } +} + +module.exports = HistoryValue;