feat(backend) passando parametro para retornar os resultados raw

adriano 2023-10-23 09:12:16 -03:00
parent b15fd47c8b
commit bbc3dbb047
1 changed files with 127 additions and 94 deletions

View File

@ -11,8 +11,8 @@ import Queue from "../models/Queue";
import UserOnlineTime from "../models/UserOnlineTime"; import UserOnlineTime from "../models/UserOnlineTime";
import { Op, Sequelize, literal } from "sequelize"; import { Op, Sequelize, literal } from "sequelize";
import format from 'date-fns/format'; import format from "date-fns/format";
import ptBR from 'date-fns/locale/pt-BR'; import ptBR from "date-fns/locale/pt-BR";
import { splitDateTime } from "../helpers/SplitDateTime"; import { splitDateTime } from "../helpers/SplitDateTime";
import ListUserOnlineOffline from "../services/UserServices/ListUsersOnlineOfflineService"; import ListUserOnlineOffline from "../services/UserServices/ListUsersOnlineOfflineService";
import ListUserParamiterService from "../services/UserServices/ListUserParamiterService"; import ListUserParamiterService from "../services/UserServices/ListUserParamiterService";
@ -23,8 +23,7 @@ import CountTicketsByUserQueue from "../services/UserServices/CountTicketsByUser
import ShowQueuesByUser from "../services/UserServices/ShowQueuesByUser"; import ShowQueuesByUser from "../services/UserServices/ShowQueuesByUser";
// import { filter } from "bluebird";
// import { filter } from "bluebird";
import { getIO } from "../libs/socket"; import { getIO } from "../libs/socket";
import { Json } from "sequelize/types/lib/utils"; import { Json } from "sequelize/types/lib/utils";
@ -39,82 +38,125 @@ type IndexQuery = {
type ReportOnQueue = { type ReportOnQueue = {
userId: string; userId: string;
identifier: string; identifier: string;
} };
export const reportUserByDateStartDateEnd = async (req: Request, res: Response): Promise<Response> => {
export const reportUserByDateStartDateEnd = async (
req: Request,
res: Response
): Promise<Response> => {
if (req.user.profile !== "master" && req.user.profile !== "admin") { if (req.user.profile !== "master" && req.user.profile !== "admin") {
throw new AppError("ERR_NO_PERMISSION", 403); throw new AppError("ERR_NO_PERMISSION", 403);
} }
const { userId, startDate, endDate, pageNumber } = req.query as IndexQuery const { userId, startDate, endDate, pageNumber } = req.query as IndexQuery;
const { tickets, count, hasMore } = await ShowTicketReport({ userId, startDate, endDate, pageNumber }); const { tickets, count, hasMore } = await ShowTicketReport({
userId,
startDate,
endDate,
pageNumber
});
return res.status(200).json({ tickets, count, hasMore }); return res.status(200).json({ tickets, count, hasMore });
}; };
export const reportUserService = async (
export const reportUserService = async (req: Request, res: Response): Promise<Response> => { req: Request,
res: Response
): Promise<Response> => {
if (req.user.profile !== "master" && req.user.profile !== "admin") { if (req.user.profile !== "master" && req.user.profile !== "admin") {
throw new AppError("ERR_NO_PERMISSION", 403); throw new AppError("ERR_NO_PERMISSION", 403);
} }
const { userId, startDate, endDate } = req.query as IndexQuery const { userId, startDate, endDate } = req.query as IndexQuery;
let usersProfile = await ListUserParamiterService({
profile: "user",
raw: true
});
let usersProfile = await ListUserParamiterService({ profile: 'user' }) const sumUserOlineTime = await ShowUserServiceReport({
startDate,
endDate,
userId
});
const closedByUser = await ShowUserServiceReport({
startDate,
endDate,
ticketStatus: "closed",
userId
});
const openByUser = await ShowUserServiceReport({
startDate,
endDate,
ticketStatus: "open",
userId
});
const sumUserOlineTime = await ShowUserServiceReport({ startDate, endDate, userId }); let dateTime = splitDateTime(
const closedByUser = await ShowUserServiceReport({ startDate, endDate, ticketStatus: 'closed', userId }); new Date(format(new Date(), "yyyy-MM-dd HH:mm:ss", { locale: ptBR }))
const openByUser = await ShowUserServiceReport({ startDate, endDate, ticketStatus: 'open', userId }); );
const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate });
let dateTime = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }))) const openByUserOnQueue = await CountTicketsByUserQueue({
const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate }) startDate: startDate,
endDate: endDate,
status: "open",
clientChatStart: true
});
const openByUserOutQueue = await CountTicketsByUserQueue({
startDate: startDate,
endDate: endDate,
status: "open",
clientChatStart: false
});
const openByUserOnQueue = await CountTicketsByUserQueue({ startDate: startDate, endDate: endDate, status: 'open', clientChatStart: true }) const closedByUserOnQueue = await CountTicketsByUserQueue({
const openByUserOutQueue = await CountTicketsByUserQueue({ startDate: startDate, endDate: endDate, status: 'open', clientChatStart: false }) startDate: startDate,
endDate: endDate,
const closedByUserOnQueue = await CountTicketsByUserQueue({ startDate: startDate, endDate: endDate, status: 'closed', clientChatStart: true }) status: "closed",
const closedUserOutQueue = await CountTicketsByUserQueue({ startDate: startDate, endDate: endDate, status: 'closed', clientChatStart: false }) clientChatStart: true
});
const closedUserOutQueue = await CountTicketsByUserQueue({
startDate: startDate,
endDate: endDate,
status: "closed",
clientChatStart: false
});
// let openQueueInOut = openByUserOnQueue.concat(openByUserOutQueue) // let openQueueInOut = openByUserOnQueue.concat(openByUserOutQueue)
// let closedQueueInOut = closedByUserOnQueue.concat(closedUserOutQueue) // let closedQueueInOut = closedByUserOnQueue.concat(closedUserOutQueue)
const queuesByUser = await ShowQueuesByUser({ profile: "user" });
const queuesByUser = await ShowQueuesByUser({ profile: 'user' }) let openCloseOnQueue = openByUserOnQueue.concat(closedByUserOnQueue);
let openCloseOutQueue = openByUserOutQueue.concat(closedUserOutQueue);
let openCloseOnQueue = openByUserOnQueue.concat(closedByUserOnQueue)
let openCloseOutQueue = openByUserOutQueue.concat(closedUserOutQueue)
// console.log('onlineUsers: ',JSON.parse(JSON.stringify(onlineUsers))) // console.log('onlineUsers: ',JSON.parse(JSON.stringify(onlineUsers)))
// console.log('sumUserOlineTime: ', JSON.parse(JSON.stringify(sumUserOlineTime))) // console.log('sumUserOlineTime: ', JSON.parse(JSON.stringify(sumUserOlineTime)))
for (let i = 0; i < queuesByUser.length; i++) { for (let i = 0; i < queuesByUser.length; i++) {
queuesByUser[i].countOpen = 0;
queuesByUser[i].countOpen = 0 queuesByUser[i].countClosed = 0;
queuesByUser[i].countClosed = 0
for (let x = 0; x < openCloseOnQueue.length; x++) { for (let x = 0; x < openCloseOnQueue.length; x++) {
if ((queuesByUser[i].userId == openCloseOnQueue[x].userId) && if (
(queuesByUser[i].queueId == openCloseOnQueue[x].queueId && openCloseOnQueue[x].status == 'open')) { queuesByUser[i].userId == openCloseOnQueue[x].userId &&
queuesByUser[i].countOpen = openCloseOnQueue[x].totAttendance queuesByUser[i].queueId == openCloseOnQueue[x].queueId &&
openCloseOnQueue[x].status == "open"
) {
queuesByUser[i].countOpen = openCloseOnQueue[x].totAttendance;
} else if (
queuesByUser[i].userId == openCloseOnQueue[x].userId &&
queuesByUser[i].queueId == openCloseOnQueue[x].queueId &&
openCloseOnQueue[x].status == "closed"
) {
queuesByUser[i].countClosed = openCloseOnQueue[x].totAttendance;
} }
else if ((queuesByUser[i].userId == openCloseOnQueue[x].userId) &&
(queuesByUser[i].queueId == openCloseOnQueue[x].queueId && openCloseOnQueue[x].status == 'closed')) {
queuesByUser[i].countClosed = openCloseOnQueue[x].totAttendance
}
} }
} }
usersProfile.map((user: any) => { usersProfile.map((user: any) => {
let index = sumUserOlineTime.findIndex((e: any) => e.userId == user.id);
let index = sumUserOlineTime.findIndex((e: any) => e.userId == user.id)
if (index != -1) { if (index != -1) {
user.sumOnlineTime = sumUserOlineTime[index]; user.sumOnlineTime = sumUserOlineTime[index];
@ -122,101 +164,97 @@ export const reportUserService = async (req: Request, res: Response): Promise<Re
// console.log('user.sumOlineTime: 'user.sumOnlineTime) // console.log('user.sumOlineTime: 'user.sumOnlineTime)
} }
index = closedByUser.findIndex((e: any) => e.userId == user.id) index = closedByUser.findIndex((e: any) => e.userId == user.id);
if (index != -1) { if (index != -1) {
user.sumClosed = closedByUser[index]; user.sumClosed = closedByUser[index];
} }
index = openByUser.findIndex((e: any) => e.userId == user.id) index = openByUser.findIndex((e: any) => e.userId == user.id);
if (index != -1) { if (index != -1) {
user.sumOpen = openByUser[index] user.sumOpen = openByUser[index];
} }
// OPEN, CLOSED TICKETS STARTED BY USERS // OPEN, CLOSED TICKETS STARTED BY USERS
let openClosedOutQueue = {} let openClosedOutQueue = {};
let open = openCloseOutQueue.filter((e) => e.userId == user.id && e.status == 'open') let open = openCloseOutQueue.filter(
let closed = openCloseOutQueue.filter((e) => e.userId == user.id && e.status == 'closed') e => e.userId == user.id && e.status == "open"
);
let closed = openCloseOutQueue.filter(
e => e.userId == user.id && e.status == "closed"
);
openClosedOutQueue = { openClosedOutQueue = {
...openClosedOutQueue, ...openClosedOutQueue,
userId: user.id, userId: user.id,
countOpen: open && open.length > 0 ? open[0].totAttendance : 0, countOpen: open && open.length > 0 ? open[0].totAttendance : 0,
countClosed: closed && closed.length > 0 ? closed[0].totAttendance : 0 countClosed: closed && closed.length > 0 ? closed[0].totAttendance : 0
} };
user.openClosedOutQueue = openClosedOutQueue
user.openClosedOutQueue = openClosedOutQueue;
// OPEN, CLOSED TICKETS STARTED BY CLIENTS // OPEN, CLOSED TICKETS STARTED BY CLIENTS
let openClosedInQueue = queuesByUser.filter((e) => e.userId == user.id) let openClosedInQueue = queuesByUser.filter(e => e.userId == user.id);
if (openClosedInQueue && openClosedInQueue.length > 0) { if (openClosedInQueue && openClosedInQueue.length > 0) {
user.openClosedInQueue = openClosedInQueue user.openClosedInQueue = openClosedInQueue;
} }
index = onlineUsers.findIndex((e: any) => e.userId == user.id);
index = onlineUsers.findIndex((e: any) => e.userId == user.id)
if (index != -1) { if (index != -1) {
user.statusOnline = onlineUsers[index] user.statusOnline = onlineUsers[index];
} }
if (startDate.length > 0 && startDate.split('-').length == 3) { if (startDate.length > 0 && startDate.split("-").length == 3) {
let date = startDate.split('-') let date = startDate.split("-");
user.startDate = `${date[2]}/${date[1]}/${date[0]}` user.startDate = `${date[2]}/${date[1]}/${date[0]}`;
} }
if (endDate.length > 0 && endDate.split('-').length == 3) { if (endDate.length > 0 && endDate.split("-").length == 3) {
let date = endDate.split('-') let date = endDate.split("-");
user.endDate = `${date[2]}/${date[1]}/${date[0]}` user.endDate = `${date[2]}/${date[1]}/${date[0]}`;
} }
});
}) return res.status(200).json({ usersProfile: usersProfile });
return res.status(200).json({usersProfile: usersProfile});
}; };
export const reportMessagesUserByDateStartDateEnd = async (
req: Request,
export const reportMessagesUserByDateStartDateEnd = async (req: Request, res: Response): Promise<Response> => { res: Response
): Promise<Response> => {
if (req.user.profile !== "master" && req.user.profile !== "admin") { if (req.user.profile !== "master" && req.user.profile !== "admin") {
throw new AppError("ERR_NO_PERMISSION", 403); throw new AppError("ERR_NO_PERMISSION", 403);
} }
const { userId, startDate, endDate } = req.query as IndexQuery const { userId, startDate, endDate } = req.query as IndexQuery;
let data_query_messages = await ShowMessageReport(userId, startDate, endDate); let data_query_messages = await ShowMessageReport(userId, startDate, endDate);
for (var i = 0; i < data_query_messages.length; i++) { for (var i = 0; i < data_query_messages.length; i++) {
if (data_query_messages[i].fromMe) { if (data_query_messages[i].fromMe) {
data_query_messages[i].fromMe = 'Atendente' data_query_messages[i].fromMe = "Atendente";
} } else {
else { data_query_messages[i].fromMe = "Cliente";
data_query_messages[i].fromMe = 'Cliente'
} }
data_query_messages[i].id = (i + 1) data_query_messages[i].id = i + 1;
console.log('data_query_messages: ', data_query_messages[i]) console.log("data_query_messages: ", data_query_messages[i]);
} }
return res.status(200).json(data_query_messages); return res.status(200).json(data_query_messages);
}; };
export const reportOnQueue = async (
req: Request,
export const reportOnQueue = async (req: Request, res: Response): Promise<Response> => { res: Response
): Promise<Response> => {
// console.log(req.body) // console.log(req.body)
const { adminId, identifier, queueStatus, file } = req.body const { adminId, identifier, queueStatus, file } = req.body;
const io = getIO(); const io = getIO();
io.emit("queryOnQueueStatus", { io.emit("queryOnQueueStatus", {
@ -229,10 +267,5 @@ export const reportOnQueue = async (req: Request, res: Response): Promise<Respon
} }
}); });
return res.status(200).json({ message: 'ok' }) return res.status(200).json({ message: "ok" });
}; };