@tokenoftrust/storefront-runner 7.6.13 → 7.6.15

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.
@@ -16,7 +16,7 @@
16
16
  import { hostInGateList } from "@/lib/access/shared/demoGate";
17
17
  import { isOwnerCapability } from "@/lib/access/shared/capability";
18
18
  import { normalizeMemberships, staffAdmits } from "@/lib/access/dashboard/tenantSelection";
19
- import type { SessionRecord } from "./session.js";
19
+ import { isAgentSession, type SessionRecord } from "./session.js";
20
20
  import { safeNext } from "./safeNext.js";
21
21
 
22
22
  export type GateMode = "password" | "login";
@@ -26,9 +26,22 @@ export type GateMode = "password" | "login";
26
26
  * — these readers tolerate a partial record and are deny-closed on absence.
27
27
  */
28
28
  type HostAdmissionRecord = Partial<
29
- Pick<SessionRecord, "tenants" | "staffSelection" | "staff" | "resource" | "capability">
29
+ Pick<SessionRecord, "tenants" | "staffSelection" | "staff" | "resource" | "capability" | "actor">
30
30
  >;
31
31
 
32
+ /** True when a human ToT-staff session holds a live, audited vendor selection for `hostResource`. */
33
+ export function sessionStaffSelected(
34
+ record: HostAdmissionRecord | null | undefined,
35
+ hostResource: string,
36
+ nowSeconds: number = Math.floor(Date.now() / 1000),
37
+ ): boolean {
38
+ if (!record || isAgentSession(record)) return false;
39
+ if (!Array.isArray(record.staff) || record.staff.length === 0) return false;
40
+ const target = (hostResource ?? "").trim();
41
+ if (!target) return false;
42
+ return staffAdmits({ selection: record.staffSelection, nowSeconds }, target);
43
+ }
44
+
32
45
  /**
33
46
  * The owner/admin/member standing a session holds FOR THIS EXACT HOST tenant, or
34
47
  * null when it is not a member of the host. Resolved SERVER-SIDE and BOUND to
@@ -114,34 +114,55 @@ function idMatchParams(exact: string, prefix: string): string[] {
114
114
  return [exact, prefix, `${prefix.slice(0, -1)}/`];
115
115
  }
116
116
 
117
- /** Inserts a pending delivery for every eligible install of each inventory event of `topic` whose id matches. */
118
- function deliveryStatement(input: {
117
+ type InventoryEventTopic = typeof INVENTORY_CHANGED_TOPIC | typeof INVENTORY_STOCKTAKE_COMPLETED_TOPIC;
118
+
119
+ /**
120
+ * One statement inserting a pending delivery for every eligible install of each inventory event whose
121
+ * id and topic match any of `events`. A zero-row insert is a valid outcome.
122
+ */
123
+ export function inventoryDeliveriesStatement(input: {
119
124
  tenantId: string;
120
125
  stage: CommerceStage;
121
126
  now: string;
122
- idEquals: string;
123
- idPrefix: string;
124
- topic?: typeof INVENTORY_CHANGED_TOPIC | typeof INVENTORY_STOCKTAKE_COMPLETED_TOPIC;
127
+ events: readonly { idEquals: string; idPrefix: string; topic: InventoryEventTopic }[];
125
128
  }): SqlStatement {
126
- const topic = input.topic ?? INVENTORY_CHANGED_TOPIC;
129
+ if (input.events.length === 0) throw new Error("inventory-deliveries-need-an-event");
130
+ const matches = input.events.map(({ topic }) => `(${idMatchSql("e.id")} AND e.topic = '${topic}')`).join(" OR ");
127
131
  return {
128
132
  sql: `INSERT INTO webhook_deliveries
129
133
  (delivery_id, event_id, install_id, tenant_id, topic, state, attempts, created_at, updated_at)
130
- SELECT e.id || ':' || i.install_id, e.id, i.install_id, i.tenant_id, '${topic}', 'pending', 0, ?, ?
134
+ SELECT e.id || ':' || i.install_id, e.id, i.install_id, i.tenant_id, e.topic, 'pending', 0, ?, ?
131
135
  FROM webhook_events AS e JOIN app_installs AS i ON ${ELIGIBLE_INVENTORY_INSTALL_SQL}
132
- WHERE ${idMatchSql("e.id")} AND e.tenant_id = ? AND e.topic = '${topic}'
136
+ WHERE (${matches}) AND e.tenant_id = ?
133
137
  ON CONFLICT DO NOTHING`,
134
138
  params: [
135
139
  input.now,
136
140
  input.now,
137
141
  input.tenantId,
138
142
  appInstallEnvForStage(input.stage),
139
- ...idMatchParams(input.idEquals, input.idPrefix),
143
+ ...input.events.flatMap(({ idEquals, idPrefix }) => idMatchParams(idEquals, idPrefix)),
140
144
  input.tenantId,
141
145
  ],
142
146
  };
143
147
  }
144
148
 
149
+ /** Inserts a pending delivery for every eligible install of each inventory event of `topic` whose id matches. */
150
+ function deliveryStatement(input: {
151
+ tenantId: string;
152
+ stage: CommerceStage;
153
+ now: string;
154
+ idEquals: string;
155
+ idPrefix: string;
156
+ topic?: InventoryEventTopic;
157
+ }): SqlStatement {
158
+ return inventoryDeliveriesStatement({
159
+ tenantId: input.tenantId,
160
+ stage: input.stage,
161
+ now: input.now,
162
+ events: [{ idEquals: input.idEquals, idPrefix: input.idPrefix, topic: input.topic ?? INVENTORY_CHANGED_TOPIC }],
163
+ });
164
+ }
165
+
145
166
  /**
146
167
  * Statements appended to the same D1 batch as a state change that can move availability. For each
147
168
  * scoped sku the tenant's ledger holds, they recompute availability and store it in
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/storefront-runner",
3
- "version": "7.6.13",
3
+ "version": "7.6.15",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
6
6
  "packageManager": "pnpm@11.9.0",