@tokenoftrust/storefront-runner 7.6.4 → 7.6.6

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,7 +14,8 @@
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). Those handlers project an agent's view; the gate only admits.
17
+ * call), and the stock levels and movement history reads (`admin:read`). Those
18
+ * handlers project an agent's view; the gate only admits.
18
19
  *
19
20
  * Everything else — every other `/api` route (so no derived credential such as the
20
21
  * CLI sign-in code or the activity-ingest token), `/admin`, `/cockpit`, `/dev`,
@@ -41,6 +42,10 @@ export const AGENT_ALLOWED_ROUTES: ReadonlyArray<{ method: string; path: string
41
42
  export const AGENT_ROLE_ROUTES: ReadonlyArray<{ method: string; path: string; role: AgentRoleId }> = [
42
43
  { method: "GET", path: "/api/admin/orders", role: ADMIN_READ_ROLE },
43
44
  { method: "GET", path: "/api/admin/orders/:id", role: ADMIN_READ_ROLE },
45
+ { method: "GET", path: "/api/admin/inventory/levels", role: ADMIN_READ_ROLE },
46
+ { method: "GET", path: "/api/admin/inventory/history", role: ADMIN_READ_ROLE },
47
+ { method: "GET", path: "/api/admin/inventory/stocktakes", role: ADMIN_READ_ROLE },
48
+ { method: "GET", path: "/api/admin/inventory/stocktakes/:id", role: ADMIN_READ_ROLE },
44
49
  { method: "POST", path: "/api/admin/fulfillment/fulfil-by-id", role: ORDERS_FULFIL_BY_ID_ROLE },
45
50
  ];
46
51
 
@@ -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>[]>,
@@ -1006,19 +1062,30 @@ export async function importSourceOnHand(
1006
1062
  input: {
1007
1063
  tenantId: string;
1008
1064
  stage: CommerceStage;
1009
- lines: readonly { sku: string; onHand: number }[];
1065
+ lines: readonly { sku: string; onHand: number; locationId?: string }[];
1010
1066
  runId: string;
1011
1067
  asOf: string;
1012
1068
  },
1013
1069
  ): Promise<void> {
1014
1070
  for (const line of input.lines) {
1071
+ const locationId = line.locationId ?? MIRROR_LOCATION_UNSPECIFIED;
1072
+ if (!(await mirrorLocationKnown(db, { tenantId: input.tenantId, stage: input.stage, locationId }))) {
1073
+ throw new InventoryMovementError("invalid-movement");
1074
+ }
1075
+ }
1076
+ for (const line of input.lines) {
1077
+ const locationId = line.locationId ?? MIRROR_LOCATION_UNSPECIFIED;
1015
1078
  await appendSourceOnHand(db, {
1016
1079
  tenantId: input.tenantId,
1017
1080
  stage: input.stage,
1018
1081
  sku: line.sku,
1019
- locationId: MIRROR_LOCATION_UNSPECIFIED,
1082
+ locationId,
1020
1083
  source: CATALOG_IMPORT_SOURCE,
1021
- sourceEventId: await catalogImportEventId(input.runId, line.sku, line.onHand),
1084
+ sourceEventId: await catalogImportEventId(
1085
+ input.runId,
1086
+ locationId === MIRROR_LOCATION_UNSPECIFIED ? line.sku : `${line.sku}.${locationId}`,
1087
+ line.onHand,
1088
+ ),
1022
1089
  onHand: line.onHand,
1023
1090
  asOf: input.asOf,
1024
1091
  actorId: CATALOG_IMPORT_ACTOR_ID,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/storefront-runner",
3
- "version": "7.6.4",
3
+ "version": "7.6.6",
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",