|
| 1 | +-- Phase 16 (part 1): an in-app notification inbox filled by a scheduled database job. No keys or external services involved. |
| 2 | +-- Users can read their notifications and mark them read (only the read_at column); they cannot create, edit or delete them. |
| 3 | +-- Rows are created only by public.generate_notifications(), which runs as the database owner from pg_cron and is not callable |
| 4 | +-- through the API. Each notification has a dedupe key so repeated runs never create duplicates. |
| 5 | + |
| 6 | +create table public.notifications ( |
| 7 | + id uuid primary key default gen_random_uuid(), |
| 8 | + user_id uuid not null references auth.users (id) on delete cascade, |
| 9 | + kind text not null check (kind in ('date','application','venture_task','puzzles','reminder')), |
| 10 | + title text not null check (char_length(title) between 1 and 200), |
| 11 | + body text not null default '' check (char_length(body) <= 500), |
| 12 | + link text check (link is null or link ~ '^/[A-Za-z0-9/_-]*$'), |
| 13 | + dedupe_key text not null check (char_length(dedupe_key) <= 200), |
| 14 | + read_at timestamptz, |
| 15 | + created_at timestamptz not null default now(), |
| 16 | + unique (user_id, dedupe_key) |
| 17 | +); |
| 18 | +create index notifications_inbox_idx on public.notifications (user_id, created_at desc); |
| 19 | + |
| 20 | +alter table public.notifications enable row level security; |
| 21 | +create policy notifications_select on public.notifications for select to authenticated using (user_id = (select auth.uid())); |
| 22 | +create policy notifications_update on public.notifications for update to authenticated |
| 23 | + using (user_id = (select auth.uid())) with check (user_id = (select auth.uid())); |
| 24 | +revoke update on public.notifications from authenticated, anon; |
| 25 | +grant update (read_at) on public.notifications to authenticated; |
| 26 | + |
| 27 | +create or replace function public.generate_notifications() |
| 28 | +returns int |
| 29 | +language plpgsql |
| 30 | +security definer |
| 31 | +set search_path = '' |
| 32 | +as $$ |
| 33 | +declare |
| 34 | + n int := 0; |
| 35 | + c int; |
| 36 | +begin |
| 37 | + drop table if exists pg_temp._u; |
| 38 | + create temporary table pg_temp._u on commit drop as |
| 39 | + select au.id as user_id, |
| 40 | + (now() at time zone case when exists (select 1 from pg_catalog.pg_timezone_names z where z.name = s.timezone) |
| 41 | + then s.timezone else 'UTC' end)::date as today |
| 42 | + from auth.users au |
| 43 | + left join public.user_settings s on s.user_id = au.id; |
| 44 | + |
| 45 | + -- Important dates in the next 3 days (29 February is observed on 28 February in non-leap years). |
| 46 | + insert into public.notifications (user_id, kind, title, body, link, dedupe_key) |
| 47 | + select d.user_id, 'date', |
| 48 | + d.title || case t.off when 0 then ' is today' when 1 then ' is tomorrow' else ' is in ' || t.off || ' days' end, |
| 49 | + d.kind, '/personal', 'date:' || d.id || ':' || (u.today + t.off) |
| 50 | + from public.important_dates d |
| 51 | + join pg_temp._u u on u.user_id = d.user_id |
| 52 | + cross join generate_series(0, 3) as t(off) |
| 53 | + where d.deleted_at is null |
| 54 | + and ( |
| 55 | + (d.month = extract(month from u.today + t.off) and d.day = extract(day from u.today + t.off)) |
| 56 | + or (d.month = 2 and d.day = 29 and extract(month from u.today + t.off) = 2 and extract(day from u.today + t.off) = 28 |
| 57 | + and not ((extract(year from u.today + t.off)::int % 4 = 0 and extract(year from u.today + t.off)::int % 100 <> 0) |
| 58 | + or extract(year from u.today + t.off)::int % 400 = 0)) |
| 59 | + ) |
| 60 | + on conflict (user_id, dedupe_key) do nothing; |
| 61 | + get diagnostics c = row_count; n := n + c; |
| 62 | + |
| 63 | + -- Job applications still on the wishlist whose deadline is within 3 days. |
| 64 | + insert into public.notifications (user_id, kind, title, body, link, dedupe_key) |
| 65 | + select a.user_id, 'application', 'Deadline: ' || a.company || ' (' || a.role || ')', |
| 66 | + 'Due ' || a.deadline, '/career', 'application:' || a.id || ':' || a.deadline |
| 67 | + from public.job_applications a |
| 68 | + join pg_temp._u u on u.user_id = a.user_id |
| 69 | + where a.deleted_at is null and a.status = 'wishlist' |
| 70 | + and a.deadline between u.today and u.today + 3 |
| 71 | + on conflict (user_id, dedupe_key) do nothing; |
| 72 | + get diagnostics c = row_count; n := n + c; |
| 73 | + |
| 74 | + -- Venture tasks due tomorrow or earlier (up to 7 days overdue) and not done. |
| 75 | + insert into public.notifications (user_id, kind, title, body, link, dedupe_key) |
| 76 | + select t.user_id, 'venture_task', 'Task due: ' || t.title, 'Due ' || t.due_on, '/business/' || t.venture_id, |
| 77 | + 'vtask:' || t.id || ':' || t.due_on |
| 78 | + from public.venture_tasks t |
| 79 | + join pg_temp._u u on u.user_id = t.user_id |
| 80 | + where t.deleted_at is null and t.status <> 'done' |
| 81 | + and t.due_on between u.today - 7 and u.today + 1 |
| 82 | + on conflict (user_id, dedupe_key) do nothing; |
| 83 | + get diagnostics c = row_count; n := n + c; |
| 84 | + |
| 85 | + -- Chess puzzles due for review: one notification per user per day. |
| 86 | + insert into public.notifications (user_id, kind, title, body, link, dedupe_key) |
| 87 | + select p.user_id, 'puzzles', p.cnt || (case when p.cnt = 1 then ' puzzle is' else ' puzzles are' end) || ' due for review', |
| 88 | + '', '/chess/train', 'puzzles:' || u.today |
| 89 | + from (select user_id, count(*) as cnt from public.chess_puzzles |
| 90 | + where deleted_at is null and next_due_at <= now() group by user_id) p |
| 91 | + join pg_temp._u u on u.user_id = p.user_id |
| 92 | + on conflict (user_id, dedupe_key) do nothing; |
| 93 | + get diagnostics c = row_count; n := n + c; |
| 94 | + |
| 95 | + -- Reminders whose time has come: notify once and mark them sent. |
| 96 | + insert into public.notifications (user_id, kind, title, body, link, dedupe_key) |
| 97 | + select r.user_id, 'reminder', left(r.message, 200), '', '/', 'reminder:' || r.id |
| 98 | + from public.reminders r |
| 99 | + where r.remind_at <= now() and r.sent_at is null and r.dismissed_at is null |
| 100 | + on conflict (user_id, dedupe_key) do nothing; |
| 101 | + get diagnostics c = row_count; n := n + c; |
| 102 | + update public.reminders set sent_at = now() |
| 103 | + where remind_at <= now() and sent_at is null and dismissed_at is null; |
| 104 | + |
| 105 | + return n; |
| 106 | +end $$; |
| 107 | + |
| 108 | +revoke all on function public.generate_notifications() from public, anon, authenticated; |
| 109 | + |
| 110 | +insert into public.module_registry (id, name, description, version, status, nav_label, nav_href, nav_icon, nav_order) values |
| 111 | + ('notifications', 'Notifications', 'Inbox for deadlines, dates and reminders', '0.1.0', 'active', 'Notifications', '/notifications', 'bell', 5); |
| 112 | + |
| 113 | +insert into public.workspace_modules (workspace_id, module_id) |
| 114 | +select w.id, 'notifications' from public.workspaces w |
| 115 | +on conflict do nothing; |
0 commit comments