@tokenoftrust/storefront-runner 7.6.5 → 7.6.7

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.
@@ -14,8 +14,10 @@
14
14
  * - the ROLE-GATED routes in {@link AGENT_ROLE_ROUTES}, each only while the agent's
15
15
  * grant roles name that route's role: the PII-blind order reads (`admin:read`) and
16
16
  * the fulfil-by-id operation (`orders:fulfil-by-id`, which tot20 still decides per
17
- * call), and the stock levels and movement history reads (`admin:read`). Those
18
- * handlers project an agent's view; the gate only admits.
17
+ * call), the stock levels and movement history reads, and the supplier and purchase
18
+ * order reads (`admin:read`). Those handlers project an agent's view; the gate only
19
+ * admits. No purchasing write is reachable: an agent never creates, sends or receives
20
+ * a purchase order through a route.
19
21
  *
20
22
  * Everything else — every other `/api` route (so no derived credential such as the
21
23
  * CLI sign-in code or the activity-ingest token), `/admin`, `/cockpit`, `/dev`,
@@ -44,6 +46,12 @@ export const AGENT_ROLE_ROUTES: ReadonlyArray<{ method: string; path: string; ro
44
46
  { method: "GET", path: "/api/admin/orders/:id", role: ADMIN_READ_ROLE },
45
47
  { method: "GET", path: "/api/admin/inventory/levels", role: ADMIN_READ_ROLE },
46
48
  { method: "GET", path: "/api/admin/inventory/history", role: ADMIN_READ_ROLE },
49
+ { method: "GET", path: "/api/admin/inventory/stocktakes", role: ADMIN_READ_ROLE },
50
+ { method: "GET", path: "/api/admin/inventory/stocktakes/:id", role: ADMIN_READ_ROLE },
51
+ { method: "GET", path: "/api/admin/purchasing/suppliers", role: ADMIN_READ_ROLE },
52
+ { method: "GET", path: "/api/admin/purchasing/suppliers/:id", role: ADMIN_READ_ROLE },
53
+ { method: "GET", path: "/api/admin/purchasing/orders", role: ADMIN_READ_ROLE },
54
+ { method: "GET", path: "/api/admin/purchasing/orders/:id", role: ADMIN_READ_ROLE },
47
55
  { method: "POST", path: "/api/admin/fulfillment/fulfil-by-id", role: ORDERS_FULFIL_BY_ID_ROLE },
48
56
  ];
49
57
 
