Ajustes para fechamentos de tickets automatico do bot

pull/20/head
adriano 2023-08-08 16:09:31 -03:00
parent 69d99206e9
commit 8dd865281e
4 changed files with 66 additions and 48 deletions

View File

@ -20,7 +20,6 @@ let timer: any;
const AutoCloseTickets = async () => {
try {
// const botInfo = await BotIsOnQueue('botqueue')
// if (!botInfo.userIdBot) return

View File

@ -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";

View File

@ -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<any[]> => {
const ListTicketTimeLife = async ({
timeseconds,
status,
userId
}: Request): Promise<any[]> => {
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;

View File

@ -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<void> => {
// 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<void> => {
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<void> => {
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<void> => {
console.log("Is the time within the range?", isWithinRange);
return { isWithinRange, message: outBusinessHours.message };
return {
isWithinRange,
message: outBusinessHours.message
};
}
}
};