This API provides functionality for recalculating the leaderboard ranks based on user activities and retrieving the current leaderboard data.
POST /api/v1/leaderboard/recalculate?filter={filter}- filter (required):
Defines the time period for recalculating ranks. The possible values are:day: Recalculates ranks for today's activities.month: Recalculates ranks for the current month's activities.year: Recalculates ranks for the current year's activities.
POST /api/v1/leaderboard/recalculate?filter=day- Code: 200 OK
- Content:
[
{
"user_id": 1,
"total_points": 40,
"rank": 1,
"User": {
"id": 1,
"full_name": "Santosh Sharma"
}
},
{
"user_id": 2,
"total_points": 20,
"rank": 2,
"User": {
"id": 2,
"full_name": "Hari Yadav"
}
}
]- Code: 400 Bad Request
- Content:
{
"error": "Invalid filter. Must be one of: day, month, year."
}GET /api/v1/leaderboard?search={userId}- search (optional):
A numeric user ID to search for a specific user on the leaderboard.
GET /api/v1/leaderboardGET /api/v1/leaderboard?search=2- Code: 200 OK
- Content:
[
{
"user_id": 1,
"total_points": 100,
"rank": 1,
"User": {
"id": 1,
"full_name": "Santosh Sharma"
}
},
{
"user_id": 2,
"total_points": 80,
"rank": 2,
"User": {
"id": 2,
"full_name": "Hari Yadav"
}
}
]- Code: 200 OK
- Content:
[
{
"user_id": 2,
"total_points": 80,
"rank": 2,
"User": {
"id": 2,
"full_name": "Hari Yadav"
}
},
{
"user_id": 1,
"total_points": 100,
"rank": 1,
"User": {
"id": 1,
"full_name": "Santosh Sharma"
}
}
]- Code: 400 Bad Request
- Content:
{
"error": "Invalid search value. Must be a numeric user ID."
}If you haven't already, install the required dependencies using npm:
npm installEnsure that your database is properly configured and seeded with the necessary data. You can seed activities with the seedActivities function provided in the code.
- To recalculate the ranks for a specific time period, you can make a
POSTrequest to/api/v1/leaderboard/recalculatewith a query parameter offilter. - Supported filters are
day,month, oryear. - Example (recalculate ranks for today):
curl -X POST "http://localhost:3000/api/v1/leaderboard/recalculate?filter=day"
- To get the current leaderboard, you can make a
GETrequest to/api/v1/leaderboard. - You can also search for a specific user by their
user_idby using thesearchquery parameter. - Example (retrieve the leaderboard):
curl "http://localhost:3000/api/v1/leaderboard" - Example (search for user with
user_id2):curl "http://localhost:3000/api/v1/leaderboard?search=2"
Make sure to handle potential errors such as:
- Invalid filter value in
recalculateRanksAPI. - Invalid user ID format in the
getLeaderboardAPI.
- Activity Model: Represents user activities with a
id,user_id,activity_time, andpoints. - Leaderboard Model: Stores the leaderboard rankings with
user_id,total_points, andrank. - User Model: Represents users with
idandfull_name.
Make sure the necessary associations are set up between the models for proper querying.
- "Invalid filter" error: This occurs if you pass a filter value other than
day,month, oryearto therecalculateRanksAPI. - "Invalid search value" error: This occurs if the
searchquery parameter is not a valid number representing a user ID.
This API was created by [Bibek Bhagat].