This is where we implement the backend Flask API logic for Stock-Smart.
This repository contains the backend code for the Stock-Smart system, built using Flask.
Ensure you have the following installed:
- Python 3.x
- Virtual Environment (venv)
- pip (Python package manager)
git clone https://github.com/StockSart/stock-smart-backend.git
cd stock-smart-backendpython -m venv venv
source venv/bin/activate # On Windows use: venv\Scripts\activatepip install -r requirements.txt(Configure necessary environment variables as needed.)
flask runThe API will be available at: http://127.0.0.1:5000/
Endpoint: POST /auth/signup
Request Body:
{
"name": "John Doe",
"email": "john.doe@example.com",
"password": "password123",
"phone": "1234567890",
"role": "employee" // Optional, defaults to "employee"
}Response:
{
"message": "User created successfully"
}Endpoint: POST /auth/login
Request Body:
{
"email": "john.doe@example.com",
"password": "password123"
}Response:
{
"access_token": "your_jwt_token",
"role": "employee"
}Endpoint: POST /auth/send-otp
Request Body:
{
"email": "john.doe@example.com"
}Response:
{
"message": "OTP sent successfully"
}Endpoint: POST /auth/verify-otp
Request Body:
{
"email": "john.doe@example.com",
"otp": "123456"
}Response:
{
"message": "Login successful!",
"role": "employee",
"access_token": "your_jwt_token"
}Endpoint: GET /products/
Headers:
Authorization: Bearer your_jwt_tokenResponse:
[
{
"id": "product_id",
"name": "Product Name",
"shop_id": "shop_id",
"price": 100.0,
"quantity": 10,
"threshold": 5,
"description": "Product Description",
"category": "Product Category",
"barcode": "1234567890"
},
...
]Endpoint: GET /products/{product_id}
Headers:
Authorization: Bearer your_jwt_tokenResponse:
{
"id": "product_id",
"name": "Product Name",
"shop_id": "shop_id",
"price": 100.0,
"quantity": 10,
"threshold": 5,
"description": "Product Description",
"category": "Product Category",
"barcode": "1234567890"
}Endpoint: GET /products/barcode/{barcode}
Headers:
Authorization: Bearer your_jwt_tokenResponse:
{
"id": "product_id",
"name": "Product Name",
"shop_id": "shop_id",
"price": 100.0,
"quantity": 10,
"threshold": 5,
"description": "Product Description",
"category": "Product Category",
"barcode": "1234567890"
}Endpoint: POST /products/add
Headers:
Authorization: Bearer your_jwt_tokenRequest Body:
{
"name": "Product Name",
"shop_id": "shop_id",
"price": 100.0,
"quantity": 10,
"threshold": 5,
"description": "Product Description",
"category": "Product Category",
"barcode": "1234567890"
}Response:
{
"message": "Product added successfully",
"product_id": "product_id"
}Endpoint: PUT /products/update/{product_id}
Headers:
Authorization: Bearer your_jwt_tokenRequest Body:
{
"name": "Updated Product Name",
"shop_id": "shop_id",
"price": 120.0,
"quantity": 15,
"threshold": 10,
"description": "Updated Product Description",
"category": "Updated Product Category",
"barcode": "1234567890"
}Response:
{
"message": "Product updated successfully"
}Endpoint: DELETE /products/delete/{product_id}
Headers:
Authorization: Bearer your_jwt_tokenResponse:
{
"message": "Product deleted successfully"
}Endpoint: POST /products/scan-barcode
Headers:
Authorization: Bearer your_jwt_tokenRequest Body:
{
"barcode": "1234567890"
}Response:
{
"product_id": "product_id",
"name": "Product Name",
"shop_id": "shop_id",
"price": 100.0,
"quantity": 10,
"threshold": 5,
"description": "Product Description",
"category": "Product Category",
"barcode": "1234567890"
}All endpoints that require authentication must include the Authorization header with the JWT token received after logging in.
Authorization: Bearer your_jwt_tokenReplace your_jwt_token with the actual JWT token you receive after logging in.
To run the application locally, follow these steps:
- Clone the repository.
- Create a virtual environment and activate it.
- Install the dependencies using
pip install -r requirements.txt. - Create a
.envfile with the necessary environment variables. - Run the application using
flask run.
git clone https://github.com/your-repo/stock-smart-backend.git
cd stock-smart-backend
python -m venv venv
source venv/bin/activate # On Windows, use `venv\Scripts\activate`
pip install -r requirements.txt
cp .env.example .env # Create a .env file and fill in the necessary environment variables
flask runThis will start the application on http://127.0.0.1:5000/.
To deploy the application on Render, follow the Render deployment guide and ensure that the environment variables are set correctly in the Render dashboard.
MONGO_URI="your_mongo_uri"
SECRET_KEY="your_secret_key"
mongodb_database_name="your_database_name"
mongodb_db_pass="your_database_password"
mongodb_db_user="your_database_user"
Replace the placeholder values with your actual MongoDB URI, secret key, and database credentials.
To ensure a smooth workflow for our mobile app project, please follow these conventions when contributing.
-
Never Push Directly to
main(ormaster):
All changes should go through feature branches and pull requests to ensure proper code review and testing. -
Branch Types:
- Feature Branches: For new features.
Example:feature/login-screen - Bugfix Branches: For bug fixes.
Example:bugfix/crash-on-load - Hotfix Branches: For urgent fixes on production code.
Example:hotfix/fix-payment-error - Release Branches (Optional): For preparing a release.
Example:release/v1.2.0
- Feature Branches: For new features.
-
Use Lowercase and Hyphens:
This improves readability and consistency.
Examples:feature/user-authenticationbugfix/navigation-error
-
Keep it Descriptive:
Include a short description of the change so others can easily understand the purpose.
Use a consistent commit message format to maintain a clear project history. We follow the Conventional Commits standard:
-
Format:
type(scope): description -
Types:
feat:— for new featuresfix:— for bug fixesdocs:— for documentation changesstyle:— for formatting changes (no code logic changes)refactor:— for code refactoringtest:— for adding or modifying testschore:— for changes to build process, auxiliary tools, etc.
-
Examples:
feat(auth): add login screen with validationfix(api): resolve crash when fetching user datadocs(readme): update installation instructions
-
Additional Guidelines:
- Keep the subject line concise (preferably under 50 characters).
- Include a longer description in the commit body if needed.
-
Always Open a Pull Request (PR):
- When your feature or fix is ready, open a PR to merge your branch into
main.
- When your feature or fix is ready, open a PR to merge your branch into
-
Review Process:
- At least one other team member should review the PR.
- Ensure tests pass and code style guidelines are met.
- Link related issues in the PR description.
-
Merge Strategy:
- Prefer Squash Merging: Keeps a clean history by consolidating commits.
- Rebase Regularly: Keep your branch up-to-date with the base branch to avoid conflicts.
-
Keep Commits Atomic:
- Each commit should represent a single logical change.
-
Descriptive PR Titles and Descriptions:
- Use clear titles and detailed descriptions to help others understand the purpose of the changes.
-
Document Your Workflow:
- New team members should refer to these guidelines to get up to speed quickly.