{"version":3,"file":"database-channel.mjs","names":[],"sources":["../../../../../../../notifications/src/channels/database-channel.ts"],"sourcesContent":["/**\n * `databaseChannel` — persists notifications via the bound repository.\n *\n * NOT created by the dev directly — `inApp.configure({ model })` builds it\n * and returns it. That way the read side (`inApp.listUnread`, etc.) and the\n * write side (this channel's `send`) share ONE repository instance, which\n * means cache invalidation just works.\n *\n * Route is `{ id: notifiable.id }` — the recipient's id, used to set the\n * recipient column on the persisted row.\n *\n * Multi-tenant: when the model declares a `tenant` column, this channel reads\n * the tenant value off the recipient model (`notifiable.get(tenantColumn)`)\n * and hands it to `createFor`. The recipient model is only present on the\n * synchronous send path; a tenant-scoped store should send (not queue) the\n * database channel so the recipient — and thus the tenant — is in scope.\n *\n * The dispatcher has already injected `SendOptions.idempotencyKey` into the\n * payload (when set), so `createFor` receives a payload that includes it.\n */\nimport type { NotificationContract } from \"../contracts\";\nimport type { Channel } from \"../contracts\";\nimport { defineChannel } from \"../dispatch/define-channel\";\n// Type-only import to avoid a runtime cycle with `in-app/in-app.ts`.\nimport type { BaseNotificationsRepository } from \"../in-app/base-notifications-repository\";\nimport type { DatabasePayload, Id } from \"../types\";\n\nexport function databaseChannel<TModel extends NotificationContract = NotificationContract>(\n  repo: BaseNotificationsRepository<TModel>,\n): Channel<DatabasePayload> {\n  return defineChannel<DatabasePayload>({\n    name: \"database\",\n    route: (notifiable) => ({ id: notifiable.id }),\n    async send({ payload, route, notifiable }) {\n      const id: Id = typeof route === \"object\" ? route.id : route;\n\n      const { tenantColumn } = repo;\n      const tenantId =\n        tenantColumn && notifiable ? (notifiable.get(tenantColumn) as Id) : undefined;\n\n      await repo.createFor(id, payload, tenantId);\n    },\n  });\n}\n"],"mappings":";;;AA2BA,SAAgB,gBACd,MAC0B;CAC1B,OAAO,cAA+B;EACpC,MAAM;EACN,QAAQ,gBAAgB,EAAE,IAAI,WAAW,GAAG;EAC5C,MAAM,KAAK,EAAE,SAAS,OAAO,cAAc;GACzC,MAAM,KAAS,OAAO,UAAU,WAAW,MAAM,KAAK;GAEtD,MAAM,EAAE,iBAAiB;GACzB,MAAM,WACJ,gBAAgB,aAAc,WAAW,IAAI,YAAY,IAAW;GAEtE,MAAM,KAAK,UAAU,IAAI,SAAS,QAAQ;EAC5C;CACF,CAAC;AACH"}