feat: Automatically return remote/campaign tickets to awaiting when agent does not send campaign message

feat-scaling-ticket-remote-creation
adriano 2024-05-07 09:49:49 -03:00
parent 8eb2156845
commit 96a7e23ee6
4 changed files with 101 additions and 2 deletions

View File

@ -303,7 +303,8 @@ export const remoteTicketCreation = async (
id: ticket.id, id: ticket.id,
createdAt: ticket.createdAt, createdAt: ticket.createdAt,
updatedAt: ticket.updatedAt, updatedAt: ticket.updatedAt,
whatsappId: ticket.whatsappId whatsappId: ticket.whatsappId,
status: ticket.status
}) })
); );
@ -624,6 +625,26 @@ export const update = async (
await setMessageAsRead(ticket); await setMessageAsRead(ticket);
} }
if (ticketData.status == "open" || ticketData.status == "pending") {
let ticketRemote = await get({
key: `remote:ticketId:${ticketId}`
});
if (ticketRemote) {
ticketRemote = JSON.parse(ticketRemote);
ticketRemote = {
...ticketRemote,
...{
status: ticketData.status == "open" ? "open" : "pending",
updatedAt: ticket.updatedAt
}
};
set(`remote:ticketId:${ticketId}`, JSON.stringify(ticketRemote));
}
}
if (ticketData.userId) { if (ticketData.userId) {
const dateToday = splitDateTime( const dateToday = splitDateTime(
new Date(format(new Date(), "yyyy-MM-dd HH:mm:ss", { locale: ptBR })) new Date(format(new Date(), "yyyy-MM-dd HH:mm:ss", { locale: ptBR }))

View File

@ -2,6 +2,7 @@ import { intervalToDuration } from "date-fns";
import { del, get, set } from "./RedisClient"; import { del, get, set } from "./RedisClient";
import { getIO } from "../libs/socket"; import { getIO } from "../libs/socket";
import controllByNumber from "./controllByNumber"; import controllByNumber from "./controllByNumber";
import UpdateTicketService from "../services/TicketServices/UpdateTicketService";
let timer: any; let timer: any;
@ -11,6 +12,64 @@ const AutoRemoteTickets = async () => {
if (!obj?.tickets) return; if (!obj?.tickets) return;
// console.log("remote tickets: ", obj?.tickets);
for (const ticket of obj.tickets) {
if (!ticket.includes("open") || ticket.includes("messageDateTime"))
continue;
const regex = /"updatedAt":"([^"]+)"/;
let match = ticket.match(regex);
const updatedAt = match ? match[1] : null;
console.log("updatedAt: ", updatedAt);
let timeDiff: any = intervalToDuration({
start: new Date(updatedAt),
end: new Date()
});
console.log("----------> idle time timeDiff: ", timeDiff);
if (timeDiff.minutes > 2) {
match = ticket.match(/"id":(\d+)/);
let ticketId = match ? match[1] : null;
console.log("ticketId: ", ticketId);
ticketId = JSON.parse(ticketId);
let ticketRemote = await get({
key: `remote:ticketId:${ticketId}`
});
if (ticketRemote) {
ticketRemote = JSON.parse(ticketRemote);
ticketRemote = {
...ticketRemote,
...{ status: "pending" }
};
set(`remote:ticketId:${ticketId}`, JSON.stringify(ticketRemote));
await UpdateTicketService({
ticketData: {
status: "pending",
userId: null
},
ticketId
});
const io = getIO();
io.emit("remoteTickesControllIdleOpen", {
action: "update",
ticketId
});
}
}
}
for (const ticket of obj.tickets) { for (const ticket of obj.tickets) {
if (!ticket.includes("messageDateTime")) continue; if (!ticket.includes("messageDateTime")) continue;

View File

@ -15,7 +15,7 @@ var flatten = require("flat");
interface TicketData { interface TicketData {
status?: string; status?: string;
userId?: number; userId?: number | null;
queueId?: number; queueId?: number;
statusChatEnd?: string; statusChatEnd?: string;
statusChatEndId?: number; statusChatEndId?: number;

View File

@ -158,6 +158,25 @@ const Ticket = () => {
} }
}) })
socket.on("remoteTickesControllIdleOpen", (data) => {
if (data.action === "update") {
let url_ticketId
try {
let url_split = window.location.href.split('tickets')
url_ticketId = url_split[url_split.length - 1].match(/\d+/)[0]
if (url_ticketId && +url_ticketId === data.ticketId) {
history.push("/tickets")
}
} catch (error) {
console.log('error on try do the send seen: ', error)
}
}
})
return () => { return () => {
socket.disconnect() socket.disconnect()
} }