@tokenoftrust/storefront-runner 6.17.1 → 6.17.2

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.
@@ -123,6 +123,42 @@ export async function installMayProcess(
123
123
  return row?.ok === 1;
124
124
  }
125
125
 
126
+ /** Why an install on this tenant cannot be its processor. */
127
+ export type ProcessorIneligibility = "install-inactive" | "webhook-endpoint-missing" | "processor-scope-missing";
128
+
129
+ /**
130
+ * Why `installId` cannot be the processor for `tenantId`: `not-found` when the tenant has no such
131
+ * unmanaged install, otherwise each unmet condition of {@link installMayProcess}. An eligible install
132
+ * yields an empty list.
133
+ */
134
+ export async function processorIneligibility(
135
+ db: Pick<FulfillmentDatabase, "get">,
136
+ tenantId: string,
137
+ installId: string,
138
+ ): Promise<"not-found" | ProcessorIneligibility[]> {
139
+ const row = await db.get<{ status: string; webhook_endpoint: string | null; scopes: string | null }>(
140
+ `SELECT status, webhook_endpoint, scopes FROM app_installs WHERE install_id = ? AND tenant_id = ? AND managed_kind IS NULL`,
141
+ [installId, tenantId],
142
+ );
143
+ if (!row) return "not-found";
144
+ const reasons: ProcessorIneligibility[] = [];
145
+ if (row.status !== "active") reasons.push("install-inactive");
146
+ if (!row.webhook_endpoint) reasons.push("webhook-endpoint-missing");
147
+ let scopes: unknown = [];
148
+ try {
149
+ scopes = JSON.parse(row.scopes ?? "[]");
150
+ } catch (error) {
151
+ if (!(error instanceof SyntaxError)) throw error;
152
+ }
153
+ if (!Array.isArray(scopes) || !scopes.includes(PROCESSOR_SCOPE)) reasons.push("processor-scope-missing");
154
+ return reasons;
155
+ }
156
+
157
+ /** A processor refusal: `install-not-found` only when the tenant has no such install. */
158
+ export type SetProcessorRefusal =
159
+ | { ok: false; code: "install-not-found" | "conflict" | "location-invalid" }
160
+ | { ok: false; code: "install-not-processor"; reasons: ProcessorIneligibility[] };
161
+
126
162
  /**
127
163
  * Sets or clears the (tenant, stage, location) processor. Switching or clearing releases the
128
164
  * previous install's active and held assignments routed through that location on that stage in
@@ -132,14 +168,17 @@ export async function installMayProcess(
132
168
  export async function setProcessor(
133
169
  db: FulfillmentDatabase,
134
170
  input: { tenantId: string; stage: CommerceStage; locationId?: string; installId: string | null; actorId: string; now: string },
135
- ): Promise<{ ok: true; installId: string | null } | { ok: false; code: "install-not-found" | "conflict" | "location-invalid" }> {
171
+ ): Promise<{ ok: true; installId: string | null } | SetProcessorRefusal> {
136
172
  const locationId = input.locationId ?? DEFAULT_LOCATION;
137
173
  if (!LOCATION_ID.test(locationId)) return { ok: false, code: "location-invalid" };
138
174
  for (let attempt = 0; attempt < 3; attempt++) {
139
175
  const current = await readProcessor(db, input.tenantId, input.stage, locationId);
140
176
  if ((current?.installId ?? null) === input.installId) return { ok: true, installId: input.installId };
141
177
  if (input.installId !== null && !(await installMayProcess(db, input.tenantId, input.installId))) {
142
- return { ok: false, code: "install-not-found" };
178
+ const why = await processorIneligibility(db, input.tenantId, input.installId);
179
+ if (why === "not-found") return { ok: false, code: "install-not-found" };
180
+ if (why.length) return { ok: false, code: "install-not-processor", reasons: why };
181
+ continue;
143
182
  }
144
183
  let first: SqlStatement;
145
184
  if (input.installId === null) {
@@ -17,7 +17,7 @@ import type { Queryable } from "../catalog/d1/catalog.js";
17
17
  import { prepareOrderContactKeys, sealOrderContact } from "../orders/vault/orderContactVault.js";
18
18
  import { readOrderContactCommand, type OrderContactCommand } from "../orders/vault/orderContact.js";
19
19
  import { grantConsent, grantedByHash } from "../integrations/apps/appContactConsents.js";
20
- import { readProcessor, setProcessor } from "./assignmentStore.js";
20
+ import { readProcessor, setProcessor, type ProcessorIneligibility } from "./assignmentStore.js";
21
21
  import { FulfillmentService, type FulfillmentActor, type SourceOrderSnapshot } from "./fulfillmentService.js";
22
22
  import { carrierPolicyForTenant, type CarrierPolicy } from "./readiness.js";
23
23
  import type { FulfillmentDatabase } from "./store.js";
@@ -55,13 +55,16 @@ export interface SyntheticScope {
55
55
  export type SyntheticRefusal =
56
56
  | "install-not-found"
57
57
  | "install-not-test"
58
+ | "install-not-processor"
58
59
  | "conflict"
59
60
  | "order-not-found"
60
61
  | "vault-key-unavailable"
61
62
  | "contact-refused"
62
63
  | (string & {});
63
64
 
64
- export type SyntheticResult<T> = { ok: true; value: T } | { ok: false; code: SyntheticRefusal; holds?: readonly string[] };
65
+ export type SyntheticResult<T> =
66
+ | { ok: true; value: T }
67
+ | { ok: false; code: SyntheticRefusal; holds?: readonly string[]; reasons?: readonly ProcessorIneligibility[] };
65
68
 
66
69
  /** The RFC-reserved recipient sealed for every synthetic order. Index picks the 555-01xx line. */
67
70
  export function syntheticRecipient(orderId: string, index = 0): OrderContactCommand {
@@ -95,7 +98,8 @@ function actor(scope: SyntheticScope): FulfillmentActor {
95
98
 
96
99
  /**
97
100
  * Bind `installId` as the tenant's preview processor and grant it preview recipient consent. The
98
- * install must belong to the tenant, be unmanaged, and have env `test`.
101
+ * install must belong to the tenant, be unmanaged, and have env `test`; an install that exists but
102
+ * cannot process (`install-not-processor`) names each unmet condition in `reasons`.
99
103
  */
100
104
  export async function bindTestProcessor(
101
105
  deps: SyntheticOrderDeps,
@@ -118,7 +122,7 @@ export async function bindTestProcessor(
118
122
  actorId: scope.actorId,
119
123
  now: now.toISOString(),
120
124
  });
121
- if (!bound.ok) return { ok: false, code: bound.code };
125
+ if (!bound.ok) return bound.code === "install-not-processor" ? bound : { ok: false, code: bound.code };
122
126
 
123
127
  const live = await deps.db.first(
124
128
  `SELECT 1 AS live FROM app_contact_consents
@@ -59,7 +59,7 @@ function isUniqueViolation(error: unknown): boolean {
59
59
 
60
60
  export type GrantOutcome =
61
61
  | { ok: true; consent: ConsentView }
62
- | { ok: false; code: "install-not-found" | "conflict" };
62
+ | { ok: false; code: "install-not-found" | "install-inactive" | "conflict" };
63
63
 
64
64
  export async function grantConsent(
65
65
  db: Queryable,
@@ -76,7 +76,13 @@ export async function grantConsent(
76
76
  consentId, input.installId, input.tenantId, input.stage, input.grantedBy, grantedAt, input.expiresAt,
77
77
  input.installId, input.tenantId,
78
78
  );
79
- if (rows.length !== 1) return { ok: false, code: "install-not-found" };
79
+ if (rows.length !== 1) {
80
+ const exists = await db.first(
81
+ `SELECT 1 AS present FROM app_installs WHERE install_id = ? AND tenant_id = ?`,
82
+ input.installId, input.tenantId,
83
+ );
84
+ return { ok: false, code: exists ? "install-inactive" : "install-not-found" };
85
+ }
80
86
  } catch (error) {
81
87
  if (isUniqueViolation(error)) return { ok: false, code: "conflict" };
82
88
  throw error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/storefront-runner",
3
- "version": "6.17.1",
3
+ "version": "6.17.2",
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",