feat(backend) criação da controler que retorna todos usuarios pelo profile

adriano 2023-10-23 09:09:14 -03:00
parent f9594aa114
commit df9d468700
1 changed files with 42 additions and 33 deletions

View File

@ -10,11 +10,15 @@ import UpdateUserService from "../services/UserServices/UpdateUserService";
import ShowUserService from "../services/UserServices/ShowUserService"; import ShowUserService from "../services/UserServices/ShowUserService";
import DeleteUserService from "../services/UserServices/DeleteUserService"; import DeleteUserService from "../services/UserServices/DeleteUserService";
import ListUserParamiterService from "../services/UserServices/ListUserParamiterService" import ListUserParamiterService from "../services/UserServices/ListUserParamiterService";
import User from "../models/User"; import User from "../models/User";
import { startWhoIsOnlineMonitor, stopWhoIsOnlineMonitor } from "../helpers/WhoIsOnlineMonitor" import {
import UserOnlineTIme from '../models/UserOnlineTime' startWhoIsOnlineMonitor,
stopWhoIsOnlineMonitor
} from "../helpers/WhoIsOnlineMonitor";
import UserOnlineTIme from "../models/UserOnlineTime";
import ListUser from "../services/UserServices/ListUserParamiterService";
type IndexQuery = { type IndexQuery = {
searchParam: string; searchParam: string;
@ -31,13 +35,12 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
profile profile
}); });
if (req.user.profile !== 'master') { if (req.user.profile !== "master") {
let auxUsers: Array<object> = []; let auxUsers: Array<object> = [];
for (var user of users) { for (var user of users) {
if (user.profile !== 'master') { if (user.profile !== "master") {
auxUsers.push(user) auxUsers.push(user);
} }
} }
@ -46,34 +49,44 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
return res.json({ users, count, hasMore }); return res.json({ users, count, hasMore });
// const { users, count, hasMore } = await ListUsersService({ // const { users, count, hasMore } = await ListUsersService({
// searchParam, // searchParam,
// pageNumber // pageNumber
// }); // });
// if(req.user.profile!=='master'){ // if(req.user.profile!=='master'){
// let auxUsers: Array<object> = []; // let auxUsers: Array<object> = [];
// for (var user of users) { // for (var user of users) {
// if(user.profile!=='master'){ // if(user.profile!=='master'){
// auxUsers.push(user) // auxUsers.push(user)
// } // }
// } // }
// return res.json({ users: auxUsers, count, hasMore }); // return res.json({ users: auxUsers, count, hasMore });
// } // }
// return res.json({ users, count, hasMore }); // return res.json({ users, count, hasMore });
}; };
export const all = async (req: Request, res: Response): Promise<Response> => {
const { profile } = req.query as IndexQuery;
const users = await ListUser({
profile
});
return res.json({ users });
};
export const store = async (req: Request, res: Response): Promise<Response> => { export const store = async (req: Request, res: Response): Promise<Response> => {
const { email, password, name, profile, queueIds } = req.body; const { email, password, name, profile, queueIds } = req.body;
if (req.url === "/signup" && (await CheckSettingsHelper("userCreation")) === "disabled") { if (
req.url === "/signup" &&
(await CheckSettingsHelper("userCreation")) === "disabled"
) {
throw new AppError("ERR_USER_CREATION_DISABLED", 403); throw new AppError("ERR_USER_CREATION_DISABLED", 403);
} else if (req.url !== "/signup" && req.user.profile !== "master") { } else if (req.url !== "/signup" && req.user.profile !== "master") {
throw new AppError("ERR_NO_PERMISSION", 403); throw new AppError("ERR_NO_PERMISSION", 403);
@ -93,9 +106,8 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
user user
}); });
// await stopWhoIsOnlineMonitor() // await stopWhoIsOnlineMonitor()
await startWhoIsOnlineMonitor() await startWhoIsOnlineMonitor();
return res.status(200).json(user); return res.status(200).json(user);
}; };
@ -108,17 +120,18 @@ export const show = async (req: Request, res: Response): Promise<Response> => {
return res.status(200).json(user); return res.status(200).json(user);
}; };
export const logoutUser = async (
export const logoutUser = async (req: Request, res: Response): Promise<Response> => { req: Request,
res: Response
): Promise<Response> => {
const { userId } = req.params; const { userId } = req.params;
await stopWhoIsOnlineMonitor();
await stopWhoIsOnlineMonitor()
let onlineTime = { let onlineTime = {
userId: userId, userId: userId,
status: 'logout...' status: "logout..."
} };
const io = getIO(); const io = getIO();
io.emit("onlineStatus", { io.emit("onlineStatus", {
@ -126,13 +139,12 @@ export const logoutUser = async (req: Request, res: Response): Promise<Response>
userOnlineTime: onlineTime userOnlineTime: onlineTime
}); });
await startWhoIsOnlineMonitor() await startWhoIsOnlineMonitor();
// //
return res.status(200).json({}); return res.status(200).json({});
}; };
export const update = async ( export const update = async (
req: Request, req: Request,
res: Response res: Response
@ -165,26 +177,23 @@ export const remove = async (
throw new AppError("ERR_NO_PERMISSION", 403); throw new AppError("ERR_NO_PERMISSION", 403);
} }
await DeleteUserService(userId); await DeleteUserService(userId);
const io = getIO(); const io = getIO();
io.emit("user", { io.emit("user", {
action: "delete", action: "delete",
userId userId
}); });
//test del
//test del await stopWhoIsOnlineMonitor();
await stopWhoIsOnlineMonitor()
io.emit("onlineStatus", { io.emit("onlineStatus", {
action: "delete", action: "delete",
userOnlineTime: userId userOnlineTime: userId
}); });
await startWhoIsOnlineMonitor() await startWhoIsOnlineMonitor();
// //
return res.status(200).json({ message: "User deleted" }); return res.status(200).json({ message: "User deleted" });