@iveri/contracts 0.94.0 → 0.96.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.
@@ -0,0 +1,6 @@
1
+ /** Where an invoice stands. An invoice is a snapshot: its lines never change after it is issued. */
2
+ export declare enum BillingInvoiceStatus {
3
+ OPEN = "open",
4
+ PAID = "paid",
5
+ VOID = "void"
6
+ }
@@ -0,0 +1,7 @@
1
+ /** Where an invoice stands. An invoice is a snapshot: its lines never change after it is issued. */
2
+ export var BillingInvoiceStatus;
3
+ (function (BillingInvoiceStatus) {
4
+ BillingInvoiceStatus["OPEN"] = "open";
5
+ BillingInvoiceStatus["PAID"] = "paid";
6
+ BillingInvoiceStatus["VOID"] = "void";
7
+ })(BillingInvoiceStatus || (BillingInvoiceStatus = {}));
@@ -0,0 +1,2 @@
1
+ export * from './billing-invoice-status.enum.js';
2
+ export * from './usage.type.js';
@@ -0,0 +1,2 @@
1
+ export * from './billing-invoice-status.enum.js';
2
+ export * from './usage.type.js';
@@ -0,0 +1,103 @@
1
+ import type { IsoDateTime } from '../../type/iso-date-time.type.js';
2
+ import type { Nullable } from '../../type/nil.type.js';
3
+ import type { UUID } from '../../type/uuid.type.js';
4
+ import type { IveriProduct } from '../platform/iveri-product.enum.js';
5
+ import type { BillingInvoiceStatus } from './billing-invoice-status.enum.js';
6
+ /**
7
+ * A plan's limits for one calendar month in UTC: a metric's name to the most the plan allows, or null for no limit. A
8
+ * metric the plan does not name is not limited by it. Limits are data on the plan, edited like its price.
9
+ */
10
+ export type UsageLimits = Record<string, Nullable<number>>;
11
+ /** A plan Iveri sells for a product, as a company reads it against its usage. */
12
+ export interface UsagePlan {
13
+ id: UUID;
14
+ code: string;
15
+ name: string;
16
+ product: IveriProduct;
17
+ /** The price for one month, in the currency's minor unit. */
18
+ amountMinor: number;
19
+ currency: string;
20
+ /**
21
+ * True while the price and the limits are placeholders nobody has decided. Every screen that shows the plan says so
22
+ * while it is true.
23
+ */
24
+ isPlaceholder: boolean;
25
+ limits: UsageLimits;
26
+ }
27
+ /** One metric this month against the plan. */
28
+ export interface UsageMetricSummary {
29
+ metric: string;
30
+ quantity: number;
31
+ /** The plan's limit, or null when the plan does not limit it. */
32
+ limit: Nullable<number>;
33
+ }
34
+ /**
35
+ * `GET /billing/usage/summary?product=` (a company's own) or `?tenantId=&product=` (behind `billing:usage:read-any`).
36
+ * The period is the current calendar month in UTC, its end exclusive. `plan` is null when nothing is on sale for the
37
+ * product, and then nothing is limited.
38
+ */
39
+ export interface UsageSummary {
40
+ tenantId: UUID;
41
+ product: IveriProduct;
42
+ periodStart: IsoDateTime;
43
+ periodEnd: IsoDateTime;
44
+ plan: Nullable<UsagePlan>;
45
+ metrics: UsageMetricSummary[];
46
+ }
47
+ /** One line of an invoice, as it was when the invoice was issued. */
48
+ export interface UsageInvoiceLine {
49
+ description: string;
50
+ quantity: number;
51
+ unitAmountMinor: number;
52
+ amountMinor: number;
53
+ }
54
+ /**
55
+ * An invoice as a company reads it. One built from a month's usage carries that month as its period; an invoice made
56
+ * by hand carries none.
57
+ */
58
+ export interface UsageInvoice {
59
+ id: UUID;
60
+ number: string;
61
+ status: BillingInvoiceStatus;
62
+ currency: string;
63
+ totalMinor: number;
64
+ periodStart: Nullable<IsoDateTime>;
65
+ periodEnd: Nullable<IsoDateTime>;
66
+ issuedAt: IsoDateTime;
67
+ lines: UsageInvoiceLine[];
68
+ }
69
+ /**
70
+ * `POST /billing/usage`. `tenantId` names the company the usage is for, and is refused with a 403 unless the token
71
+ * holds `billing:usage:record-any` and comes from Iveri's operator workspace. Without it the usage is the token's own
72
+ * company's. The `Idempotency-Key` header is unique per company the usage is for.
73
+ */
74
+ export interface RecordUsageRequest {
75
+ tenantId?: UUID;
76
+ metric: string;
77
+ periodStart: IsoDateTime;
78
+ periodEnd: IsoDateTime;
79
+ quantity: number;
80
+ source: string;
81
+ }
82
+ /** `PUT /platform/usage-plans/:tenantId`: the plan a company is on for a product, from the next request on. */
83
+ export interface AssignUsagePlanRequest {
84
+ product: IveriProduct;
85
+ planCode: string;
86
+ }
87
+ /** Which plan a company is on for a product, and whether an operator chose it or it is the product's default. */
88
+ export interface UsagePlanAssignment {
89
+ tenantId: UUID;
90
+ product: IveriProduct;
91
+ plan: UsagePlan;
92
+ isDefault: boolean;
93
+ }
94
+ /**
95
+ * Carried in the `details` of the 403 a product answers when a plan's limit refuses something, beside the product's
96
+ * own reason, so a client can say which limit was reached.
97
+ */
98
+ export declare const USAGE_LIMIT_REACHED_REASON = "usage-limit-reached";
99
+ export interface UsageLimitReachedDetails {
100
+ reason: typeof USAGE_LIMIT_REACHED_REASON;
101
+ metric: string;
102
+ limit: number;
103
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Carried in the `details` of the 403 a product answers when a plan's limit refuses something, beside the product's
3
+ * own reason, so a client can say which limit was reached.
4
+ */
5
+ export const USAGE_LIMIT_REACHED_REASON = 'usage-limit-reached';
@@ -17,6 +17,12 @@ export interface ChatAppWebhook {
17
17
  url: string;
18
18
  events: ChatWebhookEvent[];
19
19
  isActive: boolean;
20
+ /**
21
+ * Until when the secret the last rotation replaced still signs beside the new one, or null when only one does.
22
+ * Every delivery meanwhile carries a `v1` for each, so a server still checking the old secret keeps accepting them.
23
+ * Ended early by `POST …/end-secret-overlap`.
24
+ */
25
+ previousSecretExpiresAt: Nullable<IsoDateTime>;
20
26
  createdAt: IsoDateTime;
21
27
  updatedAt: IsoDateTime;
22
28
  }
@@ -35,7 +41,8 @@ export interface UpdateChatAppWebhookRequest {
35
41
  }
36
42
  /**
37
43
  * `POST /apps/:appId/webhooks` and `POST …/rotate-secret`. The only answers that hold the signing secret: Baasi does
38
- * not keep it, and after a rotation the old one stops signing at once.
44
+ * not keep it. After a rotation the old secret keeps signing beside the new one until `previousSecretExpiresAt` (a day,
45
+ * unless the deployment says otherwise), so the server can be moved over without refusing a delivery.
39
46
  */
40
47
  export interface CreatedChatAppWebhook extends ChatAppWebhook {
41
48
  /** `bwh_` and 32 random bytes in base64url. The HMAC key for `Baasi-Signature`. */
@@ -61,7 +68,8 @@ export interface ChatAppWebhookDelivery {
61
68
  export type ChatAppWebhookDeliveriesPage = CursorPage<ChatAppWebhookDelivery>;
62
69
  /**
63
70
  * The body every webhook carries, signed with `Baasi-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256 of
64
- * "<t>.<body>">`. Users are named by the app's own ids for them. `id` is stable across retries, so a server that has
71
+ * "<t>.<body>">`, with a second `v1=` for the replaced secret while a rotation overlaps: accept the request when any
72
+ * `v1` matches. Users are named by the app's own ids for them. `id` is stable across retries, so a server that has
65
73
  * seen it may answer 200 and do nothing.
66
74
  */
67
75
  export interface ChatWebhookPayload<TType extends ChatWebhookEvent = ChatWebhookEvent, TData = unknown> {
@@ -15,5 +15,11 @@ export declare enum ChatRefusal {
15
15
  INVITATION_EXPIRED = "invitation-expired",
16
16
  SUSPENDED = "suspended",
17
17
  ATTACHMENT_REFUSED = "attachment-refused",
18
- REQUEST_PENDING = "request-pending"
18
+ REQUEST_PENDING = "request-pending",
19
+ /**
20
+ * The app's plan does not allow it this month: a new user over the monthly-active-user limit, or a message over the
21
+ * message limit. The value is `USAGE_LIMIT_REACHED_REASON` from `@iveri/contracts/billing`, and the details name the
22
+ * metric and the limit.
23
+ */
24
+ USAGE_LIMIT = "usage-limit-reached"
19
25
  }
@@ -17,4 +17,10 @@ export var ChatRefusal;
17
17
  ChatRefusal["SUSPENDED"] = "suspended";
18
18
  ChatRefusal["ATTACHMENT_REFUSED"] = "attachment-refused";
19
19
  ChatRefusal["REQUEST_PENDING"] = "request-pending";
20
+ /**
21
+ * The app's plan does not allow it this month: a new user over the monthly-active-user limit, or a message over the
22
+ * message limit. The value is `USAGE_LIMIT_REACHED_REASON` from `@iveri/contracts/billing`, and the details name the
23
+ * metric and the limit.
24
+ */
25
+ ChatRefusal["USAGE_LIMIT"] = "usage-limit-reached";
20
26
  })(ChatRefusal || (ChatRefusal = {}));
@@ -0,0 +1,24 @@
1
+ import type { Nullable } from '../../type/nil.type.js';
2
+ import type { UUID } from '../../type/uuid.type.js';
3
+ import type { UsageInvoice, UsageSummary } from '../billing/usage.type.js';
4
+ /**
5
+ * What Baasi meters for each app, as the metric names billing stores and a plan's limits name. A month is a calendar
6
+ * month in UTC. Iveri's own product tenants, which are not apps, are not metered.
7
+ */
8
+ export declare enum ChatUsageMetric {
9
+ /** People who used the app at least once this month, each counted once. */
10
+ MONTHLY_ACTIVE_USERS = "chat.monthly-active-users",
11
+ /** Messages somebody wrote this month. A product's or a group's own notices are not counted. */
12
+ MESSAGES = "chat.messages",
13
+ /** Bytes of attachments stored this month. */
14
+ STORED_BYTES = "chat.stored-bytes"
15
+ }
16
+ /**
17
+ * `GET /apps/:appId/usage`: the app's month against its plan, and the invoices built from its usage. `summary` is null
18
+ * when billing could not be asked, which the console says rather than showing zeroes.
19
+ */
20
+ export interface ChatAppUsage {
21
+ appId: UUID;
22
+ summary: Nullable<UsageSummary>;
23
+ invoices: UsageInvoice[];
24
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * What Baasi meters for each app, as the metric names billing stores and a plan's limits name. A month is a calendar
3
+ * month in UTC. Iveri's own product tenants, which are not apps, are not metered.
4
+ */
5
+ export var ChatUsageMetric;
6
+ (function (ChatUsageMetric) {
7
+ /** People who used the app at least once this month, each counted once. */
8
+ ChatUsageMetric["MONTHLY_ACTIVE_USERS"] = "chat.monthly-active-users";
9
+ /** Messages somebody wrote this month. A product's or a group's own notices are not counted. */
10
+ ChatUsageMetric["MESSAGES"] = "chat.messages";
11
+ /** Bytes of attachments stored this month. */
12
+ ChatUsageMetric["STORED_BYTES"] = "chat.stored-bytes";
13
+ })(ChatUsageMetric || (ChatUsageMetric = {}));
@@ -28,5 +28,6 @@ export * from './chat-realtime.type.js';
28
28
  export * from './chat-refusal.enum.js';
29
29
  export * from './chat-report-reason.enum.js';
30
30
  export * from './chat-report-target.enum.js';
31
+ export * from './chat-usage.type.js';
31
32
  export * from './chat-webhook-delivery-status.enum.js';
32
33
  export * from './chat-webhook-event.enum.js';
@@ -28,5 +28,6 @@ export * from './chat-realtime.type.js';
28
28
  export * from './chat-refusal.enum.js';
29
29
  export * from './chat-report-reason.enum.js';
30
30
  export * from './chat-report-target.enum.js';
31
+ export * from './chat-usage.type.js';
31
32
  export * from './chat-webhook-delivery-status.enum.js';
32
33
  export * from './chat-webhook-event.enum.js';
@@ -70,6 +70,15 @@ export interface Connection {
70
70
  * usable: the client id and secret are typed in, the token is only ever granted.
71
71
  */
72
72
  connectedAt: Nullable<IsoDateTime>;
73
+ /**
74
+ * Until when the signing secret this connection's last rotation replaced still signs beside the
75
+ * new one, or null when only one secret signs.
76
+ *
77
+ * Only an `hmac-signature` connection ever has one, and only after an update that replaced its
78
+ * `secret` with {@link UpdateConnectionBody.previousSigningSecretValidForSeconds}. Null once that
79
+ * moment has passed, so a reader never has to compare it with the clock.
80
+ */
81
+ previousSigningSecretExpiresAt: Nullable<IsoDateTime>;
73
82
  /** Stops *new* dispatches being accepted. Does not cancel ones already queued. */
74
83
  isActive: boolean;
75
84
  createdAt: IsoDateTime;
@@ -129,6 +138,21 @@ export interface UpdateConnectionBody {
129
138
  credentials?: Nullable<Record<string, string>>;
130
139
  variables?: Nullable<Record<string, string>>;
131
140
  isActive?: boolean;
141
+ /**
142
+ * Keeps the `secret` this update replaces signing beside the new one for this many seconds,
143
+ * 1 to `MAX_SIGNING_SECRET_OVERLAP_SECONDS`. **`hmac-signature` connections only**, and
144
+ * only with a `credentials` object that changes the `secret`; anything else is refused.
145
+ *
146
+ * Omitted, the replaced secret stops signing at once, as it always has. A receiver still
147
+ * checking the old secret then refuses every request until it is updated, and those requests
148
+ * dead-letter on the 4xx. With an overlap, every request carries a `v1` for each secret, so a
149
+ * receiver holding either accepts it.
150
+ *
151
+ * **At most two secrets sign at once.** Rotating again during an overlap keeps the secret this
152
+ * update replaces and drops the older one. Ending an overlap early is
153
+ * `DELETE /api/v1/connections/:id/previous-signing-secret`.
154
+ */
155
+ previousSigningSecretValidForSeconds?: number;
132
156
  }
133
157
  /**
134
158
  * `POST /api/v1/connections/:id/verify`.
@@ -13,6 +13,10 @@ export declare enum ConnectorSignatureFormat {
13
13
  * header on commas, recompute over the timestamp, a dot and the body exactly as received,
14
14
  * compare in constant time, and refuse a timestamp older than a few minutes. A request with no
15
15
  * body is signed over `<t>.` alone.
16
+ *
17
+ * While a replaced secret is still within its overlap the header carries one `v1=` per live
18
+ * secret, the newest first (`t=…,v1=<new>,v1=<old>`), which is how Stripe signs during a roll:
19
+ * a receiver accepts the request when any `v1` matches the secret it holds.
16
20
  */
17
21
  STRIPE_V1 = "stripe-v1"
18
22
  }
@@ -14,6 +14,10 @@ export var ConnectorSignatureFormat;
14
14
  * header on commas, recompute over the timestamp, a dot and the body exactly as received,
15
15
  * compare in constant time, and refuse a timestamp older than a few minutes. A request with no
16
16
  * body is signed over `<t>.` alone.
17
+ *
18
+ * While a replaced secret is still within its overlap the header carries one `v1=` per live
19
+ * secret, the newest first (`t=…,v1=<new>,v1=<old>`), which is how Stripe signs during a roll:
20
+ * a receiver accepts the request when any `v1` matches the secret it holds.
17
21
  */
18
22
  ConnectorSignatureFormat["STRIPE_V1"] = "stripe-v1";
19
23
  })(ConnectorSignatureFormat || (ConnectorSignatureFormat = {}));
@@ -31,6 +31,13 @@ export declare const CONNECTOR_CREDENTIAL_PRIVATE_KEY_ID = "privateKeyId";
31
31
  * that could rename it could only name it wrongly.
32
32
  */
33
33
  export declare const CONNECTOR_CREDENTIAL_SECRET = "secret";
34
+ /**
35
+ * The longest a replaced signing secret may keep signing beside its successor: seven days.
36
+ *
37
+ * Long enough for a receiver deployed weekly to pick up the new secret, short enough that a secret
38
+ * somebody replaced because it leaked stops being accepted within a week of their saying so.
39
+ */
40
+ export declare const MAX_SIGNING_SECRET_OVERLAP_SECONDS = 604800;
34
41
  /**
35
42
  * How a request is signed. `HMAC_SIGNATURE` only.
36
43
  *
@@ -26,3 +26,10 @@ export const CONNECTOR_CREDENTIAL_PRIVATE_KEY_ID = 'privateKeyId';
26
26
  * that could rename it could only name it wrongly.
27
27
  */
28
28
  export const CONNECTOR_CREDENTIAL_SECRET = 'secret';
29
+ /**
30
+ * The longest a replaced signing secret may keep signing beside its successor: seven days.
31
+ *
32
+ * Long enough for a receiver deployed weekly to pick up the new secret, short enough that a secret
33
+ * somebody replaced because it leaked stops being accepted within a week of their saying so.
34
+ */
35
+ export const MAX_SIGNING_SECRET_OVERLAP_SECONDS = 604_800;
@@ -238,6 +238,18 @@ export declare enum UserPermission {
238
238
  BILLING_USAGE_READ = "billing:usage:read",
239
239
  /** Record billable usage on behalf of a tenant service. */
240
240
  BILLING_USAGE_RECORD = "billing:usage:record",
241
+ /**
242
+ * Record usage for a company the request names in `tenantId`, rather than the token's own. A platform permission:
243
+ * held only by a service credential in Iveri's operator workspace, so `chat-api` can meter each app it hosts
244
+ * without a key in every app's tenant. The request is authorized and audited as acting for the company it names.
245
+ */
246
+ BILLING_USAGE_RECORD_ANY = "billing:usage:record-any",
247
+ /**
248
+ * Read a company the request names in `tenantId`: its usage this month against its plan, and the invoices billed
249
+ * from that usage. A platform permission, like `record-any`: `chat-api` holds it to enforce an app's limits and to
250
+ * show the app's owner the app's usage.
251
+ */
252
+ BILLING_USAGE_READ_ANY = "billing:usage:read-any",
241
253
  /** Read payment sessions and their provider-neutral status. */
242
254
  BILLING_PAYMENT_READ = "billing:payment:read",
243
255
  /**
@@ -637,6 +649,9 @@ export declare enum UserPermission {
637
649
  * The permissions that act across companies. They are granted only inside Iveri's own operator workspace, which each
638
650
  * service knows as `PLATFORM_TENANT_ID`, and a service honours one only from that workspace: identity refuses them on
639
651
  * any other workspace's role or key, and a route that uses one also checks where the token came from.
652
+ *
653
+ * Two shapes: `platform:*`, the operator console's own, and a service's `*-any`, where a request names the company it
654
+ * acts for in an explicit `tenantId` (option D of `iveri-infra/docs/cross-tenant-service-credential-decision.md`).
640
655
  */
641
656
  export declare const PLATFORM_PERMISSIONS: readonly UserPermission[];
642
657
  /** Whether a permission acts across companies, and so may only be held in Iveri's operator workspace. */
@@ -285,6 +285,18 @@ export var UserPermission;
285
285
  UserPermission["BILLING_USAGE_READ"] = "billing:usage:read";
286
286
  /** Record billable usage on behalf of a tenant service. */
287
287
  UserPermission["BILLING_USAGE_RECORD"] = "billing:usage:record";
288
+ /**
289
+ * Record usage for a company the request names in `tenantId`, rather than the token's own. A platform permission:
290
+ * held only by a service credential in Iveri's operator workspace, so `chat-api` can meter each app it hosts
291
+ * without a key in every app's tenant. The request is authorized and audited as acting for the company it names.
292
+ */
293
+ UserPermission["BILLING_USAGE_RECORD_ANY"] = "billing:usage:record-any";
294
+ /**
295
+ * Read a company the request names in `tenantId`: its usage this month against its plan, and the invoices billed
296
+ * from that usage. A platform permission, like `record-any`: `chat-api` holds it to enforce an app's limits and to
297
+ * show the app's owner the app's usage.
298
+ */
299
+ UserPermission["BILLING_USAGE_READ_ANY"] = "billing:usage:read-any";
288
300
  // ── Payment ────────────────────────────────────────────────────────────
289
301
  /** Read payment sessions and their provider-neutral status. */
290
302
  UserPermission["BILLING_PAYMENT_READ"] = "billing:payment:read";
@@ -704,6 +716,9 @@ export var UserPermission;
704
716
  * The permissions that act across companies. They are granted only inside Iveri's own operator workspace, which each
705
717
  * service knows as `PLATFORM_TENANT_ID`, and a service honours one only from that workspace: identity refuses them on
706
718
  * any other workspace's role or key, and a route that uses one also checks where the token came from.
719
+ *
720
+ * Two shapes: `platform:*`, the operator console's own, and a service's `*-any`, where a request names the company it
721
+ * acts for in an explicit `tenantId` (option D of `iveri-infra/docs/cross-tenant-service-credential-decision.md`).
707
722
  */
708
723
  export const PLATFORM_PERMISSIONS = Object.freeze([
709
724
  UserPermission.PLATFORM_TENANT_READ,
@@ -712,6 +727,8 @@ export const PLATFORM_PERMISSIONS = Object.freeze([
712
727
  UserPermission.PLATFORM_LICENCE_MANAGE,
713
728
  UserPermission.PLATFORM_LICENCE_CHECK,
714
729
  UserPermission.PLATFORM_BACKOFFICE_READ,
730
+ UserPermission.BILLING_USAGE_RECORD_ANY,
731
+ UserPermission.BILLING_USAGE_READ_ANY,
715
732
  ]);
716
733
  /** Whether a permission acts across companies, and so may only be held in Iveri's operator workspace. */
717
734
  export function isPlatformPermission(permission) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iveri/contracts",
3
- "version": "0.94.0",
3
+ "version": "0.96.0",
4
4
  "description": "Wire contracts shared by every Iveri backend and frontend. Zero runtime dependencies.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -84,6 +84,10 @@
84
84
  "types": "./dist/api/taxi/index.d.ts",
85
85
  "default": "./dist/api/taxi/index.js"
86
86
  },
87
+ "./billing": {
88
+ "types": "./dist/api/billing/index.d.ts",
89
+ "default": "./dist/api/billing/index.js"
90
+ },
87
91
  "./package.json": "./package.json"
88
92
  },
89
93
  "sideEffects": false,