From d74e3a7a974c46faabcd30eadc6d755472884ef9 Mon Sep 17 00:00:00 2001 From: adriano Date: Sun, 15 May 2022 23:48:06 -0300 Subject: [PATCH] commit antes da limpeza das variaveis nao utilizada --- backend/src/controllers/ReportController.ts | 91 +- backend/src/controllers/TicketController.ts | 25 +- backend/src/controllers/UserController.ts | 102 ++- .../helpers/OnlineReporEmiterInfoByUser.ts | 26 + .../helpers/SchedulingNotifySendMessage.ts | 34 +- backend/src/helpers/SumOlineTimeNow.ts | 32 + backend/src/helpers/WhoIsOnlineMonitor.ts | 229 +++-- backend/src/libs/socket.ts | 194 ++-- backend/src/routes/reportRoutes.ts | 2 + backend/src/routes/userRoutes.ts | 7 +- backend/src/server.ts | 4 +- .../TicketServices/CreateTicketService.ts | 26 +- .../CreateOrUpdateOnlineUserService.ts | 192 ++-- .../UserServices/DeleteUserService.ts | 7 +- .../ListUsersOnlineOfflineService.ts | 7 + .../services/UserServices/ListUsersService.ts | 57 +- .../UserServices/ShowUserServiceReport.ts | 134 ++- .../components/NotificationsPopOver/index.js | 21 + frontend/src/components/ReportModal/index.js | 126 +++ frontend/src/pages/Report/index.js | 853 +++++++++++++----- frontend/src/pages/SchedulesReminder/index.js | 587 ++++++------ 21 files changed, 1916 insertions(+), 840 deletions(-) create mode 100644 backend/src/helpers/OnlineReporEmiterInfoByUser.ts create mode 100644 backend/src/helpers/SumOlineTimeNow.ts create mode 100644 frontend/src/components/ReportModal/index.js diff --git a/backend/src/controllers/ReportController.ts b/backend/src/controllers/ReportController.ts index 817b068..f0adbe4 100644 --- a/backend/src/controllers/ReportController.ts +++ b/backend/src/controllers/ReportController.ts @@ -10,13 +10,15 @@ import User from "../models/User"; import Queue from "../models/Queue"; import UserOnlineTime from "../models/UserOnlineTime"; -import { Op, Sequelize } from "sequelize"; +import { Op, Sequelize,literal } from "sequelize"; import format from 'date-fns/format'; import ptBR from 'date-fns/locale/pt-BR'; import { splitDateTime } from "../helpers/SplitDateTime"; import ListUserOnlineOffline from "../services/UserServices/ListUsersOnlineOfflineService"; import ListUserParamiterService from "../services/UserServices/ListUserParamiterService"; - + +import ShowUserServiceReport from "../services/UserServices/ShowUserServiceReport"; + type IndexQuery = { userId: string; @@ -27,12 +29,12 @@ type IndexQuery = { export const reportUserByDateStartDateEnd = async (req: Request, res: Response): Promise => { - if (req.user.profile !== "master" && req.user.profile !== "admin") { throw new AppError("ERR_NO_PERMISSION", 403); - } + } - const { userId, startDate, endDate } = req.query as IndexQuery + const { userId, startDate, endDate } = req.query as IndexQuery + const data_query = await ShowTicketReport(userId, startDate, endDate); @@ -41,6 +43,85 @@ export const reportUserByDateStartDateEnd = async (req: Request, res: Response): }; +export const reportUserService= async (req: Request, res: Response): Promise => { + + if (req.user.profile !== "master" && req.user.profile !== "admin") { + throw new AppError("ERR_NO_PERMISSION", 403); + } + const { userId, startDate, endDate } = req.query as IndexQuery + + + //test del + console.log('startDate: ', startDate) + console.log('endDate: ', endDate) + console.log('userId: ', userId) + + 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}); + + let dateTime = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }))) + const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate }) + + console.log('sumUserOlineTime: ',sumUserOlineTime ) + console.log('closedByUser: ',closedByUser ) + console.log('openByUser: ',openByUser ) + console.log('usersProfile: ', usersProfile) + console.log('') + + + usersProfile.map((user:any) => { + + let index = sumUserOlineTime.findIndex((e:any) => e.userId == user.id) + + if (index != -1) { + user.sumOnlineTime = sumUserOlineTime[index]; + } + + index = closedByUser.findIndex((e:any) => e.userId == user.id) + + if (index != -1) { + user.sumClosed = closedByUser[index]; + } + + index = openByUser.findIndex((e:any) => e.userId == user.id) + + if (index != -1) { + user.sumOpen = openByUser[index] + } + + + index = onlineUsers.findIndex((e:any) => e.userId == user.id) + + if (index != -1) { + user.statusOnline = onlineUsers[index] + } + + if(startDate.length>0 && startDate.split('-').length == 3){ + let date = startDate.split('-') + user.startDate = `${date[2]}/${date[1]}/${date[0]}` + } + + if(endDate.length>0 && endDate.split('-').length == 3){ + let date = endDate.split('-') + user.endDate = `${date[2]}/${date[1]}/${date[0]}` + } + + }) + console.log('') + console.log('usersProfile2: ',usersProfile) + // + + + // const data_query = await ShowTicketReport(userId, startDate, endDate); + + return res.status(200).json(usersProfile); + +}; + + export const reportMessagesUserByDateStartDateEnd = async (req: Request, res: Response): Promise => { diff --git a/backend/src/controllers/TicketController.ts b/backend/src/controllers/TicketController.ts index 3f73353..8f7558e 100644 --- a/backend/src/controllers/TicketController.ts +++ b/backend/src/controllers/TicketController.ts @@ -13,7 +13,11 @@ import CreateSchedulingNotifyService from "../services/SchedulingNotifyServices import ListSchedulingNotifyContactService from "../services/SchedulingNotifyServices/ListSchedulingNotifyContactService"; import {isScheduling} from "../helpers/CheckSchedulingReminderNotify" - + +import ptBR from 'date-fns/locale/pt-BR'; +import { splitDateTime } from "../helpers/SplitDateTime"; +import format from 'date-fns/format'; + type IndexQuery = { @@ -37,6 +41,8 @@ interface TicketData { import ListStatusChatEndService from "../services/StatusChatEndService/ListStatusChatEndService"; import Ticket from "../models/Ticket"; +import ShowUserServiceReport from "../services/UserServices/ShowUserServiceReport"; +import TicketEmiterSumOpenClosedByUser from "../helpers/OnlineReporEmiterInfoByUser"; export const index = async (req: Request, res: Response): Promise => { const { @@ -140,6 +146,8 @@ export const update = async ( req: Request, res: Response ): Promise = const { ticketId } = req.params; + const userOldInfo = await Ticket.findByPk(ticketId) + let ticket2 = {} if(req.body['status'] === "closed"){ @@ -151,6 +159,8 @@ export const update = async ( req: Request, res: Response ): Promise = ticketId }); + + /////////////////////////////// const whatsapp = await ShowWhatsAppService(ticket.whatsappId); @@ -208,6 +218,19 @@ export const update = async ( req: Request, res: Response ): Promise = } + + // test del + + if(userOldInfo){ + + const dateToday = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }))) + + TicketEmiterSumOpenClosedByUser(userOldInfo.userId.toString(), dateToday.fullDate, dateToday.fullDate) + + } + + // + return res.status(200).json(ticket2); }; diff --git a/backend/src/controllers/UserController.ts b/backend/src/controllers/UserController.ts index a04de26..bc08d46 100644 --- a/backend/src/controllers/UserController.ts +++ b/backend/src/controllers/UserController.ts @@ -10,35 +10,66 @@ import UpdateUserService from "../services/UserServices/UpdateUserService"; import ShowUserService from "../services/UserServices/ShowUserService"; import DeleteUserService from "../services/UserServices/DeleteUserService"; +import ListUserParamiterService from "../services/UserServices/ListUserParamiterService" +import User from "../models/User"; + +import { startWhoIsOnlineMonitor, stopWhoIsOnlineMonitor } from "../helpers/WhoIsOnlineMonitor" +import UserOnlineTIme from '../models/UserOnlineTime' + type IndexQuery = { searchParam: string; pageNumber: string; + profile?: string; }; export const index = async (req: Request, res: Response): Promise => { - const { searchParam, pageNumber } = req.query as IndexQuery; + const { searchParam, pageNumber, profile } = req.query as IndexQuery; const { users, count, hasMore } = await ListUsersService({ searchParam, - pageNumber + pageNumber, + profile }); - if(req.user.profile!=='master'){ - - let auxUsers: Array = []; + if (req.user.profile !== 'master') { - for (var user of users) { - if(user.profile!=='master'){ + let auxUsers: Array = []; + + for (var user of users) { + if (user.profile !== 'master') { auxUsers.push(user) - } - } - - return res.json({ users: auxUsers, count, hasMore }); - } + } + } + + return res.json({ users: auxUsers, count, hasMore }); + } return res.json({ users, count, hasMore }); + + + + // const { users, count, hasMore } = await ListUsersService({ + // searchParam, + // pageNumber + // }); + + // if(req.user.profile!=='master'){ + + // let auxUsers: Array = []; + + // 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 }); }; + export const store = async (req: Request, res: Response): Promise => { const { email, password, name, profile, queueIds } = req.body; @@ -62,6 +93,10 @@ export const store = async (req: Request, res: Response): Promise => { user }); + + await stopWhoIsOnlineMonitor() + await startWhoIsOnlineMonitor() + return res.status(200).json(user); }; @@ -73,6 +108,35 @@ export const show = async (req: Request, res: Response): Promise => { return res.status(200).json(user); }; + +export const logoutUser = async (req: Request, res: Response): Promise => { + const { userId } = req.params; + + //const user = await ShowUserService(userId); + + console.log('userId: ', userId) + + //test del + 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({}); +}; + + export const update = async ( req: Request, res: Response @@ -105,13 +169,27 @@ export const remove = async ( throw new AppError("ERR_NO_PERMISSION", 403); } + await DeleteUserService(userId); + const io = getIO(); io.emit("user", { action: "delete", userId }); + + //test del + await stopWhoIsOnlineMonitor() + + io.emit("onlineStatus", { + action: "delete", + userOnlineTime: userId + }); + + await startWhoIsOnlineMonitor() + // + return res.status(200).json({ message: "User deleted" }); }; diff --git a/backend/src/helpers/OnlineReporEmiterInfoByUser.ts b/backend/src/helpers/OnlineReporEmiterInfoByUser.ts new file mode 100644 index 0000000..382d001 --- /dev/null +++ b/backend/src/helpers/OnlineReporEmiterInfoByUser.ts @@ -0,0 +1,26 @@ +import ptBR from 'date-fns/locale/pt-BR'; +import { splitDateTime } from "../helpers/SplitDateTime"; +import format from 'date-fns/format'; +import ShowUserServiceReport from '../services/UserServices/ShowUserServiceReport'; +import { getIO } from "../libs/socket"; + + +const TicketEmiterSumOpenClosedByUser = async (userId: string, startDate: string, endDate: string) => { + + const openByUser: any[] = await ShowUserServiceReport({ startDate: startDate, endDate:endDate, ticketStatus: 'open', userId: userId.toString() }); + const closedByUser: any[] = await ShowUserServiceReport({ startDate: endDate, endDate:endDate, ticketStatus: 'closed', userId: userId.toString() }); + + //openByUser : [ { id: 13, status: 'online' } ] + + const io = getIO(); + io.emit("onlineStatus", { + action: "update", + userOnlineTime: { + sumOpen: openByUser.length > 0 ? openByUser[0] : { userId: userId, count: '' }, + sumClosed: closedByUser.length > 0 ? closedByUser[0] : { userId: userId, count: '' }, + } + }); + +}; + +export default TicketEmiterSumOpenClosedByUser \ No newline at end of file diff --git a/backend/src/helpers/SchedulingNotifySendMessage.ts b/backend/src/helpers/SchedulingNotifySendMessage.ts index 0622a6e..64f3d6d 100644 --- a/backend/src/helpers/SchedulingNotifySendMessage.ts +++ b/backend/src/helpers/SchedulingNotifySendMessage.ts @@ -25,23 +25,27 @@ const monitor = async () => { //console.log(dateParm); - - const { schedulingNotifies, count, hasMore } = await ListSchedulingNotifyService({ searchParam: dateParm, pageNumber: "1" }); + try { + + const { schedulingNotifies, count, hasMore } = await ListSchedulingNotifyService({ searchParam: dateParm, pageNumber: "1" }); - if(schedulingNotifies.length){ - - const ticket = await ShowTicketService(schedulingNotifies[0].ticketId); - - SetTicketMessagesAsRead(ticket); + if(schedulingNotifies.length){ + + const ticket = await ShowTicketService(schedulingNotifies[0].ticketId); + + SetTicketMessagesAsRead(ticket); + + await SendWhatsAppMessage({ + body: schedulingNotifies[0].message, ticket + }); + + DeleteSchedulingNotifyService(schedulingNotifies[0].id) + + } - await SendWhatsAppMessage({ - body: schedulingNotifies[0].message, ticket - }); - - DeleteSchedulingNotifyService(schedulingNotifies[0].id) - - } - + } catch (error) { + console.log('>>> SchedulingNotifiySendMessage.ts error: ', error) + } }; diff --git a/backend/src/helpers/SumOlineTimeNow.ts b/backend/src/helpers/SumOlineTimeNow.ts new file mode 100644 index 0000000..2d4d781 --- /dev/null +++ b/backend/src/helpers/SumOlineTimeNow.ts @@ -0,0 +1,32 @@ + +import { addHours, addMinutes, addSeconds, intervalToDuration, add } from "date-fns"; + + const sumOnlineTimeNow = (oldOnlineTimeSum:any) => { + + let onlineTime = new Date() + + onlineTime.setUTCHours(new Date(oldOnlineTimeSum.onlineTime).getHours()) + onlineTime.setUTCMinutes(new Date(oldOnlineTimeSum.onlineTime).getMinutes()) + onlineTime.setUTCSeconds(new Date(oldOnlineTimeSum.onlineTime).getSeconds()) + + let newtTime = intervalToDuration({ start: new Date(oldOnlineTimeSum.updatedAt), end: new Date() }) + + + if (newtTime.hours && +newtTime.hours > 0) { + onlineTime = addHours(onlineTime, newtTime.hours) + } + if (newtTime.minutes && +newtTime.minutes > 0) { + onlineTime = addMinutes(onlineTime, newtTime.minutes) + } + if (newtTime.seconds && +newtTime.seconds > 0) { + onlineTime = addSeconds(onlineTime, newtTime.seconds) + } + + const isoDate = new Date(onlineTime); + const newOnlinetime = isoDate.toJSON().slice(0, 19).replace('T', ' '); + console.log('sum new online time: ', newOnlinetime) + + return newOnlinetime +} + +export default sumOnlineTimeNow \ No newline at end of file diff --git a/backend/src/helpers/WhoIsOnlineMonitor.ts b/backend/src/helpers/WhoIsOnlineMonitor.ts index 2f6381d..a657bcb 100644 --- a/backend/src/helpers/WhoIsOnlineMonitor.ts +++ b/backend/src/helpers/WhoIsOnlineMonitor.ts @@ -11,6 +11,13 @@ import format from 'date-fns/format'; import ptBR from 'date-fns/locale/pt-BR'; import ListUserOnlineOffline from "../services/UserServices/ListUsersOnlineOfflineService"; import { check } from "prettier"; +import { addHours, addMinutes, addSeconds, intervalToDuration, add } from "date-fns"; +import { date } from "faker"; + +import sumOnlineTimeNow from '../helpers/SumOlineTimeNow' +import UserOnlineTime from "../models/UserOnlineTime"; + + let whoIsOnline_monitor: any; // const listUserId:any[] = [{'id':8, status: 'offline'},{'id':3, status: 'offline'},{'id':5, status: 'offline'}] @@ -19,20 +26,48 @@ let count = 0 let countTest = 0 let uuid: any = 0 let dateTime = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }))) -let checked: boolean = false -let count2 = 0 +let timeInterval = 5 +let lstOnline: any = [] +let deleted: boolean = false; + const setOfflineAllUsers = async () => { - const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate, status: 'online' }) + // const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate, status: 'online' }) - console.log(' onlineUsers: ', onlineUsers) + // console.log('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX onlineUsers: ', onlineUsers) - if (onlineUsers.length > 0) { - onlineUsers.forEach(async user => { - await createOrUpdateOnlineUserService({ userId: user.userId, status: 'offline' }) - }); + // if (onlineUsers.length > 0) { + // onlineUsers.forEach(async user => { + // await createOrUpdateOnlineUserService({ userId: user.userId, status: 'offline' }) + // }); + // } +} + +const emitterOnline = (user: any, status: string, showOnlineTime: boolean = true) => { + + if (!user.id) return + + let newOnlinetime = sumOnlineTimeNow(user) + + let onlineTime: any = { + userId: user.id, + status: status, + onlineTime: newOnlinetime, } + + if (!showOnlineTime) { + onlineTime = { + userId: user.id, + status: status + } + } + + const io = getIO(); + io.emit("onlineStatus", { + action: "update", + userOnlineTime: onlineTime + }); } @@ -40,46 +75,13 @@ const monitor = async () => { const test = require('./../libs/socket'); - console.log(' TEST OBJ', test.ob) - - //test del - - if(countTest > 5){ - countTest = 0 - } - - if (countTest == 0) { - - //console.log('NEW USERS QUERY EMITER!') - - listUserId = await ListUserParamiterService({ profile: 'user' }) - - //console.log('----------- listUserId: ', listUserId) - - } - - countTest++ - // - - if(listUserId.length == 0){ - return; - } - - - listUserId.forEach((u) => { - const io = getIO(); - io.emit("isOnline", { action: "online", userId: u.id }); - }); - - - if(test.ob){ + if (test.ob) { if (count > 1) { count = 0 } if (count == 0) { - // uuid = test.uuid uuid = test.ob.uuid } @@ -87,57 +89,138 @@ const monitor = async () => { if (uuid) { if (uuid == test.ob.uuid) { - console.log('ALL USERS CLIENTS OFFLINE 1...........') + console.log('ALL USERS CLIENTS OFFLINE 1...........') - await setOfflineAllUsers() + //await setOfflineAllUsers() test.ob.listOnline = [] - test.ob.listOnline2 = [] + test.ob.listOnlineAux = [] test.ob.uuid = undefined - checked = true + + } + + } + + } else { + console.log(' * TEST OBJ', test.ob) + + + test.ob.listOnline.forEach(async (el: any) => { + + + const indexAux = lstOnline.findIndex((e: any) => e.id == el.id) + + + if (indexAux == -1) { + console.log(' entrou indexAux: ', indexAux) + + const userOnline = await createOrUpdateOnlineUserService({ userId: el.id, status: 'online' }) + + if (userOnline) { + + el.onlineTime = userOnline.onlineTime + el.updatedAt = userOnline.updatedAt, + + console.log(' * CREATED OR UPDATED USER TO ONLINE: ', userOnline.userId) + + lstOnline.push({ 'id': el.id, 'status': 'online' }) + } + + } + + if (el.status == 'waiting...') { + emitterOnline(el, el.status, false) } else { - console.log('*usersOnline: ', test.ob.listOnline) - checked = false - count2 = 0 + emitterOnline(el, el.status) } - } - else { - console.log('ALL USERS CLIENTS OFFLINE 2...........') - if(!checked){ - await setOfflineAllUsers() - checked = true + + + }); + + + console.log('----------- 1 lstOnline: ', lstOnline) + + + let difference = lstOnline.filter((x: any) => !test.ob.listOnline.map((e: any) => e.id).includes(x.id)); + + console.log(' * diference: ', difference) + + difference.forEach(async (e: any) => { + + const index = lstOnline.findIndex((x: any) => x.id == e.id) + + if (index != -1) { + + lstOnline.splice(index, 1) + + console.log('----------- 2 lstOnline: ', lstOnline) + + const userOnline = await createOrUpdateOnlineUserService({ userId: e.id, status: 'offline' }) + + if (userOnline) { + console.log(' * UPDATED USER TO OFFLINE: ', userOnline.userId) + } + + } - - } + + }) + + } - - - console.log(' | count: ',count, ' | test.ob.listOnline: ', test.ob.listOnline) + count++ } - else { - console.log('ALL USERS CLIENTS OFFLINE 3...........') - if(!checked && count2>10){ - await setOfflineAllUsers() - checked = true - } - if(count2 < 12){ - count2++ - } - - console.log('>>>>>>>>>>>>>>>>>>> count2: ', count2) + if (countTest > 5) { + countTest = 0 } + + if (countTest == 0) { + try { + console.log(' Carregando usuárion no listUserId...') + listUserId = await ListUserParamiterService({ profile: 'user' }) + + } catch (error) { + console.log('There was an erro on ListUserParamiterService: ', error) + return + } + + } + + countTest = 1 + + listUserId.forEach((u) => { + + // console.log(' *** listUserId: ', listUserId) + const io = getIO(); + io.emit("isOnline", { action: "online", userId: u.id }); + }); + + + console.log('***************** TIME INTERVAL: ', timeInterval) + + }; -export const startWhoIsOnlinegMonitor = async (mileseconds: number) => { +export const startWhoIsOnlineMonitor = async (mileseconds?: number) => { - whoIsOnline_monitor = setInterval(monitor, mileseconds) + listUserId = [] + count = 0 + countTest = 0 + uuid = 0 + + if (mileseconds) { + timeInterval = mileseconds + } + + + whoIsOnline_monitor = setInterval(monitor, timeInterval) } diff --git a/backend/src/libs/socket.ts b/backend/src/libs/socket.ts index 384fb6a..55bcd33 100644 --- a/backend/src/libs/socket.ts +++ b/backend/src/libs/socket.ts @@ -5,7 +5,7 @@ import { logger } from "../utils/logger"; import { v4 as uuidv4 } from 'uuid'; import ListUserParamiterService from "../services/UserServices/ListUserParamiterService"; - +import { addHours, addMinutes, addSeconds, intervalToDuration, add } from "date-fns"; let io: SocketIO; @@ -15,15 +15,20 @@ import { splitDateTime } from "../helpers/SplitDateTime"; import format from 'date-fns/format'; import ptBR from 'date-fns/locale/pt-BR'; import ListUserOnlineOffline from "../services/UserServices/ListUsersOnlineOfflineService"; - -let count: number = 0 + +let count: number = 0 let listOnline: any[] = [] -let listOnline2: any[] = [] - +let listOnlineAux: any[] = [] let countOnline: number = 0 -//let obj = { DESCENT_STEPS: 5000, ALPHA: 0.0005} +let obj: any = { listOnline: [], uuid: null, listOnlineAux: [] } + + + + +let lstOnline: any[] = [] +let lstOnlineAux: any[] = [] +let lstTry: any[] = [] -let obj:any = { listOnline: [], listOnline2: [], uuid: null} let dateTime = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }))) @@ -31,6 +36,9 @@ let dateTime = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', // + + + export const initIO = (httpServer: Server): SocketIO => { io = new SocketIO(httpServer, { cors: { @@ -43,69 +51,135 @@ export const initIO = (httpServer: Server): SocketIO => { logger.info("Client Connected"); - socket.on("online", async (userId: string) => { + socket.on("online", (userId: any) => { - - console.log(' ----------------------------- listOnlie: ', obj.listOnline) + obj.uuid = uuidv4() - const indexUser = obj.listOnline.findIndex((e:any) => e.id == userId) + if (userId.logoutUserId) { - if (indexUser == -1) { - obj.listOnline.push({ 'id': userId }) + let index = lstOnline.findIndex((x: any) => x.id == userId.logoutUserId) - const onlineUser = await createOrUpdateOnlineUserService({ userId: userId, status: 'online' }) + if (index != -1) { - const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate, status: 'online' }) - - countOnline = onlineUsers.length - - console.log(' PUSHED NEW ID: ', userId) - } - - - const index = obj.listOnline2.findIndex((e:any) => e.id == userId) - - if (index == -1) { - obj.listOnline2.push({ 'id': userId }) - } - - - if (count > (countOnline - 1)) { - - var difference = obj.listOnline.filter((x:any) => !obj.listOnline2.map((e:any) => e.id).includes(x.id)); - - console.log('############> difference: ', difference) - - if (difference.length > 0) { - - difference.forEach(async (x:any) => { - - let index = obj.listOnline.findIndex((e:any) => e.id == x.id) - - if (index != -1) { - obj.listOnline.splice(index, 1) - const onlineUser = await createOrUpdateOnlineUserService({ userId: x.id, status: 'offline' }) - - const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate, status: 'online' }) - - countOnline = onlineUsers.length - } - }) + lstOnline.splice(index, 1) } - obj.listOnline2 = [] - count = 0 + + index = lstOnlineAux.findIndex((x: any) => x.id == userId.logoutUserId) + + if (index != -1) { + + lstOnlineAux.splice(index, 1) + + } + + return } - - - obj.uuid = uuidv4() - exports.ob = obj + if (lstOnline.length == 0) { + + const index = listOnlineAux.findIndex((e: any) => e.id == userId) + + if (index == -1) { + listOnlineAux.push({ 'id': userId }) + } + else { + console.log(' -------------- PULO FORA') + return + } + + lstOnline.push({ 'id': userId, 'status': 'online', 'try': 0 }) + + lstOnlineAux.push({ 'id': userId }) + console.log(' 1 PUSHED NEW USER ID 1: ', userId) + + obj.listOnline = lstOnline + + } + else { + + const indexAux = lstOnlineAux.findIndex((e: any) => e.id == userId) + + if (indexAux == -1) { + lstOnlineAux.push({ 'id': userId }) + } + + const index = lstOnline.findIndex((e: any) => e.id == userId) + + if (index == -1) { + + + lstOnline.push({ 'id': userId, 'status': 'online', 'try': 0 }) + + + console.log(' 2 PUSHED NEW USER ID: ', userId) + + obj.listOnline = lstOnline + } + else { + + if (countOnline > (lstOnline.length - 1)) { + + lstOnline.forEach((x: any) => { + if (lstOnlineAux.map((e: any) => e.id).includes(x.id)) { + x.try = 0 + x.status = 'online' + } + }) + + + var difference = lstOnline.filter((x: any) => !lstOnlineAux.map((e: any) => e.id).includes(x.id)); + + //console.log('>>>>>>>>>>> difference: ', difference) + + if (difference.length > 0) { + + difference.forEach((e) => { + + e.try += 1 + + if (e.try > 1) { + e.status = 'waiting...' + } + + if (e.try > 3) { + + const index = lstOnline.findIndex((x: any) => x.id == e.id) + + if (index != -1) { + + lstOnline.splice(index, 1) + + } + } + + }) + + console.log(' difference2: ', difference) + + } + obj.listOnline = lstOnline + obj.listOnlineAux = lstOnlineAux + + // console.log(' >>>>>> lstOnline: ', lstOnline) + // console.log('') + // console.log(' >>>>>> lstOnlineAux: ', lstOnlineAux) + + lstOnlineAux = [] + listOnlineAux = [] + + countOnline = -1 + } + + + countOnline++ + } + + } + + exports.ob = obj - count++ - - }); @@ -141,4 +215,4 @@ export const getIO = (): SocketIO => { // exports.listOnlineUsers = listUserId // exports.listUserId - \ No newline at end of file + diff --git a/backend/src/routes/reportRoutes.ts b/backend/src/routes/reportRoutes.ts index c3a751c..857a6ba 100644 --- a/backend/src/routes/reportRoutes.ts +++ b/backend/src/routes/reportRoutes.ts @@ -9,6 +9,8 @@ const reportRoutes = express.Router(); reportRoutes.get("/reports", isAuth, ReportController.reportUserByDateStartDateEnd); +reportRoutes.get("/reports/user/services", isAuth, ReportController.reportUserService); + reportRoutes.get("/reports/messages", isAuth, ReportController.reportMessagesUserByDateStartDateEnd); export default reportRoutes; diff --git a/backend/src/routes/userRoutes.ts b/backend/src/routes/userRoutes.ts index e19f685..8dede31 100644 --- a/backend/src/routes/userRoutes.ts +++ b/backend/src/routes/userRoutes.ts @@ -6,15 +6,16 @@ import * as UserController from "../controllers/UserController"; const userRoutes = Router(); +userRoutes.get("/users", isAuth, UserController.index); -userRoutes.get("/users", isAuth, UserController.index); - -userRoutes.post("/users", isAuth, UserController.store); +userRoutes.post("/users", isAuth, UserController.store); userRoutes.put("/users/:userId", isAuth, UserController.update); userRoutes.get("/users/:userId", isAuth, UserController.show); +userRoutes.get("/users/logout/:userId", isAuth, UserController.logoutUser); + userRoutes.delete("/users/:userId", isAuth, UserController.remove); export default userRoutes; diff --git a/backend/src/server.ts b/backend/src/server.ts index 37a78aa..ac9bccc 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -5,7 +5,7 @@ import { logger } from "./utils/logger"; import { StartAllWhatsAppsSessions } from "./services/WbotServices/StartAllWhatsAppsSessions"; import { startSchedulingMonitor } from "./helpers/SchedulingNotifySendMessage" -import { startWhoIsOnlinegMonitor } from "./helpers/WhoIsOnlineMonitor" +import { startWhoIsOnlineMonitor } from "./helpers/WhoIsOnlineMonitor" const server = app.listen(process.env.PORT, () => { logger.info(`Server started on port: ${process.env.PORT}`); @@ -16,4 +16,4 @@ StartAllWhatsAppsSessions(); gracefulShutdown(server); startSchedulingMonitor(5000) -startWhoIsOnlinegMonitor(5000) +startWhoIsOnlineMonitor(2000) diff --git a/backend/src/services/TicketServices/CreateTicketService.ts b/backend/src/services/TicketServices/CreateTicketService.ts index ee3fbe2..ea66db2 100644 --- a/backend/src/services/TicketServices/CreateTicketService.ts +++ b/backend/src/services/TicketServices/CreateTicketService.ts @@ -4,6 +4,15 @@ import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp"; import Ticket from "../../models/Ticket"; import ShowContactService from "../ContactServices/ShowContactService"; + +import { getIO } from "../../libs/socket"; +import ShowUserServiceReport from "../UserServices/ShowUserServiceReport"; + +import format from 'date-fns/format'; +import ptBR from 'date-fns/locale/pt-BR'; +import { splitDateTime } from "../../helpers/SplitDateTime"; +import TicketEmiterSumOpenClosedByUser from "../../helpers/OnlineReporEmiterInfoByUser"; + interface Request { contactId: number; status: string; @@ -15,7 +24,7 @@ const CreateTicketService = async ({ status, userId }: Request): Promise => { - const defaultWhatsapp = await GetDefaultWhatsApp(userId); + const defaultWhatsapp = await GetDefaultWhatsApp(userId); await CheckContactOpenTickets(contactId); @@ -34,6 +43,21 @@ const CreateTicketService = async ({ throw new AppError("ERR_CREATING_TICKET"); } + + //test del + // 2022-05-11T05:20:33.000Z, + // const dateToday = ticket.createdAt.toISOString().split('T')[0] + + const dateToday = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }))) + + //openByUser : [ { id: 13, status: 'online' } ] + + TicketEmiterSumOpenClosedByUser(userId.toString(), dateToday.fullDate, dateToday.fullDate) + + + // + + return ticket; }; diff --git a/backend/src/services/UserServices/CreateOrUpdateOnlineUserService.ts b/backend/src/services/UserServices/CreateOrUpdateOnlineUserService.ts index b79f496..b7fbe80 100644 --- a/backend/src/services/UserServices/CreateOrUpdateOnlineUserService.ts +++ b/backend/src/services/UserServices/CreateOrUpdateOnlineUserService.ts @@ -4,113 +4,183 @@ import { splitDateTime } from "../../helpers/SplitDateTime"; import { Op, Sequelize } from "sequelize"; import { addHours, addMinutes, addSeconds, intervalToDuration, add } from "date-fns"; import format from 'date-fns/format'; -import ptBR from 'date-fns/locale/pt-BR'; +import ptBR from 'date-fns/locale/pt-BR'; import { stat } from "fs"; +import { getIO } from "../../libs/socket"; -interface Request { +import User from '../../models/User' + +interface Request { userId: string | number, - status: string, + status: string, +} + +const formatDateTimeString = (date: Date) => { + + let newDate = new Date() + newDate.setUTCHours(date.getHours()) + newDate.setUTCMinutes(date.getMinutes()) + newDate.setUTCSeconds(date.getSeconds()) + + const newD = new Date(newDate); + const newDateString = newD.toJSON().slice(0, 19).replace('T', ' '); + + return newDateString } -const CreateOrUpdateUserOnlineTime = async ({ - userId, - status, -}: Request): Promise => { +const CreateOrUpdateUserOnlineTime = async ({ + userId, + status, +}: Request): Promise => { + - let userOnlineTime = null; - // let dateTime = splitDateTime(new Date(new Date() + 'UTC')) - let dateTime = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }) )) - + const io = getIO(); + let userOnlineTime:any = null; - userOnlineTime = await UserOnlineTime.findOne({ - where: { + const user = await User.findOne({ where: { id: userId } }); + + if (!user) { + console.log(' yyyyyyyyyyyyyyyyyyyyyyyyyy não tem mais esse usuario id: ', userId) + return userOnlineTime + } + + + let dateTime = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }))) + + userOnlineTime = await UserOnlineTime.findOne({ + where: { [Op.and]: [ { - userId: userId + userId: userId }, - { + { "$createdAt$": Sequelize.where(Sequelize.fn("date", Sequelize.col("createdAt")), `${dateTime.fullDate}`) - }, - ] - } - }); + }, + ] + } + }); - if(userOnlineTime){ - try{ + if (userOnlineTime) { + try { let oldStatus = userOnlineTime.status - - if(oldStatus=='online' && status === 'offline'){ - //updatedAt - let newtTime = intervalToDuration({ start: userOnlineTime.updatedAt, end: new Date()}) - console.log('TESTANDO INTERVAL DURATION: ', newtTime) - console.log('hours: ', newtTime.hours, ' | minutes: ', newtTime.minutes, ' | seconds: ', newtTime.seconds) - + + console.log('>>>>>>>>> oldStatus: ', oldStatus, ' | new status', status) + + if (oldStatus == 'online' && status === 'offline') { + //updatedAt + let newtTime = intervalToDuration({ start: userOnlineTime.updatedAt, end: new Date() }) + console.log('TESTANDO INTERVAL DURATION: ', newtTime) + + console.log('hours: ', newtTime.hours, ' | minutes: ', newtTime.minutes, ' | seconds: ', newtTime.seconds) + let onlineTime = new Date() - + onlineTime.setUTCHours(userOnlineTime.onlineTime.getHours()) onlineTime.setUTCMinutes(userOnlineTime.onlineTime.getMinutes()) onlineTime.setUTCSeconds(userOnlineTime.onlineTime.getSeconds()) - - console.log('userOnlineTime.onlineTime: ',userOnlineTime.onlineTime) - console.log('userOnlineTime.onlineTime.getHours(): ',userOnlineTime.onlineTime.getHours()) - console.log('userOnlineTime.onlineTime.getMinutes(): ',userOnlineTime.onlineTime.getMinutes()) - console.log('userOnlineTime.onlineTime.getSeconds(): ',userOnlineTime.onlineTime.getSeconds()) - console.log('online time 3: ', onlineTime) - - - if(newtTime.hours && +newtTime.hours > 0){ - onlineTime = addHours(onlineTime, newtTime.hours) + console.log('userOnlineTime.onlineTime: ', userOnlineTime.onlineTime) + console.log('userOnlineTime.onlineTime.getHours(): ', userOnlineTime.onlineTime.getHours()) + console.log('userOnlineTime.onlineTime.getMinutes(): ', userOnlineTime.onlineTime.getMinutes()) + console.log('userOnlineTime.onlineTime.getSeconds(): ', userOnlineTime.onlineTime.getSeconds()) + + console.log('online time 3: ', onlineTime) + + + if (newtTime.hours && +newtTime.hours > 0) { + onlineTime = addHours(onlineTime, newtTime.hours) } - if(newtTime.minutes && +newtTime.minutes > 0){ - onlineTime = addMinutes(onlineTime,newtTime.minutes) + if (newtTime.minutes && +newtTime.minutes > 0) { + onlineTime = addMinutes(onlineTime, newtTime.minutes) } - if(newtTime.seconds && +newtTime.seconds > 0){ + if (newtTime.seconds && +newtTime.seconds > 0) { onlineTime = addSeconds(onlineTime, newtTime.seconds) - } - - console.log('online time 4: ', onlineTime) - + } + + console.log('online time 4: ', onlineTime) + const isoDate = new Date(onlineTime); const mySQLDateString = isoDate.toJSON().slice(0, 19).replace('T', ' '); console.log('mySQLDateString: ', mySQLDateString) - + await userOnlineTime.update({ status, onlineTime: mySQLDateString }) - + + //test del + + const updatedAtString = formatDateTimeString(userOnlineTime.updatedAt) + const createdAtString = formatDateTimeString(userOnlineTime.createdAt) + + console.log('CreatedAt string: ', createdAtString) + console.log('UpdatedAt string: ', updatedAtString) + // + + io.emit("onlineStatus", { + action: "update", + userOnlineTime: { + userId: userOnlineTime.userId, + status: userOnlineTime.status, + onlineTime: mySQLDateString, + createdAt: createdAtString, + updatedAt: updatedAtString + } + }); + } - else if(oldStatus=='offline' && status === 'online'){ + else if (oldStatus == 'offline' && status === 'online') { await userOnlineTime.update({ status }) + + io.emit("onlineStatus", { + action: "update", + userOnlineTime: { + userId: userOnlineTime.userId, + status: userOnlineTime.status, + createdAt: formatDateTimeString(userOnlineTime.createdAt), + updatedAt: formatDateTimeString(userOnlineTime.updatedAt) + } + }); } - else{ + else { console.log('NOT UPDATED THE USER: ', userOnlineTime.userId) } - - }catch(err){ + + } catch (err) { throw new AppError("ERR_NO_USER_ONLINE_FOUND", 404); - - } + + } } - if(!userOnlineTime){ + if (!userOnlineTime) { userOnlineTime = await UserOnlineTime.create( - { + { userId, status, onlineTime: `${dateTime.fullDate} 00:00:00` }) + io.emit("onlineStatus", { + action: "update", + userOnlineTime: { + userId: userOnlineTime.userId, + status: userOnlineTime.status, + createdAt: formatDateTimeString(userOnlineTime.createdAt), + updatedAt: formatDateTimeString(userOnlineTime.updatedAt) + } + }); + + + } - + return userOnlineTime - } +} - - - export default CreateOrUpdateUserOnlineTime \ No newline at end of file + + +export default CreateOrUpdateUserOnlineTime \ No newline at end of file diff --git a/backend/src/services/UserServices/DeleteUserService.ts b/backend/src/services/UserServices/DeleteUserService.ts index ffaf5f0..ef454e3 100644 --- a/backend/src/services/UserServices/DeleteUserService.ts +++ b/backend/src/services/UserServices/DeleteUserService.ts @@ -1,7 +1,8 @@ import User from "../../models/User"; import AppError from "../../errors/AppError"; import Ticket from "../../models/Ticket"; -import UpdateDeletedUserOpenTicketsStatus from "../../helpers/UpdateDeletedUserOpenTicketsStatus"; +import UpdateDeletedUserOpenTicketsStatus from "../../helpers/UpdateDeletedUserOpenTicketsStatus"; + const DeleteUserService = async (id: string | number): Promise => { const user = await User.findOne({ @@ -20,7 +21,9 @@ const DeleteUserService = async (id: string | number): Promise => { UpdateDeletedUserOpenTicketsStatus(userOpenTickets); } - await user.destroy(); + + await user.destroy(); + }; export default DeleteUserService; diff --git a/backend/src/services/UserServices/ListUsersOnlineOfflineService.ts b/backend/src/services/UserServices/ListUsersOnlineOfflineService.ts index cca0fe0..1fe4205 100644 --- a/backend/src/services/UserServices/ListUsersOnlineOfflineService.ts +++ b/backend/src/services/UserServices/ListUsersOnlineOfflineService.ts @@ -13,6 +13,7 @@ const ListUserOnlineOffline = async ({date, userId, status}: Request): Promise => { - const whereCondition = { - [Op.or]: [ - { - "$User.name$": Sequelize.where( - Sequelize.fn("LOWER", Sequelize.col("User.name")), - "LIKE", - `%${searchParam.toLowerCase()}%` - ) - }, - { email: { [Op.like]: `%${searchParam.toLowerCase()}%` } } - ] - }; - const limit = 20; + + let whereCondition = {} + + if (profile.length > 0) { + whereCondition = { + profile: profile + } + } + else { + whereCondition = { + [Op.or]: [ + { + "$User.name$": Sequelize.where( + Sequelize.fn("LOWER", Sequelize.col("User.name")), + "LIKE", + `%${searchParam.toLowerCase()}%` + ) + }, + { email: { [Op.like]: `%${searchParam.toLowerCase()}%` } } + ] + }; + } + + // const whereCondition = { + // [Op.or]: [ + // { + // "$User.name$": Sequelize.where( + // Sequelize.fn("LOWER", Sequelize.col("User.name")), + // "LIKE", + // `%${searchParam.toLowerCase()}%` + // ) + // }, + // { email: { [Op.like]: `%${searchParam.toLowerCase()}%` } } + // ] + // }; + + + + // const limit = 20; + const limit = 100; const offset = limit * (+pageNumber - 1); const { count, rows: users } = await User.findAndCountAll({ diff --git a/backend/src/services/UserServices/ShowUserServiceReport.ts b/backend/src/services/UserServices/ShowUserServiceReport.ts index 651d60c..c6129ab 100644 --- a/backend/src/services/UserServices/ShowUserServiceReport.ts +++ b/backend/src/services/UserServices/ShowUserServiceReport.ts @@ -9,66 +9,110 @@ import { userInfo } from "os"; import { Op, where } from "sequelize"; -import { Sequelize } from "sequelize"; -import moment from 'moment'; - -import { startOfDay, endOfDay, parseISO, getDate} from "date-fns"; +import { Sequelize } from "sequelize"; +import moment from 'moment'; + +import { startOfDay, endOfDay, parseISO, getDate } from "date-fns"; import { string } from "yup/lib/locale"; import Whatsapp from "../../models/Whatsapp"; +import UserOnlineTime from "../../models/UserOnlineTime"; + + +interface Request { + startDate: string; + endDate: string; + userId?: string; + ticketStatus?: string; +} + + //Report by user, startDate, endDate -const ShowUserServiceReport = async (id: string | number, startDate: string, endDate: string): Promise => { - +const ShowUserServiceReport = async ({ + startDate, + endDate, + userId="", + ticketStatus="" +}: Request): Promise => { + + // Para contornar a gambiarra do ShowTicketReport + userId=='0' ? userId='' : userId = userId + let where_clause = {} + let objQuery: Object[] = [] - if(id=='0'){ - where_clause = { - createdAt: { - [Op.gte]: startDate+' 00:00:00.000000', - [Op.lte]: endDate +' 23:59:59.999999' - }, + + if (ticketStatus.length>0) { + + if (userId.length>0) { + where_clause = { + userId: userId, + status: ticketStatus, + createdAt: { + [Op.gte]: startDate + ' 00:00:00.000000', + [Op.lte]: endDate + ' 23:59:59.999999' + }, + } } - } - else{ - where_clause = { - userid: id, - createdAt: { - [Op.gte]: startDate+' 00:00:00.000000', - [Op.lte]: endDate +' 23:59:59.999999' - }, + else { + where_clause = { + status: ticketStatus, + createdAt: { + [Op.gte]: startDate + ' 00:00:00.000000', + [Op.lte]: endDate + ' 23:59:59.999999' + }, + } } + + objQuery = await Ticket.findAll({ + + where: where_clause, + raw: true, + attributes: ['userId', [Sequelize.fn("COUNT", Sequelize.col("Ticket.status")), "count"]], + group: ['userId'] + + }); } - - - const ticket = await Ticket.findAll({ + else { - // where: where_clause , + if (userId.length>0) { + where_clause = { + userId: userId, + createdAt: { + [Op.gte]: startDate + ' 00:00:00.000000', + [Op.lte]: endDate + ' 23:59:59.999999' + }, + } + } + else { + where_clause = { + createdAt: { + [Op.gte]: startDate + ' 00:00:00.000000', + [Op.lte]: endDate + ' 23:59:59.999999' + }, + } + } - // attributes: ['id', 'status', [Sequelize.fn("DATE_FORMAT",Sequelize.col("Ticket.createdAt"),"%d/%m/%Y %H:%i:%s"),"createdAt"], - // [Sequelize.fn("DATE_FORMAT",Sequelize.col("Ticket.updatedAt"),"%d/%m/%Y %H:%i:%s"),"updatedAt"]], + objQuery = await UserOnlineTime.findAll({ - // include: [ - // { - // model: Message, - // required:true, - // separate: true, + where: where_clause, + raw: true, + // attributes: ['userId', [Sequelize.literal(`( SELECT SUM(time_to_sec(TIME(onlineTime)) ))`), 'sum']], + attributes: ['userId', [Sequelize.literal(`( SELECT SEC_TO_TIME(SUM(time_to_sec(TIME(onlineTime)) )))`), 'sum']], + group: ['userId'] - // attributes: ['body', 'read', 'mediaType','fromMe', 'mediaUrl', [Sequelize.fn("DATE_FORMAT",Sequelize.col("createdAt"),"%d/%m/%Y %H:%i:%s"),"createdAt"]], + }); - // order: [ - // ['createdAt', 'ASC'] - // ] - // } - // ], - - }); - - - if (!ticket) { - throw new AppError("ERR_NO_TICKET_FOUND", 404); } - return ticket; -}; + + + + if (!objQuery) { + throw new AppError("ERR_NO_OBJ_QUERY_FOUND", 404); + } + + return objQuery; +}; export default ShowUserServiceReport; diff --git a/frontend/src/components/NotificationsPopOver/index.js b/frontend/src/components/NotificationsPopOver/index.js index d84c192..599795f 100644 --- a/frontend/src/components/NotificationsPopOver/index.js +++ b/frontend/src/components/NotificationsPopOver/index.js @@ -59,6 +59,10 @@ const NotificationsPopOver = () => { const historyRef = useRef(history); + + const { handleLogout } = useContext(AuthContext); + + useEffect(() => { soundAlertRef.current = play; @@ -82,6 +86,23 @@ const NotificationsPopOver = () => { useEffect(() => { const socket = openSocket(process.env.REACT_APP_BACKEND_URL); + + socket.on("onlineStatus", (data) => { + + if(data.action === "logout"){ + + console.log('___________data.userId: ', data.userOnlineTime['status']) + + if(`${user.id}` === data.userOnlineTime['userId']){ + + socket.emit("online", {logoutUserId: user.id}) + handleLogout(); + } + + } + + + }); socket.on("isOnline", (data) => { diff --git a/frontend/src/components/ReportModal/index.js b/frontend/src/components/ReportModal/index.js new file mode 100644 index 0000000..169c853 --- /dev/null +++ b/frontend/src/components/ReportModal/index.js @@ -0,0 +1,126 @@ +import React, { useState, useEffect, useReducer, useContext, useRef } from "react"; +import Box from '@mui/material/Box'; +import Button from '@mui/material/Button'; +import Dialog from '@mui/material/Dialog'; +import DialogActions from '@mui/material/DialogActions'; +import DialogContent from '@mui/material/DialogContent'; +import DialogContentText from '@mui/material/DialogContentText'; +import DialogTitle from '@mui/material/DialogTitle'; +import FormControl from '@mui/material/FormControl'; +import FormControlLabel from '@mui/material/FormControlLabel'; +import InputLabel from '@mui/material/InputLabel'; +import MenuItem from '@mui/material/MenuItem'; +import Select from '@mui/material/Select'; +import Switch from '@mui/material/Switch'; + + + +export default function MaxWidthDialog(props) { + const [open, setOpen] = useState(false); + const [fullWidth, setFullWidth] = useState(true); + const [currency, setCurrency] = useState(props.reportOption); + + +useEffect(()=>{ + + props.func(currency); + +},[currency, props]) + + + const handleClickOpen = () => { + setOpen(true); + }; + + const handleClose = () => { + setOpen(false); + }; + + const handleMaxWidthChange = (event) => { + + setCurrency( event.target.value) + + console.log('event.target.value: ', event.target.value) + }; + + const handleFullWidthChange = (event) => { + setFullWidth(event.target.checked); + }; + + return ( + + + + Relatórios + + + + Escolha uma opção de relatório abaixo + + + + + + opcoes + + + + + + {/* + } + label="Full width" + /> */} + + + + + + + + + + ); +} diff --git a/frontend/src/pages/Report/index.js b/frontend/src/pages/Report/index.js index dc5c022..d23fd0e 100644 --- a/frontend/src/pages/Report/index.js +++ b/frontend/src/pages/Report/index.js @@ -1,98 +1,129 @@ -import React, { useState, useEffect, useReducer, useContext, useRef} from "react"; -import MainContainer from "../../components/MainContainer"; -import api from "../../services/api"; -import SelectField from "../../components/Report/SelectField"; +import React, { useState, useEffect, useReducer, useContext, useRef } from "react"; +import MainContainer from "../../components/MainContainer"; +import api from "../../services/api"; +import SelectField from "../../components/Report/SelectField"; //import { data } from '../../components/Report/MTable/data'; -import DatePicker1 from '../../components/Report/DatePicker' -import DatePicker2 from '../../components/Report/DatePicker' -import MTable from "../../components/Report/MTable"; +import DatePicker1 from '../../components/Report/DatePicker' +import DatePicker2 from '../../components/Report/DatePicker' +import MTable from "../../components/Report/MTable"; import PropTypes from 'prop-types'; import Box from '@mui/material/Box'; import { AuthContext } from "../../context/Auth/AuthContext"; import { Can } from "../../components/Can"; -import { Button } from "@material-ui/core"; +import { Button } from "@material-ui/core"; - -// test del - -// import ExportCSV from '../../components/Report/ExportCSV' +import ReportModal from "../../components/ReportModal"; +import MaterialTable from 'material-table'; +import Delete from '@material-ui/icons/Delete'; +import Edit from '@material-ui/icons/Edit'; +import LogoutIcon from '@material-ui/icons/CancelOutlined'; import { CSVLink } from "react-csv"; + +import openSocket from "socket.io-client"; + +const report = [{ 'value': '1', 'label': 'Atendimento por atendentes' }, { 'value': '2', 'label': 'Usuários online/offline' }] + let columns = [ - { - key: 'ticket.whatsapp.name', - label: 'Loja', - - }, - { - key: 'id', - label: 'id Mensagem', + key: 'ticket.whatsapp.name', + label: 'Loja', + }, { - key: 'ticket.id', - label: 'id Conversa', + key: 'id', + label: 'id Mensagem', }, { - key: 'ticket.contact.name', - label: 'Cliente', + key: 'ticket.id', + label: 'id Conversa', }, { - key: 'ticket.user.name', - label: 'Atendente', - }, - { - key: 'body', - label: 'Mensagem', - }, - { - key: 'fromMe', - label: 'Sentido', - }, - { - key: 'createdAt', - label: 'Criada', - }, - { - key: 'ticket.contact.number', - label: 'Telefone cliente', - }, - { - key: 'ticket.queue.name', - label: 'Fila', - }, - { - key: 'ticket.status', - label: 'Status', + key: 'ticket.contact.name', + label: 'Cliente', }, -] + { + key: 'ticket.user.name', + label: 'Atendente', + }, + { + key: 'body', + label: 'Mensagem', + }, + { + key: 'fromMe', + label: 'Sentido', + }, + { + key: 'createdAt', + label: 'Criada', + }, + { + key: 'ticket.contact.number', + label: 'Telefone cliente', + }, + { + key: 'ticket.queue.name', + label: 'Fila', + }, + { + key: 'ticket.status', + label: 'Status', + }] // -const reducerQ = (state, action) =>{ - if(action.type === 'LOAD_QUERY'){ - - - const queries = action.payload + + + + +const reducerQ = (state, action) => { + + + + + if (action.type === "DELETE_USER_STATUS") { + + const userId = action.payload; + + console.log('Entrou no delete user status userId: ', userId) + + const userIndex = state.findIndex((u) => `${u.id}` === `${userId}`); + + console.log('>>>>>>>>>>>>>>>>>>>>> userIndex: ', userIndex) + + if (userIndex !== -1) { + state.splice(userIndex, 1); + } + + return [...state]; + } + + + + if (action.type === 'LOAD_QUERY') { + + + const queries = action.payload const newQueries = [] queries.forEach((query) => { - + const queryIndex = state.findIndex((q) => q.id === query.id) - - if(queryIndex !== -1){ + + if (queryIndex !== -1) { state[queryIndex] = query } - else{ + else { newQueries.push(query) } @@ -100,7 +131,82 @@ const reducerQ = (state, action) =>{ return [...state, ...newQueries] } - + + + if (action.type === "UPDATE_STATUS_ONLINE") { + + let onlineUser = action.payload + let index = -1 + + console.log('sssssssssstate: ', state, ' | ONLINE USERS: onlineUser.userId ', onlineUser.userId) + + if (onlineUser.sumOpen || onlineUser.sumClosed) { + index = state.findIndex((e) => ((onlineUser.sumOpen && e.id === onlineUser.sumOpen.userId) || (onlineUser.sumClosed && e.id === onlineUser.sumClosed.userId))) + } + else { + index = state.findIndex((e) => `${e.id}` === `${onlineUser.userId}`) + } + + console.log(' *********************** index: ', index) + + + + if (index !== -1) { + + console.log('ENTROU NO INDEX') + + + if (!("statusOnline" in state[index])) { + state[index].statusOnline = onlineUser + } + else if ("statusOnline" in state[index]) { + state[index].statusOnline['status'] = onlineUser.status + } + + + if ("onlineTime" in onlineUser) { + + if ("sumOnlineTime" in state[index]) { + state[index].sumOnlineTime['sum'] = (onlineUser.onlineTime).split(" ")[1] + } + else if (!("sumOnlineTime" in state[index])) { + state[index].sumOnlineTime = { userId: onlineUser.userId, sum: (onlineUser.onlineTime).split(" ")[1] } + } + } + + + if (onlineUser.sumOpen) { + + if ("sumOpen" in state[index]) { + console.log(' >>>>>>>>>>>>>>>>>> sumOpen 1 | state[index].sumOpen["count"]: ', state[index].sumOpen['count'], ' | onlineUser.sumOpen.count: ', onlineUser.sumOpen.count) + state[index].sumOpen['count'] = onlineUser.sumOpen.count + } else if (!("sumOpen" in state[index])) { + console.log(' >>>>>>>>>>>>>>>>>> sumOpen 1') + state[index].sumOpen = onlineUser.sumOpen + } + + } + + if (onlineUser.sumClosed) { + + if ("sumClosed" in state[index]) { + console.log(' >>>>>>>>>>>>>>>>>> sumClosed 1 | state[index].sumClosed["count"]: ', state[index].sumClosed['count'], ' | onlineUser.sumClosed.count: ', onlineUser.sumClosed.count) + state[index].sumClosed['count'] = onlineUser.sumClosed.count + } else if (!("sumClosed" in state[index])) { + console.log(' >>>>>>>>>>>>>>>>>> sumOpen 1') + state[index].sumClosed = onlineUser.sumClosed + } + + } + + + } + return [...state] + + } + + + if (action.type === "RESET") { return []; } @@ -110,7 +216,7 @@ const reducerQ = (state, action) =>{ const reducer = (state, action) => { - + if (action.type === "LOAD_USERS") { const users = action.payload; const newUsers = []; @@ -125,14 +231,24 @@ const reducer = (state, action) => { }); return [...state, ...newUsers]; - } + } + + if (action.type === "DELETE_USER") { + const userId = action.payload; + + const userIndex = state.findIndex((u) => u.id === userId); + if (userIndex !== -1) { + state.splice(userIndex, 1); + } + return [...state]; + } if (action.type === "RESET") { return []; } }; - + function Item(props) { @@ -165,88 +281,61 @@ Item.propTypes = { PropTypes.func, PropTypes.object, ]), -}; +}; + - let columnsData = [ - { title: 'Unidade', field: 'whatsapp.name' }, - { title: 'Atendente', field: 'user.name' }, - { title: 'Contato', field: 'contact.number' }, - { title: 'Nome', field: 'contact.name' }, - { title: 'Assunto', field: 'queue.name' }, - { title: 'Status', field: 'status' }, - { title: 'Criado', field: 'createdAt' }, - - { title: 'Atualizado', field: 'updatedAt', + { title: 'Unidade', field: 'whatsapp.name' }, + { title: 'Atendente', field: 'user.name' }, + { title: 'Contato', field: 'contact.number' }, + { title: 'Nome', field: 'contact.name' }, + { title: 'Assunto', field: 'queue.name' }, + { title: 'Status', field: 'status' }, + { title: 'Criado', field: 'createdAt' }, + { + title: 'Atualizado', field: 'updatedAt', - /*cellStyle: { - backgroundColor: '#039be5', - color: '#FFF' - }, - headerStyle: { - backgroundColor: '#039be5', - fontSize: 12 - }*/ - - }, + }]; - - ]; - /*const currencies = [ - { - value: '1', - label: 'Adriano', - }, - { - value: '2', - label: 'Aguinaldo', - }, - { - value: '3', - label: 'Maria', - }, - { - value: '4', - label: 'Suely', - }, - { - value: '0', - label: '', - }, - - ];*/ - - +const Report = () => { -const Report = () => { - - const csvLink = useRef() + const csvLink = useRef() + + const { user: userA } = useContext(AuthContext); - const { user: userA } = useContext(AuthContext); - //-------- const [searchParam] = useState(""); //const [loading, setLoading] = useState(false); //const [hasMore, setHasMore] = useState(false); - const [pageNumber, setPageNumber] = useState(1); - const [users, dispatch] = useReducer(reducer, []); + const [pageNumber, setPageNumber] = useState(1); + const [users, dispatch] = useReducer(reducer, []); //const [columns, setColums] = useState([]) - const [startDate, setDatePicker1] = useState(new Date()) - const [endDate, setDatePicker2] = useState(new Date()) - const [userId, setUser] = useState(null) + const [startDate, setDatePicker1] = useState(new Date()) + const [endDate, setDatePicker2] = useState(new Date()) + const [userId, setUser] = useState(null) const [query, dispatchQ] = useReducer(reducerQ, []) - const [dataCSV, setDataCSV] = useState([]) + const [dataCSV, setDataCSV] = useState([]) const [isMount, setIsMount] = useState(true); + const [reportOption, setReport] = useState('1') + const [reporList,] = useState(report) + const [profile, setProfile] = useState('') + const [dataRows, setData] = useState([]); + const [selectedUserId, setSelectedUserId] = useState(null); + + + useEffect(() => { - dispatch({ type: "RESET" }); + dispatch({ type: "RESET" }); dispatchQ({ type: "RESET" }) - + setPageNumber(1); - }, [searchParam]); + }, [searchParam, profile]); + + useEffect(() => { //setLoading(true); @@ -255,13 +344,18 @@ const Report = () => { const fetchUsers = async () => { try { + + console.log('profile: ', profile) + const { data } = await api.get("/users/", { - params: { searchParam, pageNumber }, - }); - + params: { searchParam, pageNumber, profile }, + }); + dispatch({ type: "LOAD_USERS", payload: data.users }); //setHasMore(data.hasMore); - //setLoading(false); + //setLoading(false); + + } catch (err) { console.log(err); } @@ -271,28 +365,45 @@ const Report = () => { }, 500); return () => clearTimeout(delayDebounceFn); - }, [searchParam, pageNumber]); - + }, [searchParam, pageNumber, reportOption, profile]); + useEffect(() => { - //setLoading(true); + //setLoading(true); const delayDebounceFn = setTimeout(() => { const fetchQueries = async () => { try { - const dataQuery = await api.get("/reports/", {params: {userId, startDate, endDate },}); - dispatchQ({ type: "RESET" }) - dispatchQ({ type: "LOAD_QUERY", payload: dataQuery.data }); + if (reportOption === '1') { - //setLoading(false); + const dataQuery = await api.get("/reports/", { params: { userId, startDate, endDate }, }); + dispatchQ({ type: "RESET" }) + dispatchQ({ type: "LOAD_QUERY", payload: dataQuery.data }); - console.log('dataQuery: ', dataQuery.data) + //setLoading(false); - console.log() + console.log('dataQuery: ', dataQuery.data) + + console.log() + + } + else if (reportOption === '2') { + + const dataQuery = await api.get("/reports/user/services", { params: { userId, startDate, endDate }, }); + dispatchQ({ type: "RESET" }) + dispatchQ({ type: "LOAD_QUERY", payload: dataQuery.data }); + + //setLoading(false); + + console.log('REPORT 2 dataQuery : ', dataQuery.data) + + console.log() + + } } catch (err) { console.log(err); @@ -304,149 +415,399 @@ const Report = () => { }, 500); return () => clearTimeout(delayDebounceFn); - }, [userId, startDate, endDate]); - + }, [userId, startDate, endDate, reportOption]); + + + // Get from child 1 + const datePicker1Value = (data) => { -// Get from child 1 -const datePicker1Value = (data) => { - setDatePicker1(data) -} + } + + // Get from child 2 + const datePicker2Value = (data) => { -// Get from child 2 -const datePicker2Value = (data) => { - setDatePicker2(data) -} + } + + // Get from child 3 + const textFieldSelectUser = (data) => { -// Get from child 3 -const textFieldSelectUser = (data) => { - setUser(data) -} + } - -// test del + // Get from report option + const reportValue = (data) => { -const handleCSVMessages = () =>{ - - // setLoading(true); + setReport(data) - const fetchQueries = async () => { + console.log(' data: ', data) + } - try { + useEffect(() => { - const dataQuery = await api.get("/reports/messages", {params: {userId, startDate, endDate },}); + if (reportOption === '1') { + setProfile('') + } + else if (reportOption === '2') { + setProfile('user') + } - // console.log('dataQuery messages: ', dataQuery.data) + }, [reportOption]) - if(dataQuery.data.length > 0){ - let dataCSVFormat = dataQuery.data ; + // useEffect(() => { - for(var i = 0; i>>>>>>>>>>>>>>>>> New query: ', query) + + // }, [query]) + + + + // test del + + const handleCSVMessages = () => { + + // setLoading(true); + + const fetchQueries = async () => { + + try { + + const dataQuery = await api.get("/reports/messages", { params: { userId, startDate, endDate }, }); + + // console.log('dataQuery messages: ', dataQuery.data) + + if (dataQuery.data.length > 0) { + + let dataCSVFormat = dataQuery.data; + + for (var i = 0; i < dataCSVFormat.length; i++) { + if (dataCSVFormat[i].fromMe) { + dataCSVFormat[i].fromMe = 'Atendente' + } + else { + dataCSVFormat[i].fromMe = 'Cliente' + } } + + // console.log('dataCSVFormat: ', dataCSVFormat) + + setDataCSV(dataCSVFormat) + setIsMount(false); + // setDataCSV(dataQuery.data) } - // console.log('dataCSVFormat: ', dataCSVFormat) - - setDataCSV(dataCSVFormat) - setIsMount(false); - // setDataCSV(dataQuery.data) - } + // setLoading(false); - // setLoading(false); + } catch (err) { + console.log(err); + } + }; - } catch (err) { - console.log(err); + fetchQueries(); + + } + + + + + + useEffect(() => { + + if (isMount) { + return; } + + csvLink.current.link.click() + + }, [dataCSV, isMount, csvLink]); + + + + + useEffect(() => { + + if (reportOption === '2') { + + const socket = openSocket(process.env.REACT_APP_BACKEND_URL); + + socket.on("onlineStatus", (data) => { + + // setLoading(true); + + + let date = new Date().toLocaleDateString('pt-BR').split('/') + let dateToday = `${date[2]}-${date[1]}-${date[0]}` + + console.log('date: ', new Date(startDate).toLocaleDateString('pt-BR')) + console.log('date2: ', startDate) + + + if (data.action === "logout" || (data.action === "update" && + ((`${startDate}` === `${endDate}`) && (`${endDate}` === `${dateToday}`) && (`${startDate}` === `${dateToday}`)))) { + + console.log('UPDATE FROM ONLINE/OFFLINE LOGED USERS: ', data.userOnlineTime, ' | data.action : ', data.action) + + dispatchQ({ type: "UPDATE_STATUS_ONLINE", payload: data.userOnlineTime }); + + } + else if (data.action === "delete") { + dispatchQ({ type: "DELETE_USER_STATUS", payload: data.userOnlineTime }); + } + + // setLoading(false); + + }); + + socket.on("user", (data) => { + + if (data.action === "delete") { + console.log(' entrou no delete user: ', data) + dispatch({ type: "DELETE_USER", payload: +data.userId }); + } + }); + + return () => { + socket.disconnect(); + }; + + } + + + }, [reportOption, startDate, endDate]); + + + // const handleDeleteRows = (id) => { + + // let _data = [...dataRows]; + + // _data.forEach(rd => { + // _data = _data.filter(t => t.id !== id); + // }); + // setData(_data); + + // }; + + + useEffect(() => { + + //if (!loading) { + + + // setData(query.map(({ scheduleReminder, ...others }) => ( + // { ...others, 'scheduleReminder': `${others.statusChatEndId}` === '3' ? 'Agendamento' : 'Lembrete' } + // ))) + // } + + setData(query.map((column) => { return { ...column } })) + + }, [query]) + + const handleLogouOnlineUser = async (userId) => { + try { + await api.get(`/users/logout/${userId}`); + //toast.success(("Desloged!")); + //handleDeleteRows(scheduleId) + } catch (err) { + // toastError(err); + } + + setSelectedUserId(null); }; - fetchQueries(); -} - - - - - -useEffect(() => { - - if(isMount){ - return; - } - - csvLink.current.link.click() - -}, [dataCSV, isMount, csvLink]); - - - - return ( + return ( ( + role={userA.profile} + perform="ticket-report:show" + yes={() => ( + + + + + { + return { 'value': obj.id, 'label': obj.name } + })} /> + + + - - - - { - return {'value': obj.id, 'label': obj.name} - })}/> - - - - - - -
- -
- -
- + + + {/* + +
+ +
*/} + + +
- - - - - + - + + + {reportOption === '1' && + + + } + {reportOption === '2' && + + imagem de perfil do whatsapp }, + { title: 'Nome', field: 'name' }, + + { + title: 'Status', field: 'statusOnline.status', + + cellStyle: (e, rowData) => { + + if (rowData['statusOnline'] && rowData['statusOnline'].status) { + + if (rowData['statusOnline'].status === 'offline') { + + return { color: "red" }; + } + else if (rowData['statusOnline'].status === 'online') { + return { color: "green" }; + } + else if (rowData['statusOnline'].status === 'logout...') { + return { color: "orange" }; + } + else if (rowData['statusOnline'].status === 'waiting...') { + return { color: "orange" }; + } + } + + + }, + + }, + + { title: 'Tempo online', field: 'sumOnlineTime.sum' }, + { title: 'Data inicio', field: 'startDate' }, + { title: 'Data fim', field: 'endDate' }, + { title: 'Em atendimento', field: 'sumOpen.count' }, + { title: 'Finalizado', field: 'sumClosed.count' }, + + ] + } + data={dataRows} + // icons={tableIcons} + + actions={[ + (rowData) => { + + if (rowData.statusOnline && + rowData.statusOnline['status'] && + rowData.statusOnline['status'] === 'online') { + + + return { + icon: LogoutIcon, + tooltip: 'deslogar', + disable: false, + onClick: (event, rowData) => { + + console.log(' ROW DATA INFO: ', rowData, ' | rowData: ', rowData.id) + handleLogouOnlineUser(rowData.id) + } + } + + + } + } + ]} + + + options={ + { + search: true, + selection: false, + paging: false, + padding: 'dense', + sorting: true, + //loadingType: 'linear', + searchFieldStyle: { + width: 300, + }, + + pageSize: 20, + headerStyle: { + position: "sticky", + top: "0" + }, + maxBodyHeight: "400px", + + rowStyle: { + fontSize: 14, + } + + + // cellStyle: (rowData) => { + // return { + // fontSize: 12, + // color: "#fff", + + // }; + // } + + + + + + + + }} + /> + + } + + + +
- - )} - /> + + )} + /> ) }; diff --git a/frontend/src/pages/SchedulesReminder/index.js b/frontend/src/pages/SchedulesReminder/index.js index a6f798b..1a7ee3c 100644 --- a/frontend/src/pages/SchedulesReminder/index.js +++ b/frontend/src/pages/SchedulesReminder/index.js @@ -1,16 +1,16 @@ -import React, { useState, useEffect, useReducer} from "react"; -import MainContainer from "../../components/MainContainer"; -import api from "../../services/api"; - +import React, { useState, useEffect, useReducer } from "react"; +import MainContainer from "../../components/MainContainer"; +import api from "../../services/api"; + //import { data } from '../../components/Report/MTable/data'; -import DatePicker1 from '../../components/Report/DatePicker' -import DatePicker2 from '../../components/Report/DatePicker' +import DatePicker1 from '../../components/Report/DatePicker' +import DatePicker2 from '../../components/Report/DatePicker' //import { Button } from "@material-ui/core"; - + import PropTypes from 'prop-types'; import Box from '@mui/material/Box'; - - + + import SearchIcon from "@material-ui/icons/Search"; import TextField from "@material-ui/core/TextField"; @@ -20,41 +20,41 @@ import Button from "@material-ui/core/Button"; import MaterialTable from 'material-table'; import Delete from '@material-ui/icons/Delete'; -import Edit from '@material-ui/icons/Edit'; +import Edit from '@material-ui/icons/Edit'; + - import { render } from '@testing-library/react'; // import Modal from "../../../..ChatEnd/ModalChatEnd"; -import Modal from "../../components/ModalUpdateScheduleReminder"; +import Modal from "../../components/ModalUpdateScheduleReminder"; import openSocket from "socket.io-client"; - - - - -import { toast } from "react-toastify"; -import toastError from "../../errors/toastError"; + + + + +import { toast } from "react-toastify"; +import toastError from "../../errors/toastError"; import ConfirmationModal from "../../components/ConfirmationModal"; -const reducerQ = (state, action) =>{ +const reducerQ = (state, action) => { - if(action.type === 'LOAD_QUERY'){ - - - const queries = action.payload + if (action.type === 'LOAD_QUERY') { + + + const queries = action.payload const newQueries = [] queries.forEach((query) => { - + const queryIndex = state.findIndex((q) => q.id === query.id) - - if(queryIndex !== -1){ + + if (queryIndex !== -1) { state[queryIndex] = query } - else{ + else { newQueries.push(query) } @@ -80,16 +80,16 @@ const reducerQ = (state, action) =>{ // } // } - if (action.type === "DELETE_SCHEDULING") { - + if (action.type === "DELETE_SCHEDULING") { + const scheduleId = action.payload; const scheduleIndex = state.findIndex((u) => u.id === scheduleId); - + if (scheduleIndex !== -1) { state.splice(scheduleIndex, 1); } - return [...state]; + return [...state]; } @@ -102,8 +102,8 @@ const reducerQ = (state, action) =>{ // return [...state]; // } - - + + if (action.type === "RESET") { return []; } @@ -113,7 +113,7 @@ const reducerQ = (state, action) =>{ // const reducer = (state, action) => { - + // if (action.type === "LOAD_STATUS_CHAT_END") { // const users = action.payload; // const newUsers = []; @@ -135,7 +135,7 @@ const reducerQ = (state, action) =>{ // } // }; - + function Item(props) { @@ -168,46 +168,33 @@ Item.propTypes = { PropTypes.func, PropTypes.object, ]), -}; +}; - -// let columnsData = [ - -// { title: 'Foto', field: 'ticket.contact.profilePicUrl', render: rowData => }, - -// { title: 'Nome', field: 'ticket.contact.name' }, -// { title: 'Contato', field: 'ticket.contact.number' }, -// { title: 'schedulingTime', field: 'schedulingTime' }, -// { title: 'schedulingDate', field: 'schedulingDate' }, -// { title: 'message', field: 'message' }, - -// ]; - - -const SchedulesReminder = () => { - - +const SchedulesReminder = () => { + + + //-------- const [searchParam] = useState(""); const [loading, setLoading] = useState(null); //const [hasMore, setHasMore] = useState(false); - const [pageNumber, setPageNumber] = useState(1); + const [pageNumber, setPageNumber] = useState(1); // const [users, dispatch] = useReducer(reducer, []); //const [columns, setColums] = useState([]) - const [startDate, setDatePicker1] = useState(new Date()) - const [endDate, setDatePicker2] = useState(new Date()) - - const [query, dispatchQ] = useReducer(reducerQ, []) + const [startDate, setDatePicker1] = useState(new Date()) + const [endDate, setDatePicker2] = useState(new Date()) + + const [query, dispatchQ] = useReducer(reducerQ, []) const [contactNumber, setContactNumber] = useState(""); - + const [resetChild, setReset] = useState(false) const [selectedSchedule, setSelectedSchedule] = useState(null); - const [confirmModalOpen, setConfirmModalOpen] = useState(false); + const [confirmModalOpen, setConfirmModalOpen] = useState(false); const [dataRows, setData] = useState([]); const [statusEndChat, setStatusEndChat] = useState(null) @@ -217,13 +204,13 @@ const SchedulesReminder = () => { useEffect(() => { - const socket = openSocket(process.env.REACT_APP_BACKEND_URL); + const socket = openSocket(process.env.REACT_APP_BACKEND_URL); socket.on("schedulingNotify", (data) => { - - setLoading(true); + + setLoading(true); // if (data.action === "update" || data.action === "create") { @@ -233,7 +220,7 @@ const SchedulesReminder = () => { // } if (data.action === "delete") { - + dispatchQ({ type: "DELETE_SCHEDULING", payload: +data.schedulingNotifyId }); //handleDeleteRows(data.schedulingNotifyId) } @@ -248,11 +235,11 @@ const SchedulesReminder = () => { }; }, []); - + useEffect(() => { // dispatch({ type: "RESET" }); dispatchQ({ type: "RESET" }) - + setPageNumber(1); }, [searchParam]); @@ -267,12 +254,12 @@ const SchedulesReminder = () => { try { const statusChatEndLoad = await api.get("/statusChatEnd", { params: { searchParam, pageNumber }, - }); - + }); + // dispatch({ type: "LOAD_STATUS_CHAT_END", payload: statusChatEndLoad.data }); - - + + // setStatusEndChat(statusChatEndLoad.data.filter(status => (status.id == '2' || status.id == '3'))) @@ -291,21 +278,21 @@ const SchedulesReminder = () => { }, 500); return () => clearTimeout(delayDebounceFn); }, [searchParam, pageNumber]); - - useEffect(() => { - setLoading(true); + + useEffect(() => { + setLoading(true); const delayDebounceFn = setTimeout(() => { const fetchQueries = async () => { - try { + try { - const dataQuery = await api.get("/schedules/", {params: {contactNumber, startDate, endDate },}); + const dataQuery = await api.get("/schedules/", { params: { contactNumber, startDate, endDate }, }); dispatchQ({ type: "RESET" }) - dispatchQ({ type: "LOAD_QUERY", payload: dataQuery.data }); + dispatchQ({ type: "LOAD_QUERY", payload: dataQuery.data }); setLoading(false); } catch (err) { @@ -320,274 +307,274 @@ const SchedulesReminder = () => { }, [contactNumber, startDate, endDate]); - - + + useEffect(() => { - if(!loading){ - - + if (!loading) { + + setData(query.map(({ scheduleReminder, ...others }) => ( { ...others, 'scheduleReminder': `${others.statusChatEndId}` === '3' ? 'Agendamento' : 'Lembrete' } - ))) + ))) } }, [loading, query]) - -// Get from child 1 -const datePicker1Value = (data) => { - + + // Get from child 1 + const datePicker1Value = (data) => { + setDatePicker1(data) -} + } + + // Get from child 2 + const datePicker2Value = (data) => { -// Get from child 2 -const datePicker2Value = (data) => { - setDatePicker2(data) -} - - - -const handleSearch = (event) => { - setContactNumber(event.target.value.toLowerCase()); -}; - -const handleClear = () => { - - setContactNumber('') - - setReset(true) - -} - -const handleCloseConfirmationModal = () => { - setConfirmModalOpen(false); - - setSelectedSchedule(null); -}; - - -// const handleDeleteRows = (id) => { - -// let _data = [...dataRows]; - -// _data.forEach(rd => { -// _data = _data.filter(t => t.id !== id); -// }); -// setData(_data); - -// }; - - -const handleDeleteSchedule = async (scheduleId) => { - try { - await api.delete(`/schedule/${scheduleId}`); - toast.success(("Lembrete/Agendamento deletado com sucesso!")); - //handleDeleteRows(scheduleId) - } catch (err) { - toastError(err); } - - setSelectedSchedule(null); -}; - -const handleUpdateSchedule = async (scheduleData, rowsDataNew) => { - try { - await api.post("/schedule", scheduleData); - toast.success(("Lembrete/Agendamento atualizado com sucesso!")); - ////////////////// - - const dataUpdate = [...dataRows]; - const index = rowsDataNew.tableData['id']; - dataUpdate[index] = rowsDataNew; + const handleSearch = (event) => { + setContactNumber(event.target.value.toLowerCase()); + }; - setData([...dataUpdate].map(({ scheduleReminder, ...others }) => ( - { ...others, 'scheduleReminder': `${others.statusChatEndId}` === '3' ? 'Agendamento' : 'Lembrete' } + const handleClear = () => { + + setContactNumber('') + + setReset(true) + + } + + const handleCloseConfirmationModal = () => { + setConfirmModalOpen(false); + + setSelectedSchedule(null); + }; + + + // const handleDeleteRows = (id) => { + + // let _data = [...dataRows]; + + // _data.forEach(rd => { + // _data = _data.filter(t => t.id !== id); + // }); + // setData(_data); + + // }; + + + const handleDeleteSchedule = async (scheduleId) => { + try { + await api.delete(`/schedule/${scheduleId}`); + toast.success(("Lembrete/Agendamento deletado com sucesso!")); + //handleDeleteRows(scheduleId) + } catch (err) { + toastError(err); + } + + setSelectedSchedule(null); + }; + + + + const handleUpdateSchedule = async (scheduleData, rowsDataNew) => { + try { + + await api.post("/schedule", scheduleData); + toast.success(("Lembrete/Agendamento atualizado com sucesso!")); + ////////////////// + + const dataUpdate = [...dataRows]; + const index = rowsDataNew.tableData['id']; + dataUpdate[index] = rowsDataNew; + + setData([...dataUpdate].map(({ scheduleReminder, ...others }) => ( + { ...others, 'scheduleReminder': `${others.statusChatEndId}` === '3' ? 'Agendamento' : 'Lembrete' } ))); - - - - - ///////////////// - } catch (err) { - toastError(err); + + + ///////////////// + + + } catch (err) { + toastError(err); + } + // + setSelectedSchedule(null); + }; + + + const chatEndVal = (data, rowsDataNew) => { + + if (data) { + + + + + + + handleUpdateSchedule(data, rowsDataNew) + + } + } - // - setSelectedSchedule(null); -}; -const chatEndVal = (data, rowsDataNew) => { - - if(data){ + const handleModal = (rowData) => { - - - - + // NATY - handleUpdateSchedule(data, rowsDataNew) - - } - -} - - -const handleModal = (rowData) => { - - // NATY - - render() - -}; + />) - return ( + }; - - - - - - - - - ), - }} - /> - + return ( - - - - - - - {/* */} - - - - + + + + + + + + ), + }} + /> + - - - - + + - handleDeleteSchedule(selectedSchedule.id)} - > - Deseja realmente deletar esse lembrete/agendamento ? - + + {/* */} - imagem de perfil do whatsapp }, - { title: 'Nome', field: 'ticket.contact.name' }, - { title: 'Contato', field: 'ticket.contact.number' }, - { title: 'Lemb/Agen', field: 'scheduleReminder'}, - { title: 'Envio', field: 'schedulingTime' }, - { title: 'Data', field: 'schedulingDate' }, - { title: 'Mensagem', field: 'message', width: "80%" }, - - ] + + + + + + + + + + + + + handleDeleteSchedule(selectedSchedule.id)} + > + Deseja realmente deletar esse lembrete/agendamento ? + + + imagem de perfil do whatsapp }, + { title: 'Nome', field: 'ticket.contact.name' }, + { title: 'Contato', field: 'ticket.contact.number' }, + { title: 'Lemb/Agen', field: 'scheduleReminder' }, + { title: 'Envio', field: 'schedulingTime' }, + { title: 'Data', field: 'schedulingDate' }, + { title: 'Mensagem', field: 'message', width: "80%" }, + + ] + } + data={dataRows} + // icons={tableIcons} + + actions={[ + { + icon: Edit, + tooltip: 'Editar', + onClick: (event, rowData) => { + + setSelectedSchedule(rowData); + handleModal(rowData) } - data={dataRows} - // icons={tableIcons} - actions={[ - { - icon: Edit, - tooltip: 'Editar', - onClick: (event, rowData) => { - - setSelectedSchedule(rowData); - handleModal(rowData) - } - - }, - { - icon: Delete, - tooltip: 'Deletar', - onClick: (event, rowData) => { - - setSelectedSchedule(rowData); - setConfirmModalOpen(true); - } - // onClick: handleDeleteRows - } - ]} + }, + { + icon: Delete, + tooltip: 'Deletar', + onClick: (event, rowData) => { - options={ - { - search: true, - selection: false, - paging: false, - padding: 'dense', - sorting: true, - //loadingType: 'linear', - searchFieldStyle: { - width: 300, - }, + setSelectedSchedule(rowData); + setConfirmModalOpen(true); + } + // onClick: handleDeleteRows + } + ]} - pageSize: 20, - headerStyle: { - position: "sticky", - top: "0" - }, - maxBodyHeight: "400px", + options={ + { + search: true, + selection: false, + paging: false, + padding: 'dense', + sorting: true, + //loadingType: 'linear', + searchFieldStyle: { + width: 300, + }, - rowStyle: { - fontSize: 12, - } - - }} - /> + pageSize: 20, + headerStyle: { + position: "sticky", + top: "0" + }, + maxBodyHeight: "400px", - - + rowStyle: { + fontSize: 12, + } - - + }} + /> + + + + +
+
) - - + + }; export default SchedulesReminder;