medusajs / medusa

Building blocks for digital commerce
https://medusajs.com
MIT License
24.31k stars 2.37k forks source link

Error in product variant inventory service : Cannot read properties of null (reading 'id') #6496

Open Stouffi opened 6 months ago

Stouffi commented 6 months ago

Bug report

Describe the bug

When a draft order contains a custom line item (i.e. a line item not related to any variant), this will make the endpoint GET /store/carts/[id-of-the-draft-order-cart] crash if an inventory service is registered in the modules of the project configuration.

System information

Medusa version (including plugins): 1.20.2

const plugins = [
  `medusa-fulfillment-manual`,
  `medusa-payment-manual`,
  {
    resolve: `@medusajs/file-local`,
    options: {
      upload_dir: "uploads",
    },
  },
  {
    resolve: "@medusajs/admin",
    /** @type {import('@medusajs/admin').PluginOptions} */
    options: {
      autoRebuild: true,
      develop: {
        open: process.env.OPEN_BROWSER !== "false",
      },
    },
  },
];

const modules = {
  inventoryService: {
    resolve: "@medusajs/inventory",
  },
  stockLocationService: {
    resolve: "@medusajs/stock-location",
  },
  /*eventBus: {
    resolve: "@medusajs/event-bus-redis",
    options: {
      redisUrl: REDIS_URL
    }
  },
  cacheService: {
    resolve: "@medusajs/cache-redis",
    options: {
      redisUrl: REDIS_URL
    }
  },*/
};

Node.js version: 20.11.1 Database: PostgreSQL 12 Operating system: 5.15.133.1-microsoft-standard-WSL2 Browser (if relevant):

Steps to reproduce the behavior

  1. Setup a fresh medusa backend
  2. Setup multi-warehouse related modules
  3. Start the medusa backend (either with develop or start script)
  4. Create a draft order from the admin dashboard and add at least one custom line item.
  5. In the draft order page use the cart id from the raw data to GET localhost:9000/store/carts/[cart-id]
  6. The api returns
    {
    "code": "unknown_error",
    "type": "unknown_error",
    "message": "An unknown error occurred."
    }

The following error is printed in the backend logs

error:   Cannot read properties of null (reading 'id')
TypeError: Cannot read properties of null (reading 'id')
    at /home/stouffi/projects/stouffi/stouffi-medusa/node_modules/@medusajs/medusa/dist/services/product-variant-inventory.js:880:111
    at Array.map (<anonymous>)
    at ProductVariantInventoryService.<anonymous> (/home/stouffi/projects/stouffi/stouffi-medusa/node_modules/@medusajs/medusa/dist/services/product-variant-inventory.js:880:83)
    at step (/home/stouffi/projects/stouffi/stouffi-medusa/node_modules/@medusajs/medusa/dist/services/product-variant-inventory.js:59:23)
    at Object.next (/home/stouffi/projects/stouffi/stouffi-medusa/node_modules/@medusajs/medusa/dist/services/product-variant-inventory.js:40:53)
    at /home/stouffi/projects/stouffi/stouffi-medusa/node_modules/@medusajs/medusa/dist/services/product-variant-inventory.js:34:71
    at new Promise (<anonymous>)
    at __awaiter (/home/stouffi/projects/stouffi/stouffi-medusa/node_modules/@medusajs/medusa/dist/services/product-variant-inventory.js:30:12)
    at ProductVariantInventoryService.setVariantAvailability (/home/stouffi/projects/stouffi/stouffi-medusa/node_modules/@medusajs/medusa/dist/services/product-variant-inventory.js:872:16)
    at /home/stouffi/projects/stouffi/stouffi-medusa/node_modules/@medusajs/medusa/dist/api/routes/store/carts/get-cart.js:193:69

Expected behavior

No errors and the cart data being returned by the api.

Additional context

This code from https://github.com/medusajs/medusa/blob/develop/packages/medusa/src/services/product-variant-inventory.ts#L832 is causing the bug.

const { variantInventoryMap, inventoryLocationMap } =
      await this.getAvailabilityContext(
        variants.map((v) => v.id), // v is nullable hence the error.
        salesChannelId,
        availabilityContext
      )
olivermrbl commented 6 months ago

Thanks for highlighting this. Would you be up for sending a PR with a fix?

Stouffi commented 6 months ago

I've tried to dig deeper and find out that a few other parts of the code may omit to check for null variant. I've made some changes on a fork based on a change in the type of LineItem Entity to add | null for variant relation as it was already the case for variant_id. This single change helped me to spot every piece of code affected by unchecked nulls. I'll submit a PR soon today or tomorrow. I added comments where I'm not sure about the expected behavior handling custom line items so that we can discuss it in reviews