@@ -26,6 +26,8 @@ export const V1_SCOPES = [
26
26
  "customers:read:minimal",
27
27
  "inventory:read",
28
28
  "inventory:write",
29
+ "purchasing:read",
30
+ "purchasing:write",
29
31
  "reports:read",
30
32
  "attribution:write",
31
33
  "widgets:launch",
@@ -7,6 +7,7 @@ import { mirrorBalanceKey, serializeMirrorBalances } from "./mirrorBalanceProjec
7
7
 
8
8
  const ID = /^[A-Za-z0-9._:-]{1,128}$/;
9
9
  const INVENTORY_CHANGED_TOPIC = "inventory.changed";
10
+ export const INVENTORY_STOCKTAKE_COMPLETED_TOPIC = "inventory.stocktake.completed";
10
11
  const INVENTORY_READ_SCOPE = "inventory:read";
11
12
  const INVENTORY_DATASCHEMA_VERSION = "1";
12
13
  /** Binds tenant, then the install env the event's stage admits (appInstallEnvForStage). */
@@ -113,14 +114,22 @@ function idMatchParams(exact: string, prefix: string): string[] {
113
114
  return [exact, prefix, `${prefix.slice(0, -1)}/`];
114
115
  }
115
116
 
116
- /** Inserts a pending delivery for every eligible install of each `inventory.changed` event whose id matches. */
117
- function deliveryStatement(input: { tenantId: string; stage: CommerceStage; now: string; idEquals: string; idPrefix: string }): SqlStatement {
117
+ /** Inserts a pending delivery for every eligible install of each inventory event of `topic` whose id matches. */
118
+ function deliveryStatement(input: {
119
+ tenantId: string;
120
+ stage: CommerceStage;
121
+ now: string;
122
+ idEquals: string;
123
+ idPrefix: string;
124
+ topic?: typeof INVENTORY_CHANGED_TOPIC | typeof INVENTORY_STOCKTAKE_COMPLETED_TOPIC;
125
+ }): SqlStatement {
126
+ const topic = input.topic ?? INVENTORY_CHANGED_TOPIC;
118
127
  return {
119
128
  sql: `INSERT INTO webhook_deliveries
120
129
  (delivery_id, event_id, install_id, tenant_id, topic, state, attempts, created_at, updated_at)
121
- SELECT e.id || ':' || i.install_id, e.id, i.install_id, i.tenant_id, '${INVENTORY_CHANGED_TOPIC}', 'pending', 0, ?, ?
130
+ SELECT e.id || ':' || i.install_id, e.id, i.install_id, i.tenant_id, '${topic}', 'pending', 0, ?, ?
122
131
  FROM webhook_events AS e JOIN app_installs AS i ON ${ELIGIBLE_INVENTORY_INSTALL_SQL}
123
- WHERE ${idMatchSql("e.id")} AND e.tenant_id = ? AND e.topic = '${INVENTORY_CHANGED_TOPIC}'
132
+ WHERE ${idMatchSql("e.id")} AND e.tenant_id = ? AND e.topic = '${topic}'
124
133
  ON CONFLICT DO NOTHING`,
125
134
  params: [
126
135
  input.now,
@@ -247,6 +256,53 @@ export function movementEventStatements(input: {
247
256
  ];
248
257
  }
249
258
 
259
+ /** The payload of an `inventory.stocktake.completed` event; closed key set, no person or owner identifier. */
260
+ export interface StocktakeCompletedPayload {
261
+ sessionId: string;
262
+ locationId: string;
263
+ kind: "full" | "cycle";
264
+ completedAt: string;
265
+ lineCount: number;
266
+ varianceCount: number;
267
+ netDelta: number;
268
+ }
269
+
270
+ /** The event id of a completed stocktake: one event per session, so a replayed completion never emits twice. */
271
+ export function stocktakeCompletedEventId(sessionId: string): string {
272
+ return `stk.${sessionId}`;
273
+ }
274
+
275
+ /**
276
+ * The `inventory.stocktake.completed` event and its pending deliveries, for the same batch that flips
277
+ * the session to completed. Delivered to installs holding `inventory:read`, like `inventory.changed`.
278
+ */
279
+ export function stocktakeCompletedEventStatements(input: {
280
+ tenantId: string;
281
+ stage: CommerceStage;
282
+ now: string;
283
+ payload: StocktakeCompletedPayload;
284
+ }): SqlStatement[] {
285
+ const eventId = stocktakeCompletedEventId(input.payload.sessionId);
286
+ const { sessionId, locationId, kind, completedAt, lineCount, varianceCount, netDelta } = input.payload;
287
+ return [
288
+ {
289
+ sql: `INSERT INTO webhook_events (id, tenant_id, topic, dataschema_version, payload, created_at)
290
+ VALUES (?, ?, '${INVENTORY_STOCKTAKE_COMPLETED_TOPIC}', '${INVENTORY_DATASCHEMA_VERSION}',
291
+ json_object('sessionId', ?, 'locationId', ?, 'kind', ?, 'completedAt', ?, 'lineCount', ?, 'varianceCount', ?, 'netDelta', ?), ?)
292
+ ON CONFLICT DO NOTHING`,
293
+ params: [eventId, input.tenantId, sessionId, locationId, kind, completedAt, lineCount, varianceCount, netDelta, input.now],
294
+ },
295
+ deliveryStatement({
296
+ tenantId: input.tenantId,
297
+ stage: input.stage,
298
+ now: input.now,
299
+ idEquals: eventId,
300
+ idPrefix: `${eventId}.`,
301
+ topic: INVENTORY_STOCKTAKE_COMPLETED_TOPIC,
302
+ }),
303
+ ];
304
+ }
305
+
250
306
  /** Best-effort dispatch of the pending deliveries a committed transaction wrote for events whose id matches. */
251
307
  export async function notifyInventoryDeliveries(
252
308
  all: (sql: string, params: unknown[]) => Promise<Record<string, unknown>[]>,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/storefront-runner",
3
- "version": "7.6.5",
3
+ "version": "7.6.7",
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",