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";
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
import ListUserParamiterService from "../services/UserServices/ListUserParamiterService"
|
|
|
|
import User from "../models/User";
|
|
|
|
|
|
|
|
import { startWhoIsOnlineMonitor, stopWhoIsOnlineMonitor } from "../helpers/WhoIsOnlineMonitor"
|
|
|
|
import UserOnlineTIme from '../models/UserOnlineTime'
|
2023-06-02 18:45:14 +00:00
|
|
|
import CountTicketsByUserQueue from "../services/UserServices/CountTicketsByUserQueue";
|
|
|
|
import { splitDateTime } from "../helpers/SplitDateTime";
|
2023-06-02 20:36:04 +00:00
|
|
|
import { format, subMonths } from "date-fns";
|
2023-06-02 18:45:14 +00:00
|
|
|
import { ptBR } from "date-fns/locale";
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
type IndexQuery = {
|
|
|
|
searchParam: string;
|
|
|
|
pageNumber: string;
|
2022-05-16 02:48:06 +00:00
|
|
|
profile?: string;
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
2022-05-16 02:48:06 +00:00
|
|
|
const { searchParam, pageNumber, profile } = req.query as IndexQuery;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const { users, count, hasMore } = await ListUsersService({
|
|
|
|
searchParam,
|
2022-05-16 02:48:06 +00:00
|
|
|
pageNumber,
|
|
|
|
profile
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
if (req.user.profile !== 'master') {
|
|
|
|
|
|
|
|
let auxUsers: Array<object> = [];
|
2022-01-13 10:05:08 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
for (var user of users) {
|
|
|
|
if (user.profile !== 'master') {
|
2023-06-02 18:45:14 +00:00
|
|
|
|
|
|
|
if (req.user.profile == 'supervisor' && (user.profile == 'admin')) continue
|
|
|
|
|
2022-01-13 17:11:50 +00:00
|
|
|
auxUsers.push(user)
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const { users, count, hasMore } = await ListUsersService({
|
|
|
|
// searchParam,
|
|
|
|
// pageNumber
|
|
|
|
// });
|
|
|
|
|
|
|
|
// if(req.user.profile!=='master'){
|
|
|
|
|
|
|
|
// let auxUsers: Array<object> = [];
|
|
|
|
|
|
|
|
// for (var user of users) {
|
|
|
|
// if(user.profile!=='master'){
|
|
|
|
// auxUsers.push(user)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return res.json({ users: auxUsers, count, hasMore });
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return res.json({ users, count, hasMore });
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-12-18 16:56:45 +00:00
|
|
|
// await stopWhoIsOnlineMonitor()
|
2022-05-16 02:48:06 +00:00
|
|
|
await startWhoIsOnlineMonitor()
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
export const logoutUser = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { userId } = req.params;
|
|
|
|
|
2023-06-02 18:45:14 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
await stopWhoIsOnlineMonitor()
|
|
|
|
|
|
|
|
let onlineTime = {
|
|
|
|
userId: userId,
|
|
|
|
status: 'logout...'
|
|
|
|
}
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("onlineStatus", {
|
|
|
|
action: "logout",
|
|
|
|
userOnlineTime: onlineTime
|
|
|
|
});
|
|
|
|
|
|
|
|
await startWhoIsOnlineMonitor()
|
|
|
|
//
|
|
|
|
|
|
|
|
return res.status(200).json({});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
export const update = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
2023-06-02 18:45:14 +00:00
|
|
|
if (req.user.profile !== "admin" && req.user.profile !== "master" && req.user.profile !== "supervisor") {
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError("ERR_NO_PERMISSION", 403);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { userId } = req.params;
|
|
|
|
const userData = req.body;
|
|
|
|
|
2023-06-02 18:45:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
const dateToday = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR })))
|
|
|
|
|
2023-06-02 20:36:04 +00:00
|
|
|
const currentDate = new Date();
|
|
|
|
const tenMonthsAgo = subMonths(currentDate, 10);
|
|
|
|
const formattedDate = format(tenMonthsAgo, 'yyyy-MM-dd');
|
|
|
|
console.log('dateToday.fullDate: ', dateToday.fullDate )
|
|
|
|
console.log("formattedDate 10 months ago: ", formattedDate);
|
2023-06-02 18:45:14 +00:00
|
|
|
|
2023-06-02 20:36:04 +00:00
|
|
|
const openByUserOnQueue: any[] = await CountTicketsByUserQueue({ startDate: formattedDate, endDate: dateToday.fullDate, status: 'open', clientChatStart: true, userId: userId })
|
|
|
|
|
2023-06-02 18:45:14 +00:00
|
|
|
// console.log('------> openByUserOnQueue: ', openByUserOnQueue)
|
|
|
|
// console.log()
|
|
|
|
// console.log('------> 1 userData.queueIds: ', userData.queueIds)
|
|
|
|
|
|
|
|
let userQueuesAttendance = []
|
|
|
|
|
|
|
|
if ((openByUserOnQueue && openByUserOnQueue.length) > 0) {
|
|
|
|
|
|
|
|
userQueuesAttendance = openByUserOnQueue.filter((e: any) => !userData.queueIds.includes(e.queueId))
|
|
|
|
|
|
|
|
if (userQueuesAttendance && userQueuesAttendance.length > 0) {
|
|
|
|
|
|
|
|
const queueInAttendance = userQueuesAttendance.map((e) => e.queueId)
|
|
|
|
|
|
|
|
const mergedSet = new Set([...userData.queueIds, ...queueInAttendance])
|
|
|
|
|
|
|
|
// Convert the Set back to an array
|
|
|
|
userData.queueIds = Array.from(mergedSet)
|
|
|
|
|
|
|
|
// console.log('------> 2 userData.queueIds: ', userData.queueIds)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log('userQueuesAttendance: ', userQueuesAttendance)
|
|
|
|
|
|
|
|
// return res.status(200).json({});
|
|
|
|
|
|
|
|
let user: any = await UpdateUserService({ userData, userId });
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("user", {
|
|
|
|
action: "update",
|
|
|
|
user
|
|
|
|
});
|
|
|
|
|
2023-06-02 18:45:14 +00:00
|
|
|
user.userQueuesAttendance = userQueuesAttendance
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
await DeleteUserService(userId);
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const io = getIO();
|
|
|
|
io.emit("user", {
|
|
|
|
action: "delete",
|
|
|
|
userId
|
|
|
|
});
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
//test del
|
|
|
|
await stopWhoIsOnlineMonitor()
|
|
|
|
|
|
|
|
io.emit("onlineStatus", {
|
|
|
|
action: "delete",
|
|
|
|
userOnlineTime: userId
|
|
|
|
});
|
|
|
|
|
|
|
|
await startWhoIsOnlineMonitor()
|
|
|
|
//
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return res.status(200).json({ message: "User deleted" });
|
|
|
|
};
|