add: resend chat wa notif error user
This commit is contained in:
@@ -66,6 +66,24 @@ class NotificationErrorUserController {
|
|||||||
|
|
||||||
res.status(response.statusCode).json(response);
|
res.status(response.statusCode).json(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async resend(req, res) {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
const results = await NotificationErrorUserService.resendNotification(id);
|
||||||
|
|
||||||
|
const response = await setResponse(
|
||||||
|
results.data,
|
||||||
|
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 = NotificationErrorUserController;
|
module.exports = NotificationErrorUserController;
|
||||||
|
|||||||
@@ -55,8 +55,11 @@ const getAllNotificationErrorUserDb = async (searchParams = {}) => {
|
|||||||
const getNotificationErrorUserByIdDb = async (id) => {
|
const getNotificationErrorUserByIdDb = async (id) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT
|
SELECT
|
||||||
a.*
|
a.*,
|
||||||
|
b. is_active as contact_is_active
|
||||||
|
|
||||||
FROM notification_error_user a
|
FROM notification_error_user a
|
||||||
|
LEFT JOIN contact b ON a.contact_phone = b.contact_phone
|
||||||
WHERE a.notification_error_user_id = $1 AND a.deleted_at IS NULL
|
WHERE a.notification_error_user_id = $1 AND a.deleted_at IS NULL
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText, [id]);
|
const result = await pool.query(queryText, [id]);
|
||||||
|
|||||||
@@ -1,17 +1,38 @@
|
|||||||
const express = require('express');
|
const express = require("express");
|
||||||
const NotificationErrorUserController = require('../controllers/notification_error_user.controller');
|
const NotificationErrorUserController = require("../controllers/notification_error_user.controller");
|
||||||
const verifyToken = require("../middleware/verifyToken")
|
const verifyToken = require("../middleware/verifyToken");
|
||||||
const verifyAccess = require("../middleware/verifyAccess")
|
const verifyAccess = require("../middleware/verifyAccess");
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.route("/")
|
router
|
||||||
.get(verifyToken.verifyAccessToken, NotificationErrorUserController.getAll)
|
.route("/")
|
||||||
.post(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorUserController.create);
|
.get(verifyToken.verifyAccessToken, NotificationErrorUserController.getAll)
|
||||||
|
.post(
|
||||||
|
verifyToken.verifyAccessToken,
|
||||||
|
verifyAccess(),
|
||||||
|
NotificationErrorUserController.create
|
||||||
|
);
|
||||||
|
|
||||||
router.route("/:id")
|
router
|
||||||
.get(verifyToken.verifyAccessToken, NotificationErrorUserController.getById)
|
.route("/:id")
|
||||||
.put(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorUserController.update)
|
.get(verifyToken.verifyAccessToken, NotificationErrorUserController.getById)
|
||||||
.delete(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorUserController.delete);
|
.put(
|
||||||
|
verifyToken.verifyAccessToken,
|
||||||
|
verifyAccess(),
|
||||||
|
NotificationErrorUserController.update
|
||||||
|
)
|
||||||
|
.delete(
|
||||||
|
verifyToken.verifyAccessToken,
|
||||||
|
verifyAccess(),
|
||||||
|
NotificationErrorUserController.delete
|
||||||
|
);
|
||||||
|
|
||||||
|
router.post(
|
||||||
|
"/resend/:id",
|
||||||
|
verifyToken.verifyAccessToken,
|
||||||
|
verifyAccess(),
|
||||||
|
NotificationErrorUserController.resend
|
||||||
|
);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
@@ -3,9 +3,16 @@ const {
|
|||||||
getNotificationErrorUserByIdDb,
|
getNotificationErrorUserByIdDb,
|
||||||
createNotificationErrorUserDb,
|
createNotificationErrorUserDb,
|
||||||
updateNotificationErrorUserDb,
|
updateNotificationErrorUserDb,
|
||||||
deleteNotificationErrorUserDb
|
deleteNotificationErrorUserDb,
|
||||||
} = require('../db/notification_error_user.db');
|
} = require("../db/notification_error_user.db");
|
||||||
const { ErrorHandler } = require('../helpers/error');
|
|
||||||
|
const {
|
||||||
|
generateTokenRedirect,
|
||||||
|
shortUrltiny,
|
||||||
|
sendNotifikasi,
|
||||||
|
} = require("../db/notification_wa.db");
|
||||||
|
|
||||||
|
const { ErrorHandler } = require("../helpers/error");
|
||||||
|
|
||||||
class NotificationErrorUserService {
|
class NotificationErrorUserService {
|
||||||
// Get all Contact
|
// Get all Contact
|
||||||
@@ -13,10 +20,9 @@ class NotificationErrorUserService {
|
|||||||
try {
|
try {
|
||||||
const results = await getAllNotificationErrorUserDb(param);
|
const results = await getAllNotificationErrorUserDb(param);
|
||||||
|
|
||||||
results.data.map(element => {
|
results.data.map((element) => {});
|
||||||
});
|
|
||||||
|
|
||||||
return results
|
return results;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new ErrorHandler(error.statusCode, error.message);
|
throw new ErrorHandler(error.statusCode, error.message);
|
||||||
}
|
}
|
||||||
@@ -27,7 +33,8 @@ class NotificationErrorUserService {
|
|||||||
try {
|
try {
|
||||||
const result = await getNotificationErrorUserByIdDb(id);
|
const result = await getNotificationErrorUserByIdDb(id);
|
||||||
|
|
||||||
if (result.length < 1) throw new ErrorHandler(404, 'NotificationErrorUser not found');
|
if (result.length < 1)
|
||||||
|
throw new ErrorHandler(404, "NotificationErrorUser not found");
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -38,7 +45,7 @@ class NotificationErrorUserService {
|
|||||||
// Create NotificationErrorUser
|
// Create NotificationErrorUser
|
||||||
static async createNotificationErrorUser(data) {
|
static async createNotificationErrorUser(data) {
|
||||||
try {
|
try {
|
||||||
if (!data || typeof data !== 'object') data = {};
|
if (!data || typeof data !== "object") data = {};
|
||||||
|
|
||||||
const result = await createNotificationErrorUserDb(data);
|
const result = await createNotificationErrorUserDb(data);
|
||||||
|
|
||||||
@@ -51,12 +58,12 @@ class NotificationErrorUserService {
|
|||||||
// Update NotificationErrorUser
|
// Update NotificationErrorUser
|
||||||
static async updateNotificationErrorUser(id, data) {
|
static async updateNotificationErrorUser(id, data) {
|
||||||
try {
|
try {
|
||||||
if (!data || typeof data !== 'object') data = {};
|
if (!data || typeof data !== "object") data = {};
|
||||||
|
|
||||||
const dataExist = await getNotificationErrorUserByIdDb(id);
|
const dataExist = await getNotificationErrorUserByIdDb(id);
|
||||||
|
|
||||||
if (dataExist.length < 1) {
|
if (dataExist.length < 1) {
|
||||||
throw new ErrorHandler(404, 'NotificationErrorUser not found');
|
throw new ErrorHandler(404, "NotificationErrorUser not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await updateNotificationErrorUserDb(id, data);
|
const result = await updateNotificationErrorUserDb(id, data);
|
||||||
@@ -73,7 +80,7 @@ class NotificationErrorUserService {
|
|||||||
const dataExist = await getNotificationErrorUserByIdDb(id);
|
const dataExist = await getNotificationErrorUserByIdDb(id);
|
||||||
|
|
||||||
if (dataExist.length < 1) {
|
if (dataExist.length < 1) {
|
||||||
throw new ErrorHandler(404, 'NotificationErrorUser not found');
|
throw new ErrorHandler(404, "NotificationErrorUser not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await deleteNotificationErrorUserDb(id, userId);
|
const result = await deleteNotificationErrorUserDb(id, userId);
|
||||||
@@ -83,6 +90,72 @@ class NotificationErrorUserService {
|
|||||||
throw new ErrorHandler(error.statusCode, error.message);
|
throw new ErrorHandler(error.statusCode, error.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async resendNotification(id) {
|
||||||
|
try {
|
||||||
|
const dataExist = await getNotificationErrorUserByIdDb(id);
|
||||||
|
if (!dataExist || dataExist.length < 1) {
|
||||||
|
throw new ErrorHandler(
|
||||||
|
404,
|
||||||
|
"Data Notification Error User tidak ditemukan"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const data = dataExist[0];
|
||||||
|
|
||||||
|
if (data.contact_is_active === 1) {
|
||||||
|
throw new ErrorHandler(
|
||||||
|
400,
|
||||||
|
`Kontak dengan nomor ${
|
||||||
|
data.contact_phone || "terkait"
|
||||||
|
} tidak aktif.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const tokenRedirect = await generateTokenRedirect(
|
||||||
|
data.contact_phone,
|
||||||
|
data.contact_name,
|
||||||
|
data.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(data.contact_phone, bodyWithUrl);
|
||||||
|
|
||||||
|
const isSuccess = resultSend?.error ? false : true;
|
||||||
|
|
||||||
|
const updateData = {
|
||||||
|
is_send: isSuccess,
|
||||||
|
message_error_issue: bodyWithUrl,
|
||||||
|
};
|
||||||
|
|
||||||
|
await updateNotificationErrorUserDb(id, updateData);
|
||||||
|
|
||||||
|
if (!isSuccess) {
|
||||||
|
throw new ErrorHandler(
|
||||||
|
500,
|
||||||
|
`WhatsApp API Gagal mengirim pesan: ${
|
||||||
|
resultSend?.message || "Unknown Error"
|
||||||
|
}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
notification_error_user_id: id,
|
||||||
|
is_send: isSuccess,
|
||||||
|
short_url: shortUrl,
|
||||||
|
message: "Berhasil mengirim ulang notifikasi",
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new ErrorHandler(error.statusCode || 500, error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = NotificationErrorUserService;
|
module.exports = NotificationErrorUserService;
|
||||||
|
|||||||
@@ -75,6 +75,8 @@ class NotifikasiWaService {
|
|||||||
|
|
||||||
let bodyWithUrl = `${param.bodyMessage}\n🔗 ${shortUrl}`;
|
let bodyWithUrl = `${param.bodyMessage}\n🔗 ${shortUrl}`;
|
||||||
|
|
||||||
|
console.log(bodyWithUrl)
|
||||||
|
|
||||||
param.bodyMessage = bodyWithUrl
|
param.bodyMessage = bodyWithUrl
|
||||||
|
|
||||||
const resultNotificationErrorUser = await createNotificationErrorUserDb({
|
const resultNotificationErrorUser = await createNotificationErrorUserDb({
|
||||||
|
|||||||
Reference in New Issue
Block a user