mirror of
https://github.com/Tony0410/quietthanks.git
synced 2026-05-24 21:31:41 +08:00
Fix notification system: timezone support, subscription handling, duplicate cleanup, and debugging logs
This commit is contained in:
@@ -12,19 +12,28 @@ export async function POST() {
|
||||
// Format: HH:MM
|
||||
const timeString = `${currentHour.toString().padStart(2, "0")}:${currentMinute.toString().padStart(2, "0")}`;
|
||||
|
||||
console.log(`[Scheduler] Checking for reminders at ${timeString} (${now.toISOString()})`);
|
||||
|
||||
// Find users who have reminders enabled and time matches
|
||||
const users = await db
|
||||
.select()
|
||||
.from(schema.users)
|
||||
.where(and(eq(schema.users.reminderEnabled, 1), eq(schema.users.reminderTime, timeString)));
|
||||
|
||||
console.log(`[Scheduler] Found ${users.length} users with reminder time ${timeString}`);
|
||||
|
||||
let sent = 0;
|
||||
let failed = 0;
|
||||
|
||||
for (const user of users) {
|
||||
console.log(`[Scheduler] Processing user ${user.id} (Always: ${user.reminderAlways})`);
|
||||
|
||||
// Check if user has already made an entry today, unless they want reminders always
|
||||
if (user.reminderAlways !== 1) {
|
||||
const today = now.toISOString().split("T")[0];
|
||||
const today = now.toISOString().split("T")[0]; // YYYY-MM-DD
|
||||
// Ensure we use the user's local date if possible, but for now assuming server time matches user expectation
|
||||
// since we enforced TZ=Australia/Perth
|
||||
|
||||
const entries = await db
|
||||
.select()
|
||||
.from(schema.entries)
|
||||
@@ -32,6 +41,7 @@ export async function POST() {
|
||||
.limit(1);
|
||||
|
||||
if (entries.length > 0) {
|
||||
console.log(`[Scheduler] User ${user.id} already has an entry for ${today}, skipping`);
|
||||
continue; // Already journaled today
|
||||
}
|
||||
}
|
||||
@@ -42,8 +52,11 @@ export async function POST() {
|
||||
.from(schema.pushSubscriptions)
|
||||
.where(eq(schema.pushSubscriptions.userId, user.id));
|
||||
|
||||
console.log(`[Scheduler] User ${user.id} has ${subscriptions.length} subscriptions`);
|
||||
|
||||
for (const sub of subscriptions) {
|
||||
try {
|
||||
console.log(`[Scheduler] Sending push to ${sub.endpoint.substring(0, 20)}...`);
|
||||
const success = await sendPushNotification(
|
||||
{
|
||||
endpoint: sub.endpoint,
|
||||
|
||||
Reference in New Issue
Block a user