diff --git a/services/notification_error.service.js b/services/notification_error.service.js index e7e7304..6d054b8 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -31,6 +31,8 @@ const { } = require("../db/notification_wa.db"); const { ErrorHandler } = require("../helpers/error"); +const { deleteReminder } = require("./notifikasi-wa.service"); +const { getSparepartsByYearlyDb } = require("../db/sparepart.db"); class NotificationService { static async getAllNotification(param) { @@ -134,6 +136,16 @@ class NotificationService { notification.activity_logs = notificationLogs; + notification.is_reminder = false; + notification.spareparts_reminder = []; + if (notification.message_error_issue) { + const sparepartsReminder = await getSparepartsByYearlyDb(notification.error_code_id); + + notification.spareparts_reminder = sparepartsReminder ?? []; + notification.is_reminder = true; + + } + return notification; } catch (error) { throw new ErrorHandler(error.statusCode, error.message); @@ -150,6 +162,8 @@ class NotificationService { const notification = Array.isArray(dataExist) ? dataExist[0] : dataExist; + await deleteReminder(notification_error_id) + if (notification.is_read === true) { return { success: true, message: "Notification has already been read" }; } diff --git a/services/notifikasi-wa.service.js b/services/notifikasi-wa.service.js index aceddb3..b2f723d 100644 --- a/services/notifikasi-wa.service.js +++ b/services/notifikasi-wa.service.js @@ -51,10 +51,28 @@ class NotifikasiWaService { existing = []; } - existing.push(data); + // 👉 selalu tambahkan waktu saat insert + const now = new Date(); + + const newData = { + ...data, + time: now.toLocaleTimeString('id-ID', { + hour: '2-digit', + minute: '2-digit', + hour12: false, + }), + created_at: now.toISOString() // opsional (full timestamp) + }; + + existing.push(newData); await fs.writeFile(filePath, JSON.stringify(existing, null, 2), 'utf-8'); + await this.saveLogReminder({ + message: `Reminder berhasil disimpan`, + data + }) + console.log(`Reminder berhasil disimpan`); } catch (error) { console.log('Error saveReminder:', error); @@ -81,7 +99,20 @@ class NotifikasiWaService { existing = []; } - existing.push(data); + // 👉 selalu tambahkan waktu saat insert + const now = new Date(); + + const newData = { + ...data, + time: now.toLocaleTimeString('id-ID', { + hour: '2-digit', + minute: '2-digit', + hour12: false, + }), + created_at: now.toISOString() // opsional (full timestamp) + }; + + existing.push(newData); await fs.writeFile(filePathLog, JSON.stringify(existing, null, 2), 'utf-8'); @@ -218,8 +249,12 @@ class NotifikasiWaService { } async loadReminder() { - if (!fs.existsSync(filePath)) return []; - return JSON.parse(fs.readFileSync(filePath)); + try { + const raw = await fs.readFile(filePath, 'utf-8'); + return raw ? JSON.parse(raw) : []; + } catch (err) { + return []; // file belum ada + } } async deleteReminder(id) { @@ -235,6 +270,10 @@ class NotifikasiWaService { this.saveReminder(newData); + await this.saveLogReminder({ + message: `Reminder ${id} berhasil dihapus` + }) + console.log(`Reminder ${id} berhasil dihapus`); return true; } @@ -471,6 +510,11 @@ class NotifikasiWaService { param.bodyMessage ); + await this.saveLogReminder({ + message: `Reminder dijalankan`, + resultSend + }) + await updateNotificationErrorUserDb( resultNotificationErrorUser[0].notification_error_user_id, {