Compare commits
2 Commits
e295d781db
...
1af02c2d36
| Author | SHA1 | Date | |
|---|---|---|---|
| 1af02c2d36 | |||
| 6a949e52b5 |
@@ -29,6 +29,7 @@
|
|||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"crypto": "^1.0.1",
|
"crypto": "^1.0.1",
|
||||||
"crypto-js": "^4.2.0",
|
"crypto-js": "^4.2.0",
|
||||||
|
"dayjs": "^1.11.20",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"exceljs": "^4.4.0",
|
"exceljs": "^4.4.0",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ const { getAllContactDb } = require("../db/contact.db");
|
|||||||
const {
|
const {
|
||||||
InsertNotificationErrorDb,
|
InsertNotificationErrorDb,
|
||||||
updateNotificationErrorDb,
|
updateNotificationErrorDb,
|
||||||
getDeviceChannelReminder,
|
|
||||||
getReminderNotificationErrorByYearlyDb,
|
getReminderNotificationErrorByYearlyDb,
|
||||||
updateNotificationErrorByChanelReminderDb,
|
updateNotificationErrorByChanelReminderDb,
|
||||||
} = require("../db/notification_error.db");
|
} = require("../db/notification_error.db");
|
||||||
@@ -30,6 +29,15 @@ const baseDir = path.resolve(__dirname, '../scheduler');
|
|||||||
const filePath = path.join(baseDir, 'reminder.json');
|
const filePath = path.join(baseDir, 'reminder.json');
|
||||||
const filePathLog = path.join(baseDir, 'log.json');
|
const filePathLog = path.join(baseDir, 'log.json');
|
||||||
|
|
||||||
|
const dayjs = require('dayjs');
|
||||||
|
const utc = require('dayjs/plugin/utc');
|
||||||
|
const timezone = require('dayjs/plugin/timezone');
|
||||||
|
|
||||||
|
const timeZone = 'Asia/Jakarta';
|
||||||
|
|
||||||
|
dayjs.extend(utc);
|
||||||
|
dayjs.extend(timezone);
|
||||||
|
|
||||||
class NotifikasiWaService {
|
class NotifikasiWaService {
|
||||||
|
|
||||||
async saveReminder(data, isReset = false) {
|
async saveReminder(data, isReset = false) {
|
||||||
@@ -52,16 +60,16 @@ class NotifikasiWaService {
|
|||||||
existing = [];
|
existing = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const now = new Date();
|
const now = dayjs().tz(timeZone);
|
||||||
|
|
||||||
const newData = {
|
const newData = {
|
||||||
...data,
|
...data,
|
||||||
time: now.toLocaleTimeString('id-ID', {
|
|
||||||
hour: '2-digit',
|
// 🔹 jam (HH:mm) WIB
|
||||||
minute: '2-digit',
|
time: now.format('HH:mm'),
|
||||||
hour12: false,
|
|
||||||
}),
|
// 🔹 timestamp UTC (untuk DB)
|
||||||
created_at: now.toISOString() // opsional (full timestamp)
|
created_at: now.toISOString()
|
||||||
};
|
};
|
||||||
|
|
||||||
let finalData;
|
let finalData;
|
||||||
@@ -118,17 +126,16 @@ class NotifikasiWaService {
|
|||||||
existing = [];
|
existing = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 👉 selalu tambahkan waktu saat insert
|
const now = dayjs().tz(timeZone);
|
||||||
const now = new Date();
|
|
||||||
|
|
||||||
const newData = {
|
const newData = {
|
||||||
...data,
|
...data,
|
||||||
time: now.toLocaleTimeString('id-ID', {
|
|
||||||
hour: '2-digit',
|
// 🔹 jam (HH:mm) WIB
|
||||||
minute: '2-digit',
|
time: now.format('HH:mm'),
|
||||||
hour12: false,
|
|
||||||
}),
|
// 🔹 timestamp UTC (untuk DB)
|
||||||
created_at: now.toISOString() // opsional (full timestamp)
|
created_at: now.toISOString()
|
||||||
};
|
};
|
||||||
|
|
||||||
await existing.push(newData);
|
await existing.push(newData);
|
||||||
@@ -332,7 +339,10 @@ class NotifikasiWaService {
|
|||||||
let data = await this.loadReminder();
|
let data = await this.loadReminder();
|
||||||
|
|
||||||
if (Array.isArray(data) && data.length > 0) {
|
if (Array.isArray(data) && data.length > 0) {
|
||||||
const now = new Date();
|
|
||||||
|
// waktu sekarang (WIB)
|
||||||
|
const now = dayjs().tz(timeZone);
|
||||||
|
|
||||||
const newData = [];
|
const newData = [];
|
||||||
for (const item of data) {
|
for (const item of data) {
|
||||||
|
|
||||||
@@ -342,35 +352,22 @@ class NotifikasiWaService {
|
|||||||
// skip kalau sudah max
|
// skip kalau sudah max
|
||||||
if (item.count >= item.max) continue;
|
if (item.count >= item.max) continue;
|
||||||
|
|
||||||
const [hour, minute] = item.start_at.split(':');
|
// 🔹 parse last_run (ISO dari DB)
|
||||||
|
const lastRunDate = dayjs(item.last_run);
|
||||||
|
|
||||||
const start = new Date();
|
if (!lastRunDate.isValid()) {
|
||||||
start.setHours(parseInt(hour), parseInt(minute), 0, 0);
|
|
||||||
|
|
||||||
const lastRunDate = new Date(item.last_run);
|
|
||||||
|
|
||||||
if (isNaN(lastRunDate.getTime())) {
|
|
||||||
throw new Error(`Invalid last_run: ${item.last_run}`);
|
throw new Error(`Invalid last_run: ${item.last_run}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const nextRun = new Date(
|
// 🔹 hitung next_run berdasarkan interval (jam)
|
||||||
lastRunDate.getTime() + (item.interval * 60 * 60 * 1000)
|
const nextRun = lastRunDate.add(item.interval, 'hour');
|
||||||
);
|
|
||||||
|
|
||||||
// format ke WIB (Jakarta)
|
// 🔹 assign hasil
|
||||||
item.next_run = nextRun;
|
item.next_run = nextRun.toISOString(); // UTC (untuk DB)
|
||||||
item.next_run_indo = nextRun.toLocaleString('id-ID', {
|
item.next_run_indo = nextRun.tz(timeZone).format('DD-MM-YYYY HH:mm:ss');
|
||||||
timeZone: 'Asia/Jakarta',
|
|
||||||
year: 'numeric',
|
|
||||||
month: '2-digit',
|
|
||||||
day: '2-digit',
|
|
||||||
hour: '2-digit',
|
|
||||||
minute: '2-digit',
|
|
||||||
second: '2-digit',
|
|
||||||
hour12: false
|
|
||||||
});
|
|
||||||
|
|
||||||
if (now >= nextRun) {
|
// 🔥 trigger check
|
||||||
|
if (now.isAfter(nextRun) || now.isSame(nextRun)) {
|
||||||
|
|
||||||
const results = await getNotificationErrorByIdDb(item.notification_log);
|
const results = await getNotificationErrorByIdDb(item.notification_log);
|
||||||
const dataUsers = results?.data ?? [];
|
const dataUsers = results?.data ?? [];
|
||||||
@@ -400,18 +397,8 @@ class NotifikasiWaService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
item.last_run = now.toISOString(); // tetap UTC (standar DB)
|
||||||
item.last_run = now.toISOString();
|
item.last_run_indo = now.format('DD-MM-YYYY HH:mm:ss'); // WIB
|
||||||
item.last_run_indo = now.toLocaleString('id-ID', {
|
|
||||||
timeZone: 'Asia/Jakarta',
|
|
||||||
year: 'numeric',
|
|
||||||
month: '2-digit',
|
|
||||||
day: '2-digit',
|
|
||||||
hour: '2-digit',
|
|
||||||
minute: '2-digit',
|
|
||||||
second: '2-digit',
|
|
||||||
hour12: false
|
|
||||||
});
|
|
||||||
item.count += 1;
|
item.count += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -463,55 +450,32 @@ class NotifikasiWaService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (deviceReminder?.[0]?.reminder_at) {
|
if (deviceReminder?.[0]?.reminder_at) {
|
||||||
const timeZone = 'Asia/Jakarta';
|
// waktu sekarang (WIB)
|
||||||
|
const now = dayjs().tz(timeZone);
|
||||||
|
|
||||||
|
const currentYear = dayjs().tz(timeZone).year();
|
||||||
|
|
||||||
|
// parse dari DB
|
||||||
|
const reminderAt = dayjs.tz(
|
||||||
|
deviceReminder[0].reminder_at,
|
||||||
|
'YYYY-MM-DD HH:mm:ss.SSS',
|
||||||
|
timeZone
|
||||||
|
);
|
||||||
|
|
||||||
const now = new Date();
|
|
||||||
const year = now.getFullYear();
|
|
||||||
const reminderAt = new Date(deviceReminder[0].reminder_at);
|
|
||||||
const deviceName = deviceReminder[0].device_name ?? '-';
|
const deviceName = deviceReminder[0].device_name ?? '-';
|
||||||
|
|
||||||
// 🔹 ambil bulan & jam dalam timezone server
|
// 🔥 compare sampai menit (tanpa tahun)
|
||||||
const nowParts = new Intl.DateTimeFormat('en-US', {
|
const nowKey = now.format('MM-DD HH:mm');
|
||||||
timeZone,
|
const reminderKey = reminderAt.format('MM-DD HH:mm');
|
||||||
month: 'numeric',
|
|
||||||
hour: 'numeric',
|
|
||||||
hour12: false,
|
|
||||||
}).formatToParts(now);
|
|
||||||
|
|
||||||
const reminderParts = new Intl.DateTimeFormat('en-US', {
|
const isTriggered = nowKey >= reminderKey;
|
||||||
timeZone,
|
|
||||||
month: 'numeric',
|
|
||||||
hour: 'numeric',
|
|
||||||
hour12: false,
|
|
||||||
}).formatToParts(reminderAt);
|
|
||||||
|
|
||||||
const timeReminderString = reminderAt.toLocaleTimeString('id-ID', {
|
|
||||||
timeZone: 'Asia/Jakarta',
|
|
||||||
hour: '2-digit',
|
|
||||||
minute: '2-digit',
|
|
||||||
hour12: false
|
|
||||||
}).replace('.', ':'); // ganti titik jadi titik dua
|
|
||||||
|
|
||||||
const getPart = (parts, type) =>
|
|
||||||
parseInt(parts.find(p => p.type === type).value, 10);
|
|
||||||
|
|
||||||
const nowMonth = getPart(nowParts, 'month');
|
|
||||||
const nowHour = getPart(nowParts, 'hour');
|
|
||||||
|
|
||||||
const reminderMonth = getPart(reminderParts, 'month');
|
|
||||||
const reminderHour = getPart(reminderParts, 'hour');
|
|
||||||
|
|
||||||
// 🔥 logic utama
|
|
||||||
const isTriggered =
|
|
||||||
nowMonth > reminderMonth ||
|
|
||||||
(nowMonth === reminderMonth && nowHour >= reminderHour);
|
|
||||||
|
|
||||||
if (isTriggered) {
|
if (isTriggered) {
|
||||||
const valMqtt = chanel.value;
|
const valMqtt = chanel.value;
|
||||||
const yearly = parseInt(valMqtt.match(/\d+/)?.[0], 10);
|
const yearly = parseInt(valMqtt.match(/\d+/)?.[0], 10);
|
||||||
|
|
||||||
if (!isNaN(yearly)) {
|
if (!isNaN(yearly)) {
|
||||||
const checkNotifExist = await getReminderNotificationErrorByYearlyDb(yearly ?? chanel.value, chanel.chanel_id, year);
|
const checkNotifExist = await getReminderNotificationErrorByYearlyDb(yearly ?? chanel.value, chanel.chanel_id, currentYear);
|
||||||
|
|
||||||
if (!checkNotifExist) {
|
if (!checkNotifExist) {
|
||||||
|
|
||||||
@@ -522,7 +486,7 @@ class NotifikasiWaService {
|
|||||||
is_delivered: 0,
|
is_delivered: 0,
|
||||||
is_read: 0,
|
is_read: 0,
|
||||||
is_active: 1,
|
is_active: 1,
|
||||||
message_error_issue: `reminder ${chanel.value} in ${year}`,
|
message_error_issue: `reminder ${chanel.value} in ${currentYear}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
const resultNotificationError = await InsertNotificationErrorDb(data);
|
const resultNotificationError = await InsertNotificationErrorDb(data);
|
||||||
@@ -531,22 +495,13 @@ class NotifikasiWaService {
|
|||||||
notification_log: resultNotificationError.notification_error_id,
|
notification_log: resultNotificationError.notification_error_id,
|
||||||
error_code_id: data['error_code_id'],
|
error_code_id: data['error_code_id'],
|
||||||
error_chanel: data['error_chanel'],
|
error_chanel: data['error_chanel'],
|
||||||
start_at: timeReminderString,
|
start_at: reminderAt.format('HH:mm'),
|
||||||
interval: 1,
|
interval: 1,
|
||||||
max: 3,
|
max: 3,
|
||||||
active: 1,
|
active: 1,
|
||||||
count: 0,
|
count: 0,
|
||||||
last_run: now.toISOString(),
|
last_run: now.toISOString(),
|
||||||
last_run_indo: now.toLocaleString('id-ID', {
|
last_run_indo: now.format('DD-MM-YYYY HH:mm:ss'),
|
||||||
timeZone: 'Asia/Jakarta',
|
|
||||||
year: 'numeric',
|
|
||||||
month: '2-digit',
|
|
||||||
day: '2-digit',
|
|
||||||
hour: '2-digit',
|
|
||||||
minute: '2-digit',
|
|
||||||
second: '2-digit',
|
|
||||||
hour12: false
|
|
||||||
}),
|
|
||||||
next_run: null,
|
next_run: null,
|
||||||
next_run_indo: null
|
next_run_indo: null
|
||||||
}, false);
|
}, false);
|
||||||
|
|||||||
Reference in New Issue
Block a user