File size: 4,899 Bytes
f1b601b
323b357
 
9f571c0
 
323b357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1b601b
 
 
 
 
 
 
 
 
 
323b357
f1b601b
323b357
f1b601b
323b357
 
 
f1b601b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32d2a84
 
 
 
 
 
 
 
323b357
 
 
 
 
 
 
 
 
 
 
f1b601b
323b357
 
 
 
 
 
 
 
 
 
a76a90c
323b357
 
 
 
 
 
 
 
dbebc69
323b357
 
 
f1b601b
 
 
9bb2f7c
f1b601b
9bb2f7c
 
f1b601b
 
 
9bb2f7c
f1b601b
 
323b357
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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-level",tags=["admin","user"])
def get_level_details_and_information()->List[UserLevel]:
    levels= get_all_levels_func()
    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}")