Skip to content

[REFACTOR] Duplicate coupon logic implemented independently in app.js and js/coupon-validator.jsΒ #3365

Description

@munazzaghazali7-hash

🧩 Current Code Situation

The coupon system (CARA20: 20% off, WELCOME10: 10% off) is implemented twice, independently, in two separate files:

  • app.js β€” used on cart.html, bound to #apply-coupon-btn / #coupon-code
  • js/coupon-validator.js β€” used on checkout.html, bound to #applyCouponBtn / #couponCodeInput

Both hardcode the same coupon codes and discount percentages separately. There is no shared source of truth. If a coupon is added, removed, or its discount percentage changes, both files must be updated in sync manually β€” miss one and cart.html/checkout.html will silently disagree on what a coupon is worth.


🧠 Reason for Refactor

Extract the coupon codes/percentages into a single shared file, js/coupon-config.js, exposing window.CARA_COUPONS. Update both app.js and js/coupon-validator.js to read from this shared object instead of maintaining their own hardcoded copies. Load the new config script before both consumers on cart.html and checkout.html.


🚧 Proposed Refactor Plan

Not a live user-facing bug β€” both implementations currently work correctly on their respective pages. This is a maintainability/consistency risk: future coupon changes require remembering to update two unrelated files.


βœ… Checklist

  • I have identified the part of the code that needs refactoring.
  • I have ensured no functionality will be broken.
  • I have tested similar code paths before.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions