- Live Server(Render.com):-https://metalistic.onrender.com
- Live Demo(Google Drive):-https://drive.google.com/file/d/174WFRKsPna3hxuUSlVk8Ak-MJee1mFYg/view?usp=drive_link
-
Install Dependencies: Run
npm install
to install dependencies. -
Environment Variables: Create a
.env
file in the project root and add:PORT=8000
&MONGO=Your_Mongo_URI
-
Start the Server: Run
npm start
to start the server. -
Accessing the Server: Access the server at
http://localhost:8000
. -
Stopping the Server: Stop the server with
Ctrl + C
. -
Using Environment Variables: Access environment variables like
process.env.PORT
&process.env.MONGO
in your code. -
Troubleshooting: Check terminal for errors and ensure environment variables are correctly defined.
- Description: Retrieves a paginated list of all users.
- URL:
/api/users
- Method:
GET
- Parameters:
page
(optional): Specifies the page number for pagination. Default is1
.
- Success Response:
- Code:
200 OK
- Content:
[ { "_id": "user_id", "name": "user_name", "parent_id": "parent_user_id", "earning":"earning_of_user" }, { "_id": "user_id", "name": "user_name", "parent_id": "parent_user_id", "earning":"earning_of_user" }, ... ]
- Code:
- Error Response:
- Code:
500 Internal Server Error
- Content:
{ "error": "Internal server error" }
- Code:
- Description: Retrieves a user by their ID.
- URL:
/api/users/:userId
- Method:
GET
- Parameters:
userId
: ID of the user to retrieve.
- Success Response:
- Code:
200 OK
- Content:
{ "_id": "user_id", "name": "user_name", "parent_id": "parent_user_id", "earning":"earning_of_user" }
- Code:
- Error Response:
- Code:
404 Not Found
- Content:
{ "error": "User not found" }
- Code:
500 Internal Server Error
- Content:
{ "error": "Internal server error" }
- Code:
- Description: Adds a new user to the database.
- URL:
/api/users
- Method:
POST
- Data Params:
name
: Name of the user.parent_id
(optional): ID of the parent user.
- Success Response:
- Code:
201 Created
- Content:
{ "_id": "user_id", "name": "user_name", "parent_id": "parent_user_id", "earning":0 }
- Code:
- Error Response:
- Code:
500 Internal Server Error
- Content:
{ "error": "Internal server error" }
- Code:
- Description: Distributes earnings to users based on a predefined percentage structure.
- URL:
/api/distribute/:userId
- Method:
POST
- URL Params:
userId
: ID of the user to distribute earnings.
- Data Params:
toBeDistribute
: Amount of earning to distribute.
- Success Response:
- Code:
200 OK
- Content:
{ "message": "Earning Distributed" }
- Code:
- Error Response:
- Code:
400 Bad Request
- Content:
{ "error": "Distribution amount could not be less than 0 or undefined" }
- Code:
404 Not Found
- Content:
{ "error": "User not found" }
- Code:
500 Internal Server Error
- Content:
{ "error": "Internal server error" }
- Code:
The distributeEarning
function is an asynchronous function that handles the distribution of earnings to users based on a predefined percentage structure. Below is a breakdown of its functionality:
-
Request Parameters:
- The function accepts
req
(request) andres
(response) parameters, typical in Express.js route handlers. - It extracts
userId
from the request parameters andtoBeDistribute
from the request query.
- The function accepts
-
Input Validation:
- It checks if
toBeDistribute
is undefined or less than 0. If so, it returns a 400 Bad Request status with an error message indicating that the distribution amount cannot be less than 0 or undefined.
- It checks if
-
Retrieving User Information:
- It attempts to find the user with the provided
userId
using theUserModel.findById()
method. - If the user is not found, it returns a 404 Not Found status with an error message indicating that the user was not found.
- It attempts to find the user with the provided
-
Distribution Calculation:
- It defines a percentage array
percent
representing the distribution percentages for different levels. - It initializes
level
to 1 andparent
to the current user. - It iterates over the parent hierarchy, updating earnings for each level based on the distribution percentage.
- It calculates the remaining percentage for the user's own earning after distributing to the parent hierarchy.
- It defines a percentage array
-
Updating User Earnings:
- It updates the user's earning with the remaining percentage calculated above.
-
Response:
- If the distribution is successful, it returns a 200 OK status with a success message indicating that the earning has been distributed.
-
Error Handling:
- If an error occurs during the execution, it logs the error to the console and returns a 500 Internal Server Error status with an error message indicating an internal server error.
This function efficiently distributes earnings to users based on a hierarchical structure and predefined percentage distribution. It handles input validation, retrieves user information, performs the distribution calculation, updates user earnings, and provides appropriate responses and error handling.