Add logged user and user communities endpoint
This commit is contained in:
parent
ddcc591d12
commit
a85330e8cf
7 changed files with 180 additions and 3 deletions
|
|
@ -2,11 +2,13 @@ import { type FastifyInstance } from "fastify";
|
|||
import * as controller from "./user.js";
|
||||
|
||||
const userRoutes = async (fastify: FastifyInstance) => {
|
||||
fastify.get(`/logged`, controller.getUserLogged);
|
||||
fastify.get(`/:id`, controller.getUser);
|
||||
fastify.post(`/`, controller.postCreateUser);
|
||||
fastify.patch(`/:id`, controller.patchUser);
|
||||
fastify.delete(`/:id`, controller.deleteUser);
|
||||
fastify.get(`/:id/sessions`, controller.getSessions);
|
||||
fastify.get(`/:id/communities`, controller.getCommunities);
|
||||
};
|
||||
|
||||
export { userRoutes };
|
||||
|
|
|
|||
|
|
@ -1,5 +1,13 @@
|
|||
import type { API_ERROR } from "../errors.js";
|
||||
|
||||
interface IGetLoggedUserResponseError {
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IGetLoggedUserResponseSuccess {
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface IGetUserParams {
|
||||
id: string;
|
||||
}
|
||||
|
|
@ -92,7 +100,29 @@ interface IGetSessionsResponseSession {
|
|||
userId: string;
|
||||
}
|
||||
|
||||
interface IGetCommunitiesParams {
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface IGetCommunitiesResponseError {
|
||||
id: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IGetCommunitiesResponseSuccess {
|
||||
id: string;
|
||||
communities: IGetCommunitiesResponseCommunity[];
|
||||
}
|
||||
|
||||
interface IGetCommunitiesResponseCommunity {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export {
|
||||
type IGetLoggedUserResponseError,
|
||||
type IGetLoggedUserResponseSuccess,
|
||||
type IGetUserParams,
|
||||
type IGetUserResponseError,
|
||||
type IGetUserResponseSuccess,
|
||||
|
|
@ -110,4 +140,8 @@ export {
|
|||
type IGetSessionsResponseError,
|
||||
type IGetSessionsResponseSuccess,
|
||||
type IGetSessionsResponseSession,
|
||||
type IGetCommunitiesParams,
|
||||
type IGetCommunitiesResponseError,
|
||||
type IGetCommunitiesResponseSuccess,
|
||||
type IGetCommunitiesResponseCommunity,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import { type FastifyReply, type FastifyRequest } from "fastify";
|
||||
import type {
|
||||
IGetLoggedUserResponseError,
|
||||
IGetLoggedUserResponseSuccess,
|
||||
IGetUserParams,
|
||||
IGetUserResponseError,
|
||||
IGetUserResponseSuccess,
|
||||
|
|
@ -16,6 +18,9 @@ import type {
|
|||
IGetSessionsParams,
|
||||
IGetSessionsResponseError,
|
||||
IGetSessionsResponseSuccess,
|
||||
IGetCommunitiesParams,
|
||||
IGetCommunitiesResponseError,
|
||||
IGetCommunitiesResponseSuccess,
|
||||
} from "./types.js";
|
||||
import {
|
||||
getUserByIdAuth,
|
||||
|
|
@ -23,9 +28,27 @@ import {
|
|||
getUserSessionsByIdAuth,
|
||||
deleteUserByIdAuth,
|
||||
createUserAuth,
|
||||
getUserCommunitiesByIdAuth,
|
||||
getLoggedUserAuth,
|
||||
} from "../../services/user/user.js";
|
||||
import { API_ERROR } from "../errors.js";
|
||||
|
||||
const getUserLogged = async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const authHeader = request.headers["authorization"];
|
||||
|
||||
const user = await getLoggedUserAuth(authHeader);
|
||||
if (user === API_ERROR.ACCESS_DENIED) {
|
||||
reply.status(404);
|
||||
return {
|
||||
error: API_ERROR.ACCESS_DENIED,
|
||||
} as IGetLoggedUserResponseError;
|
||||
}
|
||||
|
||||
return {
|
||||
id: user.id,
|
||||
} as IGetLoggedUserResponseSuccess;
|
||||
};
|
||||
|
||||
const getUser = async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const { id } = request.params as IGetUserParams;
|
||||
const authHeader = request.headers["authorization"];
|
||||
|
|
@ -160,4 +183,42 @@ const getSessions = async (request: FastifyRequest, reply: FastifyReply) => {
|
|||
} as IGetSessionsResponseSuccess;
|
||||
};
|
||||
|
||||
export { getUser, postCreateUser, patchUser, deleteUser, getSessions };
|
||||
const getCommunities = async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const { id } = request.params as IGetCommunitiesParams;
|
||||
const authHeader = request.headers["authorization"];
|
||||
|
||||
const communities = await getUserCommunitiesByIdAuth(id, authHeader);
|
||||
if (!communities) {
|
||||
reply.status(404);
|
||||
return {
|
||||
id: id,
|
||||
error: API_ERROR.NOT_FOUND,
|
||||
} as IGetCommunitiesResponseError;
|
||||
}
|
||||
if (communities === API_ERROR.ACCESS_DENIED) {
|
||||
reply.status(403);
|
||||
return {
|
||||
id: id,
|
||||
error: API_ERROR.ACCESS_DENIED,
|
||||
} as IGetCommunitiesResponseError;
|
||||
}
|
||||
|
||||
return {
|
||||
id: id,
|
||||
communities: communities.map((community) => ({
|
||||
id: community.id,
|
||||
name: community.name,
|
||||
description: community.description,
|
||||
})),
|
||||
} as IGetCommunitiesResponseSuccess;
|
||||
};
|
||||
|
||||
export {
|
||||
getUserLogged,
|
||||
getUser,
|
||||
postCreateUser,
|
||||
patchUser,
|
||||
deleteUser,
|
||||
getSessions,
|
||||
getCommunities,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,9 +1,25 @@
|
|||
import type { User, Session } from "../../generated/prisma/client.js";
|
||||
import type {
|
||||
User,
|
||||
Session,
|
||||
Community,
|
||||
} from "../../generated/prisma/client.js";
|
||||
import { getUserFromAuth, isUserOwnerOrAdmin } from "../auth/helpers.js";
|
||||
import { getDB } from "../../store/store.js";
|
||||
import { API_ERROR } from "../../controllers/errors.js";
|
||||
import type { ICreateUser, IUpdateUser } from "./types.js";
|
||||
|
||||
const getLoggedUserAuth = async (
|
||||
authHeader: string | undefined,
|
||||
): Promise<User | API_ERROR.ACCESS_DENIED> => {
|
||||
const authUser = await getUserFromAuth(authHeader);
|
||||
|
||||
if (!authUser) {
|
||||
return API_ERROR.ACCESS_DENIED;
|
||||
}
|
||||
|
||||
return authUser;
|
||||
};
|
||||
|
||||
const getUserById = async (id: string): Promise<User | null> => {
|
||||
return await getDB().user.findUnique({
|
||||
where: { id: id },
|
||||
|
|
@ -133,7 +149,41 @@ const getUserSessionsByIdAuth = async (
|
|||
return sessions;
|
||||
};
|
||||
|
||||
const getUserCommunitiesById = async (
|
||||
id: string,
|
||||
): Promise<Community[] | null> => {
|
||||
return await getDB().community.findMany({
|
||||
where: {
|
||||
members: {
|
||||
some: {
|
||||
id: id,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const getUserCommunitiesByIdAuth = async (
|
||||
id: string,
|
||||
authHeader: string | undefined,
|
||||
): Promise<Community[] | null | API_ERROR.ACCESS_DENIED> => {
|
||||
const authUser = await getUserFromAuth(authHeader);
|
||||
const user = await getUserById(id);
|
||||
const communities = await getUserCommunitiesById(id);
|
||||
|
||||
if (
|
||||
!(await isUserOwnerOrAdmin(authUser, {
|
||||
user: user,
|
||||
}))
|
||||
) {
|
||||
return API_ERROR.ACCESS_DENIED;
|
||||
}
|
||||
|
||||
return communities;
|
||||
};
|
||||
|
||||
export {
|
||||
getLoggedUserAuth,
|
||||
getUserById,
|
||||
getUserByIdAuth,
|
||||
createUser,
|
||||
|
|
@ -144,4 +194,6 @@ export {
|
|||
deleteUserByIdAuth,
|
||||
getUserSessionsById,
|
||||
getUserSessionsByIdAuth,
|
||||
getUserCommunitiesById,
|
||||
getUserCommunitiesByIdAuth,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue