@miguelmorales13/nestkit 0.26.0 → 0.29.0
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.
- package/README.md +69 -0
- package/dist/alerts/alert-on-failure.decorator.d.ts +5 -0
- package/dist/alerts/alert-sinks.d.ts +29 -0
- package/dist/alerts/alerts.module.d.ts +19 -0
- package/dist/alerts/alerts.options.d.ts +1 -0
- package/dist/alerts/alerts.service.d.ts +28 -0
- package/dist/alerts/alerts.types.d.ts +66 -0
- package/dist/alerts/index.cjs +276 -0
- package/dist/alerts/index.d.ts +10 -0
- package/dist/alerts/index.js +276 -0
- package/dist/alerts/logger-with-alerts.d.ts +18 -0
- package/dist/alerts/process-guards.d.ts +21 -0
- package/dist/alerts/runtime.d.ts +4 -0
- package/dist/audit/index.cjs +13 -13
- package/dist/audit/index.js +2 -2
- package/dist/auth/index.cjs +21 -21
- package/dist/auth/index.js +3 -3
- package/dist/auth/oauth/index.cjs +101 -101
- package/dist/auth/oauth/index.js +3 -3
- package/dist/bootstrap/apply-defaults.d.ts +9 -0
- package/dist/bootstrap/index.cjs +7 -6
- package/dist/bootstrap/index.js +6 -5
- package/dist/chunk-33KFVAUZ.cjs +151 -0
- package/dist/chunk-3TUMVLPJ.js +14 -0
- package/dist/{chunk-PA24P76K.js → chunk-45SW7FBE.js} +1 -1
- package/dist/{chunk-RF75KC63.cjs → chunk-47IYXZHW.cjs} +2 -2
- package/dist/{chunk-TUNDBBQF.js → chunk-4WWOKZIO.js} +4 -4
- package/dist/{chunk-2DUEQKOI.js → chunk-5LVGCTJ3.js} +4 -4
- package/dist/chunk-A6YGIZCQ.cjs +14 -0
- package/dist/chunk-A7FI6HXV.js +40 -0
- package/dist/chunk-CU4FUIVJ.cjs +8 -0
- package/dist/{chunk-FH5UNWWW.cjs → chunk-D35TBVRO.cjs} +4 -4
- package/dist/{chunk-4MGIQFAJ.js → chunk-D3DDJWUD.js} +5 -0
- package/dist/chunk-DVGPEBUL.js +8 -0
- package/dist/{chunk-VKOPDDCC.js → chunk-ENZ7LQRE.js} +1 -1
- package/dist/{chunk-3AUOOEQO.cjs → chunk-F77X4QUN.cjs} +2 -2
- package/dist/chunk-FBNV5X6C.js +61 -0
- package/dist/{chunk-APUFKA4K.cjs → chunk-FDZQCLLK.cjs} +6 -6
- package/dist/{chunk-DYHSOULW.js → chunk-IGSNB5BX.js} +1 -1
- package/dist/chunk-IVWPA6QH.cjs +90 -0
- package/dist/{chunk-LSDY6ETG.cjs → chunk-JJKGQNGD.cjs} +5 -5
- package/dist/{chunk-ALIKVFOV.cjs → chunk-JRFE7NSO.cjs} +12 -4
- package/dist/{chunk-HVHYZXBB.cjs → chunk-KNU5RN3O.cjs} +3 -3
- package/dist/{chunk-52IOOHNX.cjs → chunk-KQPJCVN3.cjs} +26 -26
- package/dist/chunk-LGFHLAY3.cjs +61 -0
- package/dist/{chunk-2REOCMUD.cjs → chunk-LRX7AMSS.cjs} +6 -1
- package/dist/chunk-LU3HO7B3.js +151 -0
- package/dist/{chunk-4PFAKLT3.js → chunk-MUWHGBIB.js} +1 -1
- package/dist/{chunk-ZJMLEP5H.js → chunk-NJYLQDB3.js} +1 -1
- package/dist/{chunk-IFNYRZP2.js → chunk-OSRH6KYZ.js} +4 -4
- package/dist/{chunk-6RAPVLJM.js → chunk-OTE77ONL.js} +1 -1
- package/dist/chunk-UDACUMLO.cjs +40 -0
- package/dist/{chunk-PFHHLB7B.js → chunk-UGAAJRPO.js} +10 -2
- package/dist/{chunk-SPTDXUED.cjs → chunk-URLDTS2Q.cjs} +2 -2
- package/dist/{chunk-W2T2VFLC.cjs → chunk-VALDHJOO.cjs} +5 -5
- package/dist/chunk-WFCQDOSN.js +90 -0
- package/dist/consent/consent.interceptor.d.ts +20 -0
- package/dist/consent/consent.module.d.ts +25 -0
- package/dist/consent/consent.options.d.ts +30 -0
- package/dist/consent/consent.port.d.ts +17 -0
- package/dist/consent/consent.service.d.ts +44 -0
- package/dist/consent/guard/index.cjs +13 -0
- package/dist/consent/guard/index.d.ts +31 -0
- package/dist/consent/guard/index.js +13 -0
- package/dist/consent/index.cjs +271 -0
- package/dist/consent/index.d.ts +13 -0
- package/dist/consent/index.js +271 -0
- package/dist/consent/memory.store.d.ts +7 -0
- package/dist/consent/shared/index.cjs +11 -0
- package/dist/consent/shared/index.d.ts +19 -0
- package/dist/consent/shared/index.js +11 -0
- package/dist/consent/typeorm.store.d.ts +22 -0
- package/dist/crud/index.cjs +4 -4
- package/dist/crud/index.js +3 -3
- package/dist/database/postgres/index.cjs +3 -3
- package/dist/database/postgres/index.js +2 -2
- package/dist/database/supabase/index.cjs +3 -3
- package/dist/database/supabase/index.js +2 -2
- package/dist/database/typeorm/index.cjs +2 -2
- package/dist/database/typeorm/index.js +1 -1
- package/dist/devices/device-check.interceptor.d.ts +20 -0
- package/dist/devices/devices.module.d.ts +28 -0
- package/dist/devices/devices.options.d.ts +53 -0
- package/dist/devices/devices.port.d.ts +62 -0
- package/dist/devices/devices.service.d.ts +58 -0
- package/dist/devices/index.cjs +327 -0
- package/dist/devices/index.d.ts +8 -0
- package/dist/devices/index.js +327 -0
- package/dist/devices/memory.store.d.ts +12 -0
- package/dist/email/index.cjs +2 -2
- package/dist/email/index.js +1 -1
- package/dist/email/nodemailer/index.cjs +2 -2
- package/dist/email/nodemailer/index.js +1 -1
- package/dist/email/resend/index.cjs +2 -2
- package/dist/email/resend/index.js +1 -1
- package/dist/entities/index.cjs +1 -1
- package/dist/entities/index.js +1 -1
- package/dist/errors/index.cjs +7 -7
- package/dist/errors/index.js +7 -7
- package/dist/events/buses.d.ts +27 -0
- package/dist/events/events.controller.d.ts +15 -0
- package/dist/events/events.module.d.ts +29 -0
- package/dist/events/events.options.d.ts +50 -0
- package/dist/events/events.service.d.ts +49 -0
- package/dist/events/events.types.d.ts +77 -0
- package/dist/events/index.cjs +557 -0
- package/dist/events/index.d.ts +10 -0
- package/dist/events/index.js +557 -0
- package/dist/events/memory-ticket.store.d.ts +9 -0
- package/dist/google-business/google-business-http.d.ts +41 -0
- package/dist/google-business/google-business.errors.d.ts +75 -0
- package/dist/google-business/google-business.mappers.d.ts +52 -0
- package/dist/google-business/google-business.module.d.ts +26 -0
- package/dist/google-business/google-business.options.d.ts +40 -0
- package/dist/google-business/google-business.ports.d.ts +18 -0
- package/dist/google-business/google-business.service.d.ts +120 -0
- package/dist/google-business/google-business.types.d.ts +155 -0
- package/dist/google-business/index.cjs +1025 -0
- package/dist/google-business/index.d.ts +15 -0
- package/dist/google-business/index.js +1025 -0
- package/dist/google-business/testing/google-simulator.d.ts +61 -0
- package/dist/hardening/body-limits.d.ts +24 -0
- package/dist/hardening/http.types.d.ts +26 -0
- package/dist/hardening/index.cjs +179 -0
- package/dist/hardening/index.d.ts +19 -0
- package/dist/hardening/index.js +179 -0
- package/dist/hardening/memory-rate-limit.store.d.ts +10 -0
- package/dist/hardening/origin-secret.d.ts +29 -0
- package/dist/hardening/rate-limit.decorator.d.ts +8 -0
- package/dist/hardening/rate-limit.guard.d.ts +18 -0
- package/dist/hardening/rate-limit.module.d.ts +23 -0
- package/dist/hardening/rate-limit.options.d.ts +35 -0
- package/dist/hardening/rate-limit.port.d.ts +11 -0
- package/dist/hardening/redis-rate-limit.store.d.ts +23 -0
- package/dist/hardening/trust-proxy.d.ts +22 -0
- package/dist/health/health.controller.d.ts +32 -0
- package/dist/health/health.module.d.ts +15 -0
- package/dist/health/health.options.d.ts +24 -0
- package/dist/health/index.cjs +103 -0
- package/dist/health/index.d.ts +5 -0
- package/dist/health/index.js +103 -0
- package/dist/i18n/index.cjs +3 -3
- package/dist/i18n/index.js +2 -2
- package/dist/idempotency/idempotency.interceptor.d.ts +1 -0
- package/dist/idempotency/idempotency.options.d.ts +15 -0
- package/dist/idempotency/index.cjs +214 -7
- package/dist/idempotency/index.d.ts +5 -0
- package/dist/idempotency/index.js +210 -3
- package/dist/idempotency/no-idempotency.decorator.d.ts +8 -0
- package/dist/idempotency/postgres.store.d.ts +49 -0
- package/dist/idempotency/redis.store.d.ts +59 -0
- package/dist/index.cjs +21 -20
- package/dist/index.js +33 -32
- package/dist/invoicing/index.cjs +2 -2
- package/dist/invoicing/index.js +1 -1
- package/dist/jobs/defer-job.d.ts +10 -0
- package/dist/jobs/index.cjs +318 -0
- package/dist/jobs/index.d.ts +8 -0
- package/dist/jobs/index.js +318 -0
- package/dist/jobs/jobs-admin.controller.d.ts +18 -0
- package/dist/jobs/jobs.module.d.ts +33 -0
- package/dist/jobs/jobs.options.d.ts +62 -0
- package/dist/jobs/jobs.service.d.ts +53 -0
- package/dist/jobs/jobs.sql.d.ts +11 -0
- package/dist/media/index.cjs +14 -14
- package/dist/media/index.js +1 -1
- package/dist/meta/index.cjs +3 -3
- package/dist/meta/index.js +1 -1
- package/dist/notifications/index.cjs +375 -0
- package/dist/notifications/index.d.ts +8 -0
- package/dist/notifications/index.js +375 -0
- package/dist/notifications/memory.stores.d.ts +23 -0
- package/dist/notifications/notifications.controller.d.ts +11 -0
- package/dist/notifications/notifications.module.d.ts +31 -0
- package/dist/notifications/notifications.options.d.ts +33 -0
- package/dist/notifications/notifications.port.d.ts +97 -0
- package/dist/notifications/notifier.service.d.ts +31 -0
- package/dist/payments/connect/credentials-cipher.d.ts +14 -0
- package/dist/payments/connect/mercadopago-refund.d.ts +118 -0
- package/dist/payments/connect/mercadopago-signature.d.ts +6 -0
- package/dist/payments/connect/online-checkout.d.ts +69 -0
- package/dist/payments/connect/rejection.d.ts +7 -0
- package/dist/payments/connect/status.d.ts +20 -0
- package/dist/payments/connect/terminal-connections.d.ts +291 -0
- package/dist/payments/index.cjs +908 -10
- package/dist/payments/index.d.ts +10 -0
- package/dist/payments/index.js +900 -2
- package/dist/pinterest/index.cjs +3 -3
- package/dist/pinterest/index.js +1 -1
- package/dist/privacy/anonymizer.d.ts +14 -0
- package/dist/privacy/index.cjs +768 -0
- package/dist/privacy/index.d.ts +13 -0
- package/dist/privacy/index.js +768 -0
- package/dist/privacy/memory.store.d.ts +15 -0
- package/dist/privacy/privacy.module.d.ts +30 -0
- package/dist/privacy/privacy.options.d.ts +43 -0
- package/dist/privacy/privacy.port.d.ts +25 -0
- package/dist/privacy/privacy.service.d.ts +69 -0
- package/dist/privacy/privacy.types.d.ts +125 -0
- package/dist/privacy/sql.d.ts +7 -0
- package/dist/privacy/sql.store.d.ts +21 -0
- package/dist/privacy/zip.d.ts +23 -0
- package/dist/push/index.cjs +13 -13
- package/dist/push/index.js +5 -5
- package/dist/resilience/alert-channels.d.ts +39 -0
- package/dist/resilience/alert-errors.interceptor.d.ts +10 -0
- package/dist/resilience/alert-on-failure.decorator.d.ts +10 -0
- package/dist/resilience/alert.service.d.ts +77 -0
- package/dist/resilience/index.cjs +863 -0
- package/dist/resilience/index.d.ts +18 -0
- package/dist/resilience/index.js +863 -0
- package/dist/resilience/memory-alert-store.d.ts +26 -0
- package/dist/resilience/provider-health.controller.d.ts +14 -0
- package/dist/resilience/provider-unavailable.exception.d.ts +11 -0
- package/dist/resilience/resilience.module.d.ts +34 -0
- package/dist/resilience/resilience.options.d.ts +28 -0
- package/dist/resilience/resilience.types.d.ts +167 -0
- package/dist/resilience/resilient-fetch.d.ts +38 -0
- package/dist/resilience/runtime.d.ts +24 -0
- package/dist/resilience/typeorm/alert.entity.d.ts +21 -0
- package/dist/resilience/typeorm/index.cjs +102 -0
- package/dist/resilience/typeorm/index.d.ts +3 -0
- package/dist/resilience/typeorm/index.js +102 -0
- package/dist/resilience/typeorm/typeorm-alert-store.d.ts +39 -0
- package/dist/resilience/validate-destination.d.ts +63 -0
- package/dist/response/index.cjs +3 -3
- package/dist/response/index.js +2 -2
- package/dist/scheduling/cron-lock.service.d.ts +36 -0
- package/dist/scheduling/cron-unico.decorator.d.ts +27 -0
- package/dist/scheduling/fallback.store.d.ts +34 -0
- package/dist/scheduling/index.cjs +392 -0
- package/dist/scheduling/index.d.ts +18 -0
- package/dist/scheduling/index.js +392 -0
- package/dist/scheduling/lock-store.port.d.ts +19 -0
- package/dist/scheduling/memory.store.d.ts +15 -0
- package/dist/scheduling/redis.module.d.ts +31 -0
- package/dist/scheduling/redis.store.d.ts +24 -0
- package/dist/scheduling/scheduling.module.d.ts +24 -0
- package/dist/scheduling/scheduling.options.d.ts +27 -0
- package/dist/scheduling/sql.store.d.ts +26 -0
- package/dist/secrets/index.cjs +274 -0
- package/dist/secrets/index.d.ts +8 -0
- package/dist/secrets/index.js +274 -0
- package/dist/secrets/secrets.errors.d.ts +6 -0
- package/dist/secrets/secrets.module.d.ts +21 -0
- package/dist/secrets/secrets.options.d.ts +32 -0
- package/dist/secrets/secrets.service.d.ts +54 -0
- package/dist/secrets/secrets.transformer.d.ts +33 -0
- package/dist/security/hash.d.ts +8 -0
- package/dist/security/index.cjs +27 -35
- package/dist/security/index.d.ts +5 -0
- package/dist/security/index.js +24 -32
- package/dist/security/mask.d.ts +22 -0
- package/dist/security/signed-grant.d.ts +30 -0
- package/dist/storage/index.cjs +2 -2
- package/dist/storage/index.js +1 -1
- package/dist/stripe/index.cjs +7 -7
- package/dist/stripe/index.js +1 -1
- package/dist/telegram/index.cjs +2 -2
- package/dist/telegram/index.js +1 -1
- package/dist/tenancy/app-role.d.ts +24 -0
- package/dist/tenancy/db-helpers.d.ts +12 -0
- package/dist/tenancy/index.cjs +205 -0
- package/dist/tenancy/index.d.ts +14 -0
- package/dist/tenancy/index.js +205 -0
- package/dist/tenancy/pg/index.cjs +34 -0
- package/dist/tenancy/pg/index.d.ts +2 -0
- package/dist/tenancy/pg/index.js +34 -0
- package/dist/tenancy/pg/with-tenant.d.ts +23 -0
- package/dist/tenancy/request-context.d.ts +18 -0
- package/dist/tenancy/rls-guardrail.d.ts +38 -0
- package/dist/tenancy/scope.d.ts +5 -0
- package/dist/tenancy/tenancy.errors.d.ts +5 -0
- package/dist/tenancy/tenancy.middleware.d.ts +8 -0
- package/dist/tenancy/tenancy.module.d.ts +37 -0
- package/dist/tenancy/tenancy.options.d.ts +34 -0
- package/dist/tenancy/typeorm/index.cjs +41 -0
- package/dist/tenancy/typeorm/index.d.ts +2 -0
- package/dist/tenancy/typeorm/index.js +41 -0
- package/dist/tenancy/typeorm/with-tenant.d.ts +23 -0
- package/dist/tiktok/index.cjs +3 -3
- package/dist/tiktok/index.js +1 -1
- package/dist/tracking/index.cjs +3 -3
- package/dist/tracking/index.js +2 -2
- package/dist/umami/index.cjs +4 -4
- package/dist/umami/index.js +1 -1
- package/dist/whatsapp/index.cjs +2 -2
- package/dist/whatsapp/index.js +1 -1
- package/package.json +108 -3
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PUSH
|
|
3
|
+
} from "../chunk-DVGPEBUL.js";
|
|
4
|
+
import {
|
|
5
|
+
NotFoundAppException,
|
|
6
|
+
UnauthorizedAppException,
|
|
7
|
+
ValidationAppException
|
|
8
|
+
} from "../chunk-ZFIDAX7K.js";
|
|
9
|
+
import "../chunk-M3GQDRWR.js";
|
|
10
|
+
import "../chunk-VVMRZEIW.js";
|
|
11
|
+
import {
|
|
12
|
+
__decorateClass,
|
|
13
|
+
__decorateParam
|
|
14
|
+
} from "../chunk-D3DDJWUD.js";
|
|
15
|
+
|
|
16
|
+
// src/notifications/notifications.module.ts
|
|
17
|
+
import { Global, Module } from "@nestjs/common";
|
|
18
|
+
|
|
19
|
+
// src/notifications/memory.stores.ts
|
|
20
|
+
import { randomUUID } from "crypto";
|
|
21
|
+
var k = (...parts) => parts.map((p) => p ?? "").join("\0");
|
|
22
|
+
var MemoryPreferenceStore = class {
|
|
23
|
+
constructor() {
|
|
24
|
+
this.rows = /* @__PURE__ */ new Map();
|
|
25
|
+
}
|
|
26
|
+
async list(userId, scope) {
|
|
27
|
+
const prefix = k(scope, userId, "");
|
|
28
|
+
const out = {};
|
|
29
|
+
for (const [key, value] of this.rows) if (key.startsWith(prefix)) out[key.slice(prefix.length)] = value;
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
async save(userId, key, enabled, scope) {
|
|
33
|
+
this.rows.set(k(scope, userId, key), enabled);
|
|
34
|
+
}
|
|
35
|
+
async listForUsers(userIds, key, scope) {
|
|
36
|
+
const out = /* @__PURE__ */ new Map();
|
|
37
|
+
for (const id of userIds) {
|
|
38
|
+
const v = this.rows.get(k(scope, id, key));
|
|
39
|
+
if (v !== void 0) out.set(id, v);
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var identity = (d) => d.kind === "fcm" ? `fcm:${d.token}` : `web:${d.endpoint}`;
|
|
45
|
+
var MemoryDeviceStore = class {
|
|
46
|
+
constructor() {
|
|
47
|
+
this.rows = /* @__PURE__ */ new Map();
|
|
48
|
+
}
|
|
49
|
+
async listByUser(userId) {
|
|
50
|
+
return [...this.rows.entries()].filter(([, r]) => r.userId === userId && r.enabled).map(([id, r]) => ({ id, device: r.device }));
|
|
51
|
+
}
|
|
52
|
+
async remove(id) {
|
|
53
|
+
this.rows.delete(id);
|
|
54
|
+
}
|
|
55
|
+
async register(userId, device, meta = {}) {
|
|
56
|
+
const same = [...this.rows.entries()].find(([, r]) => r.userId === userId && identity(r.device) === identity(device));
|
|
57
|
+
if (same) {
|
|
58
|
+
same[1].device = device;
|
|
59
|
+
if (meta.enabled !== void 0) same[1].enabled = meta.enabled;
|
|
60
|
+
if (meta.platform !== void 0) same[1].platform = meta.platform;
|
|
61
|
+
return { id: same[0], enabled: same[1].enabled };
|
|
62
|
+
}
|
|
63
|
+
const id = randomUUID();
|
|
64
|
+
const enabled = meta.enabled ?? true;
|
|
65
|
+
this.rows.set(id, { userId, device, enabled, platform: meta.platform ?? null });
|
|
66
|
+
return { id, enabled };
|
|
67
|
+
}
|
|
68
|
+
async unregister(userId, id) {
|
|
69
|
+
const row = this.rows.get(id);
|
|
70
|
+
if (!row || row.userId !== userId) return false;
|
|
71
|
+
return this.rows.delete(id);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// src/notifications/notifications.controller.ts
|
|
76
|
+
import { Body, Controller, Delete, Get, Inject as Inject2, Param, Post, Put, Req } from "@nestjs/common";
|
|
77
|
+
|
|
78
|
+
// src/notifications/notifications.port.ts
|
|
79
|
+
var NOTIFICATIONS_OPTIONS = /* @__PURE__ */ Symbol("NOTIFICATIONS_OPTIONS");
|
|
80
|
+
var NOTIFICATION_PREFERENCES = /* @__PURE__ */ Symbol("NOTIFICATION_PREFERENCES");
|
|
81
|
+
var NOTIFICATION_DEVICES = /* @__PURE__ */ Symbol("NOTIFICATION_DEVICES");
|
|
82
|
+
|
|
83
|
+
// src/notifications/notifier.service.ts
|
|
84
|
+
import { Inject, Injectable, Logger } from "@nestjs/common";
|
|
85
|
+
function validateCatalog(catalog) {
|
|
86
|
+
const map = /* @__PURE__ */ new Map();
|
|
87
|
+
for (const n of catalog) {
|
|
88
|
+
if (!n.key) throw new Error("NotificationsModule: a notice without key.");
|
|
89
|
+
if (map.has(n.key)) throw new Error(`NotificationsModule: duplicate notice "${n.key}".`);
|
|
90
|
+
map.set(n.key, n);
|
|
91
|
+
}
|
|
92
|
+
for (const n of catalog) {
|
|
93
|
+
if (!n.governedBy) continue;
|
|
94
|
+
const owner = map.get(n.governedBy);
|
|
95
|
+
if (!owner) throw new Error(`NotificationsModule: "${n.key}" is governed by "${n.governedBy}", which is not in the catalog.`);
|
|
96
|
+
if (owner.governedBy) throw new Error(`NotificationsModule: "${n.key}" is governed by "${owner.key}", which is itself governed (one level only).`);
|
|
97
|
+
}
|
|
98
|
+
return map;
|
|
99
|
+
}
|
|
100
|
+
var NotifierService = class {
|
|
101
|
+
constructor(options, prefs, devices, push) {
|
|
102
|
+
this.options = options;
|
|
103
|
+
this.prefs = prefs;
|
|
104
|
+
this.devices = devices;
|
|
105
|
+
this.push = push;
|
|
106
|
+
this.logger = new Logger("Notifier");
|
|
107
|
+
this.catalog = validateCatalog(options.catalog);
|
|
108
|
+
}
|
|
109
|
+
/** The key whose switch governs `notice`. */
|
|
110
|
+
governing(notice) {
|
|
111
|
+
return notice.governedBy ? this.catalog.get(notice.governedBy) : notice;
|
|
112
|
+
}
|
|
113
|
+
/** Switches the user can flip (entries that are not governed by another). */
|
|
114
|
+
async preferencesOf(userId, scope) {
|
|
115
|
+
const saved = await this.prefs.list(userId, scope);
|
|
116
|
+
return this.options.catalog.filter((n) => !n.governedBy).map((n) => ({
|
|
117
|
+
key: n.key,
|
|
118
|
+
group: n.group,
|
|
119
|
+
title: n.title,
|
|
120
|
+
description: n.description,
|
|
121
|
+
defaultOn: n.defaultOn,
|
|
122
|
+
enabled: saved[n.key] ?? n.defaultOn
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
/** Returns false when the key is not a user-facing switch (not in the catalog, or governed by another). */
|
|
126
|
+
async setPreference(userId, key, enabled, scope) {
|
|
127
|
+
const n = this.catalog.get(key);
|
|
128
|
+
if (!n || n.governedBy) return false;
|
|
129
|
+
await this.prefs.save(userId, key, enabled, scope);
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
async notify(key, input) {
|
|
133
|
+
try {
|
|
134
|
+
return await this.run(key, input);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
if (this.options.neverThrow === false) throw error;
|
|
137
|
+
this.logger.warn(`notify("${key}") failed: ${error?.message ?? error}`);
|
|
138
|
+
return { accepted: this.catalog.has(key), sent: 0, failed: 0, delivered: [], skipped: [{ reason: "error" }] };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async run(key, input) {
|
|
142
|
+
const result = { accepted: false, sent: 0, failed: 0, delivered: [], skipped: [] };
|
|
143
|
+
const notice = this.catalog.get(key);
|
|
144
|
+
if (!notice) {
|
|
145
|
+
this.logger.warn(`notice "${key}" is not in the catalog: not sent.`);
|
|
146
|
+
result.skipped.push({ reason: "not_in_catalog" });
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
result.accepted = true;
|
|
150
|
+
const exclude = new Set(input.exclude ?? []);
|
|
151
|
+
const candidates = [...new Set(await this.resolveRecipients(input.to))].filter((id) => !exclude.has(id));
|
|
152
|
+
if (candidates.length === 0) {
|
|
153
|
+
result.skipped.push({ reason: "no_recipients" });
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
const owner = this.governing(notice);
|
|
157
|
+
const explicit = await this.explicitChoices(candidates, owner.key, input.preferenceScope);
|
|
158
|
+
const { to: _to, exclude: _x, preferenceScope: _p, lang, data, ...message } = input;
|
|
159
|
+
const payload = { ...message, data: { ...data, notice: key } };
|
|
160
|
+
for (const userId of candidates) {
|
|
161
|
+
if (!(explicit.get(userId) ?? owner.defaultOn)) {
|
|
162
|
+
result.skipped.push({ userId, reason: "disabled" });
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (this.options.gate && !await this.passesGate(userId, notice)) {
|
|
166
|
+
result.skipped.push({ userId, reason: "gate" });
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const reason = await this.deliver(userId, notice, payload, lang, result);
|
|
170
|
+
if (reason) result.skipped.push({ userId, reason });
|
|
171
|
+
}
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
async deliver(userId, notice, payload, lang, result) {
|
|
175
|
+
let stored;
|
|
176
|
+
try {
|
|
177
|
+
stored = await this.devices.listByUser(userId);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (this.options.neverThrow === false) throw error;
|
|
180
|
+
this.logger.warn(`devices of ${userId}: ${error.message}`);
|
|
181
|
+
return "error";
|
|
182
|
+
}
|
|
183
|
+
const allowed = notice.channels;
|
|
184
|
+
const targets = allowed ? stored.filter((s) => allowed.includes(s.device.kind)) : stored;
|
|
185
|
+
if (targets.length === 0) return "no_devices";
|
|
186
|
+
const out = await this.push.send(
|
|
187
|
+
targets.map((t) => t.device),
|
|
188
|
+
payload,
|
|
189
|
+
{ lang }
|
|
190
|
+
);
|
|
191
|
+
result.sent += out.sent;
|
|
192
|
+
result.failed += out.failed.length;
|
|
193
|
+
if (out.sent > 0) result.delivered.push(userId);
|
|
194
|
+
const dead = new Set(out.failed.filter((f) => f.stale).map((f) => f.device));
|
|
195
|
+
for (const t of targets) {
|
|
196
|
+
if (!dead.has(t.device)) continue;
|
|
197
|
+
await this.devices.remove(t.id).catch((e) => this.logger.warn(`could not prune device ${t.id}: ${e?.message ?? e}`));
|
|
198
|
+
}
|
|
199
|
+
return out.sent > 0 ? null : "error";
|
|
200
|
+
}
|
|
201
|
+
async passesGate(userId, notice) {
|
|
202
|
+
try {
|
|
203
|
+
return await this.options.gate(userId, notice);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
this.logger.warn(`gate failed for ${userId}/${notice.key}: ${error.message}`);
|
|
206
|
+
return (this.options.gateOnError ?? "allow") === "allow";
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async resolveRecipients(to) {
|
|
210
|
+
if (typeof to === "string") return [to];
|
|
211
|
+
if (Array.isArray(to)) return to;
|
|
212
|
+
if (!this.options.recipients) throw new Error("NotificationsModule: notify({ to: { scope } }) needs the `recipients` option.");
|
|
213
|
+
return this.options.recipients(to.scope);
|
|
214
|
+
}
|
|
215
|
+
async explicitChoices(userIds, key, scope) {
|
|
216
|
+
if (this.prefs.listForUsers) return this.prefs.listForUsers(userIds, key, scope);
|
|
217
|
+
const out = /* @__PURE__ */ new Map();
|
|
218
|
+
for (const id of userIds) {
|
|
219
|
+
const v = (await this.prefs.list(id, scope))[key];
|
|
220
|
+
if (v !== void 0) out.set(id, v);
|
|
221
|
+
}
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
NotifierService = __decorateClass([
|
|
226
|
+
Injectable(),
|
|
227
|
+
__decorateParam(0, Inject(NOTIFICATIONS_OPTIONS)),
|
|
228
|
+
__decorateParam(1, Inject(NOTIFICATION_PREFERENCES)),
|
|
229
|
+
__decorateParam(2, Inject(NOTIFICATION_DEVICES)),
|
|
230
|
+
__decorateParam(3, Inject(PUSH))
|
|
231
|
+
], NotifierService);
|
|
232
|
+
|
|
233
|
+
// src/notifications/notifications.controller.ts
|
|
234
|
+
function parseDevice(body) {
|
|
235
|
+
if (body && typeof body.token === "string" && body.token) return { kind: "fcm", token: body.token };
|
|
236
|
+
if (body && typeof body.endpoint === "string" && body.endpoint && typeof body.keys?.p256dh === "string" && typeof body.keys?.auth === "string") {
|
|
237
|
+
return { kind: "webpush", endpoint: body.endpoint, keys: { p256dh: body.keys.p256dh, auth: body.keys.auth } };
|
|
238
|
+
}
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
function createNotificationsController(path, opts = {}) {
|
|
242
|
+
const userOf = (req) => {
|
|
243
|
+
const id = opts.getUserId ? opts.getUserId(req) : req?.user?.sub;
|
|
244
|
+
if (!id) throw new UnauthorizedAppException();
|
|
245
|
+
return id;
|
|
246
|
+
};
|
|
247
|
+
const scopeOf = (req) => opts.getPreferenceScope?.(req);
|
|
248
|
+
let NotificationsController = class {
|
|
249
|
+
constructor(notifier, devices, _options) {
|
|
250
|
+
this.notifier = notifier;
|
|
251
|
+
this.devices = devices;
|
|
252
|
+
}
|
|
253
|
+
async register(req, body) {
|
|
254
|
+
const userId = userOf(req);
|
|
255
|
+
const device = parseDevice(body);
|
|
256
|
+
if (!device) throw new ValidationAppException("Send `token` (FCM) or `endpoint` + `keys` (Web Push).", void 0, "INVALID_DEVICE");
|
|
257
|
+
const enabled = typeof body.enabled === "boolean" ? body.enabled : void 0;
|
|
258
|
+
const platform = typeof body.platform === "string" ? body.platform : void 0;
|
|
259
|
+
return this.devices.register(userId, device, { platform, enabled });
|
|
260
|
+
}
|
|
261
|
+
async unregister(req, id) {
|
|
262
|
+
const removed = await this.devices.unregister(userOf(req), id);
|
|
263
|
+
if (!removed) throw new NotFoundAppException("Device not found", void 0, "DEVICE_NOT_FOUND");
|
|
264
|
+
return { removed: true };
|
|
265
|
+
}
|
|
266
|
+
preferences(req) {
|
|
267
|
+
return this.notifier.preferencesOf(userOf(req), scopeOf(req));
|
|
268
|
+
}
|
|
269
|
+
async save(req, body) {
|
|
270
|
+
const userId = userOf(req);
|
|
271
|
+
if (typeof body?.key !== "string" || typeof body?.enabled !== "boolean") {
|
|
272
|
+
throw new ValidationAppException("Send `key` (string) and `enabled` (boolean).", void 0, "INVALID_PREFERENCE");
|
|
273
|
+
}
|
|
274
|
+
const ok = await this.notifier.setPreference(userId, body.key, body.enabled, scopeOf(req));
|
|
275
|
+
if (!ok) throw new ValidationAppException(`"${body.key}" is not a switch.`, void 0, "UNKNOWN_NOTICE");
|
|
276
|
+
return this.notifier.preferencesOf(userId, scopeOf(req));
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
__decorateClass([
|
|
280
|
+
Post("devices"),
|
|
281
|
+
__decorateParam(0, Req()),
|
|
282
|
+
__decorateParam(1, Body())
|
|
283
|
+
], NotificationsController.prototype, "register", 1);
|
|
284
|
+
__decorateClass([
|
|
285
|
+
Delete("devices/:id"),
|
|
286
|
+
__decorateParam(0, Req()),
|
|
287
|
+
__decorateParam(1, Param("id"))
|
|
288
|
+
], NotificationsController.prototype, "unregister", 1);
|
|
289
|
+
__decorateClass([
|
|
290
|
+
Get("preferences"),
|
|
291
|
+
__decorateParam(0, Req())
|
|
292
|
+
], NotificationsController.prototype, "preferences", 1);
|
|
293
|
+
__decorateClass([
|
|
294
|
+
Put("preferences"),
|
|
295
|
+
__decorateParam(0, Req()),
|
|
296
|
+
__decorateParam(1, Body())
|
|
297
|
+
], NotificationsController.prototype, "save", 1);
|
|
298
|
+
NotificationsController = __decorateClass([
|
|
299
|
+
Controller(path),
|
|
300
|
+
__decorateParam(1, Inject2(NOTIFICATION_DEVICES)),
|
|
301
|
+
__decorateParam(2, Inject2(NOTIFICATIONS_OPTIONS))
|
|
302
|
+
], NotificationsController);
|
|
303
|
+
return NotificationsController;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// src/notifications/notifications.options.ts
|
|
307
|
+
var DEFAULT_NOTIFICATIONS_PATH = "notifications";
|
|
308
|
+
|
|
309
|
+
// src/notifications/notifications.module.ts
|
|
310
|
+
function providers(optionsProvider) {
|
|
311
|
+
return [
|
|
312
|
+
optionsProvider,
|
|
313
|
+
{
|
|
314
|
+
provide: NOTIFICATION_PREFERENCES,
|
|
315
|
+
useFactory: (o) => o.preferences ?? new MemoryPreferenceStore(),
|
|
316
|
+
inject: [NOTIFICATIONS_OPTIONS]
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
provide: NOTIFICATION_DEVICES,
|
|
320
|
+
useFactory: (o) => o.devices ?? new MemoryDeviceStore(),
|
|
321
|
+
inject: [NOTIFICATIONS_OPTIONS]
|
|
322
|
+
},
|
|
323
|
+
NotifierService
|
|
324
|
+
];
|
|
325
|
+
}
|
|
326
|
+
function controllerOf(options) {
|
|
327
|
+
const c = options.controller;
|
|
328
|
+
if (!c) return [];
|
|
329
|
+
const opts = c === true ? {} : c;
|
|
330
|
+
const d = options.devices;
|
|
331
|
+
if (d && (typeof d.register !== "function" || typeof d.unregister !== "function")) {
|
|
332
|
+
throw new Error("NotificationsModule: the controller needs `devices` to implement register() and unregister() (DeviceRegistryPort).");
|
|
333
|
+
}
|
|
334
|
+
return [createNotificationsController(opts.path ?? DEFAULT_NOTIFICATIONS_PATH, opts)];
|
|
335
|
+
}
|
|
336
|
+
var NotificationsModule = class {
|
|
337
|
+
static forRoot(options) {
|
|
338
|
+
return {
|
|
339
|
+
module: NotificationsModule,
|
|
340
|
+
controllers: controllerOf(options),
|
|
341
|
+
providers: providers({ provide: NOTIFICATIONS_OPTIONS, useValue: options }),
|
|
342
|
+
exports: [NOTIFICATIONS_OPTIONS, NOTIFICATION_PREFERENCES, NOTIFICATION_DEVICES, NotifierService]
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* `controller` and `devices` decide the controllers at registration time, so pass
|
|
347
|
+
* `controller` here (not in the factory) when you want it mounted.
|
|
348
|
+
*/
|
|
349
|
+
static forRootAsync(config) {
|
|
350
|
+
return {
|
|
351
|
+
module: NotificationsModule,
|
|
352
|
+
imports: config.imports ?? [],
|
|
353
|
+
controllers: controllerOf({ controller: config.controller }),
|
|
354
|
+
providers: providers({ provide: NOTIFICATIONS_OPTIONS, useFactory: config.useFactory, inject: config.inject ?? [] }),
|
|
355
|
+
exports: [NOTIFICATIONS_OPTIONS, NOTIFICATION_PREFERENCES, NOTIFICATION_DEVICES, NotifierService]
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
NotificationsModule = __decorateClass([
|
|
360
|
+
Global(),
|
|
361
|
+
Module({})
|
|
362
|
+
], NotificationsModule);
|
|
363
|
+
export {
|
|
364
|
+
DEFAULT_NOTIFICATIONS_PATH,
|
|
365
|
+
MemoryDeviceStore,
|
|
366
|
+
MemoryPreferenceStore,
|
|
367
|
+
NOTIFICATIONS_OPTIONS,
|
|
368
|
+
NOTIFICATION_DEVICES,
|
|
369
|
+
NOTIFICATION_PREFERENCES,
|
|
370
|
+
NotificationsModule,
|
|
371
|
+
NotifierService,
|
|
372
|
+
createNotificationsController,
|
|
373
|
+
parseDevice,
|
|
374
|
+
validateCatalog
|
|
375
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PushDevice, StoredPushDevice } from '../push/push.port.js';
|
|
2
|
+
import type { DeviceRegistryPort, PreferenceStorePort } from './notifications.port.js';
|
|
3
|
+
/** In-process preferences: tests and a single instance. */
|
|
4
|
+
export declare class MemoryPreferenceStore implements PreferenceStorePort {
|
|
5
|
+
private readonly rows;
|
|
6
|
+
list(userId: string, scope?: string): Promise<Record<string, boolean>>;
|
|
7
|
+
save(userId: string, key: string, enabled: boolean, scope?: string): Promise<void>;
|
|
8
|
+
listForUsers(userIds: string[], key: string, scope?: string): Promise<Map<string, boolean>>;
|
|
9
|
+
}
|
|
10
|
+
/** In-process devices: tests and a single instance. */
|
|
11
|
+
export declare class MemoryDeviceStore implements DeviceRegistryPort {
|
|
12
|
+
private readonly rows;
|
|
13
|
+
listByUser(userId: string): Promise<StoredPushDevice[]>;
|
|
14
|
+
remove(id: string): Promise<void>;
|
|
15
|
+
register(userId: string, device: PushDevice, meta?: {
|
|
16
|
+
platform?: string | null;
|
|
17
|
+
enabled?: boolean;
|
|
18
|
+
}): Promise<{
|
|
19
|
+
id: string;
|
|
20
|
+
enabled: boolean;
|
|
21
|
+
}>;
|
|
22
|
+
unregister(userId: string, id: string): Promise<boolean>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Type } from '@nestjs/common';
|
|
2
|
+
import type { PushDevice } from '../push/push.port.js';
|
|
3
|
+
import type { NotificationsControllerOptions } from './notifications.options.js';
|
|
4
|
+
/** Body of `POST devices`: an FCM token, or a Web Push subscription. */
|
|
5
|
+
export declare function parseDevice(body: any): PushDevice | null;
|
|
6
|
+
/**
|
|
7
|
+
* Optional HTTP surface: register/unregister a device, read/save the switches.
|
|
8
|
+
* A mixin so the route is configurable. It carries NO guard: the host puts its
|
|
9
|
+
* own (global guard / `APP_GUARD`); without a user it answers 401.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createNotificationsController(path: string, opts?: NotificationsControllerOptions): Type<unknown>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { type NotificationsControllerOptions, type NotificationsOptions } from './notifications.options.js';
|
|
3
|
+
/**
|
|
4
|
+
* Notices with a catalog, per-user switches and a notifier that never throws,
|
|
5
|
+
* on top of `PushModule` (import it too: the notifier injects `PUSH`).
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* NotificationsModule.forRoot({
|
|
9
|
+
* catalog: [{ key: 'order_ready', group: 'Orders', defaultOn: true }],
|
|
10
|
+
* preferences: new TypeOrmPreferenceStore(ds), devices: new TypeOrmDeviceStore(ds),
|
|
11
|
+
* recipients: (storeId: string) => members.idsOf(storeId),
|
|
12
|
+
* controller: { path: 'notifications' },
|
|
13
|
+
* })
|
|
14
|
+
* // anywhere: await notifier.notify('order_ready', { to: userId, title, body })
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Inject `NotifierService`.
|
|
18
|
+
*/
|
|
19
|
+
export declare class NotificationsModule {
|
|
20
|
+
static forRoot<S = any>(options: NotificationsOptions<S>): DynamicModule;
|
|
21
|
+
/**
|
|
22
|
+
* `controller` and `devices` decide the controllers at registration time, so pass
|
|
23
|
+
* `controller` here (not in the factory) when you want it mounted.
|
|
24
|
+
*/
|
|
25
|
+
static forRootAsync<S = any>(config: {
|
|
26
|
+
imports?: any[];
|
|
27
|
+
inject?: any[];
|
|
28
|
+
useFactory: (...args: any[]) => NotificationsOptions<S> | Promise<NotificationsOptions<S>>;
|
|
29
|
+
controller?: boolean | NotificationsControllerOptions;
|
|
30
|
+
}): DynamicModule;
|
|
31
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { NoticeDefinition, PreferenceStorePort, DeviceRegistryPort } from './notifications.port.js';
|
|
2
|
+
import type { PushDeviceStorePort } from '../push/push.port.js';
|
|
3
|
+
export interface NotificationsControllerOptions {
|
|
4
|
+
/** Route prefix. Default `notifications`. */
|
|
5
|
+
path?: string;
|
|
6
|
+
/** Who is asking. Default `req.user.sub`. */
|
|
7
|
+
getUserId?: (request: any) => string | undefined;
|
|
8
|
+
/** Preference partition of the request (e.g. the current store). Default none. */
|
|
9
|
+
getPreferenceScope?: (request: any) => string | undefined;
|
|
10
|
+
}
|
|
11
|
+
export interface NotificationsOptions<S = any> {
|
|
12
|
+
/** Every notice the app may send. Anything else is dropped. */
|
|
13
|
+
catalog: NoticeDefinition[];
|
|
14
|
+
/** Per-user switches. Default: in-process memory (tests, single instance). */
|
|
15
|
+
preferences?: PreferenceStorePort;
|
|
16
|
+
/** The user's devices. Default: in-process memory. Needs `register/unregister` only if the controller is on. */
|
|
17
|
+
devices?: PushDeviceStorePort | DeviceRegistryPort;
|
|
18
|
+
/** Turns a scope (a store, a role…) into user ids. Required only if you notify with `{ scope }`. */
|
|
19
|
+
recipients?: (scope: S) => Promise<string[]>;
|
|
20
|
+
/** Last word per user and notice (plan, membership…). `false` skips that user. */
|
|
21
|
+
gate?: (userId: string, notice: NoticeDefinition) => boolean | Promise<boolean>;
|
|
22
|
+
/** What a throwing `gate` means. Default `'allow'`: not knowing is worse than sending. */
|
|
23
|
+
gateOnError?: 'allow' | 'deny';
|
|
24
|
+
/** `notify` never throws; failures are logged and reported in the result. Default `true`. */
|
|
25
|
+
neverThrow?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Mount the HTTP controller (register device, read/save preferences). It has NO guard of its own:
|
|
28
|
+
* put yours (global guard, or `APP_GUARD`) — or `getUserId` returns nothing and it answers 401.
|
|
29
|
+
* `false`/omitted = not mounted.
|
|
30
|
+
*/
|
|
31
|
+
controller?: boolean | NotificationsControllerOptions;
|
|
32
|
+
}
|
|
33
|
+
export declare const DEFAULT_NOTIFICATIONS_PATH = "notifications";
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { PushDevice, PushDeviceStorePort, PushMessage } from '../push/push.port.js';
|
|
2
|
+
/** DI tokens. */
|
|
3
|
+
export declare const NOTIFICATIONS_OPTIONS: unique symbol;
|
|
4
|
+
export declare const NOTIFICATION_PREFERENCES: unique symbol;
|
|
5
|
+
export declare const NOTIFICATION_DEVICES: unique symbol;
|
|
6
|
+
/** One kind of notice the app is allowed to send. Anything not listed here is never sent. */
|
|
7
|
+
export interface NoticeDefinition {
|
|
8
|
+
/** Stable id of the notice (travels in `data.notice`). */
|
|
9
|
+
key: string;
|
|
10
|
+
/** Section in the settings screen ("Team", "Cash register"…). Free text. */
|
|
11
|
+
group: string;
|
|
12
|
+
/** Value for someone who never touched the switch. */
|
|
13
|
+
defaultOn: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Key of ANOTHER catalog entry whose switch governs this one: several notices sharing one toggle.
|
|
16
|
+
* The switch, and its default, are the governing entry's. Omit when the notice has its own.
|
|
17
|
+
*/
|
|
18
|
+
governedBy?: string;
|
|
19
|
+
/** Which kinds of device get it. Default: all. */
|
|
20
|
+
channels?: ReadonlyArray<PushDevice['kind']>;
|
|
21
|
+
/** Free label/description for the settings screen; the host translates. */
|
|
22
|
+
title?: string;
|
|
23
|
+
description?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Persistence for the per-user switches. `scope` is whatever partitions the
|
|
27
|
+
* preferences in the host (a store, a branch…); `undefined` when there is none.
|
|
28
|
+
*/
|
|
29
|
+
export interface PreferenceStorePort {
|
|
30
|
+
/** Explicit choices of one user. Missing key = never touched. */
|
|
31
|
+
list(userId: string, scope?: string): Promise<Record<string, boolean>>;
|
|
32
|
+
save(userId: string, key: string, enabled: boolean, scope?: string): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Explicit choices for ONE key across several users (fan-out without N queries).
|
|
35
|
+
* Optional: without it the notifier calls `list` per user.
|
|
36
|
+
*/
|
|
37
|
+
listForUsers?(userIds: string[], key: string, scope?: string): Promise<Map<string, boolean>>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A device store that can also register/unregister. Only the controller needs
|
|
41
|
+
* the two extra methods; sending needs {@link PushDeviceStorePort}.
|
|
42
|
+
*/
|
|
43
|
+
export interface DeviceRegistryPort extends PushDeviceStorePort {
|
|
44
|
+
/** Upsert by token/endpoint: registering again must not duplicate. `enabled` omitted = leave as is. */
|
|
45
|
+
register(userId: string, device: PushDevice, meta?: {
|
|
46
|
+
platform?: string | null;
|
|
47
|
+
enabled?: boolean;
|
|
48
|
+
}): Promise<{
|
|
49
|
+
id: string;
|
|
50
|
+
enabled: boolean;
|
|
51
|
+
}>;
|
|
52
|
+
/** Only deletes if the device belongs to that user. */
|
|
53
|
+
unregister(userId: string, id: string): Promise<boolean>;
|
|
54
|
+
}
|
|
55
|
+
export interface NotifyInput<S = unknown> extends Omit<PushMessage, 'data' | 'title' | 'body'> {
|
|
56
|
+
/** User ids, or `{ scope }` to resolve them through `recipients`. */
|
|
57
|
+
to: string | string[] | {
|
|
58
|
+
scope: S;
|
|
59
|
+
};
|
|
60
|
+
title: PushMessage['title'];
|
|
61
|
+
body: PushMessage['body'];
|
|
62
|
+
data?: Record<string, unknown>;
|
|
63
|
+
/** Partition for the preferences (same as the one the user saved them under). */
|
|
64
|
+
preferenceScope?: string;
|
|
65
|
+
/** Users left out (usually whoever triggered the notice). */
|
|
66
|
+
exclude?: string[];
|
|
67
|
+
/** Language for `I18nMessage`s. */
|
|
68
|
+
lang?: string;
|
|
69
|
+
}
|
|
70
|
+
export type SkipReason = 'not_in_catalog' | 'disabled' | 'gate' | 'no_devices' | 'no_recipients' | 'error';
|
|
71
|
+
export interface NotifyResult {
|
|
72
|
+
/** The notice was in the catalog and processed (even if nobody got it). */
|
|
73
|
+
accepted: boolean;
|
|
74
|
+
/** Devices that took the message. */
|
|
75
|
+
sent: number;
|
|
76
|
+
/** Devices that failed (stale ones are already deleted). */
|
|
77
|
+
failed: number;
|
|
78
|
+
/** Users who got at least one device. */
|
|
79
|
+
delivered: string[];
|
|
80
|
+
skipped: Array<{
|
|
81
|
+
userId?: string;
|
|
82
|
+
reason: SkipReason;
|
|
83
|
+
}>;
|
|
84
|
+
}
|
|
85
|
+
/** What the injectable notifier exposes. */
|
|
86
|
+
export interface NotifierPort<S = unknown> {
|
|
87
|
+
notify(key: string, input: NotifyInput<S>): Promise<NotifyResult>;
|
|
88
|
+
}
|
|
89
|
+
/** One switch as the settings screen shows it. */
|
|
90
|
+
export interface PreferenceView {
|
|
91
|
+
key: string;
|
|
92
|
+
group: string;
|
|
93
|
+
title?: string;
|
|
94
|
+
description?: string;
|
|
95
|
+
enabled: boolean;
|
|
96
|
+
defaultOn: boolean;
|
|
97
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type PushPort, type PushDeviceStorePort } from '../push/push.port.js';
|
|
2
|
+
import type { NotificationsOptions } from './notifications.options.js';
|
|
3
|
+
import { type NoticeDefinition, type NotifierPort, type NotifyInput, type NotifyResult, type PreferenceStorePort, type PreferenceView } from './notifications.port.js';
|
|
4
|
+
/** Throws at boot when the catalog is incoherent: a switch that points nowhere would be a lie. */
|
|
5
|
+
export declare function validateCatalog(catalog: NoticeDefinition[]): Map<string, NoticeDefinition>;
|
|
6
|
+
/**
|
|
7
|
+
* Sends only what the catalog lists, only to who left it on, only to the
|
|
8
|
+
* devices the notice's channels allow. Never throws unless `neverThrow` is
|
|
9
|
+
* `false`: notifying is an extra and must not break what triggered it.
|
|
10
|
+
*/
|
|
11
|
+
export declare class NotifierService<S = any> implements NotifierPort<S> {
|
|
12
|
+
private readonly options;
|
|
13
|
+
private readonly prefs;
|
|
14
|
+
private readonly devices;
|
|
15
|
+
private readonly push;
|
|
16
|
+
private readonly logger;
|
|
17
|
+
private readonly catalog;
|
|
18
|
+
constructor(options: NotificationsOptions<S>, prefs: PreferenceStorePort, devices: PushDeviceStorePort, push: PushPort);
|
|
19
|
+
/** The key whose switch governs `notice`. */
|
|
20
|
+
private governing;
|
|
21
|
+
/** Switches the user can flip (entries that are not governed by another). */
|
|
22
|
+
preferencesOf(userId: string, scope?: string): Promise<PreferenceView[]>;
|
|
23
|
+
/** Returns false when the key is not a user-facing switch (not in the catalog, or governed by another). */
|
|
24
|
+
setPreference(userId: string, key: string, enabled: boolean, scope?: string): Promise<boolean>;
|
|
25
|
+
notify(key: string, input: NotifyInput<S>): Promise<NotifyResult>;
|
|
26
|
+
private run;
|
|
27
|
+
private deliver;
|
|
28
|
+
private passesGate;
|
|
29
|
+
private resolveRecipients;
|
|
30
|
+
private explicitChoices;
|
|
31
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AES-256-GCM for third-party secrets stored per tenant (Mercado Pago tokens, Clip keys).
|
|
3
|
+
* Format: `base64(iv).base64(tag).base64(ciphertext)`. The key is 64 hex chars (32 bytes).
|
|
4
|
+
*/
|
|
5
|
+
export declare class CredentialsCipher {
|
|
6
|
+
private readonly key;
|
|
7
|
+
constructor(key: Buffer);
|
|
8
|
+
/** `null` when the value is missing or not 64 hex chars: the feature that needs it should switch itself off. */
|
|
9
|
+
static fromHex(hex: string | undefined | null): CredentialsCipher | null;
|
|
10
|
+
encrypt(text: string): string;
|
|
11
|
+
decrypt(packed: string): string;
|
|
12
|
+
encryptJson(value: unknown): string;
|
|
13
|
+
decryptJson<T>(packed: string): T;
|
|
14
|
+
}
|