@miguelmorales13/nestkit 0.25.0 → 0.28.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 +5 -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 +123 -115
- package/dist/auth/oauth/index.js +25 -17
- package/dist/auth/oauth/oauth-provider.d.ts +9 -0
- package/dist/auth/oauth/providers/apple.d.ts +3 -1
- package/dist/bootstrap/apply-defaults.d.ts +9 -0
- package/dist/bootstrap/index.cjs +6 -5
- package/dist/bootstrap/index.js +5 -4
- 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-A2DGK4ZL.js → chunk-5LVGCTJ3.js} +1 -1
- 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-MSTXS3MU.cjs → chunk-JJKGQNGD.cjs} +2 -2
- package/dist/{chunk-FNUZGYUF.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-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-UNEVXCFW.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 +31 -30
- 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/index.cjs +7 -7
- package/dist/payments/index.js +1 -1
- 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 +119 -2
- package/dist/security/index.d.ts +5 -0
- package/dist/security/index.js +119 -2
- 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 +107 -2
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import {
|
|
2
|
+
guard_exports
|
|
3
|
+
} from "../chunk-A7FI6HXV.js";
|
|
4
|
+
import {
|
|
5
|
+
compareVersions,
|
|
6
|
+
isAcceptedVersionValid,
|
|
7
|
+
versionAceptadaVigente
|
|
8
|
+
} from "../chunk-3TUMVLPJ.js";
|
|
9
|
+
import {
|
|
10
|
+
AuditService
|
|
11
|
+
} from "../chunk-NJYLQDB3.js";
|
|
12
|
+
import "../chunk-ETRUVZTJ.js";
|
|
13
|
+
import {
|
|
14
|
+
ForbiddenAppException,
|
|
15
|
+
ValidationAppException
|
|
16
|
+
} from "../chunk-ZFIDAX7K.js";
|
|
17
|
+
import "../chunk-M3GQDRWR.js";
|
|
18
|
+
import "../chunk-VVMRZEIW.js";
|
|
19
|
+
import {
|
|
20
|
+
__decorateClass,
|
|
21
|
+
__decorateParam
|
|
22
|
+
} from "../chunk-D3DDJWUD.js";
|
|
23
|
+
|
|
24
|
+
// src/consent/consent.module.ts
|
|
25
|
+
import { Global, Module } from "@nestjs/common";
|
|
26
|
+
|
|
27
|
+
// src/consent/consent.interceptor.ts
|
|
28
|
+
import {
|
|
29
|
+
Inject as Inject2,
|
|
30
|
+
Injectable as Injectable2
|
|
31
|
+
} from "@nestjs/common";
|
|
32
|
+
|
|
33
|
+
// src/consent/consent.options.ts
|
|
34
|
+
var CONSENT_OPTIONS = /* @__PURE__ */ Symbol("CONSENT_OPTIONS");
|
|
35
|
+
var DEFAULT_CONSENT_CODE = "CONSENT_PENDIENTE";
|
|
36
|
+
var DEFAULT_CONSENT_TTL_MS = 6e4;
|
|
37
|
+
var DEFAULT_CONSENT_TEXT = "Antes de continuar acepta los documentos legales vigentes.";
|
|
38
|
+
|
|
39
|
+
// src/consent/consent.service.ts
|
|
40
|
+
import { Inject, Injectable, Optional } from "@nestjs/common";
|
|
41
|
+
|
|
42
|
+
// src/consent/consent.port.ts
|
|
43
|
+
var CONSENT_STORE = /* @__PURE__ */ Symbol("CONSENT_STORE");
|
|
44
|
+
|
|
45
|
+
// src/consent/consent.service.ts
|
|
46
|
+
var ConsentService = class {
|
|
47
|
+
constructor(options, store, audit) {
|
|
48
|
+
this.options = options;
|
|
49
|
+
this.store = store;
|
|
50
|
+
this.audit = audit;
|
|
51
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
52
|
+
}
|
|
53
|
+
now() {
|
|
54
|
+
return (this.options.now ?? Date.now)();
|
|
55
|
+
}
|
|
56
|
+
ttl() {
|
|
57
|
+
return this.options.ttlCache ?? DEFAULT_CONSENT_TTL_MS;
|
|
58
|
+
}
|
|
59
|
+
doc(docId) {
|
|
60
|
+
const d = this.options.documentos.find((x) => x.id === docId);
|
|
61
|
+
if (!d) throw new ValidationAppException(`Unknown consent document: ${docId}`);
|
|
62
|
+
return d;
|
|
63
|
+
}
|
|
64
|
+
remember(userId, docId, version) {
|
|
65
|
+
if (this.ttl() > 0) this.cache.set(`${userId}\0${docId}`, { version, until: this.now() + this.ttl() });
|
|
66
|
+
}
|
|
67
|
+
/** Accepted version of one document (cached), or `null`. */
|
|
68
|
+
async acceptedVersion(userId, docId) {
|
|
69
|
+
const key = `${userId}\0${docId}`;
|
|
70
|
+
const hit = this.cache.get(key);
|
|
71
|
+
if (hit && hit.until > this.now()) return hit.version;
|
|
72
|
+
const version = (await this.store.get(userId, docId))?.version ?? null;
|
|
73
|
+
this.remember(userId, docId, version);
|
|
74
|
+
return version;
|
|
75
|
+
}
|
|
76
|
+
/** Pure check over a version: at or above the document's minimum. */
|
|
77
|
+
isValid(docId, version) {
|
|
78
|
+
return versionAceptadaVigente(version, this.doc(docId).minima);
|
|
79
|
+
}
|
|
80
|
+
/** Ids of the documents the user has not accepted at the minimum version. */
|
|
81
|
+
async pending(userId) {
|
|
82
|
+
const out = [];
|
|
83
|
+
for (const d of this.options.documentos) {
|
|
84
|
+
if (!this.isValid(d.id, await this.acceptedVersion(userId, d.id))) out.push(d.id);
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
/** True when every document is accepted at >= its minimum. */
|
|
89
|
+
async hasAccepted(userId) {
|
|
90
|
+
return (await this.pending(userId)).length === 0;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Records the acceptance of the **current** version. Idempotent: if that
|
|
94
|
+
* version is already stored, the original receipt (date included) is kept
|
|
95
|
+
* and nothing is audited again.
|
|
96
|
+
*/
|
|
97
|
+
async accept(userId, docId, meta) {
|
|
98
|
+
const d = this.doc(docId);
|
|
99
|
+
const previous = await this.store.get(userId, docId);
|
|
100
|
+
if (previous && previous.version === d.version) {
|
|
101
|
+
this.remember(userId, docId, d.version);
|
|
102
|
+
return previous;
|
|
103
|
+
}
|
|
104
|
+
const record = { version: d.version, acceptedAt: new Date(this.now()), meta };
|
|
105
|
+
await this.store.save(userId, docId, record);
|
|
106
|
+
this.remember(userId, docId, d.version);
|
|
107
|
+
await this.audit?.record({
|
|
108
|
+
action: "consent.accepted",
|
|
109
|
+
resource: docId,
|
|
110
|
+
resourceId: userId,
|
|
111
|
+
after: { version: d.version },
|
|
112
|
+
metadata: meta
|
|
113
|
+
});
|
|
114
|
+
return record;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
ConsentService = __decorateClass([
|
|
118
|
+
Injectable(),
|
|
119
|
+
__decorateParam(0, Inject(CONSENT_OPTIONS)),
|
|
120
|
+
__decorateParam(1, Inject(CONSENT_STORE)),
|
|
121
|
+
__decorateParam(2, Optional()),
|
|
122
|
+
__decorateParam(2, Inject(AuditService))
|
|
123
|
+
], ConsentService);
|
|
124
|
+
|
|
125
|
+
// src/consent/consent.interceptor.ts
|
|
126
|
+
var ConsentInterceptor = class {
|
|
127
|
+
constructor(options, consent) {
|
|
128
|
+
this.options = options;
|
|
129
|
+
this.consent = consent;
|
|
130
|
+
}
|
|
131
|
+
async intercept(ctx, next) {
|
|
132
|
+
const req = ctx.switchToHttp().getRequest();
|
|
133
|
+
const userId = this.options.getUserId ? this.options.getUserId(req) : req?.user?.sub;
|
|
134
|
+
if (userId) {
|
|
135
|
+
const path = req.path ?? String(req.url ?? "").split("?")[0];
|
|
136
|
+
const free = (this.options.rutasLibres ?? []).some((r) => r.test(path));
|
|
137
|
+
if (!free) {
|
|
138
|
+
const pending = await this.consent.pending(userId);
|
|
139
|
+
if (pending.length > 0) {
|
|
140
|
+
const docs = this.options.documentos.filter((d) => pending.includes(d.id));
|
|
141
|
+
throw new ForbiddenAppException(
|
|
142
|
+
this.options.textoError ?? DEFAULT_CONSENT_TEXT,
|
|
143
|
+
{
|
|
144
|
+
pendientes: pending,
|
|
145
|
+
version: docs[0]?.version,
|
|
146
|
+
versiones: Object.fromEntries(docs.map((d) => [d.id, d.version]))
|
|
147
|
+
},
|
|
148
|
+
this.options.codigoError ?? DEFAULT_CONSENT_CODE
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return next.handle();
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
ConsentInterceptor = __decorateClass([
|
|
157
|
+
Injectable2(),
|
|
158
|
+
__decorateParam(0, Inject2(CONSENT_OPTIONS))
|
|
159
|
+
], ConsentInterceptor);
|
|
160
|
+
|
|
161
|
+
// src/consent/memory.store.ts
|
|
162
|
+
var MemoryConsentStore = class {
|
|
163
|
+
constructor() {
|
|
164
|
+
this.rows = /* @__PURE__ */ new Map();
|
|
165
|
+
}
|
|
166
|
+
async get(userId, docId) {
|
|
167
|
+
return this.rows.get(`${userId}\0${docId}`) ?? null;
|
|
168
|
+
}
|
|
169
|
+
async save(userId, docId, record) {
|
|
170
|
+
this.rows.set(`${userId}\0${docId}`, record);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// src/consent/consent.module.ts
|
|
175
|
+
var ConsentModule = class {
|
|
176
|
+
static forRoot(options) {
|
|
177
|
+
const providers = [
|
|
178
|
+
{ provide: CONSENT_OPTIONS, useValue: options },
|
|
179
|
+
{ provide: CONSENT_STORE, useValue: options.puertoDeAceptaciones ?? new MemoryConsentStore() },
|
|
180
|
+
ConsentService,
|
|
181
|
+
ConsentInterceptor
|
|
182
|
+
];
|
|
183
|
+
return {
|
|
184
|
+
module: ConsentModule,
|
|
185
|
+
providers,
|
|
186
|
+
exports: [CONSENT_OPTIONS, CONSENT_STORE, ConsentService, ConsentInterceptor]
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
static forRootAsync(config) {
|
|
190
|
+
const providers = [
|
|
191
|
+
{ provide: CONSENT_OPTIONS, useFactory: config.useFactory, inject: config.inject ?? [] },
|
|
192
|
+
{
|
|
193
|
+
provide: CONSENT_STORE,
|
|
194
|
+
useFactory: (o) => o.puertoDeAceptaciones ?? new MemoryConsentStore(),
|
|
195
|
+
inject: [CONSENT_OPTIONS]
|
|
196
|
+
},
|
|
197
|
+
ConsentService,
|
|
198
|
+
ConsentInterceptor
|
|
199
|
+
];
|
|
200
|
+
return {
|
|
201
|
+
module: ConsentModule,
|
|
202
|
+
imports: config.imports ?? [],
|
|
203
|
+
providers,
|
|
204
|
+
exports: [CONSENT_OPTIONS, CONSENT_STORE, ConsentService, ConsentInterceptor]
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
ConsentModule = __decorateClass([
|
|
209
|
+
Global(),
|
|
210
|
+
Module({})
|
|
211
|
+
], ConsentModule);
|
|
212
|
+
|
|
213
|
+
// src/consent/typeorm.store.ts
|
|
214
|
+
var TypeOrmConsentStore = class {
|
|
215
|
+
constructor(db, options = {}) {
|
|
216
|
+
this.db = db;
|
|
217
|
+
this.table = assertIdent(options.table ?? "consent_acceptance");
|
|
218
|
+
}
|
|
219
|
+
async get(userId, docId) {
|
|
220
|
+
const rows = await this.db.query(
|
|
221
|
+
`SELECT version, accepted_at, meta FROM "${this.table}" WHERE user_id = $1 AND doc_id = $2 LIMIT 1`,
|
|
222
|
+
[userId, docId]
|
|
223
|
+
);
|
|
224
|
+
const r = Array.isArray(rows) ? rows[0] : rows?.rows?.[0];
|
|
225
|
+
if (!r) return null;
|
|
226
|
+
return {
|
|
227
|
+
version: r.version,
|
|
228
|
+
acceptedAt: r.accepted_at instanceof Date ? r.accepted_at : new Date(r.accepted_at),
|
|
229
|
+
meta: r.meta ?? void 0
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
async save(userId, docId, record) {
|
|
233
|
+
await this.db.query(
|
|
234
|
+
`INSERT INTO "${this.table}" (user_id, doc_id, version, accepted_at, meta)
|
|
235
|
+
VALUES ($1, $2, $3, $4, $5)
|
|
236
|
+
ON CONFLICT (user_id, doc_id)
|
|
237
|
+
DO UPDATE SET version = EXCLUDED.version, accepted_at = EXCLUDED.accepted_at, meta = EXCLUDED.meta`,
|
|
238
|
+
[userId, docId, record.version, record.acceptedAt, record.meta ? JSON.stringify(record.meta) : null]
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
function consentTableSql(table = "consent_acceptance") {
|
|
243
|
+
const t = assertIdent(table);
|
|
244
|
+
return `CREATE TABLE IF NOT EXISTS "${t}" (
|
|
245
|
+
user_id text NOT NULL,
|
|
246
|
+
doc_id text NOT NULL,
|
|
247
|
+
version text NOT NULL,
|
|
248
|
+
accepted_at timestamptz NOT NULL,
|
|
249
|
+
meta jsonb,
|
|
250
|
+
PRIMARY KEY (user_id, doc_id)
|
|
251
|
+
);`;
|
|
252
|
+
}
|
|
253
|
+
function assertIdent(name) {
|
|
254
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) throw new Error(`Invalid table name: ${name}`);
|
|
255
|
+
return name;
|
|
256
|
+
}
|
|
257
|
+
export {
|
|
258
|
+
CONSENT_OPTIONS,
|
|
259
|
+
CONSENT_STORE,
|
|
260
|
+
ConsentInterceptor,
|
|
261
|
+
ConsentModule,
|
|
262
|
+
ConsentService,
|
|
263
|
+
DEFAULT_CONSENT_CODE,
|
|
264
|
+
MemoryConsentStore,
|
|
265
|
+
TypeOrmConsentStore,
|
|
266
|
+
compareVersions,
|
|
267
|
+
guard_exports as consentGuard,
|
|
268
|
+
consentTableSql,
|
|
269
|
+
isAcceptedVersionValid,
|
|
270
|
+
versionAceptadaVigente
|
|
271
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ConsentRecord, ConsentStore } from './consent.port.js';
|
|
2
|
+
/** In-process store: fine for tests and a single instance, lost on restart. */
|
|
3
|
+
export declare class MemoryConsentStore implements ConsentStore {
|
|
4
|
+
private readonly rows;
|
|
5
|
+
get(userId: string, docId: string): Promise<ConsentRecord | null>;
|
|
6
|
+
save(userId: string, docId: string, record: ConsentRecord): Promise<void>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
var _chunkA6YGIZCQcjs = require('../../chunk-A6YGIZCQ.cjs');
|
|
6
|
+
require('../../chunk-LRX7AMSS.cjs');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
exports.compareVersions = _chunkA6YGIZCQcjs.compareVersions; exports.isAcceptedVersionValid = _chunkA6YGIZCQcjs.isAcceptedVersionValid; exports.versionAceptadaVigente = _chunkA6YGIZCQcjs.versionAceptadaVigente;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependency-free helpers, safe to import from a browser, a React app or a
|
|
3
|
+
* script (`@miguelmorales13/nestkit/consent/shared`). Nothing here touches Nest.
|
|
4
|
+
*
|
|
5
|
+
* Versions are ISO dates (`2026-10-15`), so lexicographic order is chronological
|
|
6
|
+
* order and they are compared as text.
|
|
7
|
+
*/
|
|
8
|
+
/** `-1 | 0 | 1` comparing two version strings as text. */
|
|
9
|
+
export declare function compareVersions(a: string, b: string): -1 | 0 | 1;
|
|
10
|
+
/**
|
|
11
|
+
* Is the accepted version still good? True when it is at or above the
|
|
12
|
+
* **minimum**. The minimum, not the current version, is what blocks: raising the
|
|
13
|
+
* current version for a minor wording change must not lock out clients that
|
|
14
|
+
* ship the older text and cannot render a new acceptance screen (the web in
|
|
15
|
+
* cache, an installed mobile app).
|
|
16
|
+
*/
|
|
17
|
+
export declare function versionAceptadaVigente(version: string | null | undefined, minima: string): boolean;
|
|
18
|
+
/** English alias of {@link versionAceptadaVigente}. */
|
|
19
|
+
export declare const isAcceptedVersionValid: typeof versionAceptadaVigente;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ConsentRecord, ConsentStore } from './consent.port.js';
|
|
2
|
+
/** The slice of a TypeORM `DataSource` (or any pg-like client) this store needs. */
|
|
3
|
+
export interface SqlRunner {
|
|
4
|
+
query(sql: string, params?: unknown[]): Promise<any>;
|
|
5
|
+
}
|
|
6
|
+
export interface TypeOrmConsentStoreOptions {
|
|
7
|
+
/** Table name. Defaults to `consent_acceptance`. Letters, digits and `_` only (it is interpolated). */
|
|
8
|
+
table?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Postgres store over any `{ query(sql, params) }` (a TypeORM `DataSource` fits).
|
|
12
|
+
* One row per (user, document), upserted. Create the table with {@link consentTableSql}.
|
|
13
|
+
*/
|
|
14
|
+
export declare class TypeOrmConsentStore implements ConsentStore {
|
|
15
|
+
private readonly db;
|
|
16
|
+
private readonly table;
|
|
17
|
+
constructor(db: SqlRunner, options?: TypeOrmConsentStoreOptions);
|
|
18
|
+
get(userId: string, docId: string): Promise<ConsentRecord | null>;
|
|
19
|
+
save(userId: string, docId: string, record: ConsentRecord): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
/** DDL for the table, to paste into a migration. */
|
|
22
|
+
export declare function consentTableSql(table?: string): string;
|
package/dist/crud/index.cjs
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
require('../chunk-W2T2VFLC.cjs');
|
|
5
|
+
var _chunkKQPJCVN3cjs = require('../chunk-KQPJCVN3.cjs');
|
|
7
6
|
require('../chunk-Y72D5L6I.cjs');
|
|
7
|
+
require('../chunk-VALDHJOO.cjs');
|
|
8
8
|
require('../chunk-SDWHKPXI.cjs');
|
|
9
|
-
require('../chunk-
|
|
9
|
+
require('../chunk-LRX7AMSS.cjs');
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
exports.BaseCrudService =
|
|
14
|
+
exports.BaseCrudService = _chunkKQPJCVN3cjs.BaseCrudService; exports.ListQuery = _chunkKQPJCVN3cjs.ListQuery; exports.createCrudController = _chunkKQPJCVN3cjs.createCrudController;
|
package/dist/crud/index.js
CHANGED
|
@@ -2,11 +2,11 @@ import {
|
|
|
2
2
|
BaseCrudService,
|
|
3
3
|
ListQuery,
|
|
4
4
|
createCrudController
|
|
5
|
-
} from "../chunk-
|
|
6
|
-
import "../chunk-ZJMLEP5H.js";
|
|
5
|
+
} from "../chunk-OSRH6KYZ.js";
|
|
7
6
|
import "../chunk-TCAMLNUZ.js";
|
|
7
|
+
import "../chunk-NJYLQDB3.js";
|
|
8
8
|
import "../chunk-ETRUVZTJ.js";
|
|
9
|
-
import "../chunk-
|
|
9
|
+
import "../chunk-D3DDJWUD.js";
|
|
10
10
|
export {
|
|
11
11
|
BaseCrudService,
|
|
12
12
|
ListQuery,
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
require('../../chunk-
|
|
5
|
+
var _chunk47IYXZHWcjs = require('../../chunk-47IYXZHW.cjs');
|
|
6
|
+
require('../../chunk-LRX7AMSS.cjs');
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
exports.PG_POOL =
|
|
11
|
+
exports.PG_POOL = _chunk47IYXZHWcjs.PG_POOL; exports.PgModule = _chunk47IYXZHWcjs.PgModule; exports.withTenantScope = _chunk47IYXZHWcjs.withTenantScope;
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
require('../../chunk-
|
|
5
|
+
var _chunkURLDTS2Qcjs = require('../../chunk-URLDTS2Q.cjs');
|
|
6
|
+
require('../../chunk-LRX7AMSS.cjs');
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
exports.SUPABASE_ANON_CLIENT =
|
|
11
|
+
exports.SUPABASE_ANON_CLIENT = _chunkURLDTS2Qcjs.SUPABASE_ANON_CLIENT; exports.SUPABASE_SERVICE_ROLE_CLIENT = _chunkURLDTS2Qcjs.SUPABASE_SERVICE_ROLE_CLIENT; exports.SupabaseModule = _chunkURLDTS2Qcjs.SupabaseModule;
|
|
@@ -2,8 +2,8 @@ import {
|
|
|
2
2
|
SUPABASE_ANON_CLIENT,
|
|
3
3
|
SUPABASE_SERVICE_ROLE_CLIENT,
|
|
4
4
|
SupabaseModule
|
|
5
|
-
} from "../../chunk-
|
|
6
|
-
import "../../chunk-
|
|
5
|
+
} from "../../chunk-45SW7FBE.js";
|
|
6
|
+
import "../../chunk-D3DDJWUD.js";
|
|
7
7
|
export {
|
|
8
8
|
SUPABASE_ANON_CLIENT,
|
|
9
9
|
SUPABASE_SERVICE_ROLE_CLIENT,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkLRX7AMSScjs = require('../../chunk-LRX7AMSS.cjs');
|
|
4
4
|
|
|
5
5
|
// src/database/typeorm/typeorm.module.ts
|
|
6
6
|
var _common = require('@nestjs/common');
|
|
@@ -48,7 +48,7 @@ var TypeOrmDbModule = class {
|
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
};
|
|
51
|
-
TypeOrmDbModule = exports.TypeOrmDbModule =
|
|
51
|
+
TypeOrmDbModule = exports.TypeOrmDbModule = _chunkLRX7AMSScjs.__decorateClass.call(void 0, [
|
|
52
52
|
_common.Global.call(void 0, ),
|
|
53
53
|
_common.Module.call(void 0, {})
|
|
54
54
|
], TypeOrmDbModule);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type CallHandler, type ExecutionContext, type NestInterceptor } from '@nestjs/common';
|
|
2
|
+
import type { Observable } from 'rxjs';
|
|
3
|
+
import { type DevicesOptions } from './devices.options.js';
|
|
4
|
+
import { DevicesService } from './devices.service.js';
|
|
5
|
+
/**
|
|
6
|
+
* Checks, on EVERY request that carries the device header, that the device is
|
|
7
|
+
* still good: revoked or unknown -> 401 `DEVICE_NOT_AUTHORIZED`; over the plan's
|
|
8
|
+
* limit -> `onPaused` error, except on `pausedAllowed` paths. Without the header
|
|
9
|
+
* it does nothing: the header is not a credential, it is the device's own signal
|
|
10
|
+
* so the server can cut it off (the web panel, an old app, send none).
|
|
11
|
+
*
|
|
12
|
+
* An interceptor and not a guard on purpose (same reason as `ConsentInterceptor`):
|
|
13
|
+
* it runs after every guard, so `req.user` exists. Mount it globally.
|
|
14
|
+
*/
|
|
15
|
+
export declare class DeviceCheckInterceptor implements NestInterceptor {
|
|
16
|
+
private readonly options;
|
|
17
|
+
private readonly devices;
|
|
18
|
+
constructor(options: DevicesOptions, devices: DevicesService);
|
|
19
|
+
intercept(ctx: ExecutionContext, next: CallHandler): Promise<Observable<unknown>>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type DynamicModule } from '@nestjs/common';
|
|
2
|
+
import type { DevicesOptions } from './devices.options.js';
|
|
3
|
+
/**
|
|
4
|
+
* Pairing of shared devices.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* DevicesModule.forRoot({
|
|
8
|
+
* modes: ['register', 'kitchen', 'kiosk'],
|
|
9
|
+
* store: new TypeOrmDeviceStore(ds),
|
|
10
|
+
* limit: async (tenant) => (await plans.of(tenant)).maxDevices,
|
|
11
|
+
* onPaused: (dev, limit) => new ForbiddenAppException(`Paused: plan allows ${limit}`, { limit }, 'DEVICE_PAUSED'),
|
|
12
|
+
* pausedAllowed: [/\/(sales|payments|shifts)(\/|$)/],
|
|
13
|
+
* skip: [/\/floor\/(link|personal-device)/],
|
|
14
|
+
* })
|
|
15
|
+
* // main.ts: app.useGlobalInterceptors(app.get(DeviceCheckInterceptor));
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Inject `DevicesService` for create / redeem / revoke / list; the host writes its own controllers
|
|
19
|
+
* (who may create, how the redeem endpoint is throttled).
|
|
20
|
+
*/
|
|
21
|
+
export declare class DevicesModule {
|
|
22
|
+
static forRoot(options: DevicesOptions): DynamicModule;
|
|
23
|
+
static forRootAsync(config: {
|
|
24
|
+
imports?: any[];
|
|
25
|
+
inject?: any[];
|
|
26
|
+
useFactory: (...args: any[]) => DevicesOptions | Promise<DevicesOptions>;
|
|
27
|
+
}): DynamicModule;
|
|
28
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { DeviceRecord, DeviceStorePort } from './devices.port.js';
|
|
2
|
+
export interface DeviceErrorCodes {
|
|
3
|
+
invalidCode: string;
|
|
4
|
+
limitReached: string;
|
|
5
|
+
notAuthorized: string;
|
|
6
|
+
paused: string;
|
|
7
|
+
invalidMode: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const DEFAULT_DEVICE_ERROR_CODES: DeviceErrorCodes;
|
|
10
|
+
/** No 0/O, 1/I: the code is typed by hand on a tablet. */
|
|
11
|
+
export declare const DEFAULT_CODE_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
|
12
|
+
export interface DevicesOptions {
|
|
13
|
+
/** Where devices live. Default: in-process memory (tests, single instance). */
|
|
14
|
+
store?: DeviceStorePort;
|
|
15
|
+
/** Characters the code is drawn from. Default {@link DEFAULT_CODE_ALPHABET}. Must have 16+ distinct ones. */
|
|
16
|
+
codeAlphabet?: string;
|
|
17
|
+
/** Code length, without the dash. Default 8. Minimum 6. */
|
|
18
|
+
codeLength?: number;
|
|
19
|
+
/** Dash every N characters (`ABCD-EFGH`). Default half the length; `0` for none. */
|
|
20
|
+
codeGroupSize?: number;
|
|
21
|
+
/** How long a code is good for, ms. Default 15 minutes. */
|
|
22
|
+
codeTtlMs?: number;
|
|
23
|
+
/** Modes a device can be paired for. A closed list: anything else is a 400. */
|
|
24
|
+
modes: readonly string[];
|
|
25
|
+
/**
|
|
26
|
+
* Devices the plan allows for a tenant (or the site, with `limitScope: 'site'`). `null`/omitted =
|
|
27
|
+
* unlimited. Counts ACTIVE devices; those past the limit, oldest activation first, are paused.
|
|
28
|
+
*/
|
|
29
|
+
limit?: (tenant: string, site: string | null) => number | null | Promise<number | null>;
|
|
30
|
+
limitScope?: 'tenant' | 'site';
|
|
31
|
+
/**
|
|
32
|
+
* The error to throw when a paused device asks for something (or tries to pair). Default: 403
|
|
33
|
+
* `DEVICE_PAUSED`. Return your own `AppException` to carry the host's message and code.
|
|
34
|
+
*/
|
|
35
|
+
onPaused?: (device: DeviceRecord, limit: number | null) => Error;
|
|
36
|
+
/** Error codes, to match the host's catalog. */
|
|
37
|
+
errorCodes?: Partial<DeviceErrorCodes>;
|
|
38
|
+
/** Header the device sends. Default `x-device-token`. */
|
|
39
|
+
header?: string;
|
|
40
|
+
/** Who is the tenant of the request. Default `req.user.tenantId`. Used to reject a token from another tenant. */
|
|
41
|
+
getTenant?: (request: any) => string | undefined;
|
|
42
|
+
/** Paths the interceptor never checks (the device is still registering there). */
|
|
43
|
+
skip?: RegExp[];
|
|
44
|
+
/** Paths a PAUSED device may still use (cobrar, abrir turno…). A revoked one gets none. */
|
|
45
|
+
pausedAllowed?: RegExp[];
|
|
46
|
+
/** How long a verdict is reused, ms. Default 5 000: revoking bites within seconds. `0` disables. */
|
|
47
|
+
cacheMs?: number;
|
|
48
|
+
/** Clock, for tests. */
|
|
49
|
+
now?: () => number;
|
|
50
|
+
}
|
|
51
|
+
export declare const DEFAULT_CODE_TTL_MS: number;
|
|
52
|
+
export declare const DEFAULT_DEVICE_CACHE_MS = 5000;
|
|
53
|
+
export declare const DEFAULT_DEVICE_HEADER = "x-device-token";
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export declare const DEVICES_OPTIONS: unique symbol;
|
|
2
|
+
export declare const DEVICE_STORE: unique symbol;
|
|
3
|
+
/** `pending`: code issued, nobody typed it yet. `active`: redeemed. `revoked`: cut off for good. */
|
|
4
|
+
export type DeviceStatus = 'pending' | 'active' | 'revoked';
|
|
5
|
+
export interface DeviceRecord {
|
|
6
|
+
id: string;
|
|
7
|
+
tenant: string;
|
|
8
|
+
/** Branch/site the device is tied to. */
|
|
9
|
+
site: string | null;
|
|
10
|
+
name: string;
|
|
11
|
+
/** One of `DevicesOptions.modes` (kiosk, kitchen, register…). */
|
|
12
|
+
mode: string;
|
|
13
|
+
status: DeviceStatus;
|
|
14
|
+
/** SHA-256 of the normalized code. The code itself is never stored. */
|
|
15
|
+
codeHash: string;
|
|
16
|
+
codeExpiresAt: number;
|
|
17
|
+
/** SHA-256 of the device token. Null until redeemed. The token itself is never stored. */
|
|
18
|
+
tokenHash: string | null;
|
|
19
|
+
createdAt: number;
|
|
20
|
+
activatedAt: number | null;
|
|
21
|
+
revokedAt: number | null;
|
|
22
|
+
createdBy: string | null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Persistence. Everything but `redeem` is trivial CRUD; `redeem` must be
|
|
26
|
+
* ATOMIC (a code is worth one device even under two simultaneous attempts):
|
|
27
|
+
* in SQL, `UPDATE … WHERE code_hash = $1 AND status = 'pending' AND code_expires_at > $now RETURNING *`.
|
|
28
|
+
*/
|
|
29
|
+
export interface DeviceStorePort {
|
|
30
|
+
insert(device: DeviceRecord): Promise<void>;
|
|
31
|
+
get(id: string): Promise<DeviceRecord | null>;
|
|
32
|
+
findByCodeHash(codeHash: string): Promise<DeviceRecord | null>;
|
|
33
|
+
findByTokenHash(tokenHash: string): Promise<DeviceRecord | null>;
|
|
34
|
+
/** Every device of the tenant, any status. */
|
|
35
|
+
listByTenant(tenant: string): Promise<DeviceRecord[]>;
|
|
36
|
+
/** Atomic: pending + unexpired + that code -> active with that token. Null if it did not match. */
|
|
37
|
+
redeem(codeHash: string, tokenHash: string, now: number): Promise<DeviceRecord | null>;
|
|
38
|
+
/** True if it was not already revoked. */
|
|
39
|
+
revoke(id: string, now: number): Promise<boolean>;
|
|
40
|
+
}
|
|
41
|
+
/** What the tablet gets back, once. The token is shown here and nowhere else. */
|
|
42
|
+
export interface PairedDevice {
|
|
43
|
+
deviceId: string;
|
|
44
|
+
token: string;
|
|
45
|
+
tenant: string;
|
|
46
|
+
site: string | null;
|
|
47
|
+
mode: string;
|
|
48
|
+
name: string;
|
|
49
|
+
}
|
|
50
|
+
export interface CreatedDevice {
|
|
51
|
+
deviceId: string;
|
|
52
|
+
/** `ABCD-EFGH`: shown once to the admin. */
|
|
53
|
+
code: string;
|
|
54
|
+
expiresAt: number;
|
|
55
|
+
}
|
|
56
|
+
export interface VerifiedDevice {
|
|
57
|
+
device: DeviceRecord;
|
|
58
|
+
/** Over the plan's limit: it does not work, but it is not revoked. */
|
|
59
|
+
paused: boolean;
|
|
60
|
+
/** The limit that paused it (null when not paused or unlimited). */
|
|
61
|
+
limit: number | null;
|
|
62
|
+
}
|