Skip to content

Commit

Permalink
Batch Query Users Internal
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnGrubba committed Jul 9, 2024
1 parent fffaae5 commit 77f2d9e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/api/internal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import APIRouter, Header, HTTPException, Depends, BackgroundTasks
from tools import broadcast_emails, InternalConfig, bson_to_json
from api.model import BroadCastEmailRequest, InternalProfileRequest
from crud.user import get_user
from crud.user import get_user, get_batch_users
from crud.sessions import get_session
from threading import Lock

Expand Down Expand Up @@ -68,3 +68,22 @@ async def profile(internal_req: InternalProfileRequest):
get_session(internal_req.session_token) if internal_req.session_token else None
)
return bson_to_json(get_user(sess["user_id"] if sess else internal_req.user_id))


@router.get("/batch-users")
async def batch_users(user_ids_req: str):
"""
# Get Batch User Information
## Description
This endpoint is used to get the whole profile information of multiple users. (Including Internal Information)
## Query Parameters
- **user_ids**: List of User IDs seperated by `,`
"""
ids = user_ids_req.split(",")
if len(ids) > 50:
raise HTTPException(
status_code=400, detail="Too many User IDs. Max 50 User IDs allowed."
)
return [bson_to_json(_) for _ in get_batch_users(ids)]
20 changes: 18 additions & 2 deletions src/crud/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@
InternalConfig,
insecure_cols,
)
from fastapi import HTTPException, BackgroundTasks, Response
from api.model import UserSignupRequest, LoginResponse
from fastapi import HTTPException, BackgroundTasks
from api.model import UserSignupRequest
import pymongo, bson
import datetime


def get_batch_users(user_ids: list) -> list:
"""Get a batch of users by ID
Args:
user_ids (list): List of User IDs
Returns:
list: List of User Data
"""
try:
bson_ids = [bson.ObjectId(i) for i in user_ids]
except bson.errors.InvalidId:
raise HTTPException(detail="Invalid User ID", status_code=400)
return list(users_collection.find({"_id": {"$in": bson_ids}}, insecure_cols))


def update_public_user(user_id: str, data: dict) -> None:
"""Updates Public User Data
Expand Down

0 comments on commit 77f2d9e

Please sign in to comment.