@tokenoftrust/storefront-runner 6.12.1 → 6.12.3
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.
- package/apps/storefront/migrations-apps/0030_assignment_split_shipments.sql +103 -0
- package/apps/storefront/src/lib/access/auth/loginGate.ts +2 -0
- package/apps/storefront/src/lib/customers/contactVault/dataKeys.ts +28 -1
- package/apps/storefront/src/lib/fulfillment/admin/commerceDesk.ts +13 -0
- package/apps/storefront/src/lib/fulfillment/appShipmentEvidence.ts +6 -2
- package/apps/storefront/src/lib/fulfillment/assignmentStore.ts +136 -30
- package/apps/storefront/src/lib/fulfillment/fulfillmentService.ts +323 -9
- package/apps/storefront/src/lib/fulfillment/splitShipments.ts +388 -0
- package/apps/storefront/src/lib/fulfillment/syntheticPreviewOrders.ts +5 -65
- package/apps/storefront/src/lib/integrations/webhooks/commercePayloads.ts +9 -5
- package/apps/storefront/src/lib/integrations/webhooks/deliveryStore.ts +1 -0
- package/apps/storefront/src/lib/integrations/webhooks/events.ts +1 -0
- package/apps/storefront/src/lib/orders/vault/orderContactVault.ts +74 -27
- package/package.json +1 -1
|
@@ -53,16 +53,20 @@ import {
|
|
|
53
53
|
type SqlStatement,
|
|
54
54
|
} from "./store.js";
|
|
55
55
|
import {
|
|
56
|
+
DEFAULT_LOCATION,
|
|
56
57
|
PROCESSOR_SCOPE,
|
|
57
58
|
assignmentAtGate,
|
|
59
|
+
insertAssignmentLinesStatements,
|
|
58
60
|
insertAssignmentStatement,
|
|
59
61
|
moveAssignmentStatement,
|
|
60
62
|
processorGuard,
|
|
61
63
|
readOpenAssignment,
|
|
62
|
-
|
|
64
|
+
resolveProcessor,
|
|
65
|
+
routeOrderLines,
|
|
63
66
|
type AssignmentState,
|
|
64
67
|
type OpenAssignment,
|
|
65
68
|
type ProcessorConfig,
|
|
69
|
+
type RoutedLine,
|
|
66
70
|
} from "./assignmentStore.js";
|
|
67
71
|
import {
|
|
68
72
|
CommercePayloadRefused,
|
|
@@ -86,10 +90,29 @@ import {
|
|
|
86
90
|
type AppAssignmentBinding,
|
|
87
91
|
type AppEvidenceResponse,
|
|
88
92
|
type AppShipmentView,
|
|
93
|
+
type AppWriteView,
|
|
89
94
|
type HandoffEvidence,
|
|
90
95
|
type LabelOutcomeEvidence,
|
|
91
96
|
type StoredLabelFacts,
|
|
92
97
|
} from "./appShipmentEvidence.js";
|
|
98
|
+
import {
|
|
99
|
+
allowedCarriers,
|
|
100
|
+
completesLines,
|
|
101
|
+
consumeLinesStatements,
|
|
102
|
+
insertExceptionStatement,
|
|
103
|
+
insertShipmentStatement,
|
|
104
|
+
lineRefusal,
|
|
105
|
+
orderCommittedAt,
|
|
106
|
+
readAssignmentLines,
|
|
107
|
+
readOpenExceptions,
|
|
108
|
+
remainingLines,
|
|
109
|
+
remainingQuantity,
|
|
110
|
+
shipmentCarrierRefusal,
|
|
111
|
+
shipmentCommittedGate,
|
|
112
|
+
type AssignmentExceptionEvidence,
|
|
113
|
+
type AssignmentExceptionRecord,
|
|
114
|
+
type AssignmentShipmentEvidence,
|
|
115
|
+
} from "./splitShipments.js";
|
|
93
116
|
|
|
94
117
|
export const OPAQUE_ID = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
95
118
|
|
|
@@ -168,10 +191,12 @@ export interface FulfillmentView {
|
|
|
168
191
|
evidence: CommerceEvidence;
|
|
169
192
|
placedAt: string;
|
|
170
193
|
currency: string;
|
|
171
|
-
/** The (
|
|
172
|
-
processor: { installId: string; notifiable: boolean } | null;
|
|
194
|
+
/** The processor install the order routes to (its location's, else the catch-all), when one is configured. */
|
|
195
|
+
processor: { installId: string; locationId: string; notifiable: boolean } | null;
|
|
173
196
|
/** The order's open assignment and the install that owns it. */
|
|
174
197
|
assignment: { assignmentId: string; installId: string; state: "active" | "held" } | null;
|
|
198
|
+
/** Lines the processor reported it cannot ship, awaiting the operator, oldest first. */
|
|
199
|
+
exceptions: AssignmentExceptionRecord[];
|
|
175
200
|
}
|
|
176
201
|
|
|
177
202
|
export interface FulfillmentBatch {
|
|
@@ -231,6 +256,8 @@ interface LoadedOrder {
|
|
|
231
256
|
prohibitedCarriers: string[];
|
|
232
257
|
adultSignatureRequired: boolean;
|
|
233
258
|
auditCount: number;
|
|
259
|
+
/** The order's lines by sku with their inventory locations, and the location it routes through. */
|
|
260
|
+
routing: { locationId: string; lines: RoutedLine[] };
|
|
234
261
|
processor: ProcessorConfig | null;
|
|
235
262
|
assignment: OpenAssignment | null;
|
|
236
263
|
view: FulfillmentView;
|
|
@@ -264,8 +291,19 @@ export interface AppEvidenceRequest<T> {
|
|
|
264
291
|
evidence: T;
|
|
265
292
|
}
|
|
266
293
|
|
|
294
|
+
/** One assignment-scoped app write: the order is the one the bound assignment names. */
|
|
295
|
+
export interface AssignmentWriteRequest<T> {
|
|
296
|
+
actor: FulfillmentActor;
|
|
297
|
+
idempotencyKey: string;
|
|
298
|
+
body: unknown;
|
|
299
|
+
evidence: T;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Label states a split shipment proceeds under: no whole-order label is in flight. */
|
|
303
|
+
const SPLIT_LABEL_STATES: readonly LabelState[] = ["none", "voided"];
|
|
304
|
+
|
|
267
305
|
type AppPlan =
|
|
268
|
-
| { type: "done"; view:
|
|
306
|
+
| { type: "done"; view: AppWriteView }
|
|
269
307
|
| { type: "refuse"; code: string; holds?: readonly string[] }
|
|
270
308
|
| {
|
|
271
309
|
type: "write";
|
|
@@ -273,7 +311,7 @@ type AppPlan =
|
|
|
273
311
|
sourceAdvance: boolean;
|
|
274
312
|
deliveries: readonly PlannedDelivery[];
|
|
275
313
|
/** The planned post-state: the response and the idempotency record. */
|
|
276
|
-
view:
|
|
314
|
+
view: AppWriteView;
|
|
277
315
|
};
|
|
278
316
|
|
|
279
317
|
/** Statement and delivery accumulator for a plan that may carry commerce events. */
|
|
@@ -812,7 +850,7 @@ export class FulfillmentService {
|
|
|
812
850
|
if (assignment?.state === "held" && !state.cancelPending && state.order.payment !== "refund_requested") {
|
|
813
851
|
// Resume only while the install is still the configured, notifiable processor; otherwise
|
|
814
852
|
// release it so the order waits for a new operator release.
|
|
815
|
-
const guard = processorGuard(state.order.tenantId, state.order.stage, assignment.installId);
|
|
853
|
+
const guard = processorGuard(state.order.tenantId, state.order.stage, assignment.installId, assignment.locationId);
|
|
816
854
|
this.appendAssignmentMove(plan, state, assignment, "active", version, guard);
|
|
817
855
|
plan.statements.push(
|
|
818
856
|
moveAssignmentStatement({
|
|
@@ -1109,12 +1147,41 @@ export class FulfillmentService {
|
|
|
1109
1147
|
});
|
|
1110
1148
|
if (mismatch) return { type: "refuse", code: mismatch };
|
|
1111
1149
|
const version = state.order.eventVersion + 1;
|
|
1150
|
+
const lines = await readAssignmentLines(this.db, binding.assignmentId);
|
|
1151
|
+
const remaining = remainingLines(lines);
|
|
1152
|
+
const facts = state.labelOutcome;
|
|
1153
|
+
if (remaining.length > 0 && !facts?.trackingNumber) return { type: "refuse", code: "label" };
|
|
1112
1154
|
const [first, ...rest] = this.handoffStatements(state, request.actor.actorId, version);
|
|
1113
1155
|
const predicate = appWritePredicate(binding, state.order);
|
|
1114
1156
|
const plan: PlanStatements = {
|
|
1115
1157
|
deliveries: [],
|
|
1116
1158
|
statements: [{ sql: `${first!.sql} AND ${predicate.sql}`, params: [...first!.params, ...predicate.params] }, ...rest],
|
|
1117
1159
|
};
|
|
1160
|
+
// The whole-order handoff ships every line the assignment still holds, as one shipment.
|
|
1161
|
+
if (remaining.length > 0) {
|
|
1162
|
+
const gate = orderCommittedAt(state.order, version);
|
|
1163
|
+
plan.statements.push(
|
|
1164
|
+
...consumeLinesStatements(binding.assignmentId, lines, remaining, gate),
|
|
1165
|
+
insertShipmentStatement(
|
|
1166
|
+
{
|
|
1167
|
+
shipmentId: `shp_${crypto.randomUUID()}`,
|
|
1168
|
+
assignmentId: binding.assignmentId,
|
|
1169
|
+
tenantId: state.order.tenantId,
|
|
1170
|
+
stage: state.order.stage,
|
|
1171
|
+
orderId: state.order.orderId,
|
|
1172
|
+
installId: binding.installId,
|
|
1173
|
+
carrier: evidence.carrier,
|
|
1174
|
+
service: facts!.service,
|
|
1175
|
+
trackingNumber: facts!.trackingNumber!,
|
|
1176
|
+
trackingUrl: facts!.trackingUrl,
|
|
1177
|
+
adultSignature: facts!.adultSignature,
|
|
1178
|
+
lines: remaining,
|
|
1179
|
+
now: this.now(),
|
|
1180
|
+
},
|
|
1181
|
+
gate,
|
|
1182
|
+
),
|
|
1183
|
+
);
|
|
1184
|
+
}
|
|
1118
1185
|
await this.appendOrderFulfilled(plan, state, version);
|
|
1119
1186
|
return {
|
|
1120
1187
|
type: "write",
|
|
@@ -1126,13 +1193,169 @@ export class FulfillmentService {
|
|
|
1126
1193
|
});
|
|
1127
1194
|
}
|
|
1128
1195
|
|
|
1196
|
+
/**
|
|
1197
|
+
* One shipment against the processor's assignment: some or all of its remaining line quantities.
|
|
1198
|
+
* Every release hold, the order's carrier and adult-signature policy, and the per-line remainder
|
|
1199
|
+
* refuse before anything is written. The order's compare-and-set serializes it with every other
|
|
1200
|
+
* write to the order; the shipment that leaves nothing remaining hands the order off and claims
|
|
1201
|
+
* its one `order.fulfilled`, and every other shipment emits `order.partially_fulfilled`.
|
|
1202
|
+
*/
|
|
1203
|
+
async appRecordAssignmentShipment(request: AssignmentWriteRequest<AssignmentShipmentEvidence>): Promise<AppEvidenceResponse> {
|
|
1204
|
+
const evidence = request.evidence;
|
|
1205
|
+
return this.assignmentCommit(request, async (state, binding) => {
|
|
1206
|
+
const carrier = shipmentCarrierRefusal(evidence.carrier, this.policy.prohibitedCarriers, state.prohibitedCarriers);
|
|
1207
|
+
if (carrier) return { type: "refuse", code: carrier };
|
|
1208
|
+
const holds = releaseHolds(this.readiness(state));
|
|
1209
|
+
if (holds.length > 0) return { type: "refuse", code: holds[0]!, holds };
|
|
1210
|
+
if (state.order.fulfillment !== "ready") return { type: "refuse", code: "fulfillment" };
|
|
1211
|
+
const labelInFlight = state.shipment !== null && state.shipment.labelPurchaseState !== "voided";
|
|
1212
|
+
if (labelInFlight || !SPLIT_LABEL_STATES.includes(state.order.label)) return { type: "refuse", code: "whole-order-label" };
|
|
1213
|
+
if ((state.adultSignatureRequired || this.policy.adultSignatureRequired) && evidence.adultSignature !== "accepted") {
|
|
1214
|
+
return { type: "refuse", code: "adult-signature" };
|
|
1215
|
+
}
|
|
1216
|
+
const lines = await readAssignmentLines(this.db, binding.assignmentId);
|
|
1217
|
+
const lineCode = lineRefusal(lines, evidence.lines);
|
|
1218
|
+
if (lineCode) return { type: "refuse", code: lineCode };
|
|
1219
|
+
const complete = completesLines(lines, evidence.lines);
|
|
1220
|
+
const version = state.order.eventVersion + 1;
|
|
1221
|
+
const gate = orderCommittedAt(state.order, version);
|
|
1222
|
+
const shipmentId = `shp_${crypto.randomUUID()}`;
|
|
1223
|
+
const plan: PlanStatements = {
|
|
1224
|
+
deliveries: [],
|
|
1225
|
+
statements: [
|
|
1226
|
+
assignmentOrderCas(state, binding, { complete, splitShipment: true }),
|
|
1227
|
+
...consumeLinesStatements(binding.assignmentId, lines, evidence.lines, gate),
|
|
1228
|
+
insertShipmentStatement(
|
|
1229
|
+
{
|
|
1230
|
+
shipmentId,
|
|
1231
|
+
assignmentId: binding.assignmentId,
|
|
1232
|
+
tenantId: state.order.tenantId,
|
|
1233
|
+
stage: state.order.stage,
|
|
1234
|
+
orderId: state.order.orderId,
|
|
1235
|
+
installId: binding.installId,
|
|
1236
|
+
carrier: evidence.carrier,
|
|
1237
|
+
service: evidence.service ?? null,
|
|
1238
|
+
trackingNumber: evidence.trackingNumber,
|
|
1239
|
+
trackingUrl: evidence.trackingUrl ?? null,
|
|
1240
|
+
adultSignature: evidence.adultSignature,
|
|
1241
|
+
lines: evidence.lines,
|
|
1242
|
+
now: this.now(),
|
|
1243
|
+
},
|
|
1244
|
+
gate,
|
|
1245
|
+
),
|
|
1246
|
+
auditStatement(
|
|
1247
|
+
state,
|
|
1248
|
+
request.actor.actorId,
|
|
1249
|
+
"assignment-shipment",
|
|
1250
|
+
"none",
|
|
1251
|
+
jsonText({ shipmentId, assignmentId: binding.assignmentId, lines: evidence.lines }),
|
|
1252
|
+
version,
|
|
1253
|
+
stamp(this.now(), state.auditCount + 1),
|
|
1254
|
+
),
|
|
1255
|
+
],
|
|
1256
|
+
};
|
|
1257
|
+
if (complete) {
|
|
1258
|
+
plan.statements.push(
|
|
1259
|
+
auditStatement(state, request.actor.actorId, "transition", state.order.fulfillment, "handed_off", version, stamp(this.now(), state.auditCount + 2)),
|
|
1260
|
+
terminalTransitionExpiryStatement(state.order, version, new Date(this.now())),
|
|
1261
|
+
);
|
|
1262
|
+
await this.appendOrderFulfilled(plan, state, version);
|
|
1263
|
+
} else {
|
|
1264
|
+
await this.appendOrderPartiallyFulfilled(plan, state, shipmentId);
|
|
1265
|
+
}
|
|
1266
|
+
return {
|
|
1267
|
+
type: "write",
|
|
1268
|
+
statements: plan.statements,
|
|
1269
|
+
sourceAdvance: true,
|
|
1270
|
+
deliveries: plan.deliveries,
|
|
1271
|
+
view: { assignmentId: binding.assignmentId, shipmentId, fulfilledLines: evidence.lines, complete },
|
|
1272
|
+
};
|
|
1273
|
+
});
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
/**
|
|
1277
|
+
* Lines of the processor's assignment it reports it cannot ship, for the operator desk. Every
|
|
1278
|
+
* release hold refuses, and each line's quantity is at most its remainder. Nothing is cancelled,
|
|
1279
|
+
* refunded or emitted.
|
|
1280
|
+
*/
|
|
1281
|
+
async appRecordAssignmentException(request: AssignmentWriteRequest<AssignmentExceptionEvidence>): Promise<AppEvidenceResponse> {
|
|
1282
|
+
const evidence = request.evidence;
|
|
1283
|
+
return this.assignmentCommit(request, async (state, binding) => {
|
|
1284
|
+
const holds = releaseHolds(this.readiness(state));
|
|
1285
|
+
if (holds.length > 0) return { type: "refuse", code: holds[0]!, holds };
|
|
1286
|
+
if (state.order.fulfillment !== "ready") return { type: "refuse", code: "fulfillment" };
|
|
1287
|
+
const lines = await readAssignmentLines(this.db, binding.assignmentId);
|
|
1288
|
+
const lineCode = lineRefusal(lines, evidence.lines);
|
|
1289
|
+
if (lineCode) return { type: "refuse", code: lineCode };
|
|
1290
|
+
const version = state.order.eventVersion + 1;
|
|
1291
|
+
const gate = orderCommittedAt(state.order, version);
|
|
1292
|
+
const exceptionId = `exc_${crypto.randomUUID()}`;
|
|
1293
|
+
return {
|
|
1294
|
+
type: "write",
|
|
1295
|
+
statements: [
|
|
1296
|
+
assignmentOrderCas(state, binding, { complete: false, splitShipment: false }),
|
|
1297
|
+
insertExceptionStatement(
|
|
1298
|
+
{
|
|
1299
|
+
exceptionId,
|
|
1300
|
+
assignmentId: binding.assignmentId,
|
|
1301
|
+
tenantId: state.order.tenantId,
|
|
1302
|
+
stage: state.order.stage,
|
|
1303
|
+
orderId: state.order.orderId,
|
|
1304
|
+
installId: binding.installId,
|
|
1305
|
+
code: evidence.code,
|
|
1306
|
+
lines: evidence.lines,
|
|
1307
|
+
note: evidence.note ?? null,
|
|
1308
|
+
now: this.now(),
|
|
1309
|
+
},
|
|
1310
|
+
gate,
|
|
1311
|
+
),
|
|
1312
|
+
auditStatement(
|
|
1313
|
+
state,
|
|
1314
|
+
request.actor.actorId,
|
|
1315
|
+
"assignment-exception",
|
|
1316
|
+
"none",
|
|
1317
|
+
jsonText({ exceptionId, assignmentId: binding.assignmentId, code: evidence.code }),
|
|
1318
|
+
version,
|
|
1319
|
+
stamp(this.now(), state.auditCount + 1),
|
|
1320
|
+
),
|
|
1321
|
+
],
|
|
1322
|
+
sourceAdvance: true,
|
|
1323
|
+
deliveries: [],
|
|
1324
|
+
view: { assignmentId: binding.assignmentId, exceptionId, code: evidence.code },
|
|
1325
|
+
};
|
|
1326
|
+
});
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
/**
|
|
1330
|
+
* An assignment-scoped write: the order is the one the calling install's assignment names on the
|
|
1331
|
+
* routed tenant and stage. Any other assignment answers not-assigned before anything is read or
|
|
1332
|
+
* recorded; the write then takes the same path as every other app write.
|
|
1333
|
+
*/
|
|
1334
|
+
private async assignmentCommit(
|
|
1335
|
+
request: AssignmentWriteRequest<{ assignmentId: string }>,
|
|
1336
|
+
build: (state: LoadedOrder, binding: AppAssignmentBinding) => AppPlan | Promise<AppPlan>,
|
|
1337
|
+
): Promise<AppEvidenceResponse> {
|
|
1338
|
+
const binding = appBinding(request.actor, request.evidence.assignmentId);
|
|
1339
|
+
if (!binding) return appRefusal("denied");
|
|
1340
|
+
if (!IDEMPOTENCY_KEY.test(request.idempotencyKey)) return appRefusal("malformed");
|
|
1341
|
+
const row = await this.db.get<{ order_id: string }>(
|
|
1342
|
+
`SELECT order_id FROM fulfillment_assignments WHERE assignment_id = ? AND tenant_id = ? AND stage = ? AND install_id = ?`,
|
|
1343
|
+
[binding.assignmentId, request.actor.tenantId, request.actor.stage, binding.installId],
|
|
1344
|
+
);
|
|
1345
|
+
if (!row) return appRefusal("not-assigned");
|
|
1346
|
+
return this.appCommit(
|
|
1347
|
+
{ actor: request.actor, orderId: row.order_id, idempotencyKey: request.idempotencyKey, body: request.body, evidence: request.evidence },
|
|
1348
|
+
(state, bound) => build(state, bound),
|
|
1349
|
+
);
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1129
1352
|
/**
|
|
1130
1353
|
* The app write path: the key ledger first, the assignment binding, the plan, then one batch whose
|
|
1131
1354
|
* first statement carries the install and assignment predicate and whose last records the key.
|
|
1132
1355
|
* A plan-time answer records the key alone. A batch that does not commit is re-classified.
|
|
1133
1356
|
*/
|
|
1134
1357
|
private async appCommit(
|
|
1135
|
-
request: AppEvidenceRequest<
|
|
1358
|
+
request: AppEvidenceRequest<{ assignmentId: string; expectedVersion?: number }>,
|
|
1136
1359
|
build: (state: LoadedOrder, binding: AppAssignmentBinding, purchaseId: string) => AppPlan | Promise<AppPlan>,
|
|
1137
1360
|
): Promise<AppEvidenceResponse> {
|
|
1138
1361
|
const binding = appBinding(request.actor, request.evidence.assignmentId);
|
|
@@ -1514,7 +1737,8 @@ export class FulfillmentService {
|
|
|
1514
1737
|
const version = order.eventVersion + 1;
|
|
1515
1738
|
const assignmentId = `asg_${crypto.randomUUID()}`;
|
|
1516
1739
|
const touch = touchOrder(state, false);
|
|
1517
|
-
const
|
|
1740
|
+
const locationId = state.processor?.installId === installId ? state.processor.locationId : DEFAULT_LOCATION;
|
|
1741
|
+
const guard = processorGuard(order.tenantId, order.stage, installId, locationId);
|
|
1518
1742
|
const plan: PlanStatements = {
|
|
1519
1743
|
statements: [
|
|
1520
1744
|
{ sql: `${touch.sql} AND ${guard.sql}`, params: [...touch.params, ...guard.params] },
|
|
@@ -1524,9 +1748,11 @@ export class FulfillmentService {
|
|
|
1524
1748
|
stage: order.stage,
|
|
1525
1749
|
orderId: order.orderId,
|
|
1526
1750
|
installId,
|
|
1751
|
+
locationId,
|
|
1527
1752
|
version,
|
|
1528
1753
|
now: this.now(),
|
|
1529
1754
|
}),
|
|
1755
|
+
...insertAssignmentLinesStatements({ assignmentId, version, lines: state.routing.lines }),
|
|
1530
1756
|
],
|
|
1531
1757
|
deliveries: [],
|
|
1532
1758
|
};
|
|
@@ -1574,6 +1800,23 @@ export class FulfillmentService {
|
|
|
1574
1800
|
});
|
|
1575
1801
|
}
|
|
1576
1802
|
|
|
1803
|
+
/** A partial shipment's `order.partially_fulfilled`, written only with its own shipment row. */
|
|
1804
|
+
private async appendOrderPartiallyFulfilled(plan: PlanStatements, state: LoadedOrder, shipmentId: string): Promise<void> {
|
|
1805
|
+
const { order } = state;
|
|
1806
|
+
const targets = commerceTargetsQuery(order.tenantId, "order.partially_fulfilled", order.stage);
|
|
1807
|
+
const installs = await this.db.all<{ install_id: string }>(targets.sql, targets.params);
|
|
1808
|
+
const payload: Record<string, unknown> = { id: order.sourceOrderId, status: "created", currency: order.currency };
|
|
1809
|
+
if (state.lines.length > 0) payload.lineItems = state.lines.map((line) => ({ sku: line.sku, quantity: line.quantity }));
|
|
1810
|
+
this.appendCommerceEvent(plan, {
|
|
1811
|
+
tenantId: order.tenantId,
|
|
1812
|
+
stage: order.stage,
|
|
1813
|
+
topic: "order.partially_fulfilled",
|
|
1814
|
+
payload,
|
|
1815
|
+
gate: shipmentCommittedGate(shipmentId),
|
|
1816
|
+
installIds: installs.map((row) => row.install_id),
|
|
1817
|
+
});
|
|
1818
|
+
}
|
|
1819
|
+
|
|
1577
1820
|
/**
|
|
1578
1821
|
* The processor's reconciliation read: the calling install's active assignments on this tenant
|
|
1579
1822
|
* and stage whose order is still releasable at its current event version, oldest first.
|
|
@@ -1606,6 +1849,7 @@ export class FulfillmentService {
|
|
|
1606
1849
|
if (!state || state.assignment?.assignmentId !== row.assignment_id || state.assignment.installId !== input.installId) continue;
|
|
1607
1850
|
if (state.assignment.state !== "active" || state.lines.length === 0) continue;
|
|
1608
1851
|
if (!canRelease(this.readiness(state))) continue;
|
|
1852
|
+
const lines = await readAssignmentLines(this.db, row.assignment_id);
|
|
1609
1853
|
assignments.push({
|
|
1610
1854
|
assignmentId: row.assignment_id,
|
|
1611
1855
|
orderId: row.order_id,
|
|
@@ -1614,6 +1858,18 @@ export class FulfillmentService {
|
|
|
1614
1858
|
assignedAt: row.assigned_at,
|
|
1615
1859
|
purchaseId: await purchaseIdFor(input.tenantId, input.stage, row.order_id),
|
|
1616
1860
|
lineItems: state.lines.map((line) => ({ sku: line.sku, quantity: line.quantity })),
|
|
1861
|
+
locationId: state.assignment.locationId,
|
|
1862
|
+
allowedCarriers: allowedCarriers(this.policy.prohibitedCarriers, state.prohibitedCarriers),
|
|
1863
|
+
adultSignatureRequired: state.adultSignatureRequired || this.policy.adultSignatureRequired,
|
|
1864
|
+
lines: lines.map((line) => ({
|
|
1865
|
+
lineId: line.lineId,
|
|
1866
|
+
sku: line.sku,
|
|
1867
|
+
locationId: line.locationId,
|
|
1868
|
+
quantity: line.assigned,
|
|
1869
|
+
shipped: line.shipped,
|
|
1870
|
+
cancelled: line.cancelled,
|
|
1871
|
+
remaining: remainingQuantity(line),
|
|
1872
|
+
})),
|
|
1617
1873
|
});
|
|
1618
1874
|
}
|
|
1619
1875
|
const last = page[page.length - 1];
|
|
@@ -1988,8 +2244,10 @@ export class FulfillmentService {
|
|
|
1988
2244
|
try { sourceAuthority=await this.db.get<AuthorityRow & Record<string,unknown>>(`SELECT * FROM commerce_source_authority WHERE tenant_id=? AND stage=? AND source=? AND source_order_id=?`,[tenantId,stage,order.source,order.sourceOrderId]) ?? null;
|
|
1989
2245
|
sourceHolds=sourceAuthority?.order_id!==order.orderId?["source-authority-unavailable"]:authorityHolds(sourceAuthority,this.now());
|
|
1990
2246
|
}catch{sourceHolds=["source-authority-unreadable"];}
|
|
1991
|
-
const
|
|
2247
|
+
const routing = await routeOrderLines(this.db, { tenantId, stage, orderId }, lines);
|
|
2248
|
+
const processor = await resolveProcessor(this.db, tenantId, stage, routing.locationId);
|
|
1992
2249
|
const assignment = await readOpenAssignment(this.db, tenantId, stage, orderId);
|
|
2250
|
+
const exceptions = await readOpenExceptions(this.db, { tenantId, stage, orderId });
|
|
1993
2251
|
const contacts = await readOrderContactAvailability({
|
|
1994
2252
|
all: (sql, ...params) => this.db.all(sql, params),
|
|
1995
2253
|
}, { tenantId, stage, orderIds: [orderId], now: new Date(this.now()) });
|
|
@@ -2013,6 +2271,7 @@ export class FulfillmentService {
|
|
|
2013
2271
|
prohibitedCarriers: intentProhibited,
|
|
2014
2272
|
adultSignatureRequired: intentAdult,
|
|
2015
2273
|
auditCount: audits.length,
|
|
2274
|
+
routing,
|
|
2016
2275
|
processor,
|
|
2017
2276
|
assignment,
|
|
2018
2277
|
view: {
|
|
@@ -2034,6 +2293,7 @@ export class FulfillmentService {
|
|
|
2034
2293
|
lines, evidence, placedAt:order.placedAt, currency:order.currency,
|
|
2035
2294
|
processor,
|
|
2036
2295
|
assignment: assignment ? { assignmentId: assignment.assignmentId, installId: assignment.installId, state: assignment.state } : null,
|
|
2296
|
+
exceptions,
|
|
2037
2297
|
},
|
|
2038
2298
|
};
|
|
2039
2299
|
const readiness = this.readiness(loaded);
|
|
@@ -2070,6 +2330,60 @@ export interface AssignmentReadItem {
|
|
|
2070
2330
|
assignedAt: string;
|
|
2071
2331
|
purchaseId: string;
|
|
2072
2332
|
lineItems: { sku: string; quantity: number }[];
|
|
2333
|
+
/** The processor-config location the assignment was routed through (`*` for the catch-all). */
|
|
2334
|
+
locationId: string;
|
|
2335
|
+
/** Carriers this order may ship with: the closed set minus the tenant's and the order's prohibited carriers. */
|
|
2336
|
+
allowedCarriers: string[];
|
|
2337
|
+
adultSignatureRequired: boolean;
|
|
2338
|
+
/** One entry per sku with its shipped, cancelled and remaining quantity. */
|
|
2339
|
+
lines: AssignmentReadLine[];
|
|
2340
|
+
}
|
|
2341
|
+
|
|
2342
|
+
export interface AssignmentReadLine {
|
|
2343
|
+
lineId: string;
|
|
2344
|
+
sku: string;
|
|
2345
|
+
locationId: string;
|
|
2346
|
+
quantity: number;
|
|
2347
|
+
shipped: number;
|
|
2348
|
+
cancelled: number;
|
|
2349
|
+
remaining: number;
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
/**
|
|
2353
|
+
* The first statement of an assignment shipment or exception: the order's compare-and-set at its
|
|
2354
|
+
* read version, under the install and assignment predicate. A split shipment also requires that no
|
|
2355
|
+
* whole-order label is in flight, and the completing one hands the order off.
|
|
2356
|
+
*/
|
|
2357
|
+
function assignmentOrderCas(
|
|
2358
|
+
state: LoadedOrder,
|
|
2359
|
+
binding: AppAssignmentBinding,
|
|
2360
|
+
options: { complete: boolean; splitShipment: boolean },
|
|
2361
|
+
): SqlStatement {
|
|
2362
|
+
const { order } = state;
|
|
2363
|
+
const predicate = appWritePredicate(binding, order);
|
|
2364
|
+
const label = options.splitShipment
|
|
2365
|
+
? `AND label_state IN (${SPLIT_LABEL_STATES.map(() => "?").join(", ")})
|
|
2366
|
+
AND NOT EXISTS (
|
|
2367
|
+
SELECT 1 FROM scrappy_shipments AS s
|
|
2368
|
+
WHERE s.tenant_id = ? AND s.stage = ? AND s.order_id = ? AND s.label_purchase_state <> 'voided'
|
|
2369
|
+
)`
|
|
2370
|
+
: "";
|
|
2371
|
+
return {
|
|
2372
|
+
sql: `UPDATE scrappy_commerce_orders
|
|
2373
|
+
SET event_version = event_version + 1${options.complete ? ", fulfillment_state = 'handed_off'" : ""}
|
|
2374
|
+
WHERE tenant_id = ? AND stage = ? AND order_id = ? AND event_version = ?
|
|
2375
|
+
AND payment_state = 'captured' AND stock_state = 'confirmed' AND fulfillment_state = 'ready'
|
|
2376
|
+
${label}
|
|
2377
|
+
AND ${predicate.sql}`,
|
|
2378
|
+
params: [
|
|
2379
|
+
order.tenantId,
|
|
2380
|
+
order.stage,
|
|
2381
|
+
order.orderId,
|
|
2382
|
+
order.eventVersion,
|
|
2383
|
+
...(options.splitShipment ? [...SPLIT_LABEL_STATES, order.tenantId, order.stage, order.orderId] : []),
|
|
2384
|
+
...predicate.params,
|
|
2385
|
+
],
|
|
2386
|
+
};
|
|
2073
2387
|
}
|
|
2074
2388
|
|
|
2075
2389
|
function encodeAssignmentCursor(assignedAt: string, assignmentId: string): string {
|