started migration of user domain to ts

This commit is contained in:
canove
2020-09-14 12:42:20 -03:00
parent 32f7e48362
commit f18bab145f
28 changed files with 781 additions and 340 deletions

View File

@@ -0,0 +1,69 @@
import { Request, Response } from "express";
// 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";
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;
const { users, count, hasMore } = await ListUsersService({
searchParam,
pageNumber
});
return res.json({ users, count, hasMore });
};
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);
// }
const user = await CreateUserService({
email,
password,
name,
profile
});
return res.status(200).json(user);
};
export const show = async (req: Request, res: Response): Promise<Response> => {
const { userId } = req.params;
const user = await FindUserService(userId);
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);
}
const { userId } = req.params;
const userData = req.body;
const user = await UpdateUserService({ userData, userId });
return res.status(200).json(user);
};