[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M7BE68MJ8H47JZKTBDFYR6JD":3,"skill-same:01M7BE68MJ8H47JZKTBDFYR6JD":48},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":24,"commitSha":25,"skillDirectory":26,"forksCount":27,"repositoryHtmlUrl":28,"repositoryHomepageUrl":29,"ownerHtmlUrl":30,"ownerBlogUrl":31,"ownerDescription":32,"updatedAt":33,"install":34,"resources":47},"01M7BE68MJ8H47JZKTBDFYR6JD","saasaloy-notification",null,"Runbook for the notification module, in-app notifications for apps\u002Fapp. It owns the notifications and notification_opt_outs tables, notify() in apps\u002Fapi\u002Fsrc\u002Flib\u002Fnotify.ts, the \u002Fnotifications api routes, the bell in the app header, the sidebar badge, the \u002Fnotifications page with Inbox and Preferences tabs, the notification config section, and the daily prune. Use when sending a notification from an api route, adding or changing a category, changing retention, debugging a badge or bell count that does not move, or removing the module.","mimukit\u002Fsaasaloy","mimukit","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F11677026?v=4","01M79GFSERAQQ77QPQMC1N0Y6M","Mukitul Islam Mukit","01M79GFSFA4WMGEEBHA68N59WV",4,"MIT","https:\u002F\u002Fgithub.com\u002Fmimukit\u002Fsaasaloy\u002Fblob\u002Fmain\u002Fmodules\u002Fnotification\u002Fskills\u002Fsaasaloy-notification\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"---\nname: saasaloy-notification\ndescription: Runbook for the notification module, in-app notifications for apps\u002Fapp. It owns the notifications and notification_opt_outs tables, notify() in apps\u002Fapi\u002Fsrc\u002Flib\u002Fnotify.ts, the \u002Fnotifications api routes, the bell in the app header, the sidebar badge, the \u002Fnotifications page with Inbox and Preferences tabs, the notification config section, and the daily prune. Use when sending a notification from an api route, adding or changing a category, changing retention, debugging a badge or bell count that does not move, or removing the module.\n---\n\n# notification: in-app notifications for apps\u002Fapp\n\n`notification` is a **feature module** (`saasaloy:feature`). It depends on `api`, `database`, `auth`, `app`, `validators` and `shell`, which `add` resolves and installs first.\n\nA signed-in user gets a bell in the `apps\u002Fapp` header with an unread badge and a menu of the newest rows, a Notifications row in the sidebar with the same count, and a `\u002Fnotifications` page with two tabs. Inbox lists every row with \"Load more\" and \"Mark all read\". Preferences shows one switch per category. Your api routes write a row with one call, `notify(db, { ... })`. Nothing is sent by email, SMS or push, and nothing is real time: the count refetches on a timer and on focus.\n\n## What it installs\n\n| File | What it holds |\n|---|---|\n| `packages\u002Fconfig\u002Fsrc\u002Fsections\u002Fnotification.ts` | the `notification` config section: the categories, the menu and page sizes, and the two retention periods |\n| `packages\u002Fdb\u002Fsrc\u002Fschema\u002Fnotifications.ts` | `notifications` and `notification_opt_outs`, in the dialect of the installed driver (`notifications.sqlite.ts` under `database-d1`, `notifications.pg.ts` under `database-postgres`) |\n| `packages\u002Fdb\u002Fsrc\u002Frepositories\u002Fnotifications.ts` | every query the two tables take, `pruneNotifications` included |\n| `packages\u002Fdb\u002Fsrc\u002Fseed\u002Fslices\u002Fnotifications.ts`, `notification-opt-outs.ts` | seed rows for `pnpm db:seed` |\n| `packages\u002Fvalidators\u002Fsrc\u002Fnotifications.ts` | the list query, the id param and the preferences body |\n| `apps\u002Fapi\u002Fsrc\u002Flib\u002Fnotify.ts` | `notify()`, the one function your routes call |\n| `apps\u002Fapi\u002Fsrc\u002Flib\u002Fnotification-href.ts` | `isAppPath()`, the `href` rule |\n| `apps\u002Fapi\u002Fsrc\u002Froutes\u002Fnotifications.ts` | the six routes under `\u002Fnotifications` |\n| `apps\u002Fapi\u002Fsrc\u002Flib\u002Fnotify.test.ts`, `apps\u002Fapi\u002Fsrc\u002Froutes\u002Fnotifications.test.ts` | tests, only while the testing mode is on |\n| `apps\u002Fapi\u002Fsrc\u002Flib\u002Fnotification-prune.ts` | the prune runner, only with `queue` installed |\n| `packages\u002Fqueue\u002Fsrc\u002Fjobs\u002Fnotifications.ts` | the prune job and its daily schedule, only with `queue` installed |\n| `packages\u002Fui\u002Fsrc\u002Flib\u002Fnotification\u002Fformat.ts` | `badgeText()` (`99+` above 99) and `relativeTime()` |\n| `packages\u002Fui\u002Fsrc\u002Fblocks\u002Fnotification\u002Fnotification-menu.tsx` | `NotificationMenu`: the bell, the badge and the menu |\n| `packages\u002Fui\u002Fsrc\u002Fblocks\u002Fnotification\u002Fnotification-list.tsx` | `NotificationList`: the Inbox tab |\n| `packages\u002Fui\u002Fsrc\u002Fblocks\u002Fnotification\u002Fnotification-preferences.tsx` | `NotificationPreferences`: the Preferences tab |\n| `apps\u002Fapp\u002Fsrc\u002Flib\u002Fnotifications.ts` | the query keys, `useUnreadCount`, `useMarkAllRead` and `useOpenNotification` |\n| `apps\u002Fapp\u002Fsrc\u002Fcomponents\u002Fnotification-extension.tsx` | `notificationExtension()`, the bell in the shell header |\n| `apps\u002Fapp\u002Fsrc\u002Froutes\u002Fnotifications.tsx` | the `\u002Fnotifications` page |\n\nThe three blocks are presentational. They take data and callbacks as props and import no api client. The two app files supply the behaviour.\n\n## The tables\n\n`notifications` holds one row per message: `id`, `user_id` (cascades when the user is deleted), `category`, `title`, `body`, `href`, `created_at` and `read_at`. `read_at` is null while the row is unread, and a second \"mark read\" keeps the first time. Two indexes serve the reads: `notifications_user_id_created_at_idx` for the list and `notifications_user_id_read_at_idx` for the count.\n\n`notification_opt_outs` holds one row per category a user turned off, keyed on `(user_id, category)`. No row means the category is on, so a new category starts on for every user with no backfill.\n\nEvery query in `packages\u002Fdb\u002Fsrc\u002Frepositories\u002Fnotifications.ts` filters on the session user's id. That filter is the only boundary between two users' rows. Do not write a raw `db.select().from(notifications)` in a route. `pruneNotifications` is the one query across users, and only the scheduled job calls it.\n\n## Send a notification from a route\n\nCall `notify` inside `withDb`, with the recipient's id:\n\n```ts\nimport { withDb } from \"@repo\u002Fdb\u002Fclient\";\nimport { notify } from \"..\u002Flib\u002Fnotify\";\n\nawait withDb(c, (db) =>\n  notify(db, {\n    userId: session.user.id,\n    category: \"account\",\n    title: \"Your password was changed\",\n    body: \"If this was not you, reset it now.\",\n    href: \"\u002Fsettings\",\n  })\n);\n```\n\nThe input:\n\n| Field | Rule |\n|---|---|\n| `userId` | the user who reads it. Always the recipient, never the actor. |\n| `category` | an `id` from `config.notification.categories`. An unknown id throws, and the message lists the known ids. |\n| `title` | the bold line. It is trimmed, and a blank title throws. |\n| `body` | optional second line. A blank body is stored as null. |\n| `href` | optional app path the row opens. It must start with `\u002F` and not with `\u002F\u002F`, and it must not hold a `\\`. A full URL throws. |\n\n`notify` stores the text as you pass it. Write the final words the user reads. The page never rewords an old row, and nothing translates it.\n\n`notify` answers `{ written: true, id }`, or `{ written: false, reason: \"opted_out\" }` when the user turned the category off. An opt-out skips the write. It does not throw. A category with `required: true` is always written, whatever the user's opt-out rows say.\n\nEvery throw is a bug in the caller, so a test catches it. Do not wrap `notify` in a `try` that hides the error.\n\nA row written for the user who made the request shows on the next count fetch, at most 60 s later. To show it at once, invalidate the count in the app after the mutation that caused it (see [The count](#the-count)).\n\n## Add or change a category\n\nThe defaults live in `packages\u002Fconfig\u002Fsrc\u002Fsections\u002Fnotification.ts`, and `saasaloy update` may rewrite that file. Change them in `packages\u002Fconfig\u002Fsrc\u002Fproject.ts`, which is yours:\n\n```ts\nexport const project: ProjectOverride = {\n  notification: {\n    categories: [\n      { id: \"account\", label: \"Account\", description: \"Sign-in, security, billing and membership changes.\", required: true },\n      { id: \"product\", label: \"Product\", description: \"New features, finished exports and project activity.\", required: false },\n      { id: \"comments\", label: \"Comments\", description: \"Replies to your comments.\", required: false },\n    ],\n  },\n};\n```\n\nThe rules:\n\n- An override replaces the `categories` list whole. Name `account` and `product` again if you keep them.\n- `id` is stored on every row. Never rename an `id` that is in use. Add a new one instead.\n- `label` and `description` are the switch's text on the Preferences tab.\n- `required: true` makes the switch on and disabled, with \"Required. These cannot be turned off.\" `notify` writes the category for every user, and `PUT \u002Fnotifications\u002Fpreferences` answers 400 to a body that turns it off.\n- A removed category keeps its old rows. The page still lists them under the raw id, and the count still counts them until the prune deletes them.\n\nThe same section holds `menuSize` (5 rows under the bell), `pageSize` (20 rows a page), `readRetentionDays` (30) and `unreadRetentionDays` (180). Override them in `project.ts` the same way.\n\n## The api routes\n\nThe `chained-route` patch mounts the sub-app at `\u002Fnotifications`. Every route calls `requireSession`.\n\n| Route | Answer |\n|---|---|\n| `GET \u002Fnotifications?cursor=` | one page, newest first: `{ notifications, nextCursor }`. `nextCursor` is null on the last page. |\n| `GET \u002Fnotifications\u002Funread-count` | `{ count }`, the shape `NAV_COUNTS` reads |\n| `POST \u002Fnotifications\u002Fread-all` | `{ ok, marked }` |\n| `POST \u002Fnotifications\u002F:id\u002Fread` | `{ ok }`. Another user's row answers the same 404 as a missing row. |\n| `GET \u002Fnotifications\u002Fpreferences` | `{ categories: [{ id, label, description, required, enabled }] }` |\n| `PUT \u002Fnotifications\u002Fpreferences` | body `{ categories: { [id]: boolean } }`. An unknown id or a required category turned off answers 400 and writes nothing. |\n\n## The count\n\nThe count is every unread row of the user, in every category. An opt-out stops new rows only. Rows written before the opt-out stay unread and stay in the count until the user reads them or the prune deletes them.\n\nThe bell and the sidebar badge read one query key, `[\"nav-count\", \"\u002Fnotifications\"]`. The module's `NAV_COUNTS` entry makes `AppShell` in `apps\u002Fapp\u002Fsrc\u002Fcomponents\u002Fapp-shell.tsx` the one observer that fetches it. It refetches every 60 s while the tab is visible, on every window focus, and on a `pageshow` with `persisted` set (a back-forward cache restore). `useUnreadCount()` in `apps\u002Fapp\u002Fsrc\u002Flib\u002Fnotifications.ts` reads the same key with `enabled: false` and no `queryFn`, so the bell and the badge always show the same number. Do not give it a `queryFn` or `skipToken`: the last observer's options become the query's own, and an invalidation then fetches nothing.\n\nA failed fetch keeps the last count. A network error, a non-OK answer and a body without a numeric `count` all throw, and TanStack Query keeps the last `data`. The badge never drops to 0 because of a failure. While the menu is open, a failed row fetch shows \"Could not load notifications\" with \"Try again\".\n\nA mutation that changes the count must invalidate the key, or the badge waits for the next interval:\n\n```ts\nawait queryClient.invalidateQueries({ queryKey: [\"nav-count\", \"\u002Fnotifications\"] });\n```\n\nThe module's own mark-read and mark-all-read invalidate that key and the whole `[\"notifications\"]` prefix, which covers the menu rows (`[\"notifications\", \"menu\"]`), the Inbox pages (`[\"notifications\", \"list\"]`) and the preferences (`[\"notifications\", \"preferences\"]`). Use `UNREAD_COUNT_KEY` and `NOTIFICATIONS_KEY` from `@app\u002Flib\u002Fnotifications` rather than a copy of the array.\n\n## Retention\n\n`pruneNotifications(db, now, days?)` in `packages\u002Fdb\u002Fsrc\u002Frepositories\u002Fnotifications.ts` deletes, for every user, each read row written more than `readRetentionDays` ago and each row written more than `unreadRetentionDays` ago. It answers how many rows went. `days` defaults to the config section.\n\n**With `queue` installed**, the module registers the job `notification.prune` and the schedule `notification.prune.daily`, cron `30 3 * * *` (03:30 UTC daily). The job runs through `apps\u002Fapi\u002Fsrc\u002Flib\u002Fnotification-prune.ts`, which `worker.ts` lists as `notificationPruneHandlers()` in its `handlers` table. A run before that registration throws, so the message retries and lands in the dead-letter queue. It does not report a prune that did not run.\n\n**Without `queue`**, nothing prunes and the table grows. Call the function from your own scheduled handler:\n\n```ts\nimport { withDb } from \"@repo\u002Fdb\u002Fclient\";\nimport { pruneNotifications } from \"@repo\u002Fdb\u002Frepositories\u002Fnotifications\";\n\nawait withDb(scope, (db) => pruneNotifications(db, new Date()));\n```\n\nAdd `queue` later and `saasaloy add queue` backfills the two prune files and the three queue patches. `saasaloy remove queue` takes them out again and leaves the rest of this module.\n\n## Wire-up\n\nNo manual page edit is needed. The module's own app files import the three blocks: `notification-extension.tsx` renders `NotificationMenu` in the shell header, and `routes\u002Fnotifications.tsx` renders `NotificationList` and `NotificationPreferences`. `saasaloy add` still prints \"Manual wire-up needed ... on no page yet\" for the three `@ui\u002Fblocks\u002Fnotification\u002F` files, because it flags every block target. For this module that line is wrong. Ignore it.\n\nThe patches did the wiring:\n\n| Patch | File | Effect |\n|---|---|---|\n| `plugin-array` | `apps\u002Fapp\u002Fsrc\u002Fcomponents\u002Fshell-extensions.ts` | adds `notificationExtension()` to `extensions`, so the bell renders in the header |\n| `nav-entry` | `apps\u002Fapp\u002Fsrc\u002Fcomponents\u002Fnav.ts` | adds Notifications (`BellIcon`, shortcut `G N`) to the Workspace group of the `\u002F` area in `NAV_AREAS` |\n| `const-array` | `apps\u002Fapp\u002Fsrc\u002Fcomponents\u002Fnav-counts.ts` | adds `{ to: \"\u002Fnotifications\", path: \"\u002Fnotifications\u002Funread-count\" }` to `NAV_COUNTS`, so the row shows the live count |\n| `chained-route` | `apps\u002Fapi\u002Fsrc\u002Findex.ts` | mounts `notifications` at `\u002Fnotifications` |\n| `plugin-array` | `packages\u002Fconfig\u002Fsrc\u002Fsections.ts` | adds `notificationConfig()` to `sections` |\n| `plugin-array` | `packages\u002Fqueue\u002Fsrc\u002Findex.ts` | adds `pruneNotificationsJob()` to `jobs` and `pruneNotificationsSchedule()` to `schedules`, only with `queue` |\n| `plugin-array` | `apps\u002Fapi\u002Fsrc\u002Fworker.ts` | adds `notificationPruneHandlers()` to `handlers`, only with `queue` |\n\nAfter `add`:\n\n1. Run `pnpm db:generate` to write the migration for the two tables.\n2. Apply the migration with your driver's command.\n3. Run `pnpm db:seed` if you want the seed rows.\n4. Run `pnpm dev`, sign in to `app`, and open `\u002Fnotifications`. The router regenerates `routeTree.gen.ts` on the first start.\n\nThe seed does not reset `read_at` on a seed row. A seed row you marked read stays read after `pnpm db:seed`. For a clean start, run `pnpm db:drop`, then `pnpm db:setup`, then `pnpm db:seed`.\n\n## Troubleshooting\n\n| Symptom | Cause |\n|---|---|\n| `notify: unknown category \"x\"` | `x` is not in `config.notification.categories`. Add it in `project.ts`, or fix the typo. |\n| `notify: href \"...\" is not an app-relative path` | `href` is a full URL, starts with `\u002F\u002F`, or holds a `\\`. Pass a path such as `\u002Fsettings`. |\n| `notify` answers `written: false` | the user turned that category off. Make the category `required` if it must always arrive. |\n| The badge stays after a mark-read in your own screen | the screen does not invalidate `[\"nav-count\", \"\u002Fnotifications\"]` after its mutation. |\n| The bell and the badge show the old number while the api is down | expected. A failed fetch keeps the last count. |\n| The count includes rows of a category the user turned off | expected. An opt-out stops new rows only. |\n| `notification.prune ran before apps\u002Fapi registered its runner` | `apps\u002Fapi\u002Fsrc\u002Fworker.ts` lost `notificationPruneHandlers()` from its `handlers`. |\n| The table grows with no limit | `queue` is not installed, so nothing calls `pruneNotifications`. See [Retention](#retention). |\n\n## What `remove` leaves behind\n\n`saasaloy remove notification` deletes the files it wrote and reverses its patches: the shell extension, the nav row, the `NAV_COUNTS` entry, the api mount, the config section, and the queue and worker entries when they were applied. It leaves these for you:\n\n- The `notifications` and `notification_opt_outs` tables. Run `pnpm db:generate`, read the drop migration, and apply it only when you want the rows gone.\n- Every `notify()` call you added to your own routes. Its import of `..\u002Flib\u002Fnotify` breaks the build. Search `apps\u002Fapi` for `lib\u002Fnotify` before you remove the module.\n- Every override you wrote under `notification` in `packages\u002Fconfig\u002Fsrc\u002Fproject.ts`. With the section gone, that key fails `pnpm typecheck`, so take it out.\n- Any import of `@app\u002Flib\u002Fnotifications` or a notification block that you added to your own screens.\n\n## Next\n\nRun `saasaloy-app` to change the Notifications page or to add another screen beside it, and `saasaloy-queue` to change the prune schedule.\n","# notification: in-app notifications for apps\u002Fapp\n\n`notification` is a **feature module** (`saasaloy:feature`). It depends on `api`, `database`, `auth`, `app`, `validators` and `shell`, which `add` resolves and installs first.\n\nA signed-in user gets a bell in the `apps\u002Fapp` header with an unread badge and a menu of the newest rows, a Notifications row in the sidebar with the same count, and a `\u002Fnotifications` page with two tabs. Inbox lists every row with \"Load more\" and \"Mark all read\". Preferences shows one switch per category. Your api routes write a row with one call, `notify(db, { ... })`. Nothing is sent by email, SMS or push, and nothing is real time: the count refetches on a timer and on focus.\n\n## What it installs\n\n| File | What it holds |\n|---|---|\n| `packages\u002Fconfig\u002Fsrc\u002Fsections\u002Fnotification.ts` | the `notification` config section: the categories, the menu and page sizes, and the two retention periods |\n| `packages\u002Fdb\u002Fsrc\u002Fschema\u002Fnotifications.ts` | `notifications` and `notification_opt_outs`, in the dialect of the installed driver (`notifications.sqlite.ts` under `database-d1`, `notifications.pg.ts` under `database-postgres`) |\n| `packages\u002Fdb\u002Fsrc\u002Frepositories\u002Fnotifications.ts` | every query the two tables take, `pruneNotifications` included |\n| `packages\u002Fdb\u002Fsrc\u002Fseed\u002Fslices\u002Fnotifications.ts`, `notification-opt-outs.ts` | seed rows for `pnpm db:seed` |\n| `packages\u002Fvalidators\u002Fsrc\u002Fnotifications.ts` | the list query, the id param and the preferences body |\n| `apps\u002Fapi\u002Fsrc\u002Flib\u002Fnotify.ts` | `notify()`, the one function your routes call |\n| `apps\u002Fapi\u002Fsrc\u002Flib\u002Fnotification-href.ts` | `isAppPath()`, the `href` rule |\n| `apps\u002Fapi\u002Fsrc\u002Froutes\u002Fnotifications.ts` | the six routes under `\u002Fnotifications` |\n| `apps\u002Fapi\u002Fsrc\u002Flib\u002Fnotify.test.ts`, `apps\u002Fapi\u002Fsrc\u002Froutes\u002Fnotifications.test.ts` | tests, only while the testing mode is on |\n| `apps\u002Fapi\u002Fsrc\u002Flib\u002Fnotification-prune.ts` | the prune runner, only with `queue` installed |\n| `packages\u002Fqueue\u002Fsrc\u002Fjobs\u002Fnotifications.ts` | the prune job and its daily schedule, only with `queue` installed |\n| `packages\u002Fui\u002Fsrc\u002Flib\u002Fnotification\u002Fformat.ts` | `badgeText()` (`99+` above 99) and `relativeTime()` |\n| `packages\u002Fui\u002Fsrc\u002Fblocks\u002Fnotification\u002Fnotification-menu.tsx` | `NotificationMenu`: the bell, the badge and the menu |\n| `packages\u002Fui\u002Fsrc\u002Fblocks\u002Fnotification\u002Fnotification-list.tsx` | `NotificationList`: the Inbox tab |\n| `packages\u002Fui\u002Fsrc\u002Fblocks\u002Fnotification\u002Fnotification-preferences.tsx` | `NotificationPreferences`: the Preferences tab |\n| `apps\u002Fapp\u002Fsrc\u002Flib\u002Fnotifications.ts` | the query keys, `useUnreadCount`, `useMarkAllRead` and `useOpenNotification` |\n| `apps\u002Fapp\u002Fsrc\u002Fcomponents\u002Fnotification-extension.tsx` | `notificationExtension()`, the bell in the shell header |\n| `apps\u002Fapp\u002Fsrc\u002Froutes\u002Fnotifications.tsx` | the `\u002Fnotifications` page |\n\nThe three blocks are presentational. They take data and callbacks as props and import no api client. The two app files supply the behaviour.\n\n## The tables\n\n`notifications` holds one row per message: `id`, `user_id` (cascades when the user is deleted), `category`, `title`, `body`, `href`, `created_at` and `read_at`. `read_at` is null while the row is unread, and a second \"mark read\" keeps the first time. Two indexes serve the reads: `notifications_user_id_created_at_idx` for the list and `notifications_user_id_read_at_idx` for the count.\n\n`notification_opt_outs` holds one row per category a user turned off, keyed on `(user_id, category)`. No row means the category is on, so a new category starts on for every user with no backfill.\n\nEvery query in `packages\u002Fdb\u002Fsrc\u002Frepositories\u002Fnotifications.ts` filters on the session user's id. That filter is the only boundary between two users' rows. Do not write a raw `db.select().from(notifications)` in a route. `pruneNotifications` is the one query across users, and only the scheduled job calls it.\n\n## Send a notification from a route\n\nCall `notify` inside `withDb`, with the recipient's id:\n\n```ts\nimport { withDb } from \"@repo\u002Fdb\u002Fclient\";\nimport { notify } from \"..\u002Flib\u002Fnotify\";\n\nawait withDb(c, (db) =>\n  notify(db, {\n    userId: session.user.id,\n    category: \"account\",\n    title: \"Your password was changed\",\n    body: \"If this was not you, reset it now.\",\n    href: \"\u002Fsettings\",\n  })\n);\n```\n\nThe input:\n\n| Field | Rule |\n|---|---|\n| `userId` | the user who reads it. Always the recipient, never the actor. |\n| `category` | an `id` from `config.notification.categories`. An unknown id throws, and the message lists the known ids. |\n| `title` | the bold line. It is trimmed, and a blank title throws. |\n| `body` | optional second line. A blank body is stored as null. |\n| `href` | optional app path the row opens. It must start with `\u002F` and not with `\u002F\u002F`, and it must not hold a `\\`. A full URL throws. |\n\n`notify` stores the text as you pass it. Write the final words the user reads. The page never rewords an old row, and nothing translates it.\n\n`notify` answers `{ written: true, id }`, or `{ written: false, reason: \"opted_out\" }` when the user turned the category off. An opt-out skips the write. It does not throw. A category with `required: true` is always written, whatever the user's opt-out rows say.\n\nEvery throw is a bug in the caller, so a test catches it. Do not wrap `notify` in a `try` that hides the error.\n\nA row written for the user who made the request shows on the next count fetch, at most 60 s later. To show it at once, invalidate the count in the app after the mutation that caused it (see [The count](#the-count)).\n\n## Add or change a category\n\nThe defaults live in `packages\u002Fconfig\u002Fsrc\u002Fsections\u002Fnotification.ts`, and `saasaloy update` may rewrite that file. Change them in `packages\u002Fconfig\u002Fsrc\u002Fproject.ts`, which is yours:\n\n```ts\nexport const project: ProjectOverride = {\n  notification: {\n    categories: [\n      { id: \"account\", label: \"Account\", description: \"Sign-in, security, billing and membership changes.\", required: true },\n      { id: \"product\", label: \"Product\", description: \"New features, finished exports and project activity.\", required: false },\n      { id: \"comments\", label: \"Comments\", description: \"Replies to your comments.\", required: false },\n    ],\n  },\n};\n```\n\nThe rules:\n\n- An override replaces the `categories` list whole. Name `account` and `product` again if you keep them.\n- `id` is stored on every row. Never rename an `id` that is in use. Add a new one instead.\n- `label` and `description` are the switch's text on the Preferences tab.\n- `required: true` makes the switch on and disabled, with \"Required. These cannot be turned off.\" `notify` writes the category for every user, and `PUT \u002Fnotifications\u002Fpreferences` answers 400 to a body that turns it off.\n- A removed category keeps its old rows. The page still lists them under the raw id, and the count still counts them until the prune deletes them.\n\nThe same section holds `menuSize` (5 rows under the bell), `pageSize` (20 rows a page), `readRetentionDays` (30) and `unreadRetentionDays` (180). Override them in `project.ts` the same way.\n\n## The api routes\n\nThe `chained-route` patch mounts the sub-app at `\u002Fnotifications`. Every route calls `requireSession`.\n\n| Route | Answer |\n|---|---|\n| `GET \u002Fnotifications?cursor=` | one page, newest first: `{ notifications, nextCursor }`. `nextCursor` is null on the last page. |\n| `GET \u002Fnotifications\u002Funread-count` | `{ count }`, the shape `NAV_COUNTS` reads |\n| `POST \u002Fnotifications\u002Fread-all` | `{ ok, marked }` |\n| `POST \u002Fnotifications\u002F:id\u002Fread` | `{ ok }`. Another user's row answers the same 404 as a missing row. |\n| `GET \u002Fnotifications\u002Fpreferences` | `{ categories: [{ id, label, description, required, enabled }] }` |\n| `PUT \u002Fnotifications\u002Fpreferences` | body `{ categories: { [id]: boolean } }`. An unknown id or a required category turned off answers 400 and writes nothing. |\n\n## The count\n\nThe count is every unread row of the user, in every category. An opt-out stops new rows only. Rows written before the opt-out stay unread and stay in the count until the user reads them or the prune deletes them.\n\nThe bell and the sidebar badge read one query key, `[\"nav-count\", \"\u002Fnotifications\"]`. The module's `NAV_COUNTS` entry makes `AppShell` in `apps\u002Fapp\u002Fsrc\u002Fcomponents\u002Fapp-shell.tsx` the one observer that fetches it. It refetches every 60 s while the tab is visible, on every window focus, and on a `pageshow` with `persisted` set (a back-forward cache restore). `useUnreadCount()` in `apps\u002Fapp\u002Fsrc\u002Flib\u002Fnotifications.ts` reads the same key with `enabled: false` and no `queryFn`, so the bell and the badge always show the same number. Do not give it a `queryFn` or `skipToken`: the last observer's options become the query's own, and an invalidation then fetches nothing.\n\nA failed fetch keeps the last count. A network error, a non-OK answer and a body without a numeric `count` all throw, and TanStack Query keeps the last `data`. The badge never drops to 0 because of a failure. While the menu is open, a failed row fetch shows \"Could not load notifications\" with \"Try again\".\n\nA mutation that changes the count must invalidate the key, or the badge waits for the next interval:\n\n```ts\nawait queryClient.invalidateQueries({ queryKey: [\"nav-count\", \"\u002Fnotifications\"] });\n```\n\nThe module's own mark-read and mark-all-read invalidate that key and the whole `[\"notifications\"]` prefix, which covers the menu rows (`[\"notifications\", \"menu\"]`), the Inbox pages (`[\"notifications\", \"list\"]`) and the preferences (`[\"notifications\", \"preferences\"]`). Use `UNREAD_COUNT_KEY` and `NOTIFICATIONS_KEY` from `@app\u002Flib\u002Fnotifications` rather than a copy of the array.\n\n## Retention\n\n`pruneNotifications(db, now, days?)` in `packages\u002Fdb\u002Fsrc\u002Frepositories\u002Fnotifications.ts` deletes, for every user, each read row written more than `readRetentionDays` ago and each row written more than `unreadRetentionDays` ago. It answers how many rows went. `days` defaults to the config section.\n\n**With `queue` installed**, the module registers the job `notification.prune` and the schedule `notification.prune.daily`, cron `30 3 * * *` (03:30 UTC daily). The job runs through `apps\u002Fapi\u002Fsrc\u002Flib\u002Fnotification-prune.ts`, which `worker.ts` lists as `notificationPruneHandlers()` in its `handlers` table. A run before that registration throws, so the message retries and lands in the dead-letter queue. It does not report a prune that did not run.\n\n**Without `queue`**, nothing prunes and the table grows. Call the function from your own scheduled handler:\n\n```ts\nimport { withDb } from \"@repo\u002Fdb\u002Fclient\";\nimport { pruneNotifications } from \"@repo\u002Fdb\u002Frepositories\u002Fnotifications\";\n\nawait withDb(scope, (db) => pruneNotifications(db, new Date()));\n```\n\nAdd `queue` later and `saasaloy add queue` backfills the two prune files and the three queue patches. `saasaloy remove queue` takes them out again and leaves the rest of this module.\n\n## Wire-up\n\nNo manual page edit is needed. The module's own app files import the three blocks: `notification-extension.tsx` renders `NotificationMenu` in the shell header, and `routes\u002Fnotifications.tsx` renders `NotificationList` and `NotificationPreferences`. `saasaloy add` still prints \"Manual wire-up needed ... on no page yet\" for the three `@ui\u002Fblocks\u002Fnotification\u002F` files, because it flags every block target. For this module that line is wrong. Ignore it.\n\nThe patches did the wiring:\n\n| Patch | File | Effect |\n|---|---|---|\n| `plugin-array` | `apps\u002Fapp\u002Fsrc\u002Fcomponents\u002Fshell-extensions.ts` | adds `notificationExtension()` to `extensions`, so the bell renders in the header |\n| `nav-entry` | `apps\u002Fapp\u002Fsrc\u002Fcomponents\u002Fnav.ts` | adds Notifications (`BellIcon`, shortcut `G N`) to the Workspace group of the `\u002F` area in `NAV_AREAS` |\n| `const-array` | `apps\u002Fapp\u002Fsrc\u002Fcomponents\u002Fnav-counts.ts` | adds `{ to: \"\u002Fnotifications\", path: \"\u002Fnotifications\u002Funread-count\" }` to `NAV_COUNTS`, so the row shows the live count |\n| `chained-route` | `apps\u002Fapi\u002Fsrc\u002Findex.ts` | mounts `notifications` at `\u002Fnotifications` |\n| `plugin-array` | `packages\u002Fconfig\u002Fsrc\u002Fsections.ts` | adds `notificationConfig()` to `sections` |\n| `plugin-array` | `packages\u002Fqueue\u002Fsrc\u002Findex.ts` | adds `pruneNotificationsJob()` to `jobs` and `pruneNotificationsSchedule()` to `schedules`, only with `queue` |\n| `plugin-array` | `apps\u002Fapi\u002Fsrc\u002Fworker.ts` | adds `notificationPruneHandlers()` to `handlers`, only with `queue` |\n\nAfter `add`:\n\n1. Run `pnpm db:generate` to write the migration for the two tables.\n2. Apply the migration with your driver's command.\n3. Run `pnpm db:seed` if you want the seed rows.\n4. Run `pnpm dev`, sign in to `app`, and open `\u002Fnotifications`. The router regenerates `routeTree.gen.ts` on the first start.\n\nThe seed does not reset `read_at` on a seed row. A seed row you marked read stays read after `pnpm db:seed`. For a clean start, run `pnpm db:drop`, then `pnpm db:setup`, then `pnpm db:seed`.\n\n## Troubleshooting\n\n| Symptom | Cause |\n|---|---|\n| `notify: unknown category \"x\"` | `x` is not in `config.notification.categories`. Add it in `project.ts`, or fix the typo. |\n| `notify: href \"...\" is not an app-relative path` | `href` is a full URL, starts with `\u002F\u002F`, or holds a `\\`. Pass a path such as `\u002Fsettings`. |\n| `notify` answers `written: false` | the user turned that category off. Make the category `required` if it must always arrive. |\n| The badge stays after a mark-read in your own screen | the screen does not invalidate `[\"nav-count\", \"\u002Fnotifications\"]` after its mutation. |\n| The bell and the badge show the old number while the api is down | expected. A failed fetch keeps the last count. |\n| The count includes rows of a category the user turned off | expected. An opt-out stops new rows only. |\n| `notification.prune ran before apps\u002Fapi registered its runner` | `apps\u002Fapi\u002Fsrc\u002Fworker.ts` lost `notificationPruneHandlers()` from its `handlers`. |\n| The table grows with no limit | `queue` is not installed, so nothing calls `pruneNotifications`. See [Retention](#retention). |\n\n## What `remove` leaves behind\n\n`saasaloy remove notification` deletes the files it wrote and reverses its patches: the shell extension, the nav row, the `NAV_COUNTS` entry, the api mount, the config section, and the queue and worker entries when they were applied. It leaves these for you:\n\n- The `notifications` and `notification_opt_outs` tables. Run `pnpm db:generate`, read the drop migration, and apply it only when you want the rows gone.\n- Every `notify()` call you added to your own routes. Its import of `..\u002Flib\u002Fnotify` breaks the build. Search `apps\u002Fapi` for `lib\u002Fnotify` before you remove the module.\n- Every override you wrote under `notification` in `packages\u002Fconfig\u002Fsrc\u002Fproject.ts`. With the section gone, that key fails `pnpm typecheck`, so take it out.\n- Any import of `@app\u002Flib\u002Fnotifications` or a notification block that you added to your own screens.\n\n## Next\n\nRun `saasaloy-app` to change the Notifications page or to add another screen beside it, and `saasaloy-queue` to change the prune schedule.\n",{"name":5,"description":7},"main","5ecf3d110b216ef678e06a1630f6b63a8e730bb5","modules\u002Fnotification\u002Fskills\u002Fsaasaloy-notification",0,"https:\u002F\u002Fgithub.com\u002Fmimukit\u002Fsaasaloy","https:\u002F\u002Fsaasaloy.com","https:\u002F\u002Fgithub.com\u002Fmimukit","https:\u002F\u002Fmimukit.com","Senior Software Engineer · 9+ yrs Fullstack (TS\u002FNode\u002FReact) · AI integrations, distributed systems · Building @saasaloy & @codealoy · Bangladesh 🇧🇩 \u002F Remote","2026-11-13 20:39:28",{"source":8,"skillName":5,"commands":35,"prompt":45,"zipAvailable":46},[36,39,42],{"tool":37,"command":38},"npx","npx skills add mimukit\u002Fsaasaloy --skill saasaloy-notification",{"tool":40,"command":41},"bunx","bunx skills add mimukit\u002Fsaasaloy --skill saasaloy-notification",{"tool":43,"command":44},"pnpm","pnpm dlx skills add mimukit\u002Fsaasaloy --skill saasaloy-notification","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fmimukit\u002Fsaasaloy\u002Fblob\u002Fmain\u002Fmodules\u002Fnotification\u002Fskills\u002Fsaasaloy-notification\u002FSKILL.md",true,[],[49,56,63,70,76,83],{"skillNo":50,"name":51,"nameZh":6,"description":52,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":53,"categories":54,"keywords":55,"publishedAt":19},"01M7BE75C34XHSPVJNQC8FY4Q0","saasaloy-subscription","Runbook for the subscription capability — provider-agnostic subscriptions in packages\u002Fsubscription, with a plan file, a projection table, a billable-subject file, plan entitlements and per-provider modules. Use when adding a plan, adding a plan feature or a numeric limit, gating an API route with requireFeature or requireWithinLimit, reading the 402 it returns, working out why a subject resolves to the free plan, wiring a checkout or portal route, reading a SubscriptionError, choosing or switching SUBSCRIPTION_PROVIDER, registering a webhook in the vendor's dashboard, writing a third subscription provider, or upgrading a project that still has `billing` installed.","https:\u002F\u002Fgithub.com\u002Fmimukit\u002Fsaasaloy\u002Fblob\u002Fmain\u002Fmodules\u002Fsubscription\u002Fskills\u002Fsaasaloy-subscription\u002FSKILL.md",[],[],{"skillNo":57,"name":58,"nameZh":6,"description":59,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":60,"categories":61,"keywords":62,"publishedAt":19},"01M7BE53PDQV0YDPG8RNVGTPEQ","saasaloy-cms","Runbook for the cms module, the in-house CMS behind apps\u002Fadmin. Covers code-defined collections, the Tiptap editor, drafts, revisions and redirects, the publish-time renderer, the public blog on apps\u002Fweb and its 60-second cache. Use when adding a collection or a field, rendering a stored body, reusing the editor, renderer or media field from another module, moving the blog to another path, re-rendering live posts after a renderer upgrade or a STORAGE_PUBLIC_URL change, or working out why a published post is not live yet.","https:\u002F\u002Fgithub.com\u002Fmimukit\u002Fsaasaloy\u002Fblob\u002Fmain\u002Fmodules\u002Fcms\u002Fskills\u002Fsaasaloy-cms\u002FSKILL.md",[],[],{"skillNo":64,"name":65,"nameZh":6,"description":66,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":67,"categories":68,"keywords":69,"publishedAt":19},"01M79GQ03M63V5EKSST92B3S5X","saasaloy-beta","The beta module's own procedure.","https:\u002F\u002Fgithub.com\u002Fmimukit\u002Fsaasaloy\u002Fblob\u002Fmain\u002Fpackages\u002Fcli\u002Ftest\u002Ffixtures\u002Fregistry-clean\u002Fbeta\u002Fsaasaloy-beta\u002FSKILL.md",[],[],{"skillNo":71,"name":72,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":73,"categories":74,"keywords":75,"publishedAt":19},"01M79GPXV3K8YF3M4VA0KGVE0B","helper","https:\u002F\u002Fgithub.com\u002Fmimukit\u002Fsaasaloy\u002Fblob\u002Fmain\u002Fpackages\u002Fcli\u002Ftest\u002Ffixtures\u002Fregistry-broken\u002Fbad-skill\u002Fhelper\u002FSKILL.md",[],[],{"skillNo":77,"name":78,"nameZh":6,"description":79,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":80,"categories":81,"keywords":82,"publishedAt":19},"01M79GPR8EVS1YX0W8X33Q9YD8","saasaloy-update","Runbook for an AI agent that updates this project to a newer Saasaloy CLI and newer module versions with saasaloy update. Use when the owner says \"update saasaloy\", \"upgrade the CLI\", \"pull the latest modules\", \"update the base template\", \"work the merge plan\", \"clear the update notes\", when saasaloy update or saasaloy outdated --check exits 3 or 2, when a merge plan or .saasaloy\u002Fmerge-plan.md is handed to you, or when an adopted project takes its first update. Covers the CLI upgrade, the preview, --out, the file merges, the update notes and \"Notes that may apply\", --resolved and --done, a file deleted locally and --restore, a three-way or two-way base, an update that skips releases, --from, the verify commands, and the exit codes.","https:\u002F\u002Fgithub.com\u002Fmimukit\u002Fsaasaloy\u002Fblob\u002Fmain\u002Fpackages\u002Fcli\u002Ftemplates\u002Fbase\u002F_agents\u002Fskills\u002Fsaasaloy-update\u002FSKILL.md",[],[],{"skillNo":84,"name":85,"nameZh":6,"description":86,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":87,"categories":88,"keywords":89,"publishedAt":19},"01M79GPM41WE99AQH1N5NWXPNT","saasaloy-testing","Pick the test a change needs and write it the way this project's testing mode asks. Use when the AGENTS.md testing block says tests-after or tdd, when adding or changing a route, a pure function, a provider, a table or a user-visible flow, when the owner asks \"which test do I write\", \"how do I test this route\", \"turn TDD on\", or when deciding whether a change needs a test at all.","https:\u002F\u002Fgithub.com\u002Fmimukit\u002Fsaasaloy\u002Fblob\u002Fmain\u002Fpackages\u002Fcli\u002Ftemplates\u002Fbase\u002F_agents\u002Fskills\u002Fsaasaloy-testing\u002FSKILL.md",[],[]]