From 8dd865281e0ee237adf25a56c6d401eafd6628b2 Mon Sep 17 00:00:00 2001 From: adriano Date: Tue, 8 Aug 2023 16:09:31 -0300 Subject: [PATCH] Ajustes para fechamentos de tickets automatico do bot --- backend/src/helpers/AutoCloseTickets.ts | 1 - backend/src/server.ts | 3 +- .../TicketServices/ListTicketTimeLife.ts | 69 +++++++++++-------- .../WbotServices/wbotMessageListener.ts | 41 ++++++----- 4 files changed, 66 insertions(+), 48 deletions(-) diff --git a/backend/src/helpers/AutoCloseTickets.ts b/backend/src/helpers/AutoCloseTickets.ts index 2948035..3487e4d 100644 --- a/backend/src/helpers/AutoCloseTickets.ts +++ b/backend/src/helpers/AutoCloseTickets.ts @@ -20,7 +20,6 @@ let timer: any; const AutoCloseTickets = async () => { try { - // const botInfo = await BotIsOnQueue('botqueue') // if (!botInfo.userIdBot) return diff --git a/backend/src/server.ts b/backend/src/server.ts index 5946f37..cb03ac9 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -9,10 +9,11 @@ import endPointQuery from "./helpers/old_EndPointQuery"; import { cacheSize, flushCache, loadTicketsCache } from "./helpers/TicketCache"; import { loadContactsCache } from "./helpers/ContactsCache"; -import "./helpers/CloseBotTickets"; + import { loadSchedulesCache } from "./helpers/SchedulingNotifyCache"; import { delRestoreControllFile } from "./helpers/RestoreControll"; import "./helpers/AutoCloseTickets"; +import "./helpers/CloseBotTickets"; import "./helpers/SchedulingNotifySendMessage" import axios from "axios"; diff --git a/backend/src/services/TicketServices/ListTicketTimeLife.ts b/backend/src/services/TicketServices/ListTicketTimeLife.ts index 0e8e49c..2d40a1e 100644 --- a/backend/src/services/TicketServices/ListTicketTimeLife.ts +++ b/backend/src/services/TicketServices/ListTicketTimeLife.ts @@ -1,46 +1,57 @@ - -import { Sequelize, } from "sequelize"; +import { Sequelize } from "sequelize"; const dbConfig = require("../../config/database"); -const sequelize = new Sequelize(dbConfig); -const { QueryTypes } = require('sequelize'); +const sequelize = new Sequelize(dbConfig); +const { QueryTypes } = require("sequelize"); import { splitDateTime } from "../../helpers/SplitDateTime"; -import format from 'date-fns/format'; -import ptBR from 'date-fns/locale/pt-BR'; +import format from "date-fns/format"; +import ptBR from "date-fns/locale/pt-BR"; +import BotIsOnQueue from "../../helpers/BotIsOnQueue"; -interface Request { - timeseconds: string | number; - status: string; - userId?: string | number; +interface Request { + timeseconds: string | number; + status: string; + userId?: string | number; } -const ListTicketTimeLife = async ({timeseconds, status, userId }: Request): Promise => { +const ListTicketTimeLife = async ({ + timeseconds, + status, + userId +}: Request): Promise => { + let tickets = []; - let tickets = [] + let currentDate = format(new Date(), "yyyy-MM-dd HH:mm:ss", { locale: ptBR }); - let currentDate = format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }) + // console.log('------------------> currentDate: ', currentDate) - // console.log('------------------> currentDate: ', currentDate) - - if (userId) { - // CONSULTANDO FILAS PELO ID DO USUARIO - tickets = await sequelize.query(`select user.id as user_id, user.name as user_name, t.id as ticket_id from Tickets as t inner join Users as user on - t.userId = user.id and user.name = 'botqueue' and t.status='${status}' and (TIMESTAMPDIFF(SECOND, t.updatedAt, '${currentDate}')) >= ${timeseconds};`, { type: QueryTypes.SELECT }); + if (userId) { + // CONSULTANDO FILAS PELO ID DO USUARIO + tickets = await sequelize.query( + `select user.id as user_id, user.name as user_name, t.id as ticket_id from Tickets as t inner join Users as user on + t.userId = user.id and user.name = 'botqueue' and t.status='${status}' and (TIMESTAMPDIFF(SECOND, t.updatedAt, '${currentDate}')) >= ${timeseconds};`, + { type: QueryTypes.SELECT } + ); + } else { + const botInfo = await BotIsOnQueue("botqueue"); + if (botInfo.isOnQueue) { + // CONSULTANDO FILAS PELO USUARIO + tickets = await sequelize.query( + `select id as ticket_id from Tickets where status='${status}' and userId != ${botInfo.userIdBot} and (TIMESTAMPDIFF(SECOND, updatedAt, '${currentDate}')) >= ${timeseconds};`, + { type: QueryTypes.SELECT } + ); } else { - - // CONSULTANDO FILAS PELO USUARIO - tickets = await sequelize.query(`select id as ticket_id from Tickets where status='${status}' and - (TIMESTAMPDIFF(SECOND, updatedAt, '${currentDate}')) >= ${timeseconds};`, { type: QueryTypes.SELECT }); - + // CONSULTANDO FILAS PELO USUARIO + tickets = await sequelize.query( + `select id as ticket_id from Tickets where status='${status}' and (TIMESTAMPDIFF(SECOND, updatedAt, '${currentDate}')) >= ${timeseconds};`, + { type: QueryTypes.SELECT } + ); } + } - return tickets; + return tickets; }; export default ListTicketTimeLife; - - - - diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index 7645dd9..5a4a9d1 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -81,14 +81,14 @@ import { setMessageAsRead } from "../../helpers/SetMessageAsRead"; import FindOrCreateQueryItemService from "../SLM/FindOrCreateQueryItemService"; import ShowQueryItemService from "../SLM/ShowQueryItemService"; import QueryItem from "../../models/QueryItem"; -import SettingTicket from "../../models/SettingTicket" +import SettingTicket from "../../models/SettingTicket"; import { format as _format, isWithinInterval, parse, subMinutes -} from "date-fns"; +} from "date-fns"; var lst: any[] = getWhatsappIds(); @@ -922,20 +922,18 @@ const handleMessage = async (msg: any, wbot: any): Promise => { // let groupContact: Contact | undefined; if (msg.fromMe) { + const ticketExpiration = await SettingTicket.findOne({ + where: { key: "ticketExpiration" } + }); - const ticketExpiration = await SettingTicket.findOne({ - where: { key: "ticketExpiration" } - }); - - if ( - ticketExpiration && - ticketExpiration.value == "enabled" && - ticketExpiration?.message.trim() == msg.body.trim() - ) { - console.log("*********** TICKET EXPIRATION"); - return; - } - + if ( + ticketExpiration && + ticketExpiration.value == "enabled" && + ticketExpiration?.message.trim() == msg.body.trim() + ) { + console.log("*********** TICKET EXPIRATION"); + return; + } // console.log('FROM ME: ', msg.fromMe, ' | /\u200e/.test(msg.body[0]: ', (/\u200e/.test(msg.body[0]))) @@ -1235,6 +1233,7 @@ const handleMessage = async (msg: any, wbot: any): Promise => { if ( msg.fromMe && businessTime && + businessTime.value == "enabled" && !businessTime.isWithinRange && businessTime.message.trim() == msg.body.trim() ) { @@ -1242,7 +1241,12 @@ const handleMessage = async (msg: any, wbot: any): Promise => { return; } - if (!businessTime.isWithinRange && businessTime.message.trim().length > 0) { + if ( + businessTime && + businessTime.value == "enabled" && + !businessTime.isWithinRange && + businessTime.message.trim().length > 0 + ) { botSendMessage(ticket, businessTime.message); } } catch (err) { @@ -1305,7 +1309,10 @@ const handleMessage = async (msg: any, wbot: any): Promise => { console.log("Is the time within the range?", isWithinRange); - return { isWithinRange, message: outBusinessHours.message }; + return { + isWithinRange, + message: outBusinessHours.message + }; } } };