Skip to content

Commit

Permalink
Logout Fixed (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyumat committed Jan 8, 2023
1 parent 5386e35 commit 47b0df1
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions backend/routes/auth/logout.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import User from "../../models/user.js";
import { Router } from "express";
import Dixon from "../../models/dixon.js";
Expand All @@ -11,10 +12,35 @@ router.patch("/", async (req, res) => {
return res.status(400).send("User doesn't exist.");
}

user.updatedAt = new Date();

// Delete all dixons associated with the user
await Dixon.deleteMany({ user: user._id });

let machine_ids = user.workouts[0].machines.map((machine: { machine_id: any; }) => machine.machine_id);

let machines = await Dixon.find({ machine_id: { $in: machine_ids } }).exec();

for (let i = 0; i < machines.length; i++) {
machines[i].machine_status = true;
await machines[i].save();
}

user.workouts[0].workoutEndTimestamp = Date.now();

let workoutDuration =
user.workouts[0].workoutEndTimestamp -
user.workouts[0].workoutStartTimestamp;

workoutDuration = (workoutDuration / 60000).toFixed(2);

user.workouts[0].workoutDuration = workoutDuration;

// possibly change this to handle sudden logout
user.workouts[0].machines.forEach((machine: { machine_status: boolean; }) => {
machine.machine_status = true;
});

user.savedWorkouts.push(user.workouts[0]);
user.workouts.pop();

user.logoutDateTime = Date.now();

await user.save();

Expand All @@ -26,4 +52,4 @@ router.patch("/", async (req, res) => {
}
});

export default router;
export default router;

0 comments on commit 47b0df1

Please sign in to comment.