Welcome to the E-Commerce Backend APIs project! This is a powerful and flexible backend solution built with modern technologies to support e-commerce functionalities.
| Route Type | Route Description |
|---|---|
| ๐ Features | Explore the features of the API. |
| ๐ ๏ธ Technologies Used | Discover the technologies utilized. |
| ๐ฆ Installation | Instructions to set up the project. |
| ๐ API Documentation | Detailed API documentation. |
- RESTful API: Well-structured and intuitive API endpoints designed for seamless integration into various applications. ๐
- Secure User Authentication: Robust JWT-based authentication to ensure user security and session management. ๐
- Payment Integration: Effortless payment processing through Stripe, providing a secure and user-friendly checkout experience. ๐ณ
- Effortless File Uploads: Simplify file uploads with Multer, making it easy to manage and store user-generated content. ๐
- Comprehensive Data Validation: Maintain data integrity and accuracy using Zod validation, ensuring all inputs meet predefined criteria. โ
- Real-time Notifications: Enhance user experience by sending timely password reset OTPs via Nodemailer, ensuring quick account recovery. ๐ฌ
- Performance Optimization: Boost application performance with Redis caching, reducing response times and improving load handling. โก
- Efficient Database Management: Leverage Prisma for smooth and efficient database operations with MongoDB, ensuring data accessibility and reliability. ๐๏ธ
- Node.js: ๐ JavaScript runtime environment for building scalable server-side applications.
- Express: โ๏ธ Minimalist web framework for creating robust APIs with ease.
- TypeScript: ๐ A superset of JavaScript that adds static typing for enhanced development and code quality.
- MongoDB: ๐ NoSQL database for flexible and scalable data storage solutions.
- Redis: โก In-memory data structure store, utilized as a fast caching layer to improve performance.
- Prisma: ๐ ๏ธ Modern ORM that streamlines database interactions and migrations.
- Zod: ๐ TypeScript-first schema declaration and validation library for data integrity.
- JWT: ๐ JSON Web Tokens used for secure and stateless user authentication.
- Stripe: ๐ณ Payment processing service for handling online transactions securely.
- Firebase: โ๏ธ Real-time database and cloud storage solutions for user data management.
- Multer: ๐ Middleware designed for handling file uploads seamlessly.
- Nodemailer: โ๏ธ Module for sending emails easily, enhancing user communication.
To get started with the project, follow these steps:
-
Clone the Repository
git clone https://github.com/moazelgandy2/multi-vendor-ecommerce-api.git cd multi-vendor-ecommerce-api -
Install the Dependencies
npm install OR yarn install
-
Set Up Environment Variables
To configure the application, you will need to set the following environment variables in a
.envfile:Variable Description BASE_URLBase URL of the application PORTPort number to run the server NODE_ENVEnvironment mode (e.g., development, production) DATABASE_URLMongoDB connection string REDIS_URLRedis connection string JWT_SECRETSecret key for JWT signing STRIPE_SECRET_KEYStripe secret key for payment processing STRIPE_WEBHOOK_SECRETStripe webhook secret for event handling EMAILEmail address for sending notifications PASSWORDEmail password for authentication FIREBASE_API_KEYFirebase API key FIREBASE_AUTH_DOMAINFirebase Auth domain FIREBASE_PROJECT_IDFirebase project ID FIREBASE_STORAGE_BUCKETFirebase storage bucket URL FIREBASE_MESSAGING_SENDER_IDFirebase messaging sender ID FIREBASE_APP_IDFirebase app ID FIREBASE_MEASUREMENT_IDFirebase measurement ID for analytics Make sure to replace the placeholder values with your actual configuration before running the application.
-
Run the Application
npm run dev OR yarn dev
The server will start on the specified port, and you can access the API at
http://localhost:PORT.
| Route Type | Route Description |
|---|---|
| ๐ Authentication Routes | View the routes related to user authentication. |
| ๐ Products Routes | Explore the routes for managing products. |
| ๐ท๏ธ Categories Routes | Access routes for category management. |
| โญ Reviews Routes | View routes for managing product reviews. |
| ๐ Wishlist Routes | Access routes for user wishlists. |
| ๐ Cart Routes | Explore routes for managing shopping carts. |
| ๐๏ธ Coupon Routes | View routes for managing coupons. |
| ๐๏ธ Order Routes | Access routes for order management. |
| ๐ณ Checkout Routes | Explore routes for the checkout process. |
- Base route:
/auth
-
Endpoint:
POST /signup -
Description: Registers a new user.
-
Request Body:
{ "username": "string", "email": "string", "phone": "string", "password": "string", "address": [ { "street": "string", "city": "string", "state": "string", "zip": "string", "isDefault": "boolean" } ] } -
Response:
{ "success": true, "message": "You have successfully signed up.", "data": { "token": "<jwt_token>" } } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.409 Conflict: If the email address is already in use.500 Internal Server Error: If an error occurs while creating the user.
-
Endpoint:
POST /signin -
Description: Logs in an existing user.
-
Request Body:
{ "email": "string", "password": "string" } -
Response:
{ "token": "string" } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.401 Unauthorized: If the email or password is incorrect.500 Internal Server Error: If an error occurs while logging in the user.
-
Endpoint:
POST /password/update -
Description: Changes the user's password.
-
Authorization: Requires
authMiddleware -
Request Body:
{ "oldPassword": "string", "newPassword": "string" } -
Response:
{ "message": "string" } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.401 Unauthorized: If the user is not authenticated or the old password is incorrect.500 Internal Server Error: If an error occurs while changing the password.
-
Endpoint:
POST /password/forgot -
Description: Sends a password reset OTP to the user's email.
-
Request Body:
{ "email": "string" } -
Response:
{ "message": "string" } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.404 Not Found: If the email address is not registered.500 Internal Server Error: If an error occurs while sending the OTP.
-
Note: The OTP will be sent to the user's email address for verification.
-
Endpoint:
POST /otp/verify -
Description: Verifies the OTP sent to the user's email.
-
Request Body:
{ "email": "string", "otp": "string" } -
Response:
{ "password": "string" } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.401 Unauthorized: If the OTP is incorrect or expired.500 Internal Server Error: If an error occurs while verifying the OTP.
-
Note: This generates a new temporary password for the user to log in with.
-
Endpoint:
GET /profile -
Description: Retrieves the user's profile information.
-
Authorization: Requires
authMiddleware -
Response:
{ "id": "string", "username": "string", "email": "string", "phone": "string", "address": [ { "street": "string", "city": "string", "state": "string", "zip": "string", "isDefault": "boolean" } ] } -
Errors:
401 Unauthorized: If the user is not authenticated.500 Internal Server Error: If an error occurs while fetching the user's profile.
-
Endpoint:
PUT /profile -
Description: Updates the user's profile information.
-
Authorization: Requires
authMiddleware -
Request Body:
{ "username": "string", "email": "string", "phone": "string" } -
Response:
{ "message": "string" } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.401 Unauthorized: If the user is not authenticated.500 Internal Server Error: If an error occurs while updating the user's profile.
- Base route:
/products
-
Endpoint:
GET / -
Description: Retrieves a paginated list of all products along with their associated categories, reviews, and seller information.
-
Query Parameters:
page(integer, optional): The page number to retrieve (default is 1).limit(integer, optional): The number of products to return per page (default is 10).
-
Response:
{ "products": [ { "id": "string", "name": "string", "description": "string", "price": "number", "quantity": "number", "images": ["string"], "category": { "id": "string", "name": "string" }, "reviews": [ { "id": "string", "rating": "number", "comment": "string", "user": { "id": "string", "username": "string" } } ], "seller": { "id": "string", "username": "string", "email": "string", "phone": "string" } } ], "total": "number", "page": "number", "limit": "number" } -
Note: The products are paginated based on the query parameters provided.
-
Endpoint:
GET /:id -
Description: Retrieves a single product by its ID, including associated category, reviews, and seller information.
-
Path Parameters:
id(string): The ID of the product (must be a valid 24-character string).
-
Response:
{ "status": "success", "message": "Product found", "data": { "id": "string", "name": "string", "description": "string", "price": "number", "category": { "name": "string" }, "reviews": [ { "rating": "number", "comment": "string", "user": { "username": "string", "email": "string" } } ], "seller": { "username": "string", "email": "string", "phone": "string" } } } -
Errors:
400 Bad Request: If the provided product ID is invalid or missing.404 Not Found: If the product with the given ID does not exist.500 Internal Server Error: If an error occurs while fetching the product.
-
Endpoint:
POST / -
Description: Creates a new product. This endpoint requires authentication and is restricted to users with the role of "admin" or "seller".
-
Authorization: Requires
authMiddlewareandallowedRolesmiddleware. -
Request Body:
{ "name": "string", "price": "number", "image": (required file), "categoryId": "string", "description": "string", "stock": "number" } -
Response:
{ "status": "success", "message": "Product created", "data": { "id": "string", "name": "string", "description": "string", "price": "number", "categoryId": "string", "sellerId": "string", "stock": "number", "image": "string" } } -
Errors:
400 Bad Request: If the request body is invalid, the category ID is invalid, or the image file is missing.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user does not have the required role to create a product.404 Not Found: If the specified category does not exist.500 Internal Server Error: If an error occurs while creating the product.
-
Endpoint:
PUT /:id -
Description: Updates the details of an existing product. This endpoint requires authentication and is restricted to users with the role of "admin" or "seller".
-
Authorization: Requires
authMiddlewareandallowedRolesmiddleware. -
URL Parameters:
id(required): The ID of the product to be updated (must be a valid 24-character string).
-
Request Body:
{ "name": "string", "price": "number", "image": (optional file), "categoryId": "string", "description": "string", "stock": "number" } -
Response:
{ "status": "success", "message": "Product updated", "data": { "id": "string", "name": "string", "description": "string", "price": "number", "categoryId": "string", "sellerId": "string", "stock": "number", "image": "string" } } -
Errors:
400 Bad Request: If the request body is invalid or the category ID is invalid.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user does not have the required role to update the product.404 Not Found: If the specified product does not exist.500 Internal Server Error: If an error occurs while updating the product.
-
Endpoint:
DELETE /products/:id -
Description: Deletes an existing product. This endpoint requires authentication and is restricted to users with the role of "admin" or "seller".
-
Authorization: Requires
authMiddlewareandallowedRolesmiddleware. -
URL Parameters:
id(required): The ID of the product to be deleted (must be a valid 24-character string).
-
Response:
{ "status": "success", "message": "Product deleted", "data": { "id": "string", "name": "string", "description": "string", "price": "number", "categoryId": "string", "sellerId": "string", "stock": "number", "image": "string" } } -
Errors:
400 Bad Request: If the product ID is invalid or missing.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user does not have the required role to delete the product.404 Not Found: If the specified product does not exist.500 Internal Server Error: If an error occurs while deleting the product.
- Base route:
/categories
-
Endpoint:
GET / -
Description: Retrieve a paginated list of all categories.
-
Query Parameters:
page(number, optional): The page number for pagination (defaults to 1).limit(number, optional): The number of categories per page (defaults to 5).
-
Response:
{ "status": "success", "message": "Categories retrieved, page: {page} & limit: {limit}", "data": [ { "id": "string", "name": "string", "description": "string" } ] } -
Errors:
500 Internal Server Error: If an error occurs while retrieving the categories.
-
Endpoint:
GET /:id -
Description: Retrieve a specific category along with its associated products.
-
Parameters:
id(string): The ID of the category.
-
Response:
{ "status": "success", "message": "Category with products", "data": { "id": "string", "name": "string", "description": "string", "products": [ { "name": "string", "price": "number", "description": "string", "seller": { "username": "string", "email": "string" }, "reviews": [ { "rating": "number", "comment": "string" } ] } ] } } -
Errors:
400 Bad Request: If the category ID is invalid or missing.404 Not Found: If the specified category does not exist.500 Internal Server Error: If an error occurs while fetching the category.
-
Endpoint:
POST / -
Description: Create a new product category. Only accessible by admin users.
-
Authorization: Requires
authMiddlewareandallowedRoles("admin") -
Request Body:
{ "name": "string" } -
Response:
{ "status": "success", "message": "Category created", "data": { "id": "string", "name": "string" } } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user does not have the required role to create a category.500 Internal Server Error: If an error occurs while creating the category.
-
Endpoint:
PUT /:id -
Description: Update an existing category. Only accessible by admin users.
-
Authorization: Requires
authMiddlewareandallowedRoles("admin") -
Parameters:
id(string): The ID of the category to update.
-
Request Body:
{ "name": "string" } -
Response:
{ "status": "success", "message": "Category updated", "data": { "id": "string", "name": "string" } } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user does not have the required role to update the category.404 Not Found: If the specified category does not exist.500 Internal Server Error: If an error occurs while updating the category.
-
Endpoint:
DELETE /:id -
Description: Delete a category. Only accessible by admin users.
-
Authorization: Requires
authMiddlewareandallowedRoles("admin") -
Parameters:
id(string): The ID of the category to delete.
-
Response:
{ "status": "success", "message": "Category deleted", "data": { "id": "string", "name": "string" } } -
Errors:
400 Bad Request: If the category ID is invalid or missing.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user does not have the required role to delete the category.404 Not Found: If the specified category does not exist.500 Internal Server Error: If an error occurs while deleting the category.
- Base route:
/reviews
-
Endpoint:
GET /reviews/product/:id -
Description: Fetch all reviews for a specific product.
-
Parameters:
id(string): The ID of the product.
-
Authorization: Requires
authMiddleware -
Response:
{ "status": "success", "message": "Reviews fetched successfully", "data": [ { "id": "string", "rating": "number", "comment": "string", "user": { "id": "string", "username": "string", "email": "string" } } ] } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user has already reviewed the product.404 Not Found: If the specified product does not exist.500 Internal Server Error: If an error occurs while adding the review.
-
Endpoint:
POST /product/:id -
Description: Add a new review for a specific product.
-
Parameters:
id(string): The ID of the product.
-
Authorization: Requires
authMiddleware -
Request Body:
{ "rating": "number", "comment": "string" } -
Response:
{ "status": "success", "message": "Review added successfully", "data": { "id": "string", "comment": "string", "rating": "number", "productId": "string", "userId": "string" } } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user has already reviewed the product.404 Not Found: If the specified product does not exist.500 Internal Server Error: If an error occurs while adding the review.
-
Endpoint:
PUT /reviews/:id -
Description: Update an existing review by its ID.
-
Parameters:
id(string): The ID of the review to update.
-
Authorization: Requires
authMiddleware -
Request Body:
{ "rating": "number", "comment": "string" } -
Response:
{ "status": "success", "message": "Review updated successfully.", "data": { "id": "string", "comment": "string", "rating": "number", "productId": "string", "userId": "string" } } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user is not the author of the review.404 Not Found: If the specified review does not exist.500 Internal Server Error: If an error occurs while updating the review.
-
Endpoint:
DELETE /reviews/:id -
Description: Delete an existing review by its ID.
-
Parameters:
id(string): The ID of the review to delete.
-
Authorization: Requires
authMiddleware -
Response:
{ "status": "success", "message": "Review deleted successfully.", "data": { "id": "string", "comment": "string", "rating": "number", "productId": "string", "userId": "string" } } -
Errors:
400 Bad Request: If the review ID is invalid or missing.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user is not the author of the review.404 Not Found: If the specified review does not exist.500 Internal Server Error: If an error occurs while deleting the review.
-
Endpoint:
GET /reviews/user/:id -
Description: Fetch all reviews written by a specific user.
-
Parameters:
id(string): The ID of the user whose reviews are to be fetched.
-
Authorization: Requires
authMiddlewareandadminrole. -
Response:
{ "status": "success", "message": "Reviews fetched successfully", "data": [ { "rating": "number", "comment": "string", "product": { "name": "string", "price": "number", "description": "string", "image": "string", "stock": "number", "sold": "number", "category": { "name": "string" } }, "user": { "username": "string", "email": "string", "phone": "string" } } ] } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user is not an admin.404 Not Found: If the specified user does not exist.500 Internal Server Error: If an error occurs while fetching the reviews.
- Base route:
/wishlist
-
Endpoint:
GET /me -
Description: Fetch the wishlist for the authenticated user.
-
Authorization: Requires
authMiddleware -
Response:
{ "status": "success", "message": "User wish list", "data": [ { "product": { "name": "string", "price": "number", "description": "string", "image": "string", "category": { "name": "string" }, "seller": { "username": "string", "email": "string", "phone": "string" } } } ] } -
Errors:
401 Unauthorized: If the user is not authenticated.500 Internal Server Error: If an error occurs while fetching the wishlist.
-
Endpoint:
POST /:id -
Description: Adds a product to the authenticated user's wishlist.
-
Authorization: Requires
authMiddleware -
URL Parameters:
id(string) - The ID of the product to add to the wishlist.
-
Response:
{ "status": "success", "message": "Product added to wish list", "data": [ { "product": { "name": "string", "price": "number", "description": "string", "image": "string", "category": { "name": "string" }, "seller": { "username": "string", "email": "string", "phone": "string" } } } ] } -
Errors:
400 Bad Request: If the product ID is invalid or missing.401 Unauthorized: If the user is not authenticated.500 Internal Server Error: If an error occurs while adding the product to the wishlist.
-
Endpoint:
DELETE /:id -
Description: Removes a product from the authenticated user's wishlist.
-
Authorization: Requires
authMiddleware -
URL Parameters:
id(string) - The ID of the product to remove from the wishlist.
-
Response:
{ "status": "success", "message": "Product removed from wish list", "data": [ { "product": { "name": "string", "price": "number", "description": "string", "image": "string", "category": { "name": "string" }, "seller": { "username": "string", "email": "string", "phone": "string" } } } ] } -
Errors:
400 Bad Request: If the product ID is invalid or missing.401 Unauthorized: If the user is not authenticated.500 Internal Server Error: If an error occurs while removing the product from the wishlist.
-
Endpoint:
DELETE / -
Description: Clears all products from the authenticated user's wishlist.
-
Authorization: Requires
authMiddleware -
Response:
{ "status": "success", "message": "Wish list cleared", "data": { "count": number } } -
Errors:
401 Unauthorized: If the user is not authenticated.500 Internal Server Error: If an error occurs while clearing the wishlist.
- Base route:
/cart
-
Endpoint:
GET /me -
Description: Fetches the authenticated user's cart, including all items in it.
-
Authorization: Requires
authMiddleware -
Response:
{ "status": "success", "message": "Cart fetched successfully", "data": { "id": "string", "userId": "string", "cartItems": [ { "id": "string", "productId": "string", "quantity": number, "product": { "id": "string", "name": "string", "price": number, "description": "string", "image": "string" } } ] } } -
Errors:
401 Unauthorized: If the user is not authenticated.404 Not Found: If no cart is found for the user.500 Internal Server Error: If an error occurs while fetching the cart.
-
Endpoint:
POST /product/:id -
Description: Adds a product to the authenticated user's cart. If the cart does not exist, it creates one. If the product is already in the cart, it returns an error.
-
Authorization: Requires
authMiddleware -
URL Parameters:
id(string): The ID of the product to be added to the cart.
-
Response:
{ "status": "success", "message": "Product added to cart", "data": { "id": "string", "userId": "string", "cartItems": [ { "id": "string", "productId": "string", "quantity": number, "product": { "id": "string", "name": "string", "price": number, "description": "string", "image": "string" } } ] } } -
Errors:
400 Bad Request: If the product ID is invalid or missing.401 Unauthorized: If the user is not authenticated.404 Not FoundIf the specified product does not exist.500 Internal Server Error: If an error occurs while adding the product to the cart.
-
Endpoint:
DELETE /:id -
Description: Deletes a specified cart for the authenticated user. If the user is an admin, they can delete any cart.
-
Authorization: Requires
authMiddleware -
URL Parameters:
id(string): The ID of the cart to be deleted.
-
Response:
{ "status": "success", "message": "Cart cleared successfully", "data": {} } -
Errors:
401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user is not an admin and tries to delete another user's cart.404 Not Found: If the specified cart does not exist.500 Internal Server Error: If an error occurs while deleting the cart.
-
Endpoint:
PUT /product/:id -
Description: Updates the quantity of a specified product in the user's cart.
-
Authorization: Requires
authMiddleware -
URL Parameters:
id(string): The ID of the product to be updated.
-
Request Body:
quantity(number): The new quantity of the product in the cart.
-
Response:
{ "status": "success", "message": "Cart updated successfully", "data": {} } -
Errors:
401 Unauthorized: If the user is not authenticated.404 Not Found: If the specified product does not exist.500 Internal Server Error: If an error occurs while updating the cart.
-
Endpoint:
DELETE /product/:id -
Description: Removes a specified product from the authenticated user's cart. If the cart is empty after removal, the cart is deleted.
-
Authorization: Requires
authMiddleware -
URL Parameters:
id(string): The ID of the product to be removed from the cart.
-
Response:
{ "status": "success", "message": "Product removed from cart successfully", "data": { "id": "string", "userId": "string", "cartItems": [ { "id": "string", "productId": "string", "quantity": number, "product": { "id": "string", "name": "string", "price": number, "description": "string", "image": "string" } } ] } } -
Errors:
401 Unauthorized: If the user is not authenticated.404 Not Found: If the specified product does not exist.500 Internal Server Error: If an error occurs while removing the product from the cart.
- Base route:
/coupon
-
Endpoint:
POST /create -
Description: Creates a new coupon that can be used for discounts. Only admins are authorized to create coupons.
-
Authorization: Requires
authMiddleware -
Request Body:
{ "code": "string", "discount": number, "expiryInDays": number } -
Response:
{ "status": "success", "message": "Coupon created", "data": { "code": "string", "discount": number, "expiryInDays": number } } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user is not an admin.500 Internal Server Error: If an error occurs while creating the coupon.
-
Endpoint:
DELETE /:code -
Description: Deletes a specified coupon from the system. Only admins are authorized to delete coupons.
-
Authorization: Requires
authMiddleware -
URL Parameters:
code(string): The code of the coupon to be deleted.
-
Response:
{ "status": "success", "message": "Coupon deleted", "data": { "code": "string" } } -
Errors:
400 Bad Request: If the coupon code is invalid or missing.401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user is not an admin.500 Internal Server Error: If an error occurs while deleting the coupon.
-
Endpoint:
POST /apply -
Description: Applies a coupon to the authenticated user's cart. If the coupon is valid, it updates the cart total and applies the discount.
-
Authorization: Requires
authMiddleware -
Request Body:
{ "code": "string" } -
Response:
{ "status": "success", "message": "Coupon applied", "data": { "discount": number } } -
Errors:
400 Bad Request: If the request body is missing required fields or has invalid values.401 Unauthorized: If the user is not authenticated.500 Internal Server Error: If an error occurs while applying the coupon.
- Base route:
/order
-
Endpoint:
POST /create -
Description: Creates a new order for the authenticated user based on their current cart.
-
Authorization: Requires
authMiddleware -
Query Parameters:
payment(string): The payment type (either "COD" or "CARD").
-
Response:
{ "status": "success", "message": "Order created", "data": { "order": { "id": "string", "total": number, "status": "PENDING", "paymentType": "COD" | "CARD", "delivery": "PENDING", "userId": "string", "orderItems": [ { "id": "string", "productId": "string", "quantity": number, "createdAt": "string", "updatedAt": "string" } ], "createdAt": "string", "updatedAt": "string" } } } -
Errors:
401 Unauthorized: If the user is not authenticated.404 Not Found: If the user has no carts.500 Internal Server Error: If an error occurs while creating the order.
-
Endpoint:
GET /me -
Description: Retrieves and filters orders for the authenticated user based on specified query parameters.
-
Authorization: Requires
authMiddleware -
Query Parameters:
status(string, optional): Filter orders by status (e.g., "PENDING", "ACCEPTED", "REJECTED", "DELIVERED").delivery(string, optional): Filter orders by delivery status (e.g., "PENDING", "ACCEPTED", "REJECTED", "DELIVERED").paymentType(string, optional): Filter orders by payment type (e.g., "COD", "CARD").
-
Response:
{ "status": "success", "message": "Orders found", "data": { "orders": [ { "id": "string", "total": number, "status": "PENDING" | "ACCEPTED" | "REJECTED" | "DELIVERED", "paymentType": "COD" | "CARD", "delivery": "PENDING" | "ACCEPTED" | "REJECTED" | "DELIVERED", "userId": "string", "orderItems": [ { "id": "string", "productId": "string", "quantity": number, "createdAt": "string", "updatedAt": "string" } ], "createdAt": "string", "updatedAt": "string" } ] } } -
Errors:
401 Unauthorized: If the user is not authenticated.404 Not Found: If no orders are found for the user.500 Internal Server Error: If an error occurs while fetching the orders.
-
Endpoint:
GET /me/cancel/:id -
Description: Cancels a specific order for the authenticated user by its ID.
-
Authorization: Requires
authMiddleware -
Path Parameters:
id(string, required): The unique identifier of the order to be cancelled.
-
Response:
{ "status": "success", "message": "Order cancelled", "data": { "orderData": { "id": "string", "total": number, "status": "PENDING" | "ACCEPTED" | "REJECTED" | "DELIVERED", "paymentType": "COD" | "CARD", "delivery": "PENDING" | "ACCEPTED" | "REJECTED" | "DELIVERED", "userId": "string", "orderItems": [ { "id": "string", "productId": "string", "quantity": number, "createdAt": "string", "updatedAt": "string" } ], "createdAt": "string", "updatedAt": "string" } } } -
Errors:
401 Unauthorized: If the user is not authenticated.404 Not Found: If the specified order does not exist.500 Internal Server Error: If an error occurs while cancelling the order.
-
Endpoint:
DELETE /:id -
Description: Deletes a specific order by its ID. Accessible only to users with the roles of "admin" or "seller."
-
Authorization: Requires
authMiddlewareand must have either the "admin" or "seller" role. -
Path Parameters:
id(string, required): The unique identifier of the order to be deleted.
-
Response:
{ "status": "success", "message": "Order deleted", "data": { "orderData": { "id": "string", "total": number, "status": "PENDING" | "ACCEPTED" | "REJECTED" | "DELIVERED", "paymentType": "COD" | "CARD", "delivery": "PENDING" | "ACCEPTED" | "REJECTED" | "DELIVERED", "userId": "string", "orderItems": [ { "id": "string", "productId": "string", "quantity": number, "createdAt": "string", "updatedAt": "string" } ], "createdAt": "string", "updatedAt": "string" } } } -
Errors:
401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user does not have the required role to delete the order (admin or seller).404 Not Found: If the specified order does not exist.500 Internal Server Error: If an error occurs while deleting the order.
-
Endpoint:
PUT /delivery/:id -
Description: Updates the delivery status of a specific order by its ID. Accessible only to users with the roles of "admin" or "seller."
-
Authorization: Requires
authMiddlewareand must have either the "admin" or "seller" role. -
Path Parameters:
id(string, required): The unique identifier of the order for which the delivery status is being updated.
-
Request Body:
{ "status": "PENDING" | "ACCEPTED" | "REJECTED" | "DELIVERED" } -
Response:
{ "status": "success", "message": "Order updated", "data": { "orderData": { "id": "string", "total": number, "status": "PENDING" | "ACCEPTED" | "REJECTED" | "DELIVERED", "paymentType": "COD" | "CARD", "delivery": "PENDING" | "ACCEPTED" | "REJECTED" | "DELIVERED", "userId": "string", "orderItems": [ { "id": "string", "productId": "string", "quantity": number, "createdAt": "string", "updatedAt": "string" } ], "createdAt": "string", "updatedAt": "string" } } } -
Errors:
401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user does not have the required role to update the order.404 Not Found: If the specified order does not exist.500 Internal Server Error: If an error occurs while updating the order.
- Base route:
/checkout
-
Endpoint:
POST /:id -
Description: Creates a checkout session for the authenticated user's pending order with a payment type of "CARD".
-
Authorization: Requires
authMiddleware -
Path Parameters:
id(string, required): The unique identifier of the order for which the checkout session is created.
-
Response:
{ "success": true, "message": "Checkout created", "data": { "session": { "id": "<stripe_checkout_session_id>", "object": "checkout.session", "amount_subtotal": <amount_subtotal>, "amount_total": <amount_total>, "currency": "<currency>", "billing_address_collection": "required", "cancel_url": "<cancel_url>", "customer_email": "<customer_email>", "expires_at": <expiration_timestamp>, "metadata": { "orderId": "<order_id>" }, "mode": "payment", "payment_status": "<payment_status>", "success_url": "<success_url>", "url": "<checkout_url>" } } } -
Errors:
401 Unauthorized: If the user is not authenticated.404 Not Found: If the specified order does not exist.500 Internal Server Error: If an error occurs while creating the checkout session.
-
Endpoint:
POST (root) /webhook -
Description: Receives and processes Stripe events for successful payments and updates the order status accordingly.
-
Event Types Handled:
-
checkout.session.completed:- Action: Updates the order status to "PAID" in the database.
- Outcome: The order is marked as successfully paid, and the user may receive confirmation and any relevant notifications.
-
payment_intent.payment_failed:- Action: Updates the order status to "FAILED" in the database.
- Outcome: The order is marked as failed, and the user may receive a notification regarding the payment failure.
-
-
Request Headers:
Stripe-Signature(string, required): The signature of the webhook event, used to verify its authenticity.
-
Response:
{ "received": true }
-
Endpoint:
PUT /cash -
Description: Processes payment for an order marked as cash on delivery (COD). This endpoint can only be accessed by users with the roles "admin" or "seller".
-
Authorization: Requires
authMiddlewareand role check withallowedRoles("admin", "seller"). -
Request Parameters:
- Path Parameters:
id(string, required): The unique identifier of the order being paid.
- Path Parameters:
-
Response:
{ "success": true, "message": "Order paid", "data": { "orderData": { "id": "<order_id>", "status": "PAID", "paymentType": "COD" } } } -
Errors:
401 Unauthorized: If the user is not authenticated.403 Forbidden: If the user does not have the required role to update the order.404 Not Found: If the specified order does not exist.500 Internal Server Error: If an error occurs while updating the order.