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";
|
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
import { createOrUpdateTicketCache } from '../../helpers/TicketCache'
|
|
|
|
var flatten = require('flat')
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
interface TicketData {
|
|
|
|
status?: string;
|
|
|
|
userId?: number;
|
|
|
|
queueId?: number;
|
2022-08-08 18:28:47 +00:00
|
|
|
statusChatEnd?: string
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Request {
|
|
|
|
ticketData: TicketData;
|
|
|
|
ticketId: string | number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Response {
|
|
|
|
ticket: Ticket;
|
|
|
|
oldStatus: string;
|
|
|
|
oldUserId: number | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UpdateTicketService = async ({
|
|
|
|
ticketData,
|
|
|
|
ticketId
|
|
|
|
}: Request): Promise<Response> => {
|
2022-05-19 21:29:38 +00:00
|
|
|
const { status, userId, queueId, statusChatEnd } = ticketData;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const ticket = await ShowTicketService(ticketId);
|
2023-02-07 15:47:40 +00:00
|
|
|
// await SetTicketMessagesAsRead(ticket);
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const oldStatus = ticket.status;
|
|
|
|
const oldUserId = ticket.user?.id;
|
|
|
|
|
|
|
|
if (oldStatus === "closed") {
|
|
|
|
await CheckContactOpenTickets(ticket.contact.id);
|
2022-10-25 14:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
await ticket.update({
|
|
|
|
status,
|
|
|
|
queueId,
|
2022-05-19 21:29:38 +00:00
|
|
|
userId,
|
|
|
|
statusChatEnd
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await ticket.reload();
|
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
// TEST DEL
|
|
|
|
try {
|
|
|
|
|
|
|
|
// const { name, number } = await ShowContactService(ticket.contactId)
|
|
|
|
|
|
|
|
let jsonString = JSON.stringify(ticket); //convert to string to remove the sequelize specific meta data
|
|
|
|
let ticket_obj = JSON.parse(jsonString); //to make plain json
|
|
|
|
delete ticket_obj['contact']['extraInfo']
|
|
|
|
delete ticket_obj['user']
|
|
|
|
|
|
|
|
ticket_obj = flatten(ticket_obj)
|
|
|
|
|
|
|
|
await createOrUpdateTicketCache(`ticket:${ticket.id}`, ticket_obj)
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.log('There was an error on UpdateTicketService.ts on createTicketCache: ', error)
|
|
|
|
}
|
|
|
|
//
|
|
|
|
|
2022-08-09 18:44:28 +00:00
|
|
|
let io = getIO();
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
if (ticket.status !== oldStatus || ticket.user?.id !== oldUserId) {
|
|
|
|
io.to(oldStatus).emit("ticket", {
|
|
|
|
action: "delete",
|
|
|
|
ticketId: ticket.id
|
|
|
|
});
|
2022-08-09 18:44:28 +00:00
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
io.to(ticket.status)
|
|
|
|
.to("notification")
|
|
|
|
.to(ticketId.toString())
|
|
|
|
.emit("ticket", {
|
|
|
|
action: "update",
|
|
|
|
ticket
|
|
|
|
});
|
2022-08-08 18:28:47 +00:00
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-08-09 18:44:28 +00:00
|
|
|
io.emit("ticketStatus", {
|
2022-08-08 18:28:47 +00:00
|
|
|
action: "update",
|
2022-08-09 18:44:28 +00:00
|
|
|
ticketStatus: { ticketId: ticket.id, status: ticket.status }
|
|
|
|
});
|
2022-08-08 18:28:47 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
return { ticket, oldStatus, oldUserId };
|
|
|
|
};
|
|
|
|
|
|
|
|
export default UpdateTicketService;
|