From 0866cbce79121f82351a8251d8b4c01643f4047f Mon Sep 17 00:00:00 2001 From: adriano Date: Wed, 22 May 2024 08:49:43 -0300 Subject: [PATCH] feat: pending tickets remote at the top of the ticket list --- backend/src/helpers/controllByNumber.ts | 32 ++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/backend/src/helpers/controllByNumber.ts b/backend/src/helpers/controllByNumber.ts index d74afa4..e202f6f 100644 --- a/backend/src/helpers/controllByNumber.ts +++ b/backend/src/helpers/controllByNumber.ts @@ -1,7 +1,12 @@ +import { values } from "sequelize/types/lib/operators"; +import Ticket from "../models/Ticket"; import { get, set } from "./RedisClient"; +import { getIO } from "../libs/socket"; +import UpdateTicketService from "../services/TicketServices/UpdateTicketService"; +import { v4 as uuidv4 } from "uuid"; async function controllByNumber() { - let tickets = await get({ key: "remote:ticketId*", parse: false }); + let tickets = await get({ key: "remote:ticketId*", parse: false }); if (!tickets) return { ticketIds: [], tickets: null }; @@ -9,20 +14,20 @@ async function controllByNumber() { for (const ticket of tickets) { let match = ticket.match(/"whatsappId":(\d+)/); - let whatsappId = match ? match[1] : null; + let whatsappId = match ? match[1] : null; const whatsapp = await get({ key: `whatsapp:${whatsappId}` }); match = whatsapp.match(/"number":"(\d+)"/); - let number = match ? match[1] : null; + let number = match ? match[1] : null; match = ticket.match(/"id":(\d+)/); - let ticketId = match ? match[1] : null; + let ticketId = match ? match[1] : null; number = JSON.parse(number); - ticketId = JSON.parse(ticketId); + ticketId = JSON.parse(ticketId); const index = controll.findIndex((c: any) => c.number == number); @@ -31,7 +36,22 @@ async function controllByNumber() { } } - const ticketIds = controll.map((c: any) => c.ticketId); + const ticketIds = controll.map((c: any) => c.ticketId); + + console.log("=======> ticketIds: ", ticketIds); + + for (const ticketId of ticketIds) { + const ticket: any = await Ticket.findByPk(ticketId); + + const { status } = ticket; + + if (status == "pending") { + await UpdateTicketService({ + ticketData: { statusChatEnd: uuidv4() }, + ticketId: ticket.id + }); + } + } set(`remote:controll`, JSON.stringify(ticketIds));