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

[Bug]: Wrong promotion code is not triggering request error #11177

Closed
ImraKocis opened this issue Jan 27, 2025 · 4 comments
Closed

[Bug]: Wrong promotion code is not triggering request error #11177

ImraKocis opened this issue Jan 27, 2025 · 4 comments

Comments

@ImraKocis
Copy link

Package.json file

Admin:

{
  "name": "name",
  "version": "0.0.1",
  "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",
    "format": "prettier --write ."
  },
  "dependencies": {
    "@medusajs/admin-sdk": "^2.0.7",
    "@medusajs/cli": "^2.0.7",
    "@medusajs/framework": "^2.0.7",
    "@medusajs/medusa": "^2.0.7",
    "@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",
    "envalid": "^8.0.0",
    "pg": "^8.13.1"
  },
  "devDependencies": {
    "@medusajs/test-utils": "latest",
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.37",
    "@types/jest": "^29.5.14",
    "@types/node": "^20.17.9",
    "@types/react": "^18.3.12",
    "@types/react-dom": "^18.3.1",
    "jest": "^29.7.0",
    "prettier": "4.0.0-alpha.10",
    "prop-types": "^15.8.1",
    "react": "^18.3.1",
    "react-dom": "^18.3.1",
    "ts-node": "^10.9.2",
    "typescript": "^5.7.2",
    "vite": "^5.4.11"
  },
  "engines": {
    "node": ">=20"
  }
}


Storefront:

{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "format": "prettier --write ."
  },
  "dependencies": {
    "@medusajs/js-sdk": "^2.2.0",
    "@medusajs/ui": "^4.0.3",
    "i18next": "^24.2.1",
    "i18next-resources-to-backend": "^1.2.1",
    "lodash": "^4.17.21",
    "lucide-react": "^0.472.0",
    "next": "^15.1.3",
    "next-i18n-router": "^5.5.1",
    "react": "^19.0.0",
    "react-dom": "^19.0.0",
    "react-i18next": "^15.4.0"
  },
  "devDependencies": {
    "@medusajs/types": "^2.2.0",
    "@types/lodash": "^4.14.195",
    "@types/node": "22.10.5",
    "@types/react": "19.0.3",
    "autoprefixer": "^10.4.20",
    "postcss": "^8.4.49",
    "prettier": "^3.4.2",
    "tailwindcss": "^3.4.17",
    "typescript": "5.7.2"
  }
}

Node.js version

20.9.0

Database and its version

PostgresSQL 16

Operating system name and version

Pop!_OS 22.04 LTS

Browser name

No response

What happended?

I'm working on a eCom project and I came along on issue with promo codes. Issue is that I can not get an "error" on entering wrong promo code, instead every response is 200 from the admin part.

With medusa SDK on storefront, application is behaving like:

  • Code that exists is entered -> response 200. Promotions array in cart, and contains a promotion object with data. Promotion is applied on total cart amount. It works how it suppose to.
  • Code that doesn't exists is entered -> response 200. Promotions array is emptied (if other promotion was present at the time of the request).

With standard HTTP request using postman:

  • Code that exists is entered -> same like SDK, all good.
  • Code that doesn't exists is entered -> response 200. Promotions array is untouched (if other promotion was present at the time of request, old promotions will stay active).

Expected behavior

On entering non-existing promotion code API response should be 404.

Actual behavior

With medusa SDK on storefront, application is behaving like:

  • Code that exists is entered -> response 200. Promotions array in cart, and contains a promotion object with data. Promotion is applied on total cart amount. It works how it suppose to.
  • Code that doesn't exists is entered -> response 200. Promotions array is emptied (if other promotion was present at the time of the request).

With standard HTTP request using postman:

  • Code that exists is entered -> same like SDK, all good.
  • Code that doesn't exists is entered -> response 200. Promotions array is untouched (if other promotion was present at the time of request, old promotions will stay active).

Link to reproduction repo

repo is private

@olivermrbl
Copy link
Contributor

Can you add details about the payload of the two requests? That would be helpful.

In any case, though, we don't throw a 404 on non-existing promotion codes. We just apply it if it exists, and otherwise, leave the cart in its current state.

Happy to help with the unexpected behavior of the SDK vs. Postman, but I'll be closing this issue as the 200 code is expected.

@ImraKocis
Copy link
Author

Ahh okay, thank you for your replay. So we should handle wrong promotion codes manually on frontend, or there is some other way of doing it?

With sdk:

export async function applyPromotions(codes: string[]) {
  const cartId = await getCartId();

  if (!cartId) {
    throw new Error("No existing cart found");
  }

  return sdk.store.cart
    .update(cartId, { promo_codes: codes }, {})
    .then(() => {
      revalidateTag("cart");
    })
    .catch(medusaError);
}

With HTTP request - postman:

Image

Copy link
Contributor

The difference in behavior comes from the fact that in the Next.js starter we append the new code to the already applied codes when sending the request.

@ImraKocis
Copy link
Author

Our company policy is that we will support only one promotion code, I have used your starter Next.js code in some cases but also i had to rework some minor parts.

Also one more question, on your store example, you have this wrong promotion code logic working. I was just confused because this is working on example but not on starter, also there are some other parts that are not working on starter code but works on your live example, like search. It would be nice if your example page code could be public, I know I would love it, I'm sure other devs would too :)

Anyhow thank you once again on info about promotion codes.

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

No branches or pull requests

2 participants