@venturekit/notify 0.0.46 → 0.0.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/README.md +105 -3
  2. package/dist/client.d.ts +11 -0
  3. package/dist/client.d.ts.map +1 -1
  4. package/dist/client.js +118 -13
  5. package/dist/client.js.map +1 -1
  6. package/dist/devices/devices.d.ts +135 -0
  7. package/dist/devices/devices.d.ts.map +1 -0
  8. package/dist/devices/devices.js +178 -0
  9. package/dist/devices/devices.js.map +1 -0
  10. package/dist/devices/index.d.ts +6 -0
  11. package/dist/devices/index.d.ts.map +1 -0
  12. package/dist/devices/index.js +3 -0
  13. package/dist/devices/index.js.map +1 -0
  14. package/dist/devices/push.d.ts +84 -0
  15. package/dist/devices/push.d.ts.map +1 -0
  16. package/dist/devices/push.js +86 -0
  17. package/dist/devices/push.js.map +1 -0
  18. package/dist/dispatcher.d.ts.map +1 -1
  19. package/dist/dispatcher.js +6 -1
  20. package/dist/dispatcher.js.map +1 -1
  21. package/dist/inbox/inbox.d.ts +3 -2
  22. package/dist/inbox/inbox.d.ts.map +1 -1
  23. package/dist/inbox/inbox.js.map +1 -1
  24. package/dist/index.d.ts +4 -2
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +4 -2
  27. package/dist/index.js.map +1 -1
  28. package/dist/migrations/0000_vk_notify_push_devices.sql +73 -0
  29. package/dist/migrations/0000_vk_notify_templates.sql +75 -0
  30. package/dist/migrations/vk_notify_006_dedupe_key.sql +19 -0
  31. package/dist/providers/index.d.ts +1 -1
  32. package/dist/providers/index.d.ts.map +1 -1
  33. package/dist/providers/index.js +1 -1
  34. package/dist/providers/index.js.map +1 -1
  35. package/dist/providers/push-expo.d.ts +3 -1
  36. package/dist/providers/push-expo.d.ts.map +1 -1
  37. package/dist/providers/push-expo.js +5 -3
  38. package/dist/providers/push-expo.js.map +1 -1
  39. package/dist/providers/types.d.ts +11 -0
  40. package/dist/providers/types.d.ts.map +1 -1
  41. package/dist/providers/types.js +15 -0
  42. package/dist/providers/types.js.map +1 -1
  43. package/dist/querier.d.ts +20 -0
  44. package/dist/querier.d.ts.map +1 -0
  45. package/dist/querier.js +16 -0
  46. package/dist/querier.js.map +1 -0
  47. package/dist/store/index.d.ts +1 -0
  48. package/dist/store/index.d.ts.map +1 -1
  49. package/dist/store/index.js +1 -0
  50. package/dist/store/index.js.map +1 -1
  51. package/dist/store/postgres.d.ts.map +1 -1
  52. package/dist/store/postgres.js +23 -4
  53. package/dist/store/postgres.js.map +1 -1
  54. package/dist/store/types.d.ts +18 -1
  55. package/dist/store/types.d.ts.map +1 -1
  56. package/dist/store/types.js +14 -1
  57. package/dist/store/types.js.map +1 -1
  58. package/dist/templates/index.d.ts +5 -0
  59. package/dist/templates/index.d.ts.map +1 -0
  60. package/dist/templates/index.js +3 -0
  61. package/dist/templates/index.js.map +1 -0
  62. package/dist/templates/stored.d.ts +200 -0
  63. package/dist/templates/stored.d.ts.map +1 -0
  64. package/dist/templates/stored.js +248 -0
  65. package/dist/templates/stored.js.map +1 -0
  66. package/dist/types.d.ts +22 -0
  67. package/dist/types.d.ts.map +1 -1
  68. package/package.json +14 -6
  69. package/src/migrations/0000_vk_notify_push_devices.sql +73 -0
  70. package/src/migrations/0000_vk_notify_templates.sql +75 -0
  71. package/src/migrations/vk_notify_006_dedupe_key.sql +19 -0
