diff --git a/scripts/test-order-shipping-ledger.js b/scripts/test-order-shipping-ledger.js index 9d7326e..c852de0 100644 --- a/scripts/test-order-shipping-ledger.js +++ b/scripts/test-order-shipping-ledger.js @@ -253,10 +253,25 @@ async function run() { shipping: { carrierId: carrier.id, type: 'parcel', state: 'shipped' }, }); const directShipped = shipment2.shippings[0]; + + // 9b. Import-style: delivered shipment with shipped shipping created atomically + const order3 = await store.order.create(googleAuthId, { + number: 'O-2026-003', + date: new Date().toISOString(), + entityId: customer.id, + lines: [{ sku: trackedArt.sku, name: 'Tracked Article', pu: 1, amount: 1, net: 50, taxrate: 19 }], + }); + const shipment3 = await store.shipment.create(googleAuthId, { + orderId: order3.id, + state: 'delivered', + lines: [{ sku: trackedArt.sku, amount: 1 }], + shipping: { carrierId: carrier.id, type: 'parcel', state: 'shipped' }, + }); + assert.equal(shipment3.shippings[0].state, 'shipped'); assert.equal(directShipped.state, 'shipped'); const artFinal = await store.article.getBySku(googleAuthId, trackedArt.sku); - assert.equal(artFinal.article.warehouses.reduce((sum, w) => sum + w.stock, 0), 5, 'Stock should now be 5 (7 - 2)'); + assert.equal(artFinal.article.warehouses.reduce((sum, w) => sum + w.stock, 0), 4, 'Stock should now be 4 (7 - 2 - 1)'); console.log('✓ All order shipping ledger tests passed successfully!'); } finally { diff --git a/src/store/shipment/create.js b/src/store/shipment/create.js index 7ba60fb..e3931eb 100644 --- a/src/store/shipment/create.js +++ b/src/store/shipment/create.js @@ -289,6 +289,10 @@ export default function create(provider) { ensureTax, }); + // 8b. Create the shipping (carrier) atomically with the shipment. Must run before the + // summary state ('delivered'/'returned') is written, as that would block shipping creation. + await createShipping(googleAuthId, { ...input.shipping, manifestId }, innerTx); + // 9. Synchronize order summary row and shipment summary row await syncOrderSummary(runner, { companyId, manifestId: orderId }); const validState = input.state && ['new', 'delivered', 'returned'].includes(String(input.state).trim().toLowerCase()) @@ -303,9 +307,6 @@ export default function create(provider) { state: validState, }); - // 10. Create the shipping (carrier) atomically with the shipment - await createShipping(googleAuthId, { ...input.shipping, manifestId }, innerTx); - return loadShipment(runner, q, companyId, manifestId); };