This project implements a RESTful API for managing and applying different types of discount coupons (cart-wise, product-wise, and BxGy) for an e-commerce platform.
- Create, read, update, and delete coupons
- Get applicable coupons for a given cart
- Apply a specific coupon to a cart
-
Cart-wise Coupons:
- Percentage discount on total cart value above a threshold
- Fixed amount discount on total cart value above a threshold
-
Product-wise Coupons:
- Percentage discount on specific products
- Fixed amount discount on specific products
-
BxGy (Buy X Get Y) Coupons:
- Buy a specified number of products from one set and get a specified number of products from another set for free
- Implement repetition limit
-
Expiration dates for coupons
- Stackable coupons: Ability to apply multiple coupons to a single cart
- Tiered discounts: Increasing discount percentages based on cart total
- Time-based coupons: Coupons valid only during specific hours or days
- User-specific coupons: Coupons applicable only to certain user groups or individual users
- Category-wide coupons: Discounts applicable to all products in a specific category
- Minimum purchase requirements for BxGy coupons
- Exclusions: Ability to exclude certain products or categories from coupon applicability
- The current implementation does not handle complex combinations of multiple coupon types
- Performance may degrade with a large number of coupons or cart items
- The BxGy implementation is simplified and may not cover all edge cases
- No real-time validation of product availability for BxGy coupons
- All prices and discounts are in the same currency
- Product IDs are unique across the system
- The cart structure remains consistent across all API calls
- The database is always available and responsive
- Coupon codes are not implemented; coupons are identified by their database ID
- Clone the repository
- Install dependencies:
npm install - Set up MongoDB and update the connection string in
.env - Run the server:
npm start - Run tests:
npm test
- POST /api/v1/coupons/add-new: Create a new coupon
- GET /api/v1/coupons/all: Retrieve all coupons
- GET /api/v1/coupons/get/{id}: Retrieve a specific coupon by its ID
- PUT /api/v1/coupons/update/{id}: Update a specific coupon by its ID
- DELETE /api/v1/coupons/delete/{id}: Delete a specific coupon by its ID
- POST /api/v1/coupons/applicable-coupons: Fetch all applicable coupons for a given cart
- POST /api/v1/coupons/apply-coupon/{id}: Apply a specific coupon to the cart
- Implement more complex coupon types and combinations
- Add authentication and authorization
- Improve error handling and input validation
- Implement caching for better performance
- Add more comprehensive unit and integration tests
- Implement logging and monitoring
- Create a user interface for coupon management
{
"type": "cart-wise",
"details": {
"threshold": 100,
"discount": 10
},
"expirationDate": "2024-12-31T23:59:59.999Z"
}{
"_id": "60f1a7b9e6e8f32b4c9b1234",
"type": "cart-wise",
"details": {
"threshold": 100,
"discount": 10
},
"expirationDate": "2024-12-31T23:59:59.999Z",
"createdAt": "2023-07-16T12:00:00.000Z",
"updatedAt": "2023-07-16T12:00:00.000Z"
}[
{
"_id": "60f1a7b9e6e8f32b4c9b1234",
"type": "cart-wise",
"details": {
"threshold": 100,
"discount": 10
},
"expirationDate": "2024-12-31T23:59:59.999Z",
"createdAt": "2023-07-16T12:00:00.000Z",
"updatedAt": "2023-07-16T12:00:00.000Z"
},
{
"_id": "60f1a7b9e6e8f32b4c9b5678",
"type": "product-wise",
"details": {
"product_id": "prod123",
"discount": 20
},
"expirationDate": "2024-06-30T23:59:59.999Z",
"createdAt": "2023-07-16T12:30:00.000Z",
"updatedAt": "2023-07-16T12:30:00.000Z"
}
]{
"_id": "60f1a7b9e6e8f32b4c9b1234",
"type": "cart-wise",
"details": {
"threshold": 100,
"discount": 10
},
"expirationDate": "2024-12-31T23:59:59.999Z",
"createdAt": "2023-07-16T12:00:00.000Z",
"updatedAt": "2023-07-16T12:00:00.000Z"
}{
"details": {
"threshold": 150,
"discount": 15
},
"expirationDate": "2025-06-30T23:59:59.999Z"
}{
"_id": "60f1a7b9e6e8f32b4c9b1234",
"type": "cart-wise",
"details": {
"threshold": 150,
"discount": 15
},
"expirationDate": "2025-06-30T23:59:59.999Z",
"createdAt": "2023-07-16T12:00:00.000Z",
"updatedAt": "2023-07-16T13:00:00.000Z"
}{
"message": "Coupon deleted successfully"
}{
"cart": {
"items": [
{ "product_id": "prod123", "quantity": 2, "price": 50 },
{ "product_id": "prod456", "quantity": 1, "price": 75 }
]
}
}{
"applicable_coupons": [
{
"coupon_id": "60f1a7b9e6e8f32b4c9b1234",
"type": "cart-wise",
"discount": 17.5
},
{
"coupon_id": "60f1a7b9e6e8f32b4c9b5678",
"type": "product-wise",
"discount": 20
}
]
}{
"cart": {
"items": [
{ "product_id": "prod123", "quantity": 2, "price": 50 },
{ "product_id": "prod456", "quantity": 1, "price": 75 }
]
}
}{
"updated_cart": {
"items": [
{
"product_id": "prod123",
"quantity": 2,
"price": 50,
"total_discount": 20
},
{
"product_id": "prod456",
"quantity": 1,
"price": 75,
"total_discount": 0
}
],
"total_price": 175,
"total_discount": 20,
"final_price": 155
}
}{
"type": "bxgy",
"details": {
"buy_products": [
{ "product_id": "prod123", "quantity": 2 },
{ "product_id": "prod456", "quantity": 1 }
],
"get_products": [{ "product_id": "prod789", "quantity": 1 }],
"repetition_limit": 2
},
"expirationDate": "2024-12-31T23:59:59.999Z"
}{
"_id": "60f1a7b9e6e8f32b4c9b9012",
"type": "bxgy",
"details": {
"buy_products": [
{ "product_id": "prod123", "quantity": 2 },
{ "product_id": "prod456", "quantity": 1 }
],
"get_products": [{ "product_id": "prod789", "quantity": 1 }],
"repetition_limit": 2
},
"expirationDate": "2024-12-31T23:59:59.999Z",
"createdAt": "2023-07-16T14:00:00.000Z",
"updatedAt": "2023-07-16T14:00:00.000Z"
}{
"cart": {
"items": [
{ "product_id": "prod123", "quantity": 4, "price": 50 },
{ "product_id": "prod456", "quantity": 2, "price": 75 },
{ "product_id": "prod789", "quantity": 3, "price": 25 }
]
}
}{
"updated_cart": {
"items": [
{
"product_id": "prod123",
"quantity": 4,
"price": 50,
"total_discount": 0
},
{
"product_id": "prod456",
"quantity": 2,
"price": 75,
"total_discount": 0
},
{
"product_id": "prod789",
"quantity": 3,
"price": 25,
"total_discount": 50
}
],
"total_price": 375,
"total_discount": 50,
"final_price": 325
}
}{
"cart": {
"items": [
{ "product_id": "prod123", "quantity": 2, "price": 50 },
{ "product_id": "prod456", "quantity": 1, "price": 75 }
]
}
}{
"error": "Coupon has expired"
}These test cases cover various scenarios for each API endpoint, including different coupon types and edge cases like expired coupons. They provide a comprehensive set of examples for testing the API's functionality and can serve as a reference for developers integrating with the system.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests.
This project is licensed under the MIT License - see the LICENSE.md file for details.