wisdom #26

Merged
bragaz_rexita merged 2 commits from wisdom into main 2026-01-06 11:41:36 +00:00
5 changed files with 164 additions and 96 deletions

View File

@@ -77,19 +77,13 @@ class NotificationErrorController {
static async resend(req, res) { static async resend(req, res) {
try { try {
const { id } = req.params; const { id } = req.params;
const result =
const results = await NotificationErrorService.resendNotification(id); await NotificationErrorService.resendNotification(
id,
const response = await setResponse(results, results.message);
res.status(response.statusCode).json(response);
} catch (error) {
const response = setResponse(
null,
error.message,
error.statusCode || 500
); );
res.status(response.statusCode).json(response); res.status(200).json(result);
} catch (error) {
return error;
} }
} }
} }

View File

@@ -1,14 +1,28 @@
const NotificationErrorUserService = require('../services/notification_error_user.service'); const NotificationErrorUserService = require("../services/notification_error_user.service");
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils'); const {
const { insertNotificationErrorUserSchema, updateNotificationErrorUserSchema } = require('../validate/notification_error_user.schema'); setResponse,
setResponsePaging,
checkValidate,
} = require("../helpers/utils");
const {
insertNotificationErrorUserSchema,
updateNotificationErrorUserSchema,
} = require("../validate/notification_error_user.schema");
class NotificationErrorUserController { class NotificationErrorUserController {
// Get all NotificationErrorUser // Get all NotificationErrorUser
static async getAll(req, res) { static async getAll(req, res) {
const queryParams = req.query; const queryParams = req.query;
const results = await NotificationErrorUserService.getAllNotificationErrorUser(queryParams); const results =
const response = await setResponsePaging(queryParams, results, 'Notification Error User found') await NotificationErrorUserService.getAllNotificationErrorUser(
queryParams
);
const response = await setResponsePaging(
queryParams,
results,
"Notification Error User found"
);
res.status(response.statusCode).json(response); res.status(response.statusCode).json(response);
} }
@@ -17,24 +31,35 @@ class NotificationErrorUserController {
static async getById(req, res) { static async getById(req, res) {
const { id } = req.params; const { id } = req.params;
const results = await NotificationErrorUserService.getNotificationErrorUserById(id); const results =
const response = await setResponse(results, 'Notification Error User found') await NotificationErrorUserService.getNotificationErrorUserById(id);
const response = await setResponse(
results,
"Notification Error User found"
);
res.status(response.statusCode).json(response); res.status(response.statusCode).json(response);
} }
// Create NotificationErrorUser // Create NotificationErrorUser
static async create(req, res) { static async create(req, res) {
const { error, value } = await checkValidate(insertNotificationErrorUserSchema, req) const { error, value } = await checkValidate(
insertNotificationErrorUserSchema,
req
);
if (error) { if (error) {
return res.status(400).json(setResponse(error, 'Validation failed', 400)); return res.status(400).json(setResponse(error, "Validation failed", 400));
} }
value.userId = req.user.user_id value.userId = req.user.user_id;
const results = await NotificationErrorUserService.createNotificationErrorUser(value); const results =
const response = await setResponse(results, 'Notification Error User created successfully') await NotificationErrorUserService.createNotificationErrorUser(value);
const response = await setResponse(
results,
"Notification Error User created successfully"
);
return res.status(response.statusCode).json(response); return res.status(response.statusCode).json(response);
} }
@@ -43,16 +68,23 @@ class NotificationErrorUserController {
static async update(req, res) { static async update(req, res) {
const { id } = req.params; const { id } = req.params;
const { error, value } = checkValidate(updateNotificationErrorUserSchema, req) const { error, value } = checkValidate(
updateNotificationErrorUserSchema,
req
);
if (error) { if (error) {
return res.status(400).json(setResponse(error, 'Validation failed', 400)); return res.status(400).json(setResponse(error, "Validation failed", 400));
} }
value.userId = req.user.user_id value.userId = req.user.user_id;
const results = await NotificationErrorUserService.updateNotificationErrorUser(id, value); const results =
const response = await setResponse(results, 'Notification Error User updated successfully') await NotificationErrorUserService.updateNotificationErrorUser(id, value);
const response = await setResponse(
results,
"Notification Error User updated successfully"
);
res.status(response.statusCode).json(response); res.status(response.statusCode).json(response);
} }
@@ -61,8 +93,15 @@ class NotificationErrorUserController {
static async delete(req, res) { static async delete(req, res) {
const { id } = req.params; const { id } = req.params;
const results = await NotificationErrorUserService.deleteNotificationErrorUser(id, req.user.user_id); const results =
const response = await setResponse(results, 'Notification Error User deleted successfully') await NotificationErrorUserService.deleteNotificationErrorUser(
id,
req.user.user_id
);
const response = await setResponse(
results,
"Notification Error User deleted successfully"
);
res.status(response.statusCode).json(response); res.status(response.statusCode).json(response);
} }
@@ -70,18 +109,14 @@ class NotificationErrorUserController {
static async resendByUser(req, res) { static async resendByUser(req, res) {
try { try {
const { id, contact_phone } = req.params; const { id, contact_phone } = req.params;
const result =
const results = await NotificationErrorUserService.resendNotificationByUser(id, contact_phone) await NotificationErrorUserService.resendNotificationByUser(
id,
const response = await setResponse( contact_phone
results,
results.message,
); );
res.status(200).json(result);
res.status(response.statusCode).json(response);
} catch (error) { } catch (error) {
const response = setResponse(null, error.message, error.statusCode || 500); return error;
res.status(response.statusCode).json(response);
} }
} }
} }

View File

@@ -43,6 +43,22 @@ const getNotificationByIdDb = async (id) => {
return result.recordset[0]; return result.recordset[0];
}; };
const getDeviceNotificationByIdDb = async (chanel_id) => {
const queryText = `
SELECT
device_code,
device_name,
device_location,
listen_channel
FROM m_device
WHERE listen_channel = $1
AND deleted_at IS NULL
`;
const result = await pool.query(queryText, [chanel_id]);
return result.recordset[0];
};
const getAllNotificationDb = async (searchParams = {}) => { const getAllNotificationDb = async (searchParams = {}) => {
let queryParams = []; let queryParams = [];
@@ -185,6 +201,7 @@ const getUsersNotificationErrorDb = async (id) => {
module.exports = { module.exports = {
getNotificationByIdDb, getNotificationByIdDb,
getDeviceNotificationByIdDb,
getAllNotificationDb, getAllNotificationDb,
InsertNotificationErrorDb, InsertNotificationErrorDb,
updateNotificationErrorDb, updateNotificationErrorDb,

View File

@@ -150,9 +150,10 @@ class NotificationService {
const notification = Array.isArray(dataExist) ? dataExist[0] : dataExist; const notification = Array.isArray(dataExist) ? dataExist[0] : dataExist;
if (notification.is_read === true) { if (notification.is_read === true) {
throw new ErrorHandler(400, "Notification has already been read"); return {success: true, message:"Notification has already been read"};
} }
if (!notification.is_read) { if (!notification.is_read) {
const updateStatus = await updateNotificationErrorDb( const updateStatus = await updateNotificationErrorDb(
notification_error_id, notification_error_id,

View File

@@ -1,19 +1,26 @@
const { getAllContactDb } = require('../db/contact.db'); const { getAllContactDb } = require("../db/contact.db");
const { InsertNotificationErrorDb } = require('../db/notification_error.db'); const { InsertNotificationErrorDb } = require("../db/notification_error.db");
const { createNotificationErrorUserDb, updateNotificationErrorUserDb } = require('../db/notification_error_user.db'); const {
const { generateTokenRedirect, shortUrltiny, sendNotifikasi } = require('../db/notification_wa.db'); createNotificationErrorUserDb,
updateNotificationErrorUserDb,
} = require("../db/notification_error_user.db");
const {
generateTokenRedirect,
shortUrltiny,
sendNotifikasi,
} = require("../db/notification_wa.db");
const { getErrorCodeByIdDb } = require("../db/brand_code.db");
const { getDeviceNotificationByIdDb } = require("../db/notification_error.db");
class NotifikasiWaService { class NotifikasiWaService {
async onNotification(topic, message) { async onNotification(topic, message) {
try { try {
const paramDb = { const paramDb = {
limit: 100, limit: 100,
page: 1, page: 1,
criteria: '', criteria: "",
active: 1 active: 1,
} };
// const chanel = { // const chanel = {
// "time": "2025-12-11 11:10:58", // "time": "2025-12-11 11:10:58",
@@ -22,85 +29,99 @@ class NotifikasiWaService {
// "c_6501": 0 // "c_6501": 0
// } // }
if (topic === 'morek') { if (topic === "morek") {
const dataMqtt = JSON.parse(message); const dataMqtt = JSON.parse(message);
const resultChanel = []; const resultChanel = [];
Object.entries(dataMqtt).forEach(([key, value]) => { Object.entries(dataMqtt).forEach(([key, value]) => {
if (key.startsWith('c_')) { if (key.startsWith("c_")) {
resultChanel.push({ resultChanel.push({
chanel_id: Number(key.slice(2)), chanel_id: Number(key.slice(2)),
value value,
}); });
} }
}); });
const results = await getAllContactDb(paramDb); const results = await getAllContactDb(paramDb);
const bodyMessage = `Hai Operator\n` +
`Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`;
const dataUsers = results.data; const dataUsers = results.data;
for (const chanel of resultChanel) { for (const chanel of resultChanel) {
const data = { const errorCode = await getErrorCodeByIdDb(chanel.value);
"error_code_id": chanel.value, const deviceNotification = await getDeviceNotificationByIdDb(
"error_chanel": chanel.chanel_id, chanel.chanel_id
"message_error_issue": bodyMessage, );
"is_send": false,
"is_delivered": false,
"is_read": false,
"is_active": true
}
const resultNotificationError = await InsertNotificationErrorDb(data) const data = {
error_code_id: chanel.value,
error_chanel: chanel.chanel_id,
is_send: false,
is_delivered: false,
is_read: false,
is_active: true,
};
const resultNotificationError = await InsertNotificationErrorDb(data);
for (const dataUser of dataUsers) { for (const dataUser of dataUsers) {
if (dataUser.is_active) { if (dataUser.is_active) {
const tokenRedirect = await generateTokenRedirect(
dataUser.userPhone,
dataUser.userName,
dataUser.idData
);
const encodedToken = encodeURIComponent(tokenRedirect);
const shortUrl = await shortUrltiny(encodedToken);
const bodyMessage =
`Hai ${dataUser.contact_name || "-"}\n` +
`Terjadi peringatan dengan kode error ${
errorCode?.error_code || "-"
} - ${errorCode?.error_code_name || "-"} ` +
`pada device ${
deviceNotification?.device_name || "-"
}, silahkan cek detail pada link berikut:\n` +
`${shortUrl}`;
const param = { const param = {
idData: resultNotificationError.notification_error_id, idData: resultNotificationError.notification_error_id,
userPhone: dataUser.contact_phone, userPhone: dataUser.contact_phone,
userName: dataUser.contact_name, userName: dataUser.contact_name,
bodyMessage: bodyMessage, bodyMessage: bodyMessage,
} };
const tokenRedirect = await generateTokenRedirect(param.userPhone, param.userName, param.idData) const resultNotificationErrorUser =
await createNotificationErrorUserDb({
const encodedToken = encodeURIComponent(tokenRedirect); notification_error_id:
resultNotificationError.notification_error_id,
const shortUrl = await shortUrltiny(encodedToken)
let bodyWithUrl = `${param.bodyMessage}\n🔗 ${shortUrl}`;
param.bodyMessage = bodyWithUrl
const resultNotificationErrorUser = await createNotificationErrorUserDb({
notification_error_id: resultNotificationError.notification_error_id,
contact_phone: param.userPhone, contact_phone: param.userPhone,
contact_name: param.userName, contact_name: param.userName,
is_send: false, is_send: false,
}); });
const resultSend = await sendNotifikasi(param.userPhone, param.bodyMessage); const resultSend = await sendNotifikasi(
param.userPhone,
param.bodyMessage
);
await updateNotificationErrorUserDb(resultNotificationErrorUser[0].notification_error_user_id, { await updateNotificationErrorUserDb(
resultNotificationErrorUser[0].notification_error_user_id,
{
is_send: resultSend?.error ? false : true, is_send: resultSend?.error ? false : true,
}); }
);
} }
} }
} }
} }
} catch (error) { } catch (error) {
// throw new ErrorHandler(error.statusCode, error.message); // throw new ErrorHandler(error.statusCode, error.message);
return error return error;
} }
} }
} }
module.exports = new NotifikasiWaService(); module.exports = new NotifikasiWaService();