2022-01-06 01:26:15 +00:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
import { getIO } from "../libs/socket";
|
|
|
|
|
|
|
|
import CheckSettingsHelper from "../helpers/CheckSettings";
|
|
|
|
import AppError from "../errors/AppError";
|
|
|
|
|
|
|
|
import CreateUserService from "../services/UserServices/CreateUserService";
|
|
|
|
import ListUsersService from "../services/UserServices/ListUsersService";
|
|
|
|
import UpdateUserService from "../services/UserServices/UpdateUserService";
|
|
|
|
import ShowUserService from "../services/UserServices/ShowUserService";
|
|
|
|
import DeleteUserService from "../services/UserServices/DeleteUserService";
|
|
|
|
|
|
|
|
type IndexQuery = {
|
|
|
|
searchParam: string;
|
|
|
|
pageNumber: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { searchParam, pageNumber } = req.query as IndexQuery;
|
|
|
|
|
|
|
|
const { users, count, hasMore } = await ListUsersService({
|
|
|
|
searchParam,
|
|
|
|
pageNumber
|
|
|
|
});
|
|
|
|
|
2022-01-13 17:11:50 +00:00
|
|
|
if(req.user.profile!=='master'){
|
|
|
|
|
|
|
|
let auxUsers: Array<object> = [];
|
2022-01-13 10:05:08 +00:00
|
|
|
|
2022-01-13 17:11:50 +00:00
|
|
|
for (var user of users) {
|
|
|
|
if(user.profile!=='master'){
|
|
|
|
auxUsers.push(user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({ users: auxUsers, count, hasMore });
|
|
|
|
}
|
2022-01-13 10:05:08 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return res.json({ users, count, hasMore });
|
|
|
|
};
|
|
|
|
|
|
|
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { email, password, name, profile, queueIds } = req.body;
|
|
|
|
|
2022-01-13 10:05:08 +00:00
|
|
|
if (req.url === "/signup" && (await CheckSettingsHelper("userCreation")) === "disabled") {
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError("ERR_USER_CREATION_DISABLED", 403);
|
2022-01-13 10:05:08 +00:00
|
|
|
} else if (req.url !== "/signup" && req.user.profile !== "master") {
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError("ERR_NO_PERMISSION", 403);
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await CreateUserService({
|
|
|
|
email,
|
|
|
|
password,
|
|
|
|
name,
|
|
|
|
profile,
|
|
|
|
queueIds
|
|
|
|
});
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("user", {
|
|
|
|
action: "create",
|
|
|
|
user
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).json(user);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const show = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { userId } = req.params;
|
|
|
|
|
|
|
|
const user = await ShowUserService(userId);
|
|
|
|
|
|
|
|
return res.status(200).json(user);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const update = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
2022-01-13 17:11:50 +00:00
|
|
|
if (req.user.profile !== "admin" && req.user.profile !== "master") {
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError("ERR_NO_PERMISSION", 403);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { userId } = req.params;
|
|
|
|
const userData = req.body;
|
|
|
|
|
|
|
|
const user = await UpdateUserService({ userData, userId });
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("user", {
|
|
|
|
action: "update",
|
|
|
|
user
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).json(user);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const remove = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
|
|
|
const { userId } = req.params;
|
|
|
|
|
2022-01-13 10:05:08 +00:00
|
|
|
if (req.user.profile !== "master") {
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError("ERR_NO_PERMISSION", 403);
|
|
|
|
}
|
|
|
|
|
|
|
|
await DeleteUserService(userId);
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("user", {
|
|
|
|
action: "delete",
|
|
|
|
userId
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).json({ message: "User deleted" });
|
|
|
|
};
|