Skip to content

[Bug]: revertLastVersion does not clean up OrderLineItemAdjustment, OrderShippingMethodAdjustment, and tax line records during workflow rollback #14384

@fellos-nl

Description

@fellos-nl

Package.json file

{
  "name": "@acme/store",
  "version": "0.0.1",
  "description": "Store.",
  "author": "Rosie",
  "license": "MIT",
  "keywords": [ ],
  "scripts": {
    "build": "medusa build",
    "lint": "biome lint .",
    "lint:fix": "biome lint . --write",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "database:pull": "bun x drizzle-kit pull",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "typecheck": "tsc --noEmit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "2.12.3",
    "@medusajs/admin-shared": "2.12.3",
    "@medusajs/cli": "2.12.3",
    "@medusajs/core-flows": "2.12.3",
    "@medusajs/dashboard": "2.12.3",
    "@medusajs/draft-order": "2.12.3",
    "@medusajs/framework": "2.12.3",
    "@medusajs/icons": "2.12.3",
    "@medusajs/js-sdk": "2.12.3",
    "@medusajs/medusa": "2.12.3",
    "@medusajs/payment-stripe": "2.12.3",
    "@medusajs/types": "2.12.3",
    "@medusajs/ui": "^4.0.23",
    "@supabase/supabase-js": "^2.57.4",
    "ajv": "^8.17.1",
    "ajv-draft-04": "^1.0.0",
    "date-fns": "^4.1.0",
    "drizzle-orm": "^0.44.7",
    "jsonwebtoken": "^9.0.2",
    "lodash": "^4.17.21",
    "pg": "^8.16.3",
    "react": "^18.3.1",
    "react-dom": "^18.3.1",
    "stripe": "^15.5.0",
    "zod": "^3.22.4"
  },
  "devDependencies": {
    "@medusajs/test-utils": "2.12.3",
    "@swc/core": "1.10.18",
    "@swc/jest": "^0.2.37",
    "@types/jest": "^29.5.14",
    "@types/jsonwebtoken": "^9.0.10",
    "@types/node": "^24.9.2",
    "@types/pg": "^8.15.6",
    "@types/react": "^18.3.1",
    "@types/react-dom": "^18.3.1",
    "dotenv": "^17.2.3",
    "drizzle-kit": "^0.31.7",
    "jest": "^29.7.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.9.3"
  },
  "peerDependencies": {
    "@tanstack/react-query": "5.64.2"
  },
  "engines": {
    "node": ">=20"
  },
  "resolutions": {
    "react": "^18.3.1",
    "react-dom": "^18.3.1",
    "@types/react": "^18.3.1",
    "@types/react-dom": "^18.3.1",
    "rollup": "4.44.2"
  }
}

Node.js version

v24.11.1

Database and its version

Postgres 16

Operating system name and version

N/A

Browser name

N/A

What happended?

The OrderModuleService.revertLastVersion() method is used as a compensation function in workflow rollbacks (e.g., registerOrderFulfillmentStep). When a workflow fails and rolls back, this method is called to revert the order to its previous version.

However, the implementation does not (soft-)delete several related entities that may have been created during the order change:

Entities that ARE reverted:

  • OrderChange
  • OrderChangeAction
  • OrderSummary
  • OrderItem (versioned relation)
  • OrderShipping (versioned relation)
  • OrderCreditLine
  • Return

Entities that are NOT reverted:

  • OrderLineItemAdjustment
  • OrderShippingMethodAdjustment
  • OrderLineItemTaxLine
  • OrderShippingMethodTaxLine

This is particularly problematic because applyOrderChanges_ in the same service explicitly creates line item adjustments via lineItemAdjustmentsToCreate when confirming order changes, but these are never cleaned up during rollback.

Reference: packages/modules/order/src/services/order-module-service.ts - revertLastChange_ method

Expected behavior

When revertLastVersion() is called during workflow compensation, all records created as part of that order version should be (soft-)deleted, including:

  • OrderLineItemAdjustment records
  • OrderShippingMethodAdjustment records
  • OrderLineItemTaxLine records
  • OrderShippingMethodTaxLine records

This ensures complete data consistency when rolling back order changes.

Actual behavior

Adjustment and tax line records remain in the database after revertLastVersion() is called. These orphaned records:

  1. Are associated with a version that has been reverted
  2. Become incorrectly associated with new order changes: when a new order change is created after reversion, the version number is incremented from the reverted version (e.g., version 1 → 2), reusing the same version number as the reverted change. The orphaned adjustments and tax lines now appear to belong to the new order change, corrupting the new version's data with records from a completely different operation.

Link to reproduction repo

N/A

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions