33from flask import Blueprint , request
44
55import api .queue .controller as controller
6+ from api .queue .controller import remove_from_queue_without_visit
67from api .roster .controller import min_level
78from api .database .db import db
89
@@ -86,10 +87,18 @@ def dequeue():
8687 """
8788 role: TA
8889
89- Remove the first student from the queue and create a Visit in the DB
90+ Remove the specified student from the queue and create a Visit in the DB
9091
9192 Not allowed if TA is already in a visit
9293
94+ Args:
95+ body.id: The ID of the account to dequeue
96+
97+ Body:
98+ {
99+ "id": <string>
100+ }
101+
93102 Returns:
94103 200 OK - Student was dequeued
95104 {
@@ -99,23 +108,34 @@ def dequeue():
99108 "preferred_name: <string>
100109 }
101110
102- 400 Bad Request - The queue is empty
111+ 400 Bad Request - The queue is empty or user is not in the queue
103112 403 Unauthorized - Requester does not have TA permissions
104113 {
105114 "message": <string>
106115 }
107116 """
108117
109- student = db .dequeue_student ()
118+ body = request .get_json ()
119+
120+ if not (auth_token := request .cookies .get ("auth_token" )):
121+ return {"message" : "You are not logged in!" }, 403
122+
123+ user = db .get_authenticated_user (auth_token )
124+ user_id = user ["user_id" ]
125+
126+ student = db .dequeue_specified_student (body ["id" ])
110127
111128 if student is None :
112129 return {"message" : "The queue is empty" }, 400
113130
131+ visit = db .create_visit (body ["id" ], user_id , student ["enqueue_time" ])
132+
114133 return {
115134 "id" : int (student ["user_id" ]),
116135 "username" : student ["ubit" ],
117136 "pn" : str (student ["person_num" ]),
118137 "preferred_name" : student ["preferred_name" ],
138+ "visitID" : visit
119139 }
120140
121141
@@ -215,7 +235,23 @@ def remove_self():
215235 "message": <string>
216236 }
217237 """
218- return f"{ request .path } hit 😎, remove method is used."
238+
239+ if not (auth_token := request .cookies .get ("auth_token" )):
240+ return {"message" : "You are not logged in!" }, 403
241+
242+ user = db .get_authenticated_user (auth_token )
243+
244+ if not user :
245+ return {"message" : "You are not logged in!" }, 403
246+
247+ user_id = user ["user_id" ]
248+ body = request .get_json ()
249+
250+ remove_from_queue_without_visit (user_id , f"[SELF-REMOVE]: { body ["reason" ]} " )
251+
252+
253+
254+ return {"message" :"Removed self from queue." }
219255
220256
221257@blueprint .route ("/remove-from-queue" , methods = ["POST" ])
@@ -245,7 +281,59 @@ def remove(user_id):
245281 """
246282 return f"{ request .path } hit 😎, remove method is used."
247283
284+ @blueprint .route ("/clear-queue" , methods = ["DELETE" ])
285+ @min_level ('ta' )
286+ def clear_queue ():
287+ db .clear_queue ()
288+ return {"message" : "Successfully cleared the queue." }
289+
290+
291+ @blueprint .route ("/enqueue-override-front" )
292+ @min_level ('ta' )
293+ def enqueue_override_front ():
294+ """ Exact same behavior as /enqueue-ta-override, except it sends the student to the front.
295+
296+ Args:
297+ body.identifier: A unique identifier for the student This can either be their UBIT, pn, or the id of their account
298+
299+ Body:
300+ {
301+ "identifier": <string>
302+ }
303+
304+ Returns:
305+ 200 OK - Student was added to the queue
306+ 403 Unauthorized - Requester does not have TA permissions
307+ 404 Not Found - No student matching provided identifier
308+ {
309+ "message": <string>,
310+ }
311+ """
312+ body = request .get_json ()
313+ identifier = body ["identifier" ]
314+
315+ if controller .add_to_queue_by_ta_override (identifier , True ):
316+ return {"message" : "Student was added to the front of the queue" }
317+
318+ return {"message" : "No student matching provided identifier" }, 404
319+
320+ @blueprint .route ("/end-visit" , methods = ["POST" ])
321+ @min_level ('ta' )
322+ def end_visit ():
323+ body = request .get_json ()
324+
325+ visit = body .get ("id" )
326+ reason = body .get ("reason" )
327+
328+ if visit is None or reason is None :
329+ return {"message" : "Malformed request" }, 400
330+
331+ db .end_visit (visit , reason )
332+
333+ return {"message" : "Ended the visit" }
334+
335+
248336
249337# TODO: move to end of queue (Called by TAs to add the students they just saw back to the end of the queue)
250338
251- # TODO: Clear the queue
339+
0 commit comments