2022-01-06 01:26:15 +00:00
|
|
|
import CheckContactOpenTickets from "../../helpers/CheckContactOpenTickets";
|
|
|
|
import SetTicketMessagesAsRead from "../../helpers/SetTicketMessagesAsRead";
|
|
|
|
import { getIO } from "../../libs/socket";
|
|
|
|
import Ticket from "../../models/Ticket";
|
|
|
|
import SendWhatsAppMessage from "../WbotServices/SendWhatsAppMessage";
|
|
|
|
import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService";
|
|
|
|
import ShowTicketService from "./ShowTicketService";
|
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
import { createOrUpdateTicketCache } from "../../helpers/TicketCache";
|
2023-04-20 18:48:42 +00:00
|
|
|
import AppError from "../../errors/AppError";
|
2023-07-12 14:54:29 +00:00
|
|
|
import sendWhatsAppMessageSocket from "../../helpers/SendWhatsappMessageSocket";
|
2024-02-05 15:29:49 +00:00
|
|
|
import BotIsOnQueue from "../../helpers/BotIsOnQueue";
|
2024-03-27 15:02:11 +00:00
|
|
|
import { deleteObject } from "../../helpers/RedisClient";
|
2023-07-26 20:24:10 +00:00
|
|
|
var flatten = require("flat");
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
interface TicketData {
|
|
|
|
status?: string;
|
|
|
|
userId?: number;
|
|
|
|
queueId?: number;
|
2023-07-26 20:24:10 +00:00
|
|
|
statusChatEnd?: string;
|
2024-03-27 15:02:11 +00:00
|
|
|
statusChatEndId?: number;
|
2023-07-12 14:54:29 +00:00
|
|
|
unreadMessages?: number;
|
2023-07-26 20:24:10 +00:00
|
|
|
whatsappId?: string | number;
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Request {
|
|
|
|
ticketData: TicketData;
|
2023-07-26 20:24:10 +00:00
|
|
|
ticketId: string | number;
|
|
|
|
msg?: string;
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Response {
|
|
|
|
ticket: Ticket;
|
|
|
|
oldStatus: string;
|
|
|
|
oldUserId: number | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UpdateTicketService = async ({
|
|
|
|
ticketData,
|
2023-07-26 20:24:10 +00:00
|
|
|
ticketId,
|
|
|
|
msg = ""
|
2022-01-06 01:26:15 +00:00
|
|
|
}: Request): Promise<Response> => {
|
2023-04-20 18:48:42 +00:00
|
|
|
try {
|
2023-07-26 20:24:10 +00:00
|
|
|
const {
|
|
|
|
status,
|
|
|
|
userId,
|
|
|
|
queueId,
|
|
|
|
statusChatEnd,
|
|
|
|
unreadMessages,
|
2024-03-27 15:02:11 +00:00
|
|
|
statusChatEndId,
|
2023-07-26 20:24:10 +00:00
|
|
|
whatsappId
|
|
|
|
} = ticketData;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2024-02-05 15:29:49 +00:00
|
|
|
const ticket = await ShowTicketService(ticketId);
|
|
|
|
|
|
|
|
const botInfo = await BotIsOnQueue("botqueue");
|
|
|
|
|
|
|
|
if (
|
|
|
|
status == "closed" ||
|
|
|
|
(status == "open" && ticket && `${userId}` != `${botInfo.userIdBot}`)
|
|
|
|
) {
|
|
|
|
deleteObject(`${ticket.whatsappId}`, `${ticket.contactId}`, "ura");
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
const oldStatus = ticket.status;
|
|
|
|
const oldUserId = ticket.user?.id;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
if (oldStatus === "closed") {
|
2023-07-26 20:24:10 +00:00
|
|
|
await CheckContactOpenTickets(ticket.contact.id, ticket.whatsappId);
|
2023-07-12 14:54:29 +00:00
|
|
|
}
|
2024-03-27 15:02:11 +00:00
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
await ticket.update({
|
|
|
|
status,
|
|
|
|
queueId,
|
|
|
|
userId,
|
|
|
|
unreadMessages,
|
2023-07-26 20:24:10 +00:00
|
|
|
statusChatEnd,
|
2024-03-27 15:02:11 +00:00
|
|
|
statusChatEndId,
|
2023-07-26 20:24:10 +00:00
|
|
|
whatsappId
|
2023-07-12 14:54:29 +00:00
|
|
|
});
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
await ticket.reload();
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2024-02-05 15:29:49 +00:00
|
|
|
if (msg?.trim().length > 0) {
|
2023-07-12 14:54:29 +00:00
|
|
|
setTimeout(async () => {
|
2024-03-21 12:16:31 +00:00
|
|
|
sendWhatsAppMessageSocket(ticket, `\u200e${msg}`);
|
2023-07-26 20:24:10 +00:00
|
|
|
}, 2000);
|
2023-07-12 14:54:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
// TEST DEL
|
2023-07-12 14:54:29 +00:00
|
|
|
try {
|
2023-07-26 20:24:10 +00:00
|
|
|
// const { name, number } = await ShowContactService(ticket.contactId)
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
let jsonString = JSON.stringify(ticket); //convert to string to remove the sequelize specific meta data
|
2023-07-12 14:54:29 +00:00
|
|
|
let ticket_obj = JSON.parse(jsonString); //to make plain json
|
2023-07-26 20:24:10 +00:00
|
|
|
delete ticket_obj["contact"]["extraInfo"];
|
|
|
|
delete ticket_obj["user"];
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
ticket_obj = flatten(ticket_obj);
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
await createOrUpdateTicketCache(`ticket:${ticket.id}`, ticket_obj);
|
2023-07-12 14:54:29 +00:00
|
|
|
} catch (error) {
|
2023-07-26 20:24:10 +00:00
|
|
|
console.log(
|
|
|
|
"There was an error on UpdateTicketService.ts on createTicketCache: ",
|
|
|
|
error
|
|
|
|
);
|
2023-07-12 14:54:29 +00:00
|
|
|
}
|
|
|
|
//
|
|
|
|
|
|
|
|
let io = getIO();
|
|
|
|
|
|
|
|
if (ticket.status !== oldStatus || ticket.user?.id !== oldUserId) {
|
|
|
|
io.to(oldStatus).emit("ticket", {
|
|
|
|
action: "delete",
|
|
|
|
ticketId: ticket.id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
io.to(ticket.status)
|
|
|
|
.to("notification")
|
|
|
|
.to(ticketId.toString())
|
|
|
|
.emit("ticket", {
|
|
|
|
action: "update",
|
|
|
|
ticket
|
|
|
|
});
|
|
|
|
|
|
|
|
io.emit("ticketStatus", {
|
2022-01-06 01:26:15 +00:00
|
|
|
action: "update",
|
2023-07-12 14:54:29 +00:00
|
|
|
ticketStatus: { ticketId: ticket.id, status: ticket.status }
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
2022-08-08 18:28:47 +00:00
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
return { ticket, oldStatus, oldUserId };
|
2023-04-20 18:48:42 +00:00
|
|
|
} catch (error: any) {
|
2023-07-26 20:24:10 +00:00
|
|
|
console.error("===> Error on UpdateTicketService.ts file: \n", error);
|
2023-04-20 18:48:42 +00:00
|
|
|
throw new AppError(error.message);
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
export default UpdateTicketService;
|