@tokenoftrust/storefront-runner 6.12.3 → 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.
|
@@ -44,6 +44,52 @@ CREATE TABLE `fulfillment_assignment_lines` (
|
|
|
44
44
|
CREATE UNIQUE INDEX `fulfillment_assignment_lines_sku`
|
|
45
45
|
ON `fulfillment_assignment_lines` (`assignment_id`, `sku`);
|
|
46
46
|
--> statement-breakpoint
|
|
47
|
+
-- Open assignments carry the lines of their order's latest line snapshot, read and routed as the
|
|
48
|
+
-- service reads and routes them: a snapshot with any malformed entry carries no lines; otherwise
|
|
49
|
+
-- one line per sku in first-seen order, at the sku's single confirmed allocation location, else '*'.
|
|
50
|
+
WITH `latest` AS (
|
|
51
|
+
SELECT `audit`.`tenant_id`, `audit`.`stage`, `audit`.`order_id`, `audit`.`to_state`
|
|
52
|
+
FROM `scrappy_shipment_audit` AS `audit`
|
|
53
|
+
WHERE `audit`.`kind` = 'lines' AND json_valid(`audit`.`to_state`)
|
|
54
|
+
AND NOT EXISTS (
|
|
55
|
+
SELECT 1 FROM `scrappy_shipment_audit` AS `later`
|
|
56
|
+
WHERE `later`.`tenant_id` = `audit`.`tenant_id` AND `later`.`stage` = `audit`.`stage`
|
|
57
|
+
AND `later`.`order_id` = `audit`.`order_id` AND `later`.`kind` = 'lines'
|
|
58
|
+
AND (`later`.`at` > `audit`.`at` OR (`later`.`at` = `audit`.`at` AND `later`.`audit_id` > `audit`.`audit_id`))
|
|
59
|
+
)
|
|
60
|
+
), `items` AS (
|
|
61
|
+
SELECT `a`.`assignment_id`, `a`.`tenant_id`, `a`.`stage`, `a`.`order_id`,
|
|
62
|
+
json_extract(`item`.`value`, '$.sku') AS `sku`,
|
|
63
|
+
json_extract(`item`.`value`, '$.quantity') AS `quantity`,
|
|
64
|
+
CAST(`item`.`key` AS INTEGER) AS `position`
|
|
65
|
+
FROM `fulfillment_assignments` AS `a`
|
|
66
|
+
JOIN `latest` ON `latest`.`tenant_id` = `a`.`tenant_id` AND `latest`.`stage` = `a`.`stage` AND `latest`.`order_id` = `a`.`order_id`
|
|
67
|
+
JOIN json_each(`latest`.`to_state`) AS `item`
|
|
68
|
+
WHERE `a`.`state` IN ('active', 'held') AND json_type(`latest`.`to_state`) = 'array'
|
|
69
|
+
), `malformed` AS (
|
|
70
|
+
SELECT DISTINCT `assignment_id` FROM `items`
|
|
71
|
+
WHERE typeof(`sku`) <> 'text' OR length(`sku`) NOT BETWEEN 1 AND 128 OR `sku` GLOB '*[^A-Za-z0-9._:-]*'
|
|
72
|
+
OR typeof(`quantity`) <> 'integer' OR `quantity` < 1
|
|
73
|
+
), `grouped` AS (
|
|
74
|
+
SELECT `assignment_id`, `tenant_id`, `stage`, `order_id`, `sku`, SUM(`quantity`) AS `quantity`, MIN(`position`) AS `first`
|
|
75
|
+
FROM `items`
|
|
76
|
+
WHERE `assignment_id` NOT IN (SELECT `assignment_id` FROM `malformed`)
|
|
77
|
+
GROUP BY `assignment_id`, `sku`
|
|
78
|
+
)
|
|
79
|
+
INSERT INTO `fulfillment_assignment_lines` (`assignment_id`, `line_id`, `sku`, `location_id`, `quantity_assigned`)
|
|
80
|
+
SELECT `g`.`assignment_id`,
|
|
81
|
+
CAST(ROW_NUMBER() OVER (PARTITION BY `g`.`assignment_id` ORDER BY `g`.`first`) AS TEXT),
|
|
82
|
+
`g`.`sku`,
|
|
83
|
+
COALESCE((
|
|
84
|
+
SELECT MIN(`al`.`location_id`) FROM `scrappy_allocations` AS `al`
|
|
85
|
+
WHERE `al`.`tenant_id` = `g`.`tenant_id` AND `al`.`stage` = `g`.`stage` AND `al`.`order_id` = `g`.`order_id`
|
|
86
|
+
AND `al`.`sku` = `g`.`sku` AND `al`.`state` = 'confirmed' AND `al`.`location_id` <> 'unspecified'
|
|
87
|
+
AND (`al`.`location_id` = '*' OR (length(`al`.`location_id`) BETWEEN 1 AND 128 AND `al`.`location_id` NOT GLOB '*[^A-Za-z0-9._:-]*'))
|
|
88
|
+
HAVING COUNT(DISTINCT `al`.`location_id`) = 1
|
|
89
|
+
), '*'),
|
|
90
|
+
`g`.`quantity`
|
|
91
|
+
FROM `grouped` AS `g`;
|
|
92
|
+
--> statement-breakpoint
|
|
47
93
|
-- A shipment the processor reported against an assignment. Rows are never updated or deleted.
|
|
48
94
|
CREATE TABLE `fulfillment_shipments` (
|
|
49
95
|
`shipment_id` text PRIMARY KEY NOT NULL,
|
|
@@ -284,18 +284,47 @@ export function insertAssignmentStatement(input: {
|
|
|
284
284
|
};
|
|
285
285
|
}
|
|
286
286
|
|
|
287
|
-
/**
|
|
287
|
+
/**
|
|
288
|
+
* Writes the assignment's lines, only when its assignment row was written at `version` in this
|
|
289
|
+
* batch. Each line carries what the order's earlier assignments have not shipped or cancelled; a
|
|
290
|
+
* line with nothing left is not written.
|
|
291
|
+
*/
|
|
288
292
|
export function insertAssignmentLinesStatements(input: {
|
|
289
293
|
assignmentId: string;
|
|
294
|
+
tenantId: string;
|
|
295
|
+
stage: CommerceStage;
|
|
296
|
+
orderId: string;
|
|
290
297
|
version: number;
|
|
291
298
|
lines: readonly RoutedLine[];
|
|
292
299
|
}): SqlStatement[] {
|
|
293
300
|
return input.lines.map((line) => ({
|
|
294
301
|
sql: `INSERT INTO fulfillment_assignment_lines (assignment_id, line_id, sku, location_id, quantity_assigned)
|
|
295
|
-
SELECT ?, ?, ?, ?,
|
|
302
|
+
SELECT ?, ?, ?, ?, rest.quantity FROM (
|
|
303
|
+
SELECT ? - COALESCE((
|
|
304
|
+
SELECT SUM(prior.quantity_shipped + prior.quantity_cancelled)
|
|
305
|
+
FROM fulfillment_assignment_lines AS prior
|
|
306
|
+
JOIN fulfillment_assignments AS earlier ON earlier.assignment_id = prior.assignment_id
|
|
307
|
+
WHERE earlier.tenant_id = ? AND earlier.stage = ? AND earlier.order_id = ?
|
|
308
|
+
AND earlier.assignment_id <> ? AND prior.sku = ?
|
|
309
|
+
), 0) AS quantity
|
|
310
|
+
) AS rest
|
|
311
|
+
WHERE rest.quantity > 0 AND EXISTS (
|
|
296
312
|
SELECT 1 FROM fulfillment_assignments WHERE assignment_id = ? AND state = 'active' AND event_version = ?
|
|
297
313
|
)`,
|
|
298
|
-
params: [
|
|
314
|
+
params: [
|
|
315
|
+
input.assignmentId,
|
|
316
|
+
line.lineId,
|
|
317
|
+
line.sku,
|
|
318
|
+
line.locationId,
|
|
319
|
+
line.quantity,
|
|
320
|
+
input.tenantId,
|
|
321
|
+
input.stage,
|
|
322
|
+
input.orderId,
|
|
323
|
+
input.assignmentId,
|
|
324
|
+
line.sku,
|
|
325
|
+
input.assignmentId,
|
|
326
|
+
input.version,
|
|
327
|
+
],
|
|
299
328
|
}));
|
|
300
329
|
}
|
|
301
330
|
|
|
@@ -1752,7 +1752,14 @@ export class FulfillmentService {
|
|
|
1752
1752
|
version,
|
|
1753
1753
|
now: this.now(),
|
|
1754
1754
|
}),
|
|
1755
|
-
...insertAssignmentLinesStatements({
|
|
1755
|
+
...insertAssignmentLinesStatements({
|
|
1756
|
+
assignmentId,
|
|
1757
|
+
tenantId: order.tenantId,
|
|
1758
|
+
stage: order.stage,
|
|
1759
|
+
orderId: order.orderId,
|
|
1760
|
+
version,
|
|
1761
|
+
lines: state.routing.lines,
|
|
1762
|
+
}),
|
|
1756
1763
|
],
|
|
1757
1764
|
deliveries: [],
|
|
1758
1765
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/storefront-runner",
|
|
3
|
-
"version": "6.12.
|
|
3
|
+
"version": "6.12.4",
|
|
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",
|