@tokenoftrust/storefront-runner 6.12.2 → 6.12.4
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 +149 -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 +165 -30
- package/apps/storefront/src/lib/fulfillment/fulfillmentService.ts +330 -9
- package/apps/storefront/src/lib/fulfillment/splitShipments.ts +388 -0
- 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 +25 -14
- 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,18 @@ 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({
|
|
1756
|
+
assignmentId,
|
|
1757
|
+
tenantId: order.tenantId,
|
|
1758
|
+
stage: order.stage,
|
|
1759
|
+
orderId: order.orderId,
|
|
1760
|
+
version,
|
|
1761
|
+
lines: state.routing.lines,
|
|
1762
|
+
}),
|
|
1530
1763
|
],
|
|
1531
1764
|
deliveries: [],
|
|
1532
1765
|
};
|
|
@@ -1574,6 +1807,23 @@ export class FulfillmentService {
|
|
|
1574
1807
|
});
|
|
1575
1808
|
}
|
|
1576
1809
|
|
|
1810
|
+
/** A partial shipment's `order.partially_fulfilled`, written only with its own shipment row. */
|
|
1811
|
+
private async appendOrderPartiallyFulfilled(plan: PlanStatements, state: LoadedOrder, shipmentId: string): Promise<void> {
|
|
1812
|
+
const { order } = state;
|
|
1813
|
+
const targets = commerceTargetsQuery(order.tenantId, "order.partially_fulfilled", order.stage);
|
|
1814
|
+
const installs = await this.db.all<{ install_id: string }>(targets.sql, targets.params);
|
|
1815
|
+
const payload: Record<string, unknown> = { id: order.sourceOrderId, status: "created", currency: order.currency };
|
|
1816
|
+
if (state.lines.length > 0) payload.lineItems = state.lines.map((line) => ({ sku: line.sku, quantity: line.quantity }));
|
|
1817
|
+
this.appendCommerceEvent(plan, {
|
|
1818
|
+
tenantId: order.tenantId,
|
|
1819
|
+
stage: order.stage,
|
|
1820
|
+
topic: "order.partially_fulfilled",
|
|
1821
|
+
payload,
|
|
1822
|
+
gate: shipmentCommittedGate(shipmentId),
|
|
1823
|
+
installIds: installs.map((row) => row.install_id),
|
|
1824
|
+
});
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1577
1827
|
/**
|
|
1578
1828
|
* The processor's reconciliation read: the calling install's active assignments on this tenant
|
|
1579
1829
|
* and stage whose order is still releasable at its current event version, oldest first.
|
|
@@ -1606,6 +1856,7 @@ export class FulfillmentService {
|
|
|
1606
1856
|
if (!state || state.assignment?.assignmentId !== row.assignment_id || state.assignment.installId !== input.installId) continue;
|
|
1607
1857
|
if (state.assignment.state !== "active" || state.lines.length === 0) continue;
|
|
1608
1858
|
if (!canRelease(this.readiness(state))) continue;
|
|
1859
|
+
const lines = await readAssignmentLines(this.db, row.assignment_id);
|
|
1609
1860
|
assignments.push({
|
|
1610
1861
|
assignmentId: row.assignment_id,
|
|
1611
1862
|
orderId: row.order_id,
|
|
@@ -1614,6 +1865,18 @@ export class FulfillmentService {
|
|
|
1614
1865
|
assignedAt: row.assigned_at,
|
|
1615
1866
|
purchaseId: await purchaseIdFor(input.tenantId, input.stage, row.order_id),
|
|
1616
1867
|
lineItems: state.lines.map((line) => ({ sku: line.sku, quantity: line.quantity })),
|
|
1868
|
+
locationId: state.assignment.locationId,
|
|
1869
|
+
allowedCarriers: allowedCarriers(this.policy.prohibitedCarriers, state.prohibitedCarriers),
|
|
1870
|
+
adultSignatureRequired: state.adultSignatureRequired || this.policy.adultSignatureRequired,
|
|
1871
|
+
lines: lines.map((line) => ({
|
|
1872
|
+
lineId: line.lineId,
|
|
1873
|
+
sku: line.sku,
|
|
1874
|
+
locationId: line.locationId,
|
|
1875
|
+
quantity: line.assigned,
|
|
1876
|
+
shipped: line.shipped,
|
|
1877
|
+
cancelled: line.cancelled,
|
|
1878
|
+
remaining: remainingQuantity(line),
|
|
1879
|
+
})),
|
|
1617
1880
|
});
|
|
1618
1881
|
}
|
|
1619
1882
|
const last = page[page.length - 1];
|
|
@@ -1988,8 +2251,10 @@ export class FulfillmentService {
|
|
|
1988
2251
|
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
2252
|
sourceHolds=sourceAuthority?.order_id!==order.orderId?["source-authority-unavailable"]:authorityHolds(sourceAuthority,this.now());
|
|
1990
2253
|
}catch{sourceHolds=["source-authority-unreadable"];}
|
|
1991
|
-
const
|
|
2254
|
+
const routing = await routeOrderLines(this.db, { tenantId, stage, orderId }, lines);
|
|
2255
|
+
const processor = await resolveProcessor(this.db, tenantId, stage, routing.locationId);
|
|
1992
2256
|
const assignment = await readOpenAssignment(this.db, tenantId, stage, orderId);
|
|
2257
|
+
const exceptions = await readOpenExceptions(this.db, { tenantId, stage, orderId });
|
|
1993
2258
|
const contacts = await readOrderContactAvailability({
|
|
1994
2259
|
all: (sql, ...params) => this.db.all(sql, params),
|
|
1995
2260
|
}, { tenantId, stage, orderIds: [orderId], now: new Date(this.now()) });
|
|
@@ -2013,6 +2278,7 @@ export class FulfillmentService {
|
|
|
2013
2278
|
prohibitedCarriers: intentProhibited,
|
|
2014
2279
|
adultSignatureRequired: intentAdult,
|
|
2015
2280
|
auditCount: audits.length,
|
|
2281
|
+
routing,
|
|
2016
2282
|
processor,
|
|
2017
2283
|
assignment,
|
|
2018
2284
|
view: {
|
|
@@ -2034,6 +2300,7 @@ export class FulfillmentService {
|
|
|
2034
2300
|
lines, evidence, placedAt:order.placedAt, currency:order.currency,
|
|
2035
2301
|
processor,
|
|
2036
2302
|
assignment: assignment ? { assignmentId: assignment.assignmentId, installId: assignment.installId, state: assignment.state } : null,
|
|
2303
|
+
exceptions,
|
|
2037
2304
|
},
|
|
2038
2305
|
};
|
|
2039
2306
|
const readiness = this.readiness(loaded);
|
|
@@ -2070,6 +2337,60 @@ export interface AssignmentReadItem {
|
|
|
2070
2337
|
assignedAt: string;
|
|
2071
2338
|
purchaseId: string;
|
|
2072
2339
|
lineItems: { sku: string; quantity: number }[];
|
|
2340
|
+
/** The processor-config location the assignment was routed through (`*` for the catch-all). */
|
|
2341
|
+
locationId: string;
|
|
2342
|
+
/** Carriers this order may ship with: the closed set minus the tenant's and the order's prohibited carriers. */
|
|
2343
|
+
allowedCarriers: string[];
|
|
2344
|
+
adultSignatureRequired: boolean;
|
|
2345
|
+
/** One entry per sku with its shipped, cancelled and remaining quantity. */
|
|
2346
|
+
lines: AssignmentReadLine[];
|
|
2347
|
+
}
|
|
2348
|
+
|
|
2349
|
+
export interface AssignmentReadLine {
|
|
2350
|
+
lineId: string;
|
|
2351
|
+
sku: string;
|
|
2352
|
+
locationId: string;
|
|
2353
|
+
quantity: number;
|
|
2354
|
+
shipped: number;
|
|
2355
|
+
cancelled: number;
|
|
2356
|
+
remaining: number;
|
|
2357
|
+
}
|
|
2358
|
+
|
|
2359
|
+
/**
|
|
2360
|
+
* The first statement of an assignment shipment or exception: the order's compare-and-set at its
|
|
2361
|
+
* read version, under the install and assignment predicate. A split shipment also requires that no
|
|
2362
|
+
* whole-order label is in flight, and the completing one hands the order off.
|
|
2363
|
+
*/
|
|
2364
|
+
function assignmentOrderCas(
|
|
2365
|
+
state: LoadedOrder,
|
|
2366
|
+
binding: AppAssignmentBinding,
|
|
2367
|
+
options: { complete: boolean; splitShipment: boolean },
|
|
2368
|
+
): SqlStatement {
|
|
2369
|
+
const { order } = state;
|
|
2370
|
+
const predicate = appWritePredicate(binding, order);
|
|
2371
|
+
const label = options.splitShipment
|
|
2372
|
+
? `AND label_state IN (${SPLIT_LABEL_STATES.map(() => "?").join(", ")})
|
|
2373
|
+
AND NOT EXISTS (
|
|
2374
|
+
SELECT 1 FROM scrappy_shipments AS s
|
|
2375
|
+
WHERE s.tenant_id = ? AND s.stage = ? AND s.order_id = ? AND s.label_purchase_state <> 'voided'
|
|
2376
|
+
)`
|
|
2377
|
+
: "";
|
|
2378
|
+
return {
|
|
2379
|
+
sql: `UPDATE scrappy_commerce_orders
|
|
2380
|
+
SET event_version = event_version + 1${options.complete ? ", fulfillment_state = 'handed_off'" : ""}
|
|
2381
|
+
WHERE tenant_id = ? AND stage = ? AND order_id = ? AND event_version = ?
|
|
2382
|
+
AND payment_state = 'captured' AND stock_state = 'confirmed' AND fulfillment_state = 'ready'
|
|
2383
|
+
${label}
|
|
2384
|
+
AND ${predicate.sql}`,
|
|
2385
|
+
params: [
|
|
2386
|
+
order.tenantId,
|
|
2387
|
+
order.stage,
|
|
2388
|
+
order.orderId,
|
|
2389
|
+
order.eventVersion,
|
|
2390
|
+
...(options.splitShipment ? [...SPLIT_LABEL_STATES, order.tenantId, order.stage, order.orderId] : []),
|
|
2391
|
+
...predicate.params,
|
|
2392
|
+
],
|
|
2393
|
+
};
|
|
2073
2394
|
}
|
|
2074
2395
|
|
|
2075
2396
|
function encodeAssignmentCursor(assignedAt: string, assignmentId: string): string {
|