feat: pending tickets remote at the top of the ticket list

gertec
adriano 2024-05-22 08:49:43 -03:00
parent 3574b2d8af
commit 0866cbce79
1 changed files with 26 additions and 6 deletions

View File

@ -1,7 +1,12 @@
import { values } from "sequelize/types/lib/operators";
import Ticket from "../models/Ticket";
import { get, set } from "./RedisClient"; 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() { 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 }; if (!tickets) return { ticketIds: [], tickets: null };
@ -9,20 +14,20 @@ async function controllByNumber() {
for (const ticket of tickets) { for (const ticket of tickets) {
let match = ticket.match(/"whatsappId":(\d+)/); let match = ticket.match(/"whatsappId":(\d+)/);
let whatsappId = match ? match[1] : null; let whatsappId = match ? match[1] : null;
const whatsapp = await get({ const whatsapp = await get({
key: `whatsapp:${whatsappId}` key: `whatsapp:${whatsappId}`
}); });
match = whatsapp.match(/"number":"(\d+)"/); match = whatsapp.match(/"number":"(\d+)"/);
let number = match ? match[1] : null; let number = match ? match[1] : null;
match = ticket.match(/"id":(\d+)/); match = ticket.match(/"id":(\d+)/);
let ticketId = match ? match[1] : null; let ticketId = match ? match[1] : null;
number = JSON.parse(number); number = JSON.parse(number);
ticketId = JSON.parse(ticketId); ticketId = JSON.parse(ticketId);
const index = controll.findIndex((c: any) => c.number == number); 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)); set(`remote:controll`, JSON.stringify(ticketIds));