Skip to content

Commit

Permalink
bug in building session_update_query + updated time_remaining was not…
Browse files Browse the repository at this point in the history
… being sent to DB
  • Loading branch information
deepansh96 committed Apr 1, 2023
1 parent 9191748 commit 35ca665
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions app/routers/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,16 @@ async def update_session(session_id: str, session_updates: UpdateSession):
event_obj = jsonable_encoder(Event.parse_obj({"event_type": new_event}))
if session["events"] is None:
session["events"] = [event_obj]
session_update_query["$set"] = {"events": [event_obj]}
if "$set" not in session_update_query:
session_update_query["$set"] = {"events": [event_obj]}
else:
session_update_query["$set"].update({"events": [event_obj]})
else:
session["events"].append(event_obj)
session_update_query["$push"] = {"events": event_obj}
if "$push" not in session_update_query:
session_update_query["$push"] = {"events": event_obj}
else:
session_update_query["$push"].update({"events": event_obj})

# diff between times of last two events
time_elapsed = 0
Expand Down Expand Up @@ -205,12 +211,19 @@ async def update_session(session_id: str, session_updates: UpdateSession):
# if `time_remaining` key is not present =>
# no time limit is set, no need to respond with time_remaining
time_remaining = max(0, session["time_remaining"] - time_elapsed)
session_update_query["$set"] = {"time_remaining": time_remaining}
response_content = {"time_remaining": session["time_remaining"]}
if "$set" not in session_update_query:
session_update_query["$set"] = {"time_remaining": time_remaining}
else:
session_update_query["$set"].update({"time_remaining": time_remaining})
response_content = {"time_remaining": time_remaining}

# update the document in the sessions collection
if new_event == EventType.end_quiz:
session_update_query["$set"] = {"has_quiz_ended": True}
if "$set" not in session_update_query:
session_update_query["$set"] = {"has_quiz_ended": True}
else:
session_update_query["$set"].update({"has_quiz_ended": True})

client.quiz.sessions.update_one({"_id": session_id}, session_update_query)

return JSONResponse(status_code=status.HTTP_200_OK, content=response_content)
Expand Down

0 comments on commit 35ca665

Please sign in to comment.