diff --git a/package.json b/package.json index ee2d46c..3bc2bdb 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "nodemailer": "^6.8.0", "pg": "^8.8.0", "pino": "^6.11.3", + "pm2": "^6.0.14", "stripe": "^8.138.0", "svg-captcha": "^1.4.0", "swagger-ui-express": "^4.6.0", diff --git a/routes/index.js b/routes/index.js index fabe125..c6c6732 100644 --- a/routes/index.js +++ b/routes/index.js @@ -19,7 +19,8 @@ const notificationErrorSparepart = require("./notification_error_sparepart.route const sparepart = require("./sparepart.route") const notificationErrorLog = require("./notification_error_log.route") const notificationErrorUser = require("./notification_error_user.route") -const errorCode = require("./error_code.route") +const errorCode = require("./error_code.route"); +const notifikasiWA = require("./notifikasi-wa.route"); router.use("/auth", auth); router.use("/user", users); @@ -42,5 +43,6 @@ router.use("/sparepart", sparepart) router.use("/notification-log", notificationErrorLog) router.use("/notification-user", notificationErrorUser) router.use("/error-code", errorCode) +router.use("/notifikasi-wa", notifikasiWA) module.exports = router; diff --git a/routes/notifikasi-wa.route.js b/routes/notifikasi-wa.route.js new file mode 100644 index 0000000..8fd7b3a --- /dev/null +++ b/routes/notifikasi-wa.route.js @@ -0,0 +1,14 @@ +const express = require('express'); +const router = express.Router(); +const NotifikasiWaService = require('../services/notifikasi-wa.service'); + +router.post('/restart-wa', async (req, res) => { + try { + const result = await NotifikasiWaService.restartWhatsapp(); + return res.status(200).json(result); + } catch (error) { + return res.status(500).json(error); + } +}); + +module.exports = router; \ No newline at end of file diff --git a/services/notifikasi-wa.service.js b/services/notifikasi-wa.service.js index c6461c2..3869d38 100644 --- a/services/notifikasi-wa.service.js +++ b/services/notifikasi-wa.service.js @@ -1,5 +1,8 @@ const { getAllContactDb } = require("../db/contact.db"); -const { InsertNotificationErrorDb, updateNotificationErrorDb } = require("../db/notification_error.db"); +const { + InsertNotificationErrorDb, + updateNotificationErrorDb, +} = require("../db/notification_error.db"); const { createNotificationErrorUserDb, updateNotificationErrorUserDb, @@ -12,6 +15,12 @@ const { const { getErrorCodeByBrandAndCodeDb } = require("../db/brand_code.db"); const { getDeviceNotificationByIdDb } = require("../db/notification_error.db"); +const { exec } = require("child_process"); +const util = require("util"); +const execPromise = util.promisify(exec); +const fs = require("fs"); +const path = require("path"); + class NotifikasiWaService { async onNotification(topic, message) { try { @@ -52,7 +61,10 @@ class NotifikasiWaService { Number(chanel.chanel_id) ); - const errorCode = await getErrorCodeByBrandAndCodeDb(deviceNotification?.brand_id ?? 0, chanel.value); + const errorCode = await getErrorCodeByBrandAndCodeDb( + deviceNotification?.brand_id ?? 0, + chanel.value + ); const data = { error_code_id: chanel.value, @@ -65,7 +77,7 @@ class NotifikasiWaService { const resultNotificationError = await InsertNotificationErrorDb(data); - let isSendNotification = false + let isSendNotification = false; for (const dataUser of dataUsers) { if (dataUser.is_active) { @@ -81,7 +93,9 @@ class NotifikasiWaService { const bodyMessage = `Hai ${dataUser.contact_name || "-"},\n` + - `Terjadi peringatan dengan kode ${chanel?.value ?? "-"} "${errorCode?.error_code_name ?? ""}", Chanel ${chanel?.chanel_id ?? "-"} ` + + `Terjadi peringatan dengan kode ${chanel?.value ?? "-"} "${ + errorCode?.error_code_name ?? "" + }", Chanel ${chanel?.chanel_id ?? "-"} ` + `pada device ${deviceNotification?.device_name ?? "berikut"},` + `\nSilahkan cek detail pada link :` + `${shortUrl}`; @@ -96,6 +110,7 @@ class NotifikasiWaService { const resultNotificationErrorUser = await createNotificationErrorUserDb({ notification_error_id: param.idData, + contact_phone: param.userPhone, contact_name: param.userName, message_error_issue: param.bodyMessage, @@ -107,8 +122,6 @@ class NotifikasiWaService { param.bodyMessage ); - - await updateNotificationErrorUserDb( resultNotificationErrorUser[0].notification_error_user_id, { @@ -117,7 +130,7 @@ class NotifikasiWaService { ); if (resultSend.success) { - isSendNotification = resultSend.success + isSendNotification = resultSend.success; } } } @@ -131,9 +144,53 @@ class NotifikasiWaService { ); } } - } catch (error) { - // throw new ErrorHandler(error.statusCode, error.message); - return error; + } catch (err) { + return err; + } + } + + async restartWhatsapp() { + try { + const processName = "Whatsapp-API-notification" && "cod-whatsapp-notif"; + const { stdout } = await execPromise("pm2 jlist"); + const processes = JSON.parse(stdout); + const waProcess = processes.find((p) => p.name === processName); + + if (!waProcess) throw new Error(`PM2 ${processName} not found`); + + const waProcessId = waProcess.pm_id; + const pathDelete = waProcess.pm2_env.pm_cwd; + + // const execLogs = (cmd) => { + // const p = exec(cmd); + // p.stdout.pipe(process.stdout); + // p.stderr.pipe(process.stderr); + // return new Promise((res) => p.on("exit", res)); + // }; + + // console.log(`stop proses id: ${waProcessId}`) + await execPromise(`start powershell -Command "pm2 stop ${waProcessId}"`); + + const pathFolderDelete = [ + path.join(pathDelete, ".wwebjs_auth"), + path.join(pathDelete, ".wwebjs_cache"), + ]; + + // console.log(`path: ${pathDelete}`); + + pathFolderDelete.forEach((dir) => { + if (fs.existsSync(dir)) { + // console.log(`path folder: ${dir}`); + fs.rmSync(dir, { recursive: true, force: true }); + } + }); + + // console.log(`start proses id: ${waProcessId}`); + await execPromise(`start powershell -Command "pm2 start ${waProcessId}"`); + + return { success: true, message: "WhatsApp has been restarted." }; + } catch (err) { + return err; } } }