Compare commits

...

4 Commits

2 changed files with 62 additions and 4 deletions

View File

@@ -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" };
}

View File

@@ -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,
{