@tokenoftrust/storefront-runner 7.6.5 → 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.
@@ -44,6 +44,8 @@ export const AGENT_ROLE_ROUTES: ReadonlyArray<{ method: string; path: string; ro
44
44
  { method: "GET", path: "/api/admin/orders/:id", role: ADMIN_READ_ROLE },
45
45
  { method: "GET", path: "/api/admin/inventory/levels", role: ADMIN_READ_ROLE },
46
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 },
47
49
  { method: "POST", path: "/api/admin/fulfillment/fulfil-by-id", role: ORDERS_FULFIL_BY_ID_ROLE },
48
50
  ];
49
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>[]>,
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.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",