from fastapi import FastAPI,HTTPException, Header from gamification.objects import * from gamification.logic import * from controller.jwtcoding import decode_jwt from controller.tokenManagement import verify_access_token class EditableUserLevel(BaseModel): levelId : str maxPoints : Optional[int]= None minPoints : Optional[int]= None levelName : Optional[str]= None careerPath : Optional[str]= None levelNumber : Optional[int]= None # To convert MongoDB ObjectId to string, if needed class Config: json_encoders = { ObjectId: str } gamification = FastAPI() @gamification.post("/create-feedback",tags=["user"]) def create_feedback(feedback:Feedback,authorization: str = Header(...))->bool: token = authorization.split("Bearer ")[-1] # Here, you would validate the token (e.g., check with a JWT library) decoded_user_id,decoded_access_token = decode_jwt(token) is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token) if is_valid != True: # Example check raise HTTPException(status_code=401, detail="Invalid token") try: user_feedback = UserFeedback(userId=decoded_user_id,respondedAt=datetime.now(), feedback=feedback.feedback,rating=feedback.rating) result = create_feedback_func(user_feedback) return result except Exception as e: raise HTTPException(status_code=500,detail=f"{e}") @gamification.get("/get-advanced-user-points",tags=["user"]) def get_points(authorization: str = Header(...))->IndividualUserLevel: # Extract the token from the Authorization header (Bearer token) token = authorization.split("Bearer ")[-1] # Here, you would validate the token (e.g., check with a JWT library) decoded_user_id,decoded_access_token = decode_jwt(token) is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token) if is_valid != True: # Example check raise HTTPException(status_code=401, detail="Invalid token") # do thing u want here points = get_all_points_func(userId=decoded_user_id) return points.model_dump(exclude_none=True) @gamification.get("/get-simple-user-points",tags=["user"]) def get_points(authorization: str = Header(...))->SimpleIndividualUserLevel: # Extract the token from the Authorization header (Bearer token) token = authorization.split("Bearer ")[-1] # Here, you would validate the token (e.g., check with a JWT library) decoded_user_id,decoded_access_token = decode_jwt(token) is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token) if is_valid != True: # Example check raise HTTPException(status_code=401, detail="Invalid token") # do thing u want here points = get_all_simple_points_func(userId=decoded_user_id) return points.model_dump(exclude_none=True) @gamification.get("/get-customer-info",tags=["admin"]) def get_customer_information(): try: result = get_all_customer_info() return result except Exception as e: raise HTTPException(status_code=500,detail=f"{e}") @gamification.get("/get-feedback",tags=["admin"]) def get_feedback()->List[UserFeedback]: feedbacks = get_all_feedback_func() return feedbacks @gamification.post("/create-level",tags=["admin"]) def create_level(level_obj:UserLevel)->bool: result = create_level_func(document=level_obj) return result @gamification.get("/get-levels",tags=["admin","user"]) def get_level_details_and_information()->List[UserLevel]: levels= get_all_levels_func() return levels @gamification.get("/admin/get-levels",tags=["admin","user"]) def get_level_details_and_information()->List[UserLevel]: levels= get_all_levels_func(admin=True) return levels @gamification.get("/get-levels/{careerPath}",tags=["user"]) def get_level_details_and_information(careerPath:str)->List[UserLevel]: levels= get_all_levels_func(career=careerPath) return levels @gamification.patch("/edit-level",tags=["admin"]) def edit_level(level_obj:EditableUserLevel): result = edit_level_func(level_id=level_obj.levelId,levelName=level_obj.levelName,careerPath=level_obj.careerPath,minPoints=level_obj.minPoints,maxPoints=level_obj.maxPoints,levelNumber=level_obj.levelNumber) return {"message":result} @gamification.delete("/delete-level/{levelId}",tags=["admin"]) def delete_level(levelId): try: result = delete_level_func(level_id=levelId) return {"message":result} except Exception as e: raise HTTPException(status_code=500,detail=f"{e}") from fastapi import BackgroundTasks @gamification.get("/get-top-30",tags=["user","admin"]) def get_leaderboard(background_tasks: BackgroundTasks)->List[Ranker]: from app import handle_change2 try: list_of_rankers = [] result = get_top_30() background_tasks.add_task(handle_change2, 2) list_of_rankers = [Ranker(**ranker) for ranker in result] return list_of_rankers except Exception as e: raise HTTPException(status_code=500,detail=f"{e}")