mirror of
https://github.com/Tony0410/quietthanks.git
synced 2026-05-24 13:21:38 +08:00
Fix notification system: timezone support, subscription handling, duplicate cleanup, and debugging logs
This commit is contained in:
@@ -11,7 +11,7 @@ services:
|
|||||||
- DATABASE_PATH=/app/data/quietthanks.db
|
- DATABASE_PATH=/app/data/quietthanks.db
|
||||||
- NEXT_PUBLIC_VAPID_PUBLIC_KEY=BIKukAq5-KPwJAMpksxD7UNL8XfF-oJOI0CLGGZQAY93igZgf1PYa9MVvS8GaBv-vv9ckcXPCEKdzWDCtOyQpKg
|
- NEXT_PUBLIC_VAPID_PUBLIC_KEY=BIKukAq5-KPwJAMpksxD7UNL8XfF-oJOI0CLGGZQAY93igZgf1PYa9MVvS8GaBv-vv9ckcXPCEKdzWDCtOyQpKg
|
||||||
- VAPID_PRIVATE_KEY=IBkQ14BLKFCg2PmGOWheC7xfYHS5J49vXS8duHCeDBw
|
- VAPID_PRIVATE_KEY=IBkQ14BLKFCg2PmGOWheC7xfYHS5J49vXS8duHCeDBw
|
||||||
- VAPID_EMAIL=mailto:admin@quietthanks.local
|
- VAPID_EMAIL=mailto:admin@example.com
|
||||||
- TZ=Australia/Perth
|
- TZ=Australia/Perth
|
||||||
|
|
||||||
scheduler:
|
scheduler:
|
||||||
|
|||||||
@@ -12,19 +12,28 @@ export async function POST() {
|
|||||||
// Format: HH:MM
|
// Format: HH:MM
|
||||||
const timeString = `${currentHour.toString().padStart(2, "0")}:${currentMinute.toString().padStart(2, "0")}`;
|
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
|
// Find users who have reminders enabled and time matches
|
||||||
const users = await db
|
const users = await db
|
||||||
.select()
|
.select()
|
||||||
.from(schema.users)
|
.from(schema.users)
|
||||||
.where(and(eq(schema.users.reminderEnabled, 1), eq(schema.users.reminderTime, timeString)));
|
.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 sent = 0;
|
||||||
let failed = 0;
|
let failed = 0;
|
||||||
|
|
||||||
for (const user of users) {
|
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
|
// Check if user has already made an entry today, unless they want reminders always
|
||||||
if (user.reminderAlways !== 1) {
|
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
|
const entries = await db
|
||||||
.select()
|
.select()
|
||||||
.from(schema.entries)
|
.from(schema.entries)
|
||||||
@@ -32,6 +41,7 @@ export async function POST() {
|
|||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
if (entries.length > 0) {
|
if (entries.length > 0) {
|
||||||
|
console.log(`[Scheduler] User ${user.id} already has an entry for ${today}, skipping`);
|
||||||
continue; // Already journaled today
|
continue; // Already journaled today
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,8 +52,11 @@ export async function POST() {
|
|||||||
.from(schema.pushSubscriptions)
|
.from(schema.pushSubscriptions)
|
||||||
.where(eq(schema.pushSubscriptions.userId, user.id));
|
.where(eq(schema.pushSubscriptions.userId, user.id));
|
||||||
|
|
||||||
|
console.log(`[Scheduler] User ${user.id} has ${subscriptions.length} subscriptions`);
|
||||||
|
|
||||||
for (const sub of subscriptions) {
|
for (const sub of subscriptions) {
|
||||||
try {
|
try {
|
||||||
|
console.log(`[Scheduler] Sending push to ${sub.endpoint.substring(0, 20)}...`);
|
||||||
const success = await sendPushNotification(
|
const success = await sendPushNotification(
|
||||||
{
|
{
|
||||||
endpoint: sub.endpoint,
|
endpoint: sub.endpoint,
|
||||||
|
|||||||
Reference in New Issue
Block a user