From 303098636ad914d908e3a1359288146c112aa23b Mon Sep 17 00:00:00 2001 From: adriano Date: Tue, 1 Mar 2022 00:27:17 -0300 Subject: [PATCH] =?UTF-8?q?Cria=C3=A7=C3=A3o=20completa=20do=20envio=20de?= =?UTF-8?q?=20mensagem=20programada=20para=20cliente=20ao=20finalizar=20o?= =?UTF-8?q?=20atendimento?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/app.ts | 2 +- backend/src/controllers/MessageController.ts | 5 +- backend/src/controllers/TicketController.ts | 5 ++ .../helpers/SchedulingNotifySendMessage.ts | 76 +++++++++++++++++++ backend/src/routes/messageRoutes.ts | 7 +- backend/src/server.ts | 4 + .../ListSchedulingNotifyService.ts | 35 +++++++-- .../components/TicketActionButtons/index.js | 5 +- 8 files changed, 124 insertions(+), 15 deletions(-) create mode 100644 backend/src/helpers/SchedulingNotifySendMessage.ts diff --git a/backend/src/app.ts b/backend/src/app.ts index 5aa2a5c..7864346 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -10,7 +10,7 @@ import "./database"; import uploadConfig from "./config/upload"; import AppError from "./errors/AppError"; import routes from "./routes"; -import { logger } from "./utils/logger"; +import { logger } from "./utils/logger"; Sentry.init({ dsn: process.env.SENTRY_DSN }); diff --git a/backend/src/controllers/MessageController.ts b/backend/src/controllers/MessageController.ts index 21f90aa..3cf91ec 100644 --- a/backend/src/controllers/MessageController.ts +++ b/backend/src/controllers/MessageController.ts @@ -39,7 +39,7 @@ export const store = async (req: Request, res: Response): Promise => { const { ticketId } = req.params; const { body, quotedMsg }: MessageData = req.body; const medias = req.files as Express.Multer.File[]; - + const ticket = await ShowTicketService(ticketId); SetTicketMessagesAsRead(ticket); @@ -51,6 +51,9 @@ export const store = async (req: Request, res: Response): Promise => { }) ); } else { + + console.log('------- quotedMsg: ', quotedMsg) + await SendWhatsAppMessage({ body, ticket, quotedMsg }); } diff --git a/backend/src/controllers/TicketController.ts b/backend/src/controllers/TicketController.ts index 0744b5f..1d9870f 100644 --- a/backend/src/controllers/TicketController.ts +++ b/backend/src/controllers/TicketController.ts @@ -70,6 +70,11 @@ export const index = async (req: Request, res: Response): Promise => { export const store = async (req: Request, res: Response): Promise => { const { contactId, status, userId }: TicketData = req.body; + // naty + // console.log( + // `contactId: ${contactId} \n| status: ${status} \n| userId: ${userId}` + // ) + const ticket = await CreateTicketService({ contactId, status, userId }); const io = getIO(); diff --git a/backend/src/helpers/SchedulingNotifySendMessage.ts b/backend/src/helpers/SchedulingNotifySendMessage.ts new file mode 100644 index 0000000..d349085 --- /dev/null +++ b/backend/src/helpers/SchedulingNotifySendMessage.ts @@ -0,0 +1,76 @@ +import SetTicketMessagesAsRead from "../helpers/SetTicketMessagesAsRead"; +import ShowTicketService from "../services/TicketServices/ShowTicketService"; +import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage"; + +import ListSchedulingNotifyService from "../services/SchedulingNotifyServices/ListSchedulingNotifyService"; +import DeleteSchedulingNotifyService from "../services/SchedulingNotifyServices/DeleteSchedulingNotifyService"; + +let countTest: number = 0 +let scheduler_monitor:any; + +// const test = async () => { + +// const ticket = await ShowTicketService('336'); + +// SetTicketMessagesAsRead(ticket); + +// await SendWhatsAppMessage({ body: 'Olá Sr Adriano, estamos entrando em contato para confirmar seu retorno hoje', ticket }); + +// } + + +const monitor = async () => { + countTest += 1 + console.log(`${countTest}`) + + + let date = new Date() + + let day = date.getDate().toString().padStart(2, '0'); + let month = (date.getMonth()+1).toString().padStart(2, '0'); + let year = date.getFullYear(); + + let hour = date.getHours() + let minute = date.getMinutes() + + let fullDate = `${year}-${month}-${day}`; + let dateParm = `${fullDate} ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}:00` + + console.log(dateParm); + + + const { schedulingNotifies, count, hasMore } = await ListSchedulingNotifyService({ searchParam: dateParm, pageNumber: "1" }); + + if(schedulingNotifies.length){ + + const ticket = await ShowTicketService(schedulingNotifies[0].ticketId); + + SetTicketMessagesAsRead(ticket); + + await SendWhatsAppMessage({ + body: schedulingNotifies[0].message, ticket + }); + + DeleteSchedulingNotifyService(schedulingNotifies[0].id) + + } +}; + + +export const startSchedulingMonitor =async (mileseconds: number) => { + + scheduler_monitor = setInterval(monitor, mileseconds) + +} + + +export const stopSchedulingMonitor =async ( ) => { + + clearInterval(scheduler_monitor) + +} + + + + + \ No newline at end of file diff --git a/backend/src/routes/messageRoutes.ts b/backend/src/routes/messageRoutes.ts index a97303b..31141c4 100644 --- a/backend/src/routes/messageRoutes.ts +++ b/backend/src/routes/messageRoutes.ts @@ -11,12 +11,7 @@ const upload = multer(uploadConfig); messageRoutes.get("/messages/:ticketId", isAuth, MessageController.index); -messageRoutes.post( - "/messages/:ticketId", - isAuth, - upload.array("medias"), - MessageController.store -); +messageRoutes.post("/messages/:ticketId", isAuth, upload.array("medias"), MessageController.store); messageRoutes.delete("/messages/:messageId", isAuth, MessageController.remove); diff --git a/backend/src/server.ts b/backend/src/server.ts index b76e73c..63b5dfb 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -4,6 +4,8 @@ import { initIO } from "./libs/socket"; import { logger } from "./utils/logger"; import { StartAllWhatsAppsSessions } from "./services/WbotServices/StartAllWhatsAppsSessions"; +import { startSchedulingMonitor } from "./helpers/SchedulingNotifySendMessage" + const server = app.listen(process.env.PORT, () => { logger.info(`Server started on port: ${process.env.PORT}`); }); @@ -11,3 +13,5 @@ const server = app.listen(process.env.PORT, () => { initIO(server); StartAllWhatsAppsSessions(); gracefulShutdown(server); + +startSchedulingMonitor(5000) diff --git a/backend/src/services/SchedulingNotifyServices/ListSchedulingNotifyService.ts b/backend/src/services/SchedulingNotifyServices/ListSchedulingNotifyService.ts index 94cff28..e80558d 100644 --- a/backend/src/services/SchedulingNotifyServices/ListSchedulingNotifyService.ts +++ b/backend/src/services/SchedulingNotifyServices/ListSchedulingNotifyService.ts @@ -1,4 +1,4 @@ -import { Sequelize } from "sequelize"; +import { Op, Sequelize } from "sequelize"; import SchedulingNotify from "../../models/SchedulingNotify"; interface Request { @@ -16,14 +16,39 @@ interface Request { searchParam = "", pageNumber = "1" }: Request): Promise => { - const whereCondition = { - message: Sequelize.where( - Sequelize.fn("LOWER", Sequelize.col("cpf_cnpj")), "LIKE", `%${searchParam.toLowerCase().trim()}%`) + // const whereCondition = { + // message: Sequelize.where( + + // Sequelize.fn("date", Sequelize.col("schedulingDate")), `${searchParam.toLowerCase().trim()}`, + + // ), + + // }; + + let date = searchParam.split(' ')[0] + let hour = searchParam.split(' ')[1].split(':')[0] + let minute = searchParam.split(' ')[1].split(':')[1] + + const whereCondition = { + [Op.and]: [ + { + "$schedulingDate$": Sequelize.where(Sequelize.fn("date", Sequelize.col("schedulingDate")), `${date.trim()}`) + }, + { + "$schedulingDate$": Sequelize.where(Sequelize.fn("hour", Sequelize.col("schedulingDate")), `${hour.trim()}`) + }, + { + "$schedulingDate$": Sequelize.where(Sequelize.fn("minute", Sequelize.col("schedulingDate")), `${minute.trim()}`) + } + ] }; - const limit = 20; + + + const limit = 1; const offset = limit * (+pageNumber - 1); const { count, rows: schedulingNotifies } = await SchedulingNotify.findAndCountAll({ + raw: true, where: whereCondition, limit, offset, diff --git a/frontend/src/components/TicketActionButtons/index.js b/frontend/src/components/TicketActionButtons/index.js index ee7d232..3f6891d 100644 --- a/frontend/src/components/TicketActionButtons/index.js +++ b/frontend/src/components/TicketActionButtons/index.js @@ -61,7 +61,7 @@ const TicketActionButtons = ({ ticket, schedule }) => { } - const handleModal = (status, userId) => { + const handleModal = (/*status, userId*/) => { render( { color="primary" onClick={e => { - handleModal( "closed", user?.id) + + handleModal() // handleUpdateTicketStatus(e, "closed", user?.id) }}