Skip to content

Commit

Permalink
Add POST /api/v1/users/sign-out
Browse files Browse the repository at this point in the history
  • Loading branch information
Cow-Van committed Jan 12, 2024
1 parent 8c4fcdf commit 5a8717f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
15 changes: 15 additions & 0 deletions src/api/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ router.post("/sign-in", async (_req, res) => {
});
});

router.post("/sign-out", async (req, res) => {
const user: User = res.locals.user;

if (!user.signed_in) {
return res.status(400).json({
description: `${user.first_name} ${user.last_name} is not signed in!`,
});
}

await updateUser(user.user_id, { signed_in: false });
await createSession(user.user_id, user.last_signed_in, Date.now(), false);

return res.status(200).json({
description: `Signed out as ${user.first_name} ${user.last_name}`
});
});

export default router;
20 changes: 6 additions & 14 deletions src/models/Session.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,15 @@ async function getSessionsByPassword(password: number): Promise<SessionRowDataPa
}

async function createSession(
password: number,
user_id: number,
start_time: number,
end_time: number,
amended: boolean,
end_time?: number
): Promise<boolean> {
let sql: string;
let params: any[];

if (amended) {
sql =
"INSERT INTO `sessions` (user_id, start_time, end_time, amended) SELECT user_id, ?, ?, ? FROM users WHERE password = ?)";
params = [start_time, end_time, amended, password];
} else {
sql =
"INSERT INTO `sessions` (user_id, start_time, amended) SELECT user_id, ?, ? FROM users WHERE password = ?)";
params = [start_time, amended, password];
}
const sql =
"INSERT INTO `sessions` (user_id, start_time, end_time, amended) SELECT user_id, ?, ?, ? FROM users WHERE user_id = ?)";
const params = [start_time, end_time, amended, user_id];

const [resHeader] = await database.query<ResultSetHeader>(sql, params);

return resHeader.affectedRows === 1;
Expand Down

0 comments on commit 5a8717f

Please sign in to comment.