medusajs / medusa

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

Tax lines must be joined to calculate taxes #6756

Open alessioacella opened 3 months ago

alessioacella commented 3 months ago

Bug report

The error message indicates a failure due to tax lines needing to be joined to calculate taxes, pointing to a deeper issue within the cart total calculation logic, particularly when handling deleted products with applied discounts.

System information

Medusa version (including plugins): 1.20.3 Node.js version: v18.19.1 Database: postgresql Operating system: ubuntu-server

Steps to reproduce the behavior

  1. Create a cart
  2. Add a product
  3. Apply discount to that product
  4. Delete product from admin
  5. See error from retrieving cart with totals using cartService
api-1          | Error: Tax Lines must be joined to calculate taxes
api-1          |     at NewTotalsService.<anonymous> (/usr/src/app/node_modules/@medusajs/medusa/dist/services/new-totals.js:213:39)
api-1          |     at step (/usr/src/app/node_modules/@medusajs/medusa/dist/services/new-totals.js:59:23)
api-1          |     at Object.next (/usr/src/app/node_modules/@medusajs/medusa/dist/services/new-totals.js:40:53)
api-1          |     at /usr/src/app/node_modules/@medusajs/medusa/dist/services/new-totals.js:34:71
api-1          |     at new Promise (<anonymous>)
api-1          |     at __awaiter (/usr/src/app/node_modules/@medusajs/medusa/dist/services/new-totals.js:30:12)
api-1          |     at NewTotalsService.getLineItemTotals_ (/usr/src/app/node_modules/@medusajs/medusa/dist/services/new-totals.js:184:16)
api-1          |     at NewTotalsService.<anonymous> (/usr/src/app/node_modules/@medusajs/medusa/dist/services/new-totals.js:142:46)
api-1          |     at step (/usr/src/app/node_modules/@medusajs/medusa/dist/services/new-totals.js:59:23)
api-1          |     at Object.next (/usr/src/app/node_modules/@medusajs/medusa/dist/services/new-totals.js:40:53)
api-1          |     at fulfilled (/usr/src/app/node_modules/@medusajs/medusa/dist/services/new-totals.js:31:58)
api-1          |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Expected behavior

Deleted product should be deleted from an uncompleted cart, totals should automatically works in any cases without throwing exceptions.

Screenshots

Screenshot 2024-03-20 alle 12 31 56

Code snippets

// Use cart service to retrieve cart WithTotals
const cart = await cartService.retrieveWithTotals(
    cart_id,
    {
      relations: [
        "region",
        "customer",
        "payment_sessions",
        "items.variant",
        "shipping_address",
      ],
    }
  );
olivermrbl commented 3 months ago

Update: I realise something else might be at play here. I will dig into this one and get back to you.

alessioacella commented 3 months ago

@olivermrbl, thank you for your support. I want to confirm that the issue still occurs even when "items.tax_lines" is added to the relations array.

alessioacella commented 3 months ago

Here are two more Sentry screenshots of the cart error happening in our production environment. Our current front-end workaround, which applies to creating, updating, deleting, and editing cart items following a 500 error, is far from a long-term solution. Additionally, we have implemented a "pre-check" to validate discounts, gift vouchers, price lists, etc. before transactions.

Our environment is particularly challenging due to the high volume of daily product changes, including new additions, drafts, and deletions. This dynamism highlights the need for a backend that can automatically maintain and adjust cart contents to ensure uninterrupted functionality.

I welcome any suggestions or insights on developing a more robust solution. I am ready to help in any way to deal with this problem more effectively. sentry-error-details

sentry-errors
alessioacella commented 3 months ago

@olivermrbl Any news on this?

alessioacella commented 3 months ago

Hi @olivermrbl, following up on this. Any news? If there's anything I can do to help, please let me know.

alessioacella commented 1 week ago

@olivermrbl Did you have any chance to take a look at this?

humbleEwan commented 1 week ago

If I am not mistaken, what you are trying to do is an unexpected behavior. Since the cart itself does not contain any 'product' only an array of 'line_item', what I think is happening is that when you delete the product from the admin the line item does not get deleted, but is still pointing to the product, which at this point does not exist, hence it corrupts anything related.