finished user store in typscript

This commit is contained in:
canove
2020-09-14 18:54:36 -03:00
parent eba3553a2d
commit 7f33e33078
29 changed files with 260 additions and 133 deletions

View File

@@ -1,6 +1,6 @@
import { Request, Response } from "express";
// import CheckSettingsHelper from "../helpers/CheckSettingsHelper";
import CheckSettingsHelper from "../helpers/CheckSettingsHelper";
import AppError from "../errors/AppError";
import CreateUserService from "../services/CreateUserService";
@@ -9,30 +9,31 @@ import CreateUserService from "../services/CreateUserService";
// import FindUserService from "../services/FindUserService";
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({ users, count, hasMore });
return res.json({ ok: "ok" });
};
export const store = async (req: Request, res: Response): Promise<Response> => {
const { email, password, name, profile } = req.body;
// if (
// req.url === "/signup" &&
// (await CheckSettingsHelper("userCreation")) === "disabled"
// ) {
// throw new AppError("User creation is disabled by administrator.", 403);
// } else if (req.user.profile !== "admin") {
// throw new AppError("Only administrators can create users.", 403);
// }
if (
req.url === "/signup" &&
(await CheckSettingsHelper("userCreation")) === "disabled"
) {
throw new AppError("User creation is disabled by administrator.", 403);
} else if (req.url !== "/signup" && req.user.profile !== "admin") {
throw new AppError("Only administrators can create users.", 403);
}
const user = await CreateUserService({
email,
@@ -44,26 +45,26 @@ 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);
// };