medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
26.07k stars 2.62k forks source link

[Bug]: Duplicate Customer Records Affecting Order Visibility for Registered Users #9999

Open matiasstanghelini opened 2 weeks ago

matiasstanghelini commented 2 weeks ago

Package.json file

{
  "name": "medusa-starter-default",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "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"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "latest",
    "@medusajs/cli": "latest",
    "@medusajs/framework": "latest",
    "@medusajs/medusa": "^2.0.1",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "pg": "^8.13.0"
  },
  "devDependencies": {
    "@medusajs/test-utils": "latest",
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v20.17.0

Database and its version

PostgresSQL 16

Operating system name and version

Windows 10

Browser name

Chrome

What happended?

We identified a bug in Medusa.js v2.0 where creating an account with an email previously used for a guest order leads to duplicate customer records in the customer table. This causes issues with order visibility, as users cannot see their past guest orders after registering.

Comment When a user places an order as a guest using a specific email address (e.g., user@example.com) and later registers with the same email, Medusa creates two separate customer records:

This results in two distinct accounts with the same email. Orders made as a guest are linked to the guest account, making them inaccessible when the user logs in with their newly registered account. This fragmented data leads to a poor user experience, as customers expect to view all their orders upon logging in.

image

Expected behavior

When a user registers using an email associated with a previous guest order, Medusa should:

Actual behavior

Medusa creates two separate entries in the customer table:

Link to reproduction repo

-

sergiochamba commented 2 weeks ago

I'm encountering an issue during the checkout process where the customer_id can’t be associated with the cart. In @medusajs/types, the HttpTypes.StoreUpdateCart interface doesn’t have a customer_id field. As a result, when the customer completes the order, it isn’t associated with their customer_id—instead, a guest customer is created, as if the user wasn’t logged in.

I've been troubleshooting this, initially thinking it was an issue with my storefront code, but it now seems that the problem might lie with the Medusa HttpTypes.StoreUpdateCart interface.

sdegetaus commented 1 week ago

I am also having this issue. Since I am very new to this project, I also thought my code was at fault, but this is also faulty in the starter theme from Medusa.

dhuvikamboj commented 1 week ago

I am having the same issue , for me when even if i am logged in, it will create a new guest user and link the new order with that and i am not able to list those order on order page

olivermrbl commented 1 week ago

There are two issues being described here:

Regarding the former, you can read about the behavior we will introduce here.