Add services and auth

This commit is contained in:
Aslan 2025-12-23 17:45:51 -05:00
parent 9b0b5dc040
commit 5dec454afb
46 changed files with 900 additions and 31 deletions

View file

@ -0,0 +1,53 @@
import { type FastifyReply, type FastifyRequest } from "fastify";
import type {
IUserParams,
IUserResponseError,
IUserResponseSuccess,
ISessionsResponseError,
ISessionsResponseSuccess,
} from "./types.js";
import { getUserById, getUserSessionsById } from "../../services/user/user.js";
const getUser = async (request: FastifyRequest, _reply: FastifyReply) => {
const { id } = request.params as IUserParams;
const user = await getUserById(id);
if (!user) {
return {
id: id,
error: "user does not exist",
} as IUserResponseError;
}
return {
id: user.id,
username: user.username,
email: user.email,
description: user.description,
admin: user.admin,
registerDate: user.registerDate.getTime(),
lastLogin: user.lastLogin?.getTime() ?? 0,
} as IUserResponseSuccess;
};
const getSessions = async (request: FastifyRequest, _reply: FastifyReply) => {
const { id } = request.params as IUserParams;
const authHeader = request.headers["authorization"];
const sessions = await getUserSessionsById(id, authHeader);
if (!sessions) {
return {
id: id,
error: "user does not exist or you have no access",
} as ISessionsResponseError;
}
return {
sessions: sessions.map((session) => ({
id: session.id,
userId: session.userId,
})),
} as ISessionsResponseSuccess;
};
export { getUser, getSessions };