Add more services and auth
This commit is contained in:
parent
d17f37749d
commit
cae53fab61
13 changed files with 331 additions and 57 deletions
|
|
@ -1,22 +1,41 @@
|
|||
import { type FastifyReply, type FastifyRequest } from "fastify";
|
||||
import type {
|
||||
IUserParams,
|
||||
IUserResponseError,
|
||||
IUserResponseSuccess,
|
||||
ISessionsResponseError,
|
||||
ISessionsResponseSuccess,
|
||||
IGetUserParams,
|
||||
IGetUserResponseError,
|
||||
IGetUserResponseSuccess,
|
||||
IPatchUserParams,
|
||||
IPatchUserRequest,
|
||||
IPatchUserResponseError,
|
||||
IPatchUserResponseSuccess,
|
||||
IGetSessionsParams,
|
||||
IGetSessionsResponseError,
|
||||
IGetSessionsResponseSuccess,
|
||||
} from "./types.js";
|
||||
import { getUserById, getUserSessionsById } from "../../services/user/user.js";
|
||||
import {
|
||||
getUserByIdAuth,
|
||||
updateUserByIdAuth,
|
||||
getUserSessionsByIdAuth,
|
||||
} from "../../services/user/user.js";
|
||||
import { API_ERROR } from "../errors.js";
|
||||
|
||||
const getUser = async (request: FastifyRequest, _reply: FastifyReply) => {
|
||||
const { id } = request.params as IUserParams;
|
||||
const getUser = async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const { id } = request.params as IGetUserParams;
|
||||
const authHeader = request.headers["authorization"];
|
||||
|
||||
const user = await getUserById(id);
|
||||
const user = await getUserByIdAuth(id, authHeader);
|
||||
if (!user) {
|
||||
reply.status(404);
|
||||
return {
|
||||
id: id,
|
||||
error: "user does not exist",
|
||||
} as IUserResponseError;
|
||||
error: API_ERROR.NOT_FOUND,
|
||||
} as IGetUserResponseError;
|
||||
}
|
||||
if (user === API_ERROR.ACCESS_DENIED) {
|
||||
reply.status(403);
|
||||
return {
|
||||
id: id,
|
||||
error: API_ERROR.ACCESS_DENIED,
|
||||
} as IGetUserResponseError;
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
@ -27,19 +46,55 @@ const getUser = async (request: FastifyRequest, _reply: FastifyReply) => {
|
|||
admin: user.admin,
|
||||
registerDate: user.registerDate.getTime(),
|
||||
lastLogin: user.lastLogin?.getTime() ?? 0,
|
||||
} as IUserResponseSuccess;
|
||||
} as IGetUserResponseSuccess;
|
||||
};
|
||||
|
||||
const getSessions = async (request: FastifyRequest, _reply: FastifyReply) => {
|
||||
const { id } = request.params as IUserParams;
|
||||
const patchUser = async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const { id } = request.params as IPatchUserParams;
|
||||
const patchUserRequest = request.body as IPatchUserRequest;
|
||||
const authHeader = request.headers["authorization"];
|
||||
|
||||
const sessions = await getUserSessionsById(id, authHeader);
|
||||
if (!sessions) {
|
||||
const user = await updateUserByIdAuth(id, patchUserRequest, authHeader);
|
||||
if (!user) {
|
||||
reply.status(404);
|
||||
return {
|
||||
id: id,
|
||||
error: "user does not exist or you have no access",
|
||||
} as ISessionsResponseError;
|
||||
error: API_ERROR.NOT_FOUND,
|
||||
} as IPatchUserResponseError;
|
||||
}
|
||||
if (user === API_ERROR.ACCESS_DENIED) {
|
||||
reply.status(403);
|
||||
return {
|
||||
id: id,
|
||||
error: API_ERROR.ACCESS_DENIED,
|
||||
} as IPatchUserResponseError;
|
||||
}
|
||||
|
||||
return {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
description: user.description,
|
||||
} as IPatchUserResponseSuccess;
|
||||
};
|
||||
|
||||
const getSessions = async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const { id } = request.params as IGetSessionsParams;
|
||||
const authHeader = request.headers["authorization"];
|
||||
|
||||
const sessions = await getUserSessionsByIdAuth(id, authHeader);
|
||||
if (!sessions) {
|
||||
reply.status(404);
|
||||
return {
|
||||
id: id,
|
||||
error: API_ERROR.NOT_FOUND,
|
||||
} as IGetSessionsResponseError;
|
||||
}
|
||||
if (sessions === API_ERROR.ACCESS_DENIED) {
|
||||
reply.status(403);
|
||||
return {
|
||||
id: id,
|
||||
error: API_ERROR.ACCESS_DENIED,
|
||||
} as IGetSessionsResponseError;
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
@ -47,7 +102,7 @@ const getSessions = async (request: FastifyRequest, _reply: FastifyReply) => {
|
|||
id: session.id,
|
||||
userId: session.userId,
|
||||
})),
|
||||
} as ISessionsResponseSuccess;
|
||||
} as IGetSessionsResponseSuccess;
|
||||
};
|
||||
|
||||
export { getUser, getSessions };
|
||||
export { getUser, patchUser, getSessions };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue