Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inventory reservation failures during order edit confirmation #11951

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

kushagraguglani
Copy link

Fix Inventory Reservation Failures During Order Edit Confirmation

Problem

The order edit confirmation workflow (confirmOrderEditRequestWorkflow) was experiencing failures when processing inventory reservations. Specifically, the issues occurred in these scenarios:

  1. -When no items required reservation (empty arrays)
  2. -When items had undefined raw_fulfilled_quantity values
  3. -When attempting to process zero or negative reservation quantities
  4. -When trying to access variant properties that might be null

### Changes Implemented

1. Added early validation check

if (!orderItems.items || !orderItems.items.length || !orderPreview.items) {
  return { variants: allVariants, items: allItems }
}

--This early return prevents processing when there are no items to process, avoiding unnecessary operations and potential errors

2. Added null check for fulfilled quantity

const fulfilledQty = ordItem.raw_fulfilled_quantity || "0"

This change handles cases where raw_fulfilled_quantity might be undefined by providing a default value of "0", preventing calculations from resulting in Na

  1. Added positive quantity validation
if (MathBN.gt(reservationQuantity, 0)) {
  allItems.push({
    // item properties
  })
  
  if (ordItem.variant) {
    allVariants.push(ordItem.variant)
  }
}

This condition ensures we only add items with positive reservation quantities to the allItems array, preventing the processing of items that don't need reservation.

4. Added variant null check

if (ordItem.variant) {
  allVariants.push(ordItem.variant)
}

This check verifies that the variant exists before pushing it to the allVariants array, preventing potential null reference errors.

5. Added conditional execution of reserveInventoryStep

if (items && items.length > 0) {
  reserveInventoryStep(formatedInventoryItems)
}

This ensures the reserveInventoryStep is only called when there are actual items to process, preventing attempts to reserve inventory with empty data.

Testing

These changes have been tested with various order scenarios:

  1. Orders with no items requiring reservation
  2. Orders with partially fulfilled items
  3. Orders with undefined inventory values
  4. Orders with mixed inventory statuses

Expected Behavior

With these changes, the workflow should now:

  • Gracefully handle empty data sets without errors
  • Properly calculate reservation quantities with valid defaults
  • Only process items that actually need inventory reservation
  • Skip inventory reservation when there's nothing to reserve

This improves the robustness of the order edit confirmation process and eliminates the inventory reservation failures that were occurring.

Copy link

vercel bot commented Mar 23, 2025

Someone is attempting to deploy a commit to the medusajs Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

changeset-bot bot commented Mar 28, 2025

⚠️ No Changeset found

Latest commit: ddc3723

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant