add: api restart wa

This commit is contained in:
2026-03-16 11:09:36 +07:00
parent 7122818181
commit 88ce628204
6 changed files with 127 additions and 12 deletions

View File

@@ -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,
@@ -11,6 +14,9 @@ const {
} = require("../db/notification_wa.db");
const { getErrorCodeByBrandAndCodeDb } = require("../db/brand_code.db");
const { getDeviceNotificationByIdDb } = require("../db/notification_error.db");
const { exec } = require("child_process");
const fs = require("fs");
const path = require("path");
class NotifikasiWaService {
async onNotification(topic, message) {
@@ -52,7 +58,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 +74,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 +90,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}`;
@@ -107,8 +118,6 @@ class NotifikasiWaService {
param.bodyMessage
);
await updateNotificationErrorUserDb(
resultNotificationErrorUser[0].notification_error_user_id,
{
@@ -117,7 +126,7 @@ class NotifikasiWaService {
);
if (resultSend.success) {
isSendNotification = resultSend.success
isSendNotification = resultSend.success;
}
}
}
@@ -131,11 +140,48 @@ class NotifikasiWaService {
);
}
}
} catch (error) {
// throw new ErrorHandler(error.statusCode, error.message);
return error;
} catch (err) {
return err;
}
}
async restartWhatsapp() {
return new Promise((resolve, reject) => {
exec('pm2 jlist', (err, stdout) => {
if (err) return reject({ success: false, message: "Error list PM2" });
try {
const processes = JSON.parse(stdout);
const waProcess = processes.find(p =>
p.name.toLowerCase().includes('whatsapp') ||
p.name.toLowerCase().includes('wa-api')
);
if (!waProcess) return reject({ success: false, message: "PM2 List PM2 Not Found" });
const processId = waProcess.pm_id;
exec(`pm2 stop ${processId}`, () => {
const paths = [
path.join(__dirname, "../../.wwebjs_auth"),
path.join(__dirname, "../../.wwebjs_cache")
];
paths.forEach(dir => {
if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
});
exec(`pm2 restart ${processId}`, (reErr) => {
if (reErr) return reject({ success: false, message: "Gagal restart" });
resolve({ success: true, message: `WA has been restart.` });
});
});
} catch (e) {
reject({ success: false, message: "JSON Parse Error: " + e.message });
}
});
});
}
}
module.exports = new NotifikasiWaService();
module.exports = new NotifikasiWaService();