Skip to content

feat(notifications): a notice per person and a bell for every role, on desktop and phone - #624

Open
privatejoel wants to merge 1 commit into
mainfrom
feat/notice-per-person
Open

privatejoel wants to merge 1 commit into
mainfrom
feat/notice-per-person

Conversation

@privatejoel

Copy link
Copy Markdown
Collaborator

Summary

C-17 in docs/SO_EXECUTION_COHESION.md (R-F5, section 6.4). WM-7 in docs/WORK_MANAGEMENT_SCOPE.md. This PR adds no events. C-19 adds them.

  • Migration 253 adds admin_notifications.recipient_user_id uuid null, with a foreign key to auth.users(id) on delete cascade. It also adds a partial index for the unread list of one person. A null value is a broadcast row, as before. A user id is a row for that person only. The column decides who sees a row. Thus a deleted user takes their rows with them. "Set null" would change a private notice into a broadcast row that admins can read.
  • notify(svc, tenantId, payload, { users, roles, dedupKey }) in src/api/_lib/notifications.js writes one row for each recipient. A role expands to the approved members of this tenant. A user id that is not an approved member of this tenant gets no row. The function returns { notified: <rows written> }.
  • notifyAdmins does not change. It writes one broadcast row. The false header comment ("one row per recipient") is now correct: its roles decide only whether to insert, and they do not go with the row.
  • GET /api/admin/notifications shows the caller the rows addressed to them. Admin and operator also see the broadcast rows. Before this PR, only these two roles saw the bell.
  • mark_read and mark_all_read are now open to every role. They touch only the rows that the caller sees. When a person tries to mark the row of another person, the endpoint answers 404 and changes nothing. resolve stays admin only. Each update now also filters on tenant_id.
  • The bell shows for every role, in Shell and in MobileShell. The mobile header puts the bell last, so the menu, which is anchored on the right, stays on the screen.

No new endpoint, no new requireAction. The client methods (AnvilBackend.notifications.*) already existed.

Deploy notes

  • Apply migration 253 by hand after merge: supabase/migrations/253_notification_recipient.sql. It is additive and idempotent. Then reload the PostgREST schema cache.
  • Before 253 is applied (the column is missing, PostgREST error 42703 or PGRST204):
    • notify() logs a warning and skips. It returns { notified: 0, skipped: "migration_253_not_applied" }. It does not write the notice as a broadcast row. It does not try again without the column.
    • The list shows nothing to a role that is not admin or operator. Admin and operator get the list of today. This is safe because, without the column, every row is a broadcast row.
    • notifyAdmins and the current admin bell work as before.
    • No code calls notify() yet (C-19 will), so before 253 nobody loses a notice.
  • Every role now polls the bell every 30 seconds while the tab is visible. Before this PR, only admin and operator polled.

Tests

New tests (behavioural, with PostgREST stand-ins):

  • src/v3-app/api-notifications.recipient.test.js: a role notify writes one row for each approved member. Pending members, deactivated members and members of another tenant get no row. A person named twice gets one row. When the column is missing, notify skips, writes no row, and makes one insert attempt only. notifyAdmins still writes one broadcast row.
  • src/v3-app/api-admin-notifications.mark-read.recipient.test.js: a sales engineer lists only their row. An admin lists their rows and the broadcast rows, and never the row of another person. An operator lists the broadcast rows. The recipient marks their row read. The endpoint refuses mark_read of the row of another person (also for an admin). It refuses mark_read of a broadcast row by a sales engineer. mark_all_read marks only the rows of the caller. resolve stays admin only. With the column missing, a sales engineer sees and marks nothing, and the admin bell works as before.
  • src/v3-app/components/Shell.bell.roles.test.tsx: the bell shows for sales_engineer, viewer, finance and customer_support with the unread count. A click on a row calls markRead with its id.
  • src/v3-app/components/MobileShell.bell.test.tsx: the bell is in the mobile header for sales_engineer, viewer and admin. A tap on a row calls markRead with its id.

Mutation check. I broke each rule, ran its test file, saw a failure, and restored the rule. All 13 mutations made the tests fail:

# Mutation Result
M1 notify counts members that are not approved 3 failed
M2 notify drops the tenant filter on members 3 failed
M3 notify tries again without the column when it is missing 1 failed
M4 notify writes one row, not one per recipient 2 failed
M5 a role that is not admin or operator also sees broadcast rows 3 failed
M6 admin sees every row, not only own and broadcast rows 3 failed
M7 mark_all_read selects rows without the visibility rule 2 failed
M8 mark_read selects the row without the visibility rule 4 failed
M9 with the column missing, every role gets the old list 2 failed
M10 resolve is open to every role 1 failed
M11 operator no longer sees broadcast rows 1 failed
M12 Shell shows the bell for admin and operator only (the old gate) 5 failed
M13 MobileShell has no bell 4 failed

Gates:

  • npm run check: pass.
  • npm run lint: 0 errors (511 warnings, all present before this PR).
  • npm run build: pass.
  • npm run verify: pass. It rewrote docs/RBAC_AUDIT.md and docs/WRITE_PATH_AUDIT.md. These changes are not related to this PR, so I restored the files.
  • npm run eval:golden: pass (aggregate 1.000).
  • NODE_OPTIONS=--no-experimental-webstorage npx vitest run --maxWorkers=2: 630 files, 7396 tests, all pass.
  • I did not run the migration on a local Postgres (none on this machine). The CI migrations job applies it.

Not covered

  • No events. C-19 sends the notices (submit, return, approve, handoff, and others), and @mention comes with C-26.
  • No email and no second channel (C-20).
  • No user_notification_prefs. WM-7 lists it next to migration 253, but section 6.9 of the cohesion plan gives 253 only the recipient column.
  • Signup still inserts one broadcast row per admin, and each admin sees all of them. This PR does not change that writer.
  • On a broadcast row, the bell shows "read" when any person has read it, because it reads read_by.length. This is as before. A row for one person is correct, because only that person can mark it.
  • The RLS policies on admin_notifications (migration 042) key on a tenant_id JWT claim that no hook sets. Thus they give nothing to a user JWT. All access goes through the service role and this endpoint, which applies the recipient rule. This PR does not change the policies.
  • Shortcut in notify(): it reads all approved members of the tenant and filters in code. This is fine for tens of members. Filter in the query when a tenant has thousands.

🤖 Generated with Claude Code

…n desktop and phone

C-17 (WM-7, R-F5). Migration 253 adds admin_notifications.recipient_user_id.
A null value is a broadcast row, as before. A user id is a row for that
person only.

- notify(svc, tenantId, payload, { users, roles, dedupKey }) writes one row
  per recipient. A role expands to the approved members of this tenant.
- notifyAdmins does not change. It writes one broadcast row.
- GET /api/admin/notifications shows the caller their own rows. Admin and
  operator also see the broadcast rows, as before.
- mark_read and mark_all_read are open to every role. They touch only the
  rows that the caller sees. Resolve stays admin only.
- The bell shows for every role in Shell and in MobileShell.
- Before migration 253 is applied, notify skips and logs. It does not write
  a broadcast row. The list shows nothing to a role that is not admin or
  operator.
- The header comment in notifications.js said one row per recipient. That
  was false. It is correct now.

No new events. C-19 adds them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
anvil Ready Ready Preview Oct 19, 2026 3:47pm UTC

This branch was successfully deployed

1 active deployment
Preview — 4b47fcbe Deployed Oct 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant