From 12ff3d5b21a06953e71ae74526ce92bd77d30114 Mon Sep 17 00:00:00 2001 From: Fachba Date: Wed, 29 Apr 2026 16:28:58 +0700 Subject: [PATCH 1/3] feat: enhance notification and reminder services with logging and time tracking --- services/notification_error.service.js | 3 ++ services/notifikasi-wa.service.js | 44 ++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/services/notification_error.service.js b/services/notification_error.service.js index 6d5d4fc..eb6215a 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -31,6 +31,7 @@ const { } = require("../db/notification_wa.db"); const { ErrorHandler } = require("../helpers/error"); +const { deleteReminder } = require("./notifikasi-wa.service"); class NotificationService { static async getAllNotification(param) { @@ -150,6 +151,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..2f8b3fe 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'); @@ -235,6 +266,10 @@ class NotifikasiWaService { this.saveReminder(newData); + await this.saveLogReminder({ + message: `Reminder ${id} berhasil dihapus` + }) + console.log(`Reminder ${id} berhasil dihapus`); return true; } @@ -471,6 +506,11 @@ class NotifikasiWaService { param.bodyMessage ); + await this.saveLogReminder({ + message: `Reminder dijalankan`, + resultSend + }) + await updateNotificationErrorUserDb( resultNotificationErrorUser[0].notification_error_user_id, { From 3b26e06ffe648b6d4da5ed68feafaa1953238576 Mon Sep 17 00:00:00 2001 From: Fachba Date: Wed, 29 Apr 2026 17:23:08 +0700 Subject: [PATCH 2/3] feat: integrate spareparts reminder functionality into notification service --- services/notification_error.service.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/services/notification_error.service.js b/services/notification_error.service.js index eb6215a..f81e596 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -32,6 +32,7 @@ const { const { ErrorHandler } = require("../helpers/error"); const { deleteReminder } = require("./notifikasi-wa.service"); +const { getSparepartsByYearlyDb } = require("../db/sparepart.db"); class NotificationService { static async getAllNotification(param) { @@ -135,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); From 3428ad6d1f27297ee75e901aec8082da952abbb2 Mon Sep 17 00:00:00 2001 From: Fachba Date: Wed, 29 Apr 2026 17:25:58 +0700 Subject: [PATCH 3/3] fix: improve loadReminder method to handle file reading errors and return empty array if file does not exist --- services/notifikasi-wa.service.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/services/notifikasi-wa.service.js b/services/notifikasi-wa.service.js index 2f8b3fe..b2f723d 100644 --- a/services/notifikasi-wa.service.js +++ b/services/notifikasi-wa.service.js @@ -249,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) {