|
|
|
|
@@ -614,9 +614,8 @@ class NotifikasiWaService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async onMonthlyReminder(deviceId, reminderMonthly) {
|
|
|
|
|
async onMonthlyReminder(deviceId, reminderMonthly, period) {
|
|
|
|
|
try {
|
|
|
|
|
console.log(`🔄 Memulai onMonthlyReminder untuk device ID: ${deviceId}`);
|
|
|
|
|
|
|
|
|
|
const paramDb = {
|
|
|
|
|
limit: 100,
|
|
|
|
|
@@ -625,12 +624,9 @@ class NotifikasiWaService {
|
|
|
|
|
active: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 1. Validasi device
|
|
|
|
|
console.log(`📡 Mengambil data device dengan ID: ${deviceId}`);
|
|
|
|
|
const devices = await getDeviceReminderMonthlyDb(deviceId);
|
|
|
|
|
|
|
|
|
|
if (!devices || devices.length === 0) {
|
|
|
|
|
console.error(`❌ Device dengan ID ${deviceId} tidak ditemukan`);
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: `Device dengan ID ${deviceId} tidak ditemukan di database`
|
|
|
|
|
@@ -641,19 +637,14 @@ class NotifikasiWaService {
|
|
|
|
|
const deviceName = device.device_name;
|
|
|
|
|
const currentDeviceId = device.device_id;
|
|
|
|
|
|
|
|
|
|
console.log(`✅ Device ditemukan: ${deviceName} (ID: ${currentDeviceId})`);
|
|
|
|
|
|
|
|
|
|
// 2. Validasi dan update reminder_at_monthly jika diperlukan
|
|
|
|
|
let targetReminderTime = device.reminder_at_monthly;
|
|
|
|
|
console.log(`📅 Reminder time dari DB: ${targetReminderTime}`);
|
|
|
|
|
|
|
|
|
|
if (reminderMonthly) {
|
|
|
|
|
console.log(`📝 Mengupdate reminder time ke: ${reminderMonthly}`);
|
|
|
|
|
const isValidDate = dayjs.tz(reminderMonthly, this.timeZone).isValid();
|
|
|
|
|
if (!isValidDate) {
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Format reminderMonthly tidak valid. Gunakan format YYYY-MM-DD HH:mm:ss'
|
|
|
|
|
message: 'Format Reminder Monthly tidak valid. Gunakan format YYYY-MM-DD HH:mm:ss'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -661,40 +652,30 @@ class NotifikasiWaService {
|
|
|
|
|
if (!updatedDevice) {
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Gagal mengupdate reminder monthly.'
|
|
|
|
|
message: 'Gagal mengupdate Reminder Monthly.'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targetReminderTime = reminderMonthly;
|
|
|
|
|
console.log(`✅ Reminder time berhasil diupdate ke: ${targetReminderTime}`);
|
|
|
|
|
|
|
|
|
|
} else if (!targetReminderTime) {
|
|
|
|
|
console.log(`⚠️ Tidak ada reminder time, setting default ke sekarang + 1 jam`);
|
|
|
|
|
const reminderTime = dayjs().tz(this.timeZone).add(1, 'hour').format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
|
const updatedDevice = await updateDeviceReminderMonthlyDb(deviceId, reminderTime);
|
|
|
|
|
|
|
|
|
|
if (!updatedDevice) {
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Gagal mengupdate reminder monthly dengan default time.'
|
|
|
|
|
message: 'Gagal update Reminder Monthly dengan default time.'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
targetReminderTime = reminderTime;
|
|
|
|
|
console.log(`✅ Default reminder time: ${targetReminderTime}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`📅 Target reminder time: ${targetReminderTime}`);
|
|
|
|
|
|
|
|
|
|
// 3. Parse target reminder time
|
|
|
|
|
const reminderAt = dayjs.tz(targetReminderTime, this.timeZone);
|
|
|
|
|
const now = dayjs().tz(this.timeZone);
|
|
|
|
|
|
|
|
|
|
console.log(`🕐 Waktu sekarang: ${now.format('YYYY-MM-DD HH:mm:ss')}`);
|
|
|
|
|
console.log(`🕐 Waktu reminder: ${reminderAt.format('YYYY-MM-DD HH:mm:ss')}`);
|
|
|
|
|
|
|
|
|
|
// 4. Jika sudah lewat atau sama, langsung kirim (sama seperti onCustomReminder)
|
|
|
|
|
const isEndDatePassed = now.isAfter(reminderAt) || now.isSame(reminderAt);
|
|
|
|
|
|
|
|
|
|
if (isEndDatePassed) {
|
|
|
|
|
console.log(`⏰ Reminder time sudah terlewati, mengirim reminder sekarang untuk ${deviceName}`);
|
|
|
|
|
const sendResult = await this.sendMonthlyReminderNow(
|
|
|
|
|
currentDeviceId,
|
|
|
|
|
deviceName,
|
|
|
|
|
@@ -703,13 +684,8 @@ class NotifikasiWaService {
|
|
|
|
|
return sendResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. Jika belum waktunya, jadwalkan cron job (sama seperti onCustomReminder)
|
|
|
|
|
console.log(`⏰ Menjadwalkan reminder untuk ${deviceName} pada ${reminderAt.format('YYYY-MM-DD HH:mm:ss')}`);
|
|
|
|
|
|
|
|
|
|
// Stop cron job lama jika ada
|
|
|
|
|
this.stopCronJob(currentDeviceId);
|
|
|
|
|
|
|
|
|
|
// Schedule cron job
|
|
|
|
|
const scheduleResult = await this.scheduleMonthlyReminderJob(
|
|
|
|
|
currentDeviceId,
|
|
|
|
|
deviceName,
|
|
|
|
|
@@ -719,19 +695,11 @@ class NotifikasiWaService {
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
message: `Reminder berhasil dijadwalkan untuk device ${deviceName}`,
|
|
|
|
|
schedule: scheduleResult,
|
|
|
|
|
nextRun: scheduleResult.nextRun
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('❌ Error onMonthlyReminder:', error);
|
|
|
|
|
console.error('❌ Stack trace:', error.stack);
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Error pada onMonthlyReminder: ' + error.message,
|
|
|
|
|
error: error,
|
|
|
|
|
stack: error.stack
|
|
|
|
|
};
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Error on Monthly Reminder:', err);
|
|
|
|
|
throw err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -820,7 +788,6 @@ class NotifikasiWaService {
|
|
|
|
|
|
|
|
|
|
async sendMonthlyReminderNow(deviceId, deviceName, reminderAt) {
|
|
|
|
|
try {
|
|
|
|
|
console.log(`Mengirim reminder untuk ${deviceName}`);
|
|
|
|
|
|
|
|
|
|
const paramDb = {
|
|
|
|
|
limit: 100,
|
|
|
|
|
@@ -841,7 +808,6 @@ class NotifikasiWaService {
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (checkNotifExist) {
|
|
|
|
|
console.log(`Reminder untuk ${deviceName} bulan ${currentMonth} sudah pernah dibuat`);
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: `Reminder untuk device ${deviceName} sudah pernah dibuat.`
|
|
|
|
|
@@ -957,14 +923,12 @@ class NotifikasiWaService {
|
|
|
|
|
|
|
|
|
|
this.stopCronJob(deviceId);
|
|
|
|
|
|
|
|
|
|
console.log(`Reminder berhasil dikirim untuk ${deviceName}`);
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
message: "Reminder has been sent.",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error sending monthly reminder now:', error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -981,7 +945,7 @@ class NotifikasiWaService {
|
|
|
|
|
if (!inputStartDate || !inputEndDate) {
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'start_date dan end_date harus diisi. Format: YYYY-MM-DD HH:mm:ss'
|
|
|
|
|
message: 'Start Date dan End Date harus diisi. Format: YYYY-MM-DD HH:mm:ss'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -991,14 +955,14 @@ class NotifikasiWaService {
|
|
|
|
|
if (!startDateObj.isValid() || !endDateObj.isValid()) {
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Format tanggal tidak valid. Gunakan format: YYYY-MM-DD HH:mm:ss'
|
|
|
|
|
message: 'Format tanggal Start Date dan End Date tidak valid. Gunakan format: YYYY-MM-DD HH:mm:ss'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!endDateObj.isAfter(startDateObj)) {
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'end_date harus lebih besar dari start_date'
|
|
|
|
|
message: 'End Date harus lebih besar dari Start Date'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1025,8 +989,6 @@ class NotifikasiWaService {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`Berhasil update start_date dan end_date untuk device ${deviceName}`);
|
|
|
|
|
|
|
|
|
|
this.stopCronJob(currentDeviceId);
|
|
|
|
|
|
|
|
|
|
const scheduleResult = await this.scheduleCustomReminderJob(
|
|
|
|
|
@@ -1051,16 +1013,12 @@ class NotifikasiWaService {
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
message: 'Reminder berhasil dijadwalkan dan dikirim sekarang',
|
|
|
|
|
schedule: scheduleResult,
|
|
|
|
|
sendResult: sendResult
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
message: `Reminder berhasil dijadwalkan untuk device ${deviceName}`,
|
|
|
|
|
schedule: scheduleResult,
|
|
|
|
|
nextRun: scheduleResult.nextRun
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
@@ -1079,10 +1037,6 @@ class NotifikasiWaService {
|
|
|
|
|
|
|
|
|
|
const nextRun = targetTime.format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
|
|
|
|
|
|
console.log(`Reminder untuk ${deviceName} dijadwalkan pada ${nextRun}`);
|
|
|
|
|
console.log(`Start Date: ${startDateObj.format('YYYY-MM-DD HH:mm:ss')}`);
|
|
|
|
|
console.log(`End Date: ${endDateObj.format('YYYY-MM-DD HH:mm:ss')}`);
|
|
|
|
|
|
|
|
|
|
const timeoutId = setTimeout(async () => {
|
|
|
|
|
console.log(`Waktu pengiriman tiba untuk ${deviceName} pada ${dayjs().tz(this.timeZone).format('YYYY-MM-DD HH:mm:ss')}`);
|
|
|
|
|
try {
|
|
|
|
|
@@ -1093,8 +1047,8 @@ class NotifikasiWaService {
|
|
|
|
|
await this.sendCustomReminderNow(deviceId, deviceName, startDateObj, endDateObj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`Gagal mengirim reminder untuk ${deviceName}:`, error);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw err
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@@ -1109,10 +1063,8 @@ class NotifikasiWaService {
|
|
|
|
|
const endMinute = endDateObj.minute();
|
|
|
|
|
const cronExpression = `${endMinute} ${endHour} * * *`;
|
|
|
|
|
|
|
|
|
|
console.log(`Backup cron akan berjalan setiap hari pada ${endHour}:${endMinute}`);
|
|
|
|
|
|
|
|
|
|
const cronTask = cron.schedule(cronExpression, async () => {
|
|
|
|
|
console.log(`Backup cron job berjalan untuk ${deviceName} pada ${dayjs().tz(this.timeZone).format('YYYY-MM-DD HH:mm:ss')}`);
|
|
|
|
|
try {
|
|
|
|
|
const now = dayjs().tz(this.timeZone);
|
|
|
|
|
const isEndDatePassed = now.isAfter(endDateObj) || now.isSame(endDateObj);
|
|
|
|
|
@@ -1128,18 +1080,21 @@ class NotifikasiWaService {
|
|
|
|
|
if (!checkNotifExist) {
|
|
|
|
|
await this.sendCustomReminderNow(deviceId, deviceName, startDateObj, endDateObj);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`Reminder untuk ${deviceName} sudah pernah dikirim`);
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: `Reminder untuk ${deviceName} sudah pernah dikirim`
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cronTask.stop();
|
|
|
|
|
this.cronJobs.delete(deviceId);
|
|
|
|
|
this.cronJobs.delete(`cron_${deviceId}`);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`Menunggu end_date untuk ${deviceName}: ${endDateObj.format('YYYY-MM-DD HH:mm:ss')}`);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`Backup cron job gagal untuk ${deviceName}:`, error);
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, {
|
|
|
|
|
timezone: this.timeZone
|
|
|
|
|
});
|
|
|
|
|
@@ -1166,7 +1121,13 @@ class NotifikasiWaService {
|
|
|
|
|
|
|
|
|
|
async sendCustomReminderNow(deviceId, deviceName, startDateObj, endDateObj) {
|
|
|
|
|
try {
|
|
|
|
|
console.log(`Mengirim reminder untuk ${deviceName}`);
|
|
|
|
|
|
|
|
|
|
const paramDb = {
|
|
|
|
|
limit: 100,
|
|
|
|
|
page: 1,
|
|
|
|
|
criteria: "",
|
|
|
|
|
active: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const now = dayjs().tz(this.timeZone);
|
|
|
|
|
const currentMonth = now.month() + 1;
|
|
|
|
|
@@ -1179,7 +1140,6 @@ class NotifikasiWaService {
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (checkNotifExist) {
|
|
|
|
|
console.log(`Reminder untuk ${deviceName} sudah pernah dibuat`);
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: `Reminder untuk device ${deviceName} sudah pernah dibuat.`
|
|
|
|
|
@@ -1198,12 +1158,6 @@ class NotifikasiWaService {
|
|
|
|
|
|
|
|
|
|
const resultNotificationError = await InsertNotificationErrorDb(data);
|
|
|
|
|
|
|
|
|
|
const paramDb = {
|
|
|
|
|
limit: 100,
|
|
|
|
|
page: 1,
|
|
|
|
|
criteria: "",
|
|
|
|
|
active: true,
|
|
|
|
|
};
|
|
|
|
|
const results = await getAllContactDb(paramDb);
|
|
|
|
|
const dataUsers = results.data;
|
|
|
|
|
|
|
|
|
|
@@ -1259,7 +1213,7 @@ class NotifikasiWaService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (resultSend.success) {
|
|
|
|
|
isSendNotification = resultSend.success;
|
|
|
|
|
isSendNotification = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -1290,12 +1244,11 @@ class NotifikasiWaService {
|
|
|
|
|
|
|
|
|
|
this.stopCronJob(deviceId);
|
|
|
|
|
|
|
|
|
|
console.log(`Reminder berhasil dikirim untuk ${deviceName}`);
|
|
|
|
|
return { success: true, message: "Reminder has been sent." };
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error sending reminder now:', error);
|
|
|
|
|
throw error;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Error sending reminder now:', err);
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1323,8 +1276,6 @@ class NotifikasiWaService {
|
|
|
|
|
this.cronJobs.delete(`interval_${deviceId}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async restartWhatsapp() {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
@@ -1336,19 +1287,18 @@ class NotifikasiWaService {
|
|
|
|
|
const endJSON = cleanJSON.lastIndexOf(']') + 1;
|
|
|
|
|
|
|
|
|
|
if (startJSON === -1 || endJSON === 0) {
|
|
|
|
|
throw new Error(`Terminal tidak mengembalikan JSON yang valid. Output: ${stdout}`);
|
|
|
|
|
console.error(`Terminal tidak mengembalikan JSON yang valid. Output: ${stdout}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const index = cleanJSON.slice(startJSON, endJSON);
|
|
|
|
|
const processes = JSON.parse(index);
|
|
|
|
|
const waProcess = processes.find((p) => p.name === processName);
|
|
|
|
|
|
|
|
|
|
if (!waProcess) throw new Error(`PM2 proses dengan nama "${processName}" tidak ditemukan!`);
|
|
|
|
|
if (!waProcess) console.error(`PM2 proses dengan nama ${processName} tidak ditemukan`);
|
|
|
|
|
|
|
|
|
|
const waProcessId = waProcess.pm_id;
|
|
|
|
|
const pathDelete = waProcess.pm2_env.pm_cwd;
|
|
|
|
|
|
|
|
|
|
// console.log(`stop proses id: ${waProcessId}`);
|
|
|
|
|
await execPromise(`pm2 stop ${waProcessId}`);
|
|
|
|
|
|
|
|
|
|
const pathFolderDelete = [
|
|
|
|
|
@@ -1356,8 +1306,6 @@ class NotifikasiWaService {
|
|
|
|
|
path.join(pathDelete, ".wwebjs_cache"),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// console.log(`path: ${pathDelete}`);
|
|
|
|
|
|
|
|
|
|
for (const dir of pathFolderDelete) {
|
|
|
|
|
try {
|
|
|
|
|
await fs.access(dir);
|
|
|
|
|
@@ -1371,12 +1319,19 @@ class NotifikasiWaService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// console.log(`start proses id: ${waProcessId}`);
|
|
|
|
|
await execPromise(`pm2 start ${waProcessId}`);
|
|
|
|
|
|
|
|
|
|
return { success: true, message: "WhatsApp has been restarted." };
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
message: "WhatsApp has been restarted."
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return { success: false, error: err.message || err };
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
message: `Gagal mengirim Whatsapp`
|
|
|
|
|
};
|
|
|
|
|
throw err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|