Implement tasks 40, 41, 42: web push, notification bus, reminders engine

Task 40 — Web Push (VAPID):
- Add web-push package + @types/web-push
- pnpm vapid:generate script prints VAPID_PUBLIC_KEY, VAPID_PRIVATE_KEY, NEXT_PUBLIC_VAPID_PUBLIC_KEY
- push_subscriptions schema + migration 0013
- _core/push.ts: sendPush() iterates subscriptions, prunes 404/410 stale entries
- SW push/notificationclick event handlers added to generated sw.js template
- PushOptIn client component on /settings (opt-in, disable, send test)

Task 42 — Notification bus + ntfy adapter:
- notifications table + notif_push/notif_inapp/notif_ntfy user columns (migration 0013)
- _core/notify.ts: notify() fans out to push, in-app DB, and optional ntfy POST
- NotificationBell server component in AppNav: unread badge, dropdown inbox, mark-read
- NotifyChannelToggles client component in /settings

Task 41 — Reminders engine:
- fired_at + created_by added to reminders; default channel changed to 'auto'
- _core/reminders.ts: scheduleReminder (upsert), cancelReminder, listReminders, tickReminders
- tickReminders uses pg_try_advisory_xact_lock for horizontal-scale safety
- src/instrumentation.ts starts reminder worker (30s tick) on Node.js boot
- Notes actions use scheduleReminder/cancelReminder instead of raw SQL
- Calendar createEvent: optional remindMinutesBefore, deleteEvent: cancelReminder
- Calendar-shell: "Remind me 30 min before" checkbox on new event form

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
ginnoir
2026-05-06 16:55:02 -05:00
co-authored by Claude Sonnet 4.6
parent ec20f1bc87
commit b6abe052c9
25 changed files with 986 additions and 85 deletions
+52 -69
View File
@@ -1,12 +1,12 @@
"use server";
import { and, eq } from "drizzle-orm";
import { eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { z } from "zod";
import { db } from "@/lib/db";
import { getCurrentSession } from "@/lib/session";
import { logActivity } from "@/modules/_core/activity";
import { reminders } from "@/modules/_core/schema";
import { scheduleReminder, cancelReminder } from "@/modules/_core/reminders";
import { notes } from "../schema";
import { canAccessNote, getNote } from "./queries";
@@ -29,63 +29,69 @@ export async function createNote(input: z.input<typeof noteInput>) {
const parsed = noteInput.parse(input);
const { household, user } = await getCurrentSession();
const [note] = await db.transaction(async (tx) => {
const [created] = await tx
.insert(notes)
.values({
householdId: household.id,
authorId: user.id,
title: parsed.title,
body: parsed.body,
pinned: parsed.pinned,
remindAt: parsed.remindAt ?? null,
})
.returning();
if (!created) throw new Error("Note was not created");
await syncNoteReminder(tx, {
const [note] = await db
.insert(notes)
.values({
householdId: household.id,
noteId: created.id,
remindAt: created.remindAt,
});
return [created];
});
authorId: user.id,
title: parsed.title,
body: parsed.body,
pinned: parsed.pinned,
remindAt: parsed.remindAt ?? null,
})
.returning();
if (note) await logActivity({ entityType: "notes.note", entityId: note.id, action: "create", payload: { title: note.title } });
if (!note) throw new Error("Note was not created");
if (parsed.remindAt) {
await scheduleReminder({
householdId: household.id,
entityType: "notes.note",
entityId: note.id,
fireAt: parsed.remindAt,
createdBy: user.id,
});
}
await logActivity({ entityType: "notes.note", entityId: note.id, action: "create", payload: { title: note.title } });
revalidatePath("/notes");
return note;
}
export async function updateNote(input: z.input<typeof updateNoteInput>) {
const parsed = updateNoteInput.parse(input);
const { household } = await getCurrentSession();
const { household, user } = await getCurrentSession();
await assertCanAccessNote(parsed.id, household.id);
const [note] = await db.transaction(async (tx) => {
const [updated] = await tx
.update(notes)
.set({
title: parsed.title,
body: parsed.body,
pinned: parsed.pinned,
remindAt: parsed.remindAt === undefined ? undefined : parsed.remindAt,
updatedAt: new Date(),
})
.where(eq(notes.id, parsed.id))
.returning();
const [note] = await db
.update(notes)
.set({
title: parsed.title,
body: parsed.body,
pinned: parsed.pinned,
remindAt: parsed.remindAt === undefined ? undefined : parsed.remindAt,
updatedAt: new Date(),
})
.where(eq(notes.id, parsed.id))
.returning();
if (!updated) throw new Error("Note was not updated");
if (parsed.remindAt !== undefined) {
await syncNoteReminder(tx, {
if (!note) throw new Error("Note was not updated");
if (parsed.remindAt !== undefined) {
if (parsed.remindAt) {
await scheduleReminder({
householdId: household.id,
noteId: updated.id,
remindAt: updated.remindAt,
entityType: "notes.note",
entityId: note.id,
fireAt: parsed.remindAt,
createdBy: user.id,
});
} else {
await cancelReminder("notes.note", note.id);
}
return [updated];
});
}
if (note) await logActivity({ entityType: "notes.note", entityId: note.id, action: "update", payload: { title: note.title } });
await logActivity({ entityType: "notes.note", entityId: note.id, action: "update", payload: { title: note.title } });
revalidatePath("/notes");
revalidatePath(`/notes/${parsed.id}`);
return note;
@@ -116,12 +122,8 @@ export async function deleteNote(input: { id: string }) {
const note = await getNote(parsed.id);
await logActivity({ entityType: "notes.note", entityId: parsed.id, action: "delete", payload: note ? { title: note.title } : undefined });
await db.transaction(async (tx) => {
await tx
.delete(reminders)
.where(and(eq(reminders.entityType, "notes.note"), eq(reminders.entityId, parsed.id)));
await tx.delete(notes).where(eq(notes.id, parsed.id));
});
await cancelReminder("notes.note", parsed.id);
await db.delete(notes).where(eq(notes.id, parsed.id));
revalidatePath("/notes");
}
@@ -129,22 +131,3 @@ export async function deleteNote(input: { id: string }) {
async function assertCanAccessNote(noteId: string, householdId: string) {
if (!(await canAccessNote(noteId, householdId))) throw new Error("Forbidden");
}
async function syncNoteReminder(
tx: Parameters<Parameters<typeof db.transaction>[0]>[0],
input: { householdId: string; noteId: string; remindAt: Date | null },
) {
await tx
.delete(reminders)
.where(and(eq(reminders.entityType, "notes.note"), eq(reminders.entityId, input.noteId)));
if (!input.remindAt) return;
await tx.insert(reminders).values({
householdId: input.householdId,
entityType: "notes.note",
entityId: input.noteId,
fireAt: input.remindAt,
channel: "in_app",
});
}