repair: resend chat all user & by user

This commit is contained in:
2026-01-04 19:20:43 +07:00
parent bedc948a74
commit 3eb403c557
7 changed files with 132 additions and 19 deletions

View File

@@ -71,6 +71,24 @@ class NotificationErrorController {
res.status(response.statusCode).json(response);
}
static async resend(req, res) {
try {
const { id } = req.params;
const results = 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);
}
}
}
module.exports = NotificationErrorController;

View File

@@ -67,14 +67,14 @@ class NotificationErrorUserController {
res.status(response.statusCode).json(response);
}
static async resend(req, res) {
static async resendByUser(req, res) {
try {
const { id, contact_phone } = req.params;
const results = await NotificationErrorUserService.resendNotification(id, contact_phone);
const results = await NotificationErrorUserService.resendNotificationByUser(id, contact_phone)
const response = await setResponse(
results.data,
results,
results.message,
);

View File

@@ -165,11 +165,13 @@ const getUsersNotificationErrorDb = async (id) => {
a.notification_error_user_id,
a.contact_phone,
a.contact_name,
a.is_send
c.is_active
FROM notification_error_user a
LEFT JOIN notification_error b ON a.notification_error_id = b.notification_error_id
LEFT JOIN contact c ON a.contact_phone = c.contact_phone
WHERE a.notification_error_id = $1
AND a.deleted_at IS NULL

View File

@@ -1,21 +1,40 @@
const express = require('express');
const NotificationErrorController = require('../controllers/notification_error.controller');
const verifyToken = require('../middleware/verifyToken');
const verifyAccess = require('../middleware/verifyAccess');
const express = require("express");
const NotificationErrorController = require("../controllers/notification_error.controller");
const verifyToken = require("../middleware/verifyToken");
const verifyAccess = require("../middleware/verifyAccess");
const router = express.Router();
router
.route('/')
.get(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.getAll)
router
.route('/')
.post(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.create)
.route("/")
.get(
verifyToken.verifyAccessToken,
verifyAccess(),
NotificationErrorController.getAll
);
router
.route('/:id')
.route("/")
.post(
verifyToken.verifyAccessToken,
verifyAccess(),
NotificationErrorController.create
);
router
.route("/:id")
.get(verifyToken.verifyAccessToken, NotificationErrorController.getById)
.put(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorController.update)
.put(
verifyToken.verifyAccessToken,
verifyAccess(),
NotificationErrorController.update
);
router.post(
"/resend/:id",
verifyToken.verifyAccessToken,
verifyAccess(),
NotificationErrorController.resend
);
module.exports = router;

View File

@@ -32,7 +32,7 @@ router.post(
"/resend/:id/:contact_phone",
verifyToken.verifyAccessToken,
verifyAccess(),
NotificationErrorUserController.resend
NotificationErrorUserController.resendByUser
);
module.exports = router;

View File

@@ -19,6 +19,12 @@ const { getSparepartsByErrorCodeIdDb } = require("../db/brand_sparepart.db");
const { getFileUploadByPathDb } = require("../db/file_uploads.db");
const {
generateTokenRedirect,
shortUrltiny,
sendNotifikasi,
} = require("../db/notification_wa.db");
const { ErrorHandler } = require("../helpers/error");
class NotificationService {
@@ -29,7 +35,10 @@ class NotificationService {
if (results && Array.isArray(results.data)) {
results.data = await Promise.all(
results.data.map(async (notification) => {
const usersNotification = (await getUsersNotificationErrorDb(notification.notification_error_id)) || [];
const usersNotification =
(await getUsersNotificationErrorDb(
notification.notification_error_id
)) || [];
return {
...notification,
users: usersNotification,
@@ -143,6 +152,71 @@ class NotificationService {
throw new ErrorHandler(error.statusCode, error.message);
}
}
static async resendNotification(id) {
try {
const dataExist = await getUsersNotificationErrorDb(id);
const activeUsers =
dataExist?.filter((user) => user.is_active === true) || [];
if (activeUsers.length < 1) {
throw new ErrorHandler(
404,
"No active contacts found for this notification"
);
}
if (!dataExist || dataExist.length < 1) {
throw new ErrorHandler(404, "Data Notification Error not found");
}
const results = await Promise.all(
dataExist &&
activeUsers.map(async (user) => {
const tokenRedirect = await generateTokenRedirect(
user.contact_phone,
user.contact_name,
user.notification_error_id
);
const encodedToken = encodeURIComponent(tokenRedirect);
const shortUrl = await shortUrltiny(encodedToken);
const bodyBase =
`Hai Operator\n` +
`Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`;
const bodyWithUrl = `${bodyBase}\n🔗 ${shortUrl}`;
const resultSend = await sendNotifikasi(
user.contact_phone,
bodyWithUrl
);
const isSuccess = resultSend?.error ? false : true;
await updateNotificationErrorDb(user.notification_error_id, {
is_send: isSuccess,
is_delivered: isSuccess,
});
return {
contact_name: user.contact_name,
contact_phone: user.contact_phone,
is_send: isSuccess,
is_delivered: isSuccess,
};
})
);
return {
notification_error_id: id,
user: results,
message: "Berhasil mengirim ulang notifikasi",
};
} catch (error) {
throw new ErrorHandler(error.statusCode || 500, error.message);
}
}
}
module.exports = NotificationService;

View File

@@ -91,7 +91,7 @@ class NotificationErrorUserService {
}
}
static async resendNotification(id, contact_phone) {
static async resendNotificationByUser(id, contact_phone) {
try {
const dataExist = await getNotificationErrorUserByIdDb(id);