- Clone the repo
- Run
npm install
- Create .env file, take a look at the
env.example
for the content - Run
npm run prepare
and thennpm run build
- Create a new database, you can name it whatever you want
- Modify the database information on
drizzle.config.ts
// drizzle.config.ts
export default {
// change code below to match your database config
dbCredentials: {
host: "localhost",
user: "root",
password: "root",
database: "ecom-api",
port: 3306,
},
} satisfies Config;
if you change the database config, you also need to change the database confing on ./src/db/migrate.ts
and ./src/config/db/ts
- Run the migration or generate a new migration by running
npm run db:generate // to generate a new migration
npm run db:migration // to migrate
- Run
npm run dev
to start the development server
- To seed user into database run
npx tsx ./src/seed/auth.seed.ts
- To seed products into database run
npx tsx ./src/seed/products.seed.ts
- To seed category into database run
npx tsx ./src/seed/categories.seed.ts
All routes below are protected, expect on login, register and GET /products (/products/:id also), you need to have bearer token in the authorization header. You can use either the access token, or refresh token that you will get, after register or login.
Endpoint: POST /api/auth/register
This endpoint allows users to register with the system by providing their email, password, and role.
email
(string, required): The email address of the user.password
(string, required, min 6): The password for the user's account.role
("regular_user" | "seller", required): The role of the user.
{
"email": "[email protected]",
"password": "password123",
"role": "seller"
}
{
"user": {
"id": "string"
"email": "string",
"role": "regular_user" | "seller",
"role_level": 1 | 2
},
"access_token": "string",
"refresh_token": "string"
}
Endpoint: POST /api/auth/login
This endpoint allows users to log in to the system using their email and password.
email
(string, required): The email address of the user.password
(string, required): The password for the user's account.
{
"email": "[email protected]",
"password": "password123"
}
{
"user": {
"id": "string"
"email": "string",
"role": "regular_user" | "seller",
"role_level": 1 | 2
},
"access_token": "string",
"refresh_token": "string"
}
Endpoint: GET /api/auth/current-user
This endpoint allows users to retrieve information about the currently logged-in user.
Bearer Token: Users must include a valid JWT token in the Authorization header.
{
"user": {
"id": "string"
"email": "string",
"role": "regular_user" | "seller",
"role_level": 1 | 2
},
"access_token": "string",
"refresh_token": "string"
}
Here's the documentation template for the additional endpoints:
Endpoint: GET /products
This endpoint retrieves a list of products. Users can include optional query parameters to filter the products.
page
(integer): Specifies the page number for pagination.q
(string): Searches for products containing the specified keyword.category
(string): Filters products by category.description
(string): Searches for products with descriptions containing the specified keyword.
GET /products?page=1&q=towel&category=home&description=hand%20towel
{
data: [
{
"id": 1,
"name": "Hand Towel",
"category": "Home",
"quantity": 10,
"price": 9.99,
"description": "Soft and absorbent hand towel for your home."
},
{
"id": 2,
"name": "Bath Towel",
"category": "Home",
"quantity": 5,
"price": 14.99,
"description": "Luxurious bath towel for your bathroom."
}
],
meta: {
total_item: 999,
current_page: 15,
next_page: 16,
total_page: 250
}
}
Endpoint: POST /products
This endpoint allows users to add a new product to the system.
name
(string, required): The name of the product.category_id
(integer, required): The ID of the category to which the product belongs.quantity
(integer, required): The quantity of the product available.price
(number, required): The price of the product.description
(string, required): A description of the product.
{
"name": "Hand Towel",
"category_id": 1,
"quantity": 10,
"price": 999,
"description": "Soft and absorbent hand towel for your home."
}
{
"message": "Product added successfully"
}
Endpoint: GET /products/:id
This endpoint retrieves information about a specific product identified by its ID.
GET /products/01HPY54P1DDNFQ7B08143XD7BM
{
id: string;
name: string;
created_at: Date;
updated_at: Date;
description: string;
price: number;
quantity: number;
category_id: number | null;
seller_id: string | null;
}
Endpoint: DELETE /products/:id
This endpoint sets the quantity of the specified product to 0, effectively marking it as unavailable.
DELETE /products/01HPY54P1DDNFQ7B08143XD7BM
{
"message": "Product quantity set to 0"
}
Endpoint: PUT /products/:id
This endpoint allows users to update information about a specific product identified by its ID. All fields are optional.
name
(string): The updated name of the product.category_id
(integer): The updated ID of the category to which the product belongs.quantity
(integer): The updated quantity of the product available.price
(float): The updated price of the product.description
(string): The updated description of the product.
{
"name": "New Hand Towel Name",
"price": 12.99
}
{
"message": "Product updated successfully"
}
Endpoint: GET /cart
This endpoint retrieves the contents of the user's shopping cart based on their JWT token.
Bearer Token: Users must include a valid JWT token in the Authorization header.
{
data: [
{
"product_id": 1,
"name": "Hand Towel",
"quantity": 2,
"price": 9.99
},
{
"product_id": 2,
"name": "Bath Towel",
"quantity": 1,
"price": 14.99
}
],
total_price: string,
total_quantity: number,
message: string
}
Endpoint: POST /cart
This endpoint allows users to add a product to their shopping cart.
product_id
(string, required): The ID of the product to add to the cart.quantity
(integer, required): The quantity of the product to add to the cart.
{
"product_id": "01HPY3MYYB9SD28GXMZWJD0A78",
"quantity": 2
}
{
"message": "New product added to cart" | "Product quantity updated"
}
Endpoint: PATCH /cart/:id
This endpoint allows users to update the quantity of a product in their shopping cart.
quantity
(integer, required): The updated quantity of the product in the cart.
{
"quantity": 3
}
{
"message": "Product quantity updated"
}
Endpoint: DELETE /cart/:id
This endpoint allows users to remove a product from their shopping cart.
DELETE /cart/01HPY3MYYB9SD28GXMZWJD0A78 -> product_id
{
"message": "Product in cart deleted successfully"
}
Here's the documentation for the new endpoints:
Endpoint: GET /orders
This endpoint retrieves all orders placed by the authenticated user, based on their JWT token.
Bearer Token: Users must include a valid JWT token in the Authorization header.
[
{
"order_id": 1,
"cart_id": 456,
"is_paid": "false",
"total_price": 39.97,
"quantity": 22
"date_ordered": "2024-02-18T12:30:45Z"
},
{
"order_id": 2,
"cart_id": 789,
"is_paid": "true",
"total_price": 24.98,
"quantity": 1
"date_ordered": "2024-02-17T10:15:20Z"
}
]
Endpoint: GET /orders/:id
This endpoint retrieves details of a specific order placed by the authenticated user, based on the order ID.
Bearer Token: Users must include a valid JWT token in the Authorization header.
GET /orders/1
{
"order_id": 1,
"user_id": 123,
"cart_id": 456,
"is_paid": "true",
"total_price": 39.97,
}
Endpoint: POST /orders
This endpoint allows users to create an order by specifying the cart ID.
cart_id
(string, required): The ID of the cart to create an order from.
{
"cart_id": "01HPY3MYYB9SD28GXMZWJD0A78"
}
{
"message": "Order placed successfully"
}
Here's the documentation for the new endpoint:
Endpoint: POST /pay
This endpoint allows users to make a payment by providing invoice information and card details.
invoice
(string, required): The invoice information for the payment.card_info
(object, required): An object containing the card details.cardNumber
(string, required): The credit/debit card number.cvv
(number, required): The card verification value (CVV).expiryMonth
(string, required): The expiration month of the card.expiryYear
(string, required): The expiration year of the card.
{
"invoice": "INV/123456",
"card_info": {
"cardNumber": "1234567890123456",
"cvv": 123,
"expiryMonth": "12",
"expiryYear": "2024"
}
}
{
"message": "Payment successful",
"data": {
"transcationId": "2166699451095975",
"amount": 100,
"date": "2024-02-07T09:12:11.268Z"
}
}