|
| 1 | +# Backend API Development Details (Day 5 - Day 6) |
| 2 | + |
| 3 | +This document outlines the requirements and specifications for implementing the **Authentication Module** and **Inventory Management APIs** in the backend of the project. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## **Project Context** |
| 8 | +The project aims to create a web application for local shopkeepers to manage their inventory, customers, purchases, sales, and billing efficiently. The backend plays a critical role in securely managing data and providing reliable APIs for the frontend. |
| 9 | + |
| 10 | +This phase focuses on implementing: |
| 11 | +1. **Authentication Module**: Ensures that only authorized users can access the system. |
| 12 | +2. **Inventory Management APIs**: Provides functionality for shopkeepers to manage their inventory. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## **Feature Specifications** |
| 17 | + |
| 18 | +### **1. Authentication Module** |
| 19 | +#### Overview |
| 20 | +The authentication module ensures secure access to the application by implementing: |
| 21 | +- **User Registration**: New users can sign up. |
| 22 | +- **User Login**: Existing users can log in with their credentials. |
| 23 | +- **JWT Authentication**: Protects routes by issuing JSON Web Tokens to authenticated users. |
| 24 | + |
| 25 | +#### Details |
| 26 | +- **Endpoints to Implement**: |
| 27 | + - `POST /auth/register` |
| 28 | + - Accepts user details (e.g., `name`, `email`, `password`). |
| 29 | + - Encrypts the password using `bcrypt`. |
| 30 | + - Saves the user in the database. |
| 31 | + - `POST /auth/login` |
| 32 | + - Verifies user credentials. |
| 33 | + - Issues a JWT token if credentials are valid. |
| 34 | + - **Middleware**: `authMiddleware` |
| 35 | + - Protects API routes by verifying the JWT token. |
| 36 | + |
| 37 | +#### Key Requirements |
| 38 | +- Use `bcrypt` for password hashing. |
| 39 | +- Use `jsonwebtoken` to issue and verify JWT tokens. |
| 40 | +- Include validation for all input fields (e.g., email format, password strength). |
| 41 | +- Store only the hashed password in the database. |
| 42 | +- JWT tokens should have an expiration time (e.g., 1 day). |
| 43 | + |
| 44 | +#### Database Tables |
| 45 | +- **Users Table**: |
| 46 | + - `id`: Primary key. |
| 47 | + - `name`: Full name of the user. |
| 48 | + - `email`: Unique email of the user. |
| 49 | + - `password`: Encrypted password. |
| 50 | + - `created_at`: Timestamp of user registration. |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +### **2. Inventory Management APIs** |
| 55 | +#### Overview |
| 56 | +The inventory management module allows shopkeepers to: |
| 57 | +- Add new products to their inventory. |
| 58 | +- Edit product details. |
| 59 | +- Delete products. |
| 60 | +- Retrieve a list of all products. |
| 61 | + |
| 62 | +#### Details |
| 63 | +- **Endpoints to Implement**: |
| 64 | + - `POST /products` |
| 65 | + - Adds a new product to the inventory. |
| 66 | + - Fields: `name`, `quantity`, `price`, `supplier`, `description` (optional). |
| 67 | + - `GET /products` |
| 68 | + - Retrieves all products in the inventory. |
| 69 | + - `PUT /products/:id` |
| 70 | + - Updates details of an existing product. |
| 71 | + - Fields: `name`, `quantity`, `price`, `supplier`, `description`. |
| 72 | + - `DELETE /products/:id` |
| 73 | + - Deletes a product from the inventory. |
| 74 | + |
| 75 | +#### Key Requirements |
| 76 | +- Use input validation to ensure correct data types and mandatory fields. |
| 77 | +- Handle errors such as invalid product IDs or duplicate product names. |
| 78 | +- Ensure secure operations by protecting endpoints with `authMiddleware`. |
| 79 | + |
| 80 | +#### Database Tables |
| 81 | +- **Products Table**: |
| 82 | + - `id`: Primary key. |
| 83 | + - `name`: Name of the product. |
| 84 | + - `quantity`: Quantity available. |
| 85 | + - `price`: Price per unit. |
| 86 | + - `supplier`: Supplier name or ID. |
| 87 | + - `description`: Optional field for product details. |
| 88 | + - `created_at`: Timestamp of product creation. |
| 89 | + - `updated_at`: Timestamp of the last update. |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## **Development Steps** |
| 94 | + |
| 95 | +### **Step 1: Set Up Middleware and Utilities** |
| 96 | +- Create `authMiddleware` to verify JWT tokens for protected routes. |
| 97 | +- Create utility functions for password hashing (`bcrypt.hash`) and token generation (`jsonwebtoken.sign`). |
| 98 | + |
| 99 | +### **Step 2: Implement Authentication Endpoints** |
| 100 | +- Develop the `POST /auth/register` and `POST /auth/login` endpoints. |
| 101 | +- Test user registration and login flows using `Postman`. |
| 102 | + |
| 103 | +### **Step 3: Implement Inventory APIs** |
| 104 | +- Develop CRUD operations for the `/products` endpoint. |
| 105 | +- Test each endpoint for different scenarios (e.g., successful addition, invalid data). |
| 106 | + |
| 107 | +### **Step 4: Testing and Debugging** |
| 108 | +- Test all API endpoints using `Postman` or `Swagger`. |
| 109 | +- Add unit tests for key functions and integration tests for endpoints. |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +## **Deliverables** |
| 114 | +1. Functional authentication module (register, login, JWT-based protection). |
| 115 | +2. Functional inventory management APIs (add, edit, delete, retrieve products). |
| 116 | +3. API documentation for all implemented endpoints (using Swagger/OpenAPI). |
| 117 | +4. Unit and integration tests for critical features. |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## **Notes** |
| 122 | +- Ensure code readability and maintainability by adhering to best practices. |
| 123 | +- Log errors and critical events for easier debugging. |
| 124 | +- Update the `Product Feature Tracker.md` after completing each feature. |
0 commit comments