@@ -0,0 +1,73 @@
1
+ -- @venturekit/notify — the phones a person can be pushed to.
2
+ --
3
+ -- Tables created by this migration:
4
+ -- vk_push_devices — one row per (tenant, push token): whose it is, which app, live or revoked
5
+ --
6
+ -- # Why the package owns it
7
+ --
8
+ -- Push is the one channel whose address the person does not type and the product cannot
9
+ -- look up: the app hands its token over when it is signed in, the token changes when the
10
+ -- app is reinstalled, a phone passes from one person to another, and the provider says when
11
+ -- a token is dead. Every product with an app wrote this table, the register / revoke routes
12
+ -- and the "revoke what Expo says is gone" loop for itself, and the copies differed in the one
13
+ -- place that matters — what happens to a token that changed hands. `devices/` is the one
14
+ -- implementation; `pushToUsers` and the outbox's fan-out (`createNotifyClient({ pushDevices })`)
15
+ -- read it.
16
+ --
17
+ -- # Shape
18
+ --
19
+ -- * `token` is the key, per tenant: Expo hands the same token back on every launch, so a
20
+ -- re-registration is a `last_seen_at`, and a token registered by someone else is RE-POINTED
21
+ -- to them (the phone was handed over) rather than duplicated. "No tenant" is one key
22
+ -- (`COALESCE` to the nil uuid), so a product without tenants gets one row per token too.
23
+ -- * `user_id` is whatever the product names a person by (a platform user id, a profile id),
24
+ -- TEXT like `vk_inbox_notifications.user_id`.
25
+ -- * `tenant_id` is `uuid`, like `vk_tenants.id` and `vk_tenant_scope()`, so a multi-tenant
26
+ -- project guards this table the way it guards its own:
27
+ -- SELECT vk_install_tenant_guards('public', 'vk_push_devices', '<tenant role>');
28
+ -- NULL is a device outside any tenant (a platform's own staff).
29
+ -- * Revoked, never deleted, so "why does my phone not ring" has an answer
30
+ -- (`revoked_reason`); `prunePushDevices` deletes what has been revoked long enough, and
31
+ -- `forgetPushDevices` deletes a person's rows outright for an erasure request.
32
+ --
33
+ -- # Why the `0000_` filename prefix
34
+ --
35
+ -- The runner applies every installed package's files and the project's own in one
36
+ -- alphabetic order, which puts `vk_notify_00N_*` after a project's `0xx_*` files. A project
37
+ -- adopting this table needs its own migration to run AFTER it — to install its tenant guards
38
+ -- on it, to add a foreign key to its tenants, to copy the rows of the table it replaces — so
39
+ -- this file takes the same one-character departure as `@venturekit/data`'s
40
+ -- `0000_vk_data_foundation.sql`. It depends on nothing a later migration creates
41
+ -- (`gen_random_uuid()` is core Postgres from 13), and an existing database simply gains the
42
+ -- table on its next migrate.
43
+
44
+ CREATE TABLE IF NOT EXISTS vk_push_devices (
45
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
46
+ tenant_id uuid,
47
+ user_id text NOT NULL CHECK (length(user_id) BETWEEN 1 AND 200),
48
+ token text NOT NULL CHECK (length(token) BETWEEN 8 AND 512),
49
+ provider text NOT NULL DEFAULT 'expo' CHECK (provider ~ '^[a-z][a-z0-9_-]{0,31}$'),
50
+ platform text NOT NULL CHECK (platform IN ('ios', 'android', 'web')),
51
+ -- Which of the product's apps registered it, when it has several (a parent app, a crew app).
52
+ app text CHECK (app IS NULL OR length(app) BETWEEN 1 AND 64),
53
+ locale text CHECK (locale IS NULL OR length(locale) BETWEEN 2 AND 35),
54
+ created_at timestamptz NOT NULL DEFAULT now(),
55
+ last_seen_at timestamptz NOT NULL DEFAULT now(),
56
+ revoked_at timestamptz,
57
+ -- signed_out: the person signed out on it; device_gone: the provider said the token is dead;
58
+ -- user_removed: the person's account went; stale: not seen for longer than the product keeps tokens.
59
+ revoked_reason text CHECK (revoked_reason IN ('signed_out', 'device_gone', 'user_removed', 'stale')),
60
+ CONSTRAINT vk_push_devices_revocation_chk CHECK ((revoked_at IS NULL) = (revoked_reason IS NULL))
61
+ );
62
+
63
+ -- The key: one row per token per tenant (the upsert target of `registerPushDevice`).
64
+ CREATE UNIQUE INDEX IF NOT EXISTS vk_push_devices_token_uidx
65
+ ON vk_push_devices ((COALESCE(tenant_id, '00000000-0000-0000-0000-000000000000'::uuid)), token);
66
+
67
+ -- A person's live phones — what every send reads.
68
+ CREATE INDEX IF NOT EXISTS vk_push_devices_live_idx
69
+ ON vk_push_devices (user_id, tenant_id)
70
+ WHERE revoked_at IS NULL;
71
+
72
+ COMMENT ON TABLE vk_push_devices IS
73
+ 'Push tokens per person (one per tenant and token; revoked, never deleted). Structure owned by @venturekit/notify; written through its devices helpers.';
@@ -0,0 +1,75 @@
1
+ -- @venturekit/notify — templates stored as data: the product's defaults and each tenant's own.
2
+ --
3
+ -- Tables created by this migration:
4
+ -- vk_notification_templates — versions of a template per (tenant, key, locale)
5
+ --
6
+ -- # Why the package owns it
7
+ --
8
+ -- `NotifyTemplate.render` is code, which is right for a message the developers own and wrong
9
+ -- for one a customer edits in a form (`policy/string-templates.ts` says as much, and ships
10
+ -- the renderer for the data-shaped half). This is the storage that renderer was missing — the
11
+ -- "Phase 2" the registry has promised — and every product that let a tenant reword a message
12
+ -- had written it: defaults shipped by the product, a tenant's own copy, drafts, an approval,
13
+ -- versions kept so what went out can be read back. `templates/` is the one implementation.
14
+ --
15
+ -- # Shape
16
+ --
17
+ -- * A row with `tenant_id` NULL is the product's DEFAULT for a key in a locale; a row with a
18
+ -- tenant is that tenant's COPY. The send path reads approved rows only, the tenant's copy
19
+ -- first (`resolveStoredTemplate`).
20
+ -- * `approved_at` NULL is a DRAFT. An approved row is never changed again: rewording it writes
21
+ -- the next `version`, itself a draft, so a message that went out under version 2 can always
22
+ -- be read back. At most one draft per (tenant, key, locale) — the partial unique index.
23
+ -- * `variables` are the placeholders the author declared (`{child_name}`); approval refuses a
24
+ -- text that uses one it did not declare (`undeclaredPlaceholders`). A tenant's copy inherits
25
+ -- its default's set: it cannot invent a placeholder the sender never fills.
26
+ -- * `overridable` false on a default: the product's own words, which no tenant may replace
27
+ -- (an operator notice, a legal text). Meaningless on a tenant's row.
28
+ -- * `category` is the product's grouping (preferences, approval rights), free text here; a
29
+ -- project that has a fixed set adds its CHECK.
30
+ -- * `tenant_id` is `uuid` (see `0000_vk_notify_push_devices.sql`). Defaults must stay readable by
31
+ -- every tenant, so the standard `vk_install_tenant_guards` policy (which hides NULL tenants)
32
+ -- is not the one to install here: read `tenant_id IS NULL OR tenant_id = ANY
33
+ -- (vk_tenant_scope())`, write `tenant_id = vk_acting_tenant()` — the project's migration, as
34
+ -- it names its own role.
35
+ --
36
+ -- # Why the `0000_` filename prefix
37
+ --
38
+ -- Same as `0000_vk_notify_push_devices.sql`: a project's own migration installs its policy and
39
+ -- foreign key on this table, seeds its defaults into it and copies the rows of the table it
40
+ -- replaces, so the table must exist before the project's files run.
41
+
42
+ CREATE TABLE IF NOT EXISTS vk_notification_templates (
43
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
44
+ tenant_id uuid,
45
+ key text NOT NULL CHECK (key ~ '^[A-Za-z0-9][A-Za-z0-9_.:-]{0,119}$'),
46
+ locale text NOT NULL CHECK (locale ~ '^[A-Za-z]{2,3}([_-][A-Za-z0-9]{2,8})*$'),
47
+ version integer NOT NULL DEFAULT 1 CHECK (version >= 1),
48
+ category text CHECK (category IS NULL OR length(category) BETWEEN 1 AND 64),
49
+ title text NOT NULL,
50
+ body text NOT NULL,
51
+ variables text[] NOT NULL DEFAULT '{}',
52
+ overridable boolean NOT NULL DEFAULT true,
53
+ approved_at timestamptz,
54
+ approved_by text,
55
+ created_by text,
56
+ created_at timestamptz NOT NULL DEFAULT now(),
57
+ updated_at timestamptz NOT NULL DEFAULT now()
58
+ );
59
+
60
+ -- One row per version.
61
+ CREATE UNIQUE INDEX IF NOT EXISTS vk_notification_templates_version_uidx
62
+ ON vk_notification_templates ((COALESCE(tenant_id, '00000000-0000-0000-0000-000000000000'::uuid)), key, locale, version);
63
+
64
+ -- At most one draft per (tenant, key, locale): rewording edits it in place.
65
+ CREATE UNIQUE INDEX IF NOT EXISTS vk_notification_templates_draft_uidx
66
+ ON vk_notification_templates ((COALESCE(tenant_id, '00000000-0000-0000-0000-000000000000'::uuid)), key, locale)
67
+ WHERE approved_at IS NULL;
68
+
69
+ -- The send path: a key's approved versions, the default's and one tenant's.
70
+ CREATE INDEX IF NOT EXISTS vk_notification_templates_approved_idx
71
+ ON vk_notification_templates (key, tenant_id, locale, version DESC)
72
+ WHERE approved_at IS NOT NULL;
73
+
74
+ COMMENT ON TABLE vk_notification_templates IS
75
+ 'Stored templates: the product''s defaults (tenant NULL) and tenants'' copies, versioned, draft until approved. Structure owned by @venturekit/notify; written through its templates helpers.';
@@ -0,0 +1,19 @@
1
+ -- @venturekit/notify — an outbox row once per key.
2
+ --
3
+ -- A message that must go out once however often the code that sends it runs — a weekly digest
4
+ -- retried by its cron, a webhook delivered twice, a request replayed — had nothing to hold on to:
5
+ -- the outbox took every enqueue as a new row. Products wrote the guard themselves (an advisory
6
+ -- lock and a scan of `related_kind` / `related_id`), each a little differently. Now
7
+ -- `enqueue({ dedupeKey })` writes the row only if no row holds that key yet; a second call gets
8
+ -- the earlier row back (`EnqueueResult.duplicate`), and two calls at once cannot both write, since
9
+ -- this index decides.
10
+ --
11
+ -- The key is the caller's and global to the table (`vk_notifications` has no tenant): a
12
+ -- multi-tenant product puts the tenant in it. NULL — every row written before, and every enqueue
13
+ -- without a key — is never a duplicate. Additive: no existing row or file changes.
14
+
15
+ ALTER TABLE vk_notifications ADD COLUMN IF NOT EXISTS dedupe_key TEXT;
16
+
17
+ CREATE UNIQUE INDEX IF NOT EXISTS notifications_dedupe_key_uidx
18
+ ON vk_notifications (dedupe_key)
19
+ WHERE dedupe_key IS NOT NULL;