batchOfferInventoryItemsWorkflow when an existing offer needs different stock behind it, for example to turn it into a kit or to swap a component. The workflow changes the offer ↔ inventory_item links and their required_quantity in one run.
Before you begin
You need these records in place:- An offer: the
offer_idwhose stock you change - Existing inventory items: the workflow links items that already exist and doesn’t create new ones
Change the links
1
Add a route that runs the batch
Pass the offer and the three optional lists:
create, update, and delete.src/api/admin/custom/offers/[id]/inventory/route.ts
create links a new item, update changes the required_quantity of a linked item, and delete unlinks an item. The inventory item itself stays in place after an unlink.2
Read the result
The workflow returns what changed:
created: the link definitions it addedupdated: the link definitions it rewrotedeleted: the inventory item ids it unlinked
offer.updated with { id } for the offer.Rules the batch enforces
The workflow checks the whole payload before it writes anything. Any broken rule fails the run:- Unknown offer:
Offer with id: … was not found - Unknown item in
create: the inventory item must exist - Item not linked, in
delete: fails with not found instead of a silent no-op - Same id twice: an id can appear once per list and in only one of the three lists
required_quantity defaults to 1 in create and is required in update.
The workflow doesn’t check that an item in
create is already linked or that an item in update is linked, and it doesn’t check which store owns an item. Read the offer’s inventory_item_link and the item’s store first when your input comes from a seller.Run code after the batch
The workflow exposes anofferInventoryItemsBatched hook. It receives the offer_id, the result, and any additional_data you passed.
src/workflows/hooks/offer-inventory-batched.ts
validate hook runs first with the raw input, so you can reject a batch with your own rules before any link changes.