feat: add user remove route

This commit is contained in:
canove
2020-09-14 19:24:38 -03:00
parent 7f33e33078
commit b3cf16bbd3
7 changed files with 155 additions and 115 deletions

View File

@@ -4,23 +4,23 @@ import CheckSettingsHelper from "../helpers/CheckSettingsHelper";
import AppError from "../errors/AppError";
import CreateUserService from "../services/CreateUserService";
// import UpdateUserService from "../services/UpdateUserService";
// import ListUsersService from "../services/ListUsersService";
// import FindUserService from "../services/FindUserService";
import ListUsersService from "../services/ListUsersService";
import UpdateUserService from "../services/UpdateUserService";
import FindUserService from "../services/FindUserService";
import DeleteUserService from "../services/DeleteUserService";
export const index = async (req: Request, res: Response): Promise<Response> => {
// if (req.user.profile !== "admin") {
// throw new AppError("Only administrators can access this route.", 403); // should be handled better.
// }
// const { searchParam, pageNumber } = req.query as any;
if (req.user.profile !== "admin") {
throw new AppError("Only administrators can access this route.", 403); // should be handled better.
}
const { searchParam, pageNumber } = req.query as any;
// const { users, count, hasMore } = await ListUsersService({
// searchParam,
// pageNumber
// });
const { users, count, hasMore } = await ListUsersService({
searchParam,
pageNumber
});
// return res.json({ users, count, hasMore });
return res.json({ ok: "ok" });
return res.json({ users, count, hasMore });
};
export const store = async (req: Request, res: Response): Promise<Response> => {
@@ -45,26 +45,41 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
return res.status(200).json(user);
};
// export const show = async (req: Request, res: Response): Promise<Response> => {
// const { userId } = req.params;
export const show = async (req: Request, res: Response): Promise<Response> => {
const { userId } = req.params;
// const user = await FindUserService(userId);
const user = await FindUserService(userId);
// return res.status(200).json(user);
// };
return res.status(200).json(user);
};
// export const update = async (
// req: Request,
// res: Response
// ): Promise<Response> => {
// if (req.user.profile !== "admin") {
// throw new AppError("Only administrators can edit users.", 403);
// }
export const update = async (
req: Request,
res: Response
): Promise<Response> => {
if (req.user.profile !== "admin") {
throw new AppError("Only administrators can edit users.", 403);
}
// const { userId } = req.params;
// const userData = req.body;
const { userId } = req.params;
const userData = req.body;
// const user = await UpdateUserService({ userData, userId });
const user = await UpdateUserService({ userData, userId });
// return res.status(200).json(user);
// };
return res.status(200).json(user);
};
export const remove = async (
req: Request,
res: Response
): Promise<Response> => {
const { userId } = req.params;
if (req.user.profile !== "admin") {
throw new AppError("Only administrators can delete users.", 403);
}
await DeleteUserService(userId);
return res.status(200).json({ message: "User deleted" });
};