2022-03-23 02:59:01 +00:00
|
|
|
import { subHours, subMinutes, subSeconds } from "date-fns";
|
2022-01-06 01:26:15 +00:00
|
|
|
import { Op } from "sequelize";
|
2022-04-28 20:57:57 +00:00
|
|
|
import BotIsOnQueue from "../../helpers/BotIsOnQueue";
|
2022-01-06 01:26:15 +00:00
|
|
|
import Contact from "../../models/Contact";
|
|
|
|
import Ticket from "../../models/Ticket";
|
2022-04-28 20:57:57 +00:00
|
|
|
import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService";
|
2022-01-06 01:26:15 +00:00
|
|
|
import ShowTicketService from "./ShowTicketService";
|
|
|
|
|
2022-04-28 20:57:57 +00:00
|
|
|
|
|
|
|
|
2022-01-18 13:47:19 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const FindOrCreateTicketService = async (
|
|
|
|
contact: Contact,
|
|
|
|
whatsappId: number,
|
|
|
|
unreadMessages: number,
|
|
|
|
groupContact?: Contact
|
|
|
|
): Promise<Ticket> => {
|
|
|
|
let ticket = await Ticket.findOne({
|
|
|
|
where: {
|
|
|
|
status: {
|
2022-04-28 20:57:57 +00:00
|
|
|
[Op.or]: ["open", "pending", "queueChoice"]
|
2022-01-06 01:26:15 +00:00
|
|
|
},
|
|
|
|
contactId: groupContact ? groupContact.id : contact.id
|
|
|
|
}
|
2022-09-22 18:34:12 +00:00
|
|
|
});
|
2022-04-28 20:57:57 +00:00
|
|
|
|
|
|
|
const { queues, greetingMessage } = await ShowWhatsAppService(whatsappId);
|
2022-09-22 18:34:12 +00:00
|
|
|
|
2022-07-20 15:34:12 +00:00
|
|
|
|
|
|
|
//Habilitar esse caso queira usar o bot
|
|
|
|
// const botInfo = await BotIsOnQueue('botqueue')
|
|
|
|
const botInfo = {isOnQueue: false}
|
2022-04-28 20:57:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-09-22 18:34:12 +00:00
|
|
|
if (ticket) {
|
2022-01-06 01:26:15 +00:00
|
|
|
await ticket.update({ unreadMessages });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ticket && groupContact) {
|
|
|
|
ticket = await Ticket.findOne({
|
|
|
|
where: {
|
|
|
|
contactId: groupContact.id
|
|
|
|
},
|
|
|
|
order: [["updatedAt", "DESC"]]
|
|
|
|
});
|
|
|
|
|
2022-04-28 20:57:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (ticket) {
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
await ticket.update({
|
|
|
|
status: "pending",
|
|
|
|
userId: null,
|
|
|
|
unreadMessages
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:47:19 +00:00
|
|
|
if (!ticket && !groupContact) {
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
ticket = await Ticket.findOne({
|
|
|
|
where: {
|
|
|
|
updatedAt: {
|
2022-01-18 13:47:19 +00:00
|
|
|
//[Op.between]: [+subHours(new Date(), 2), +new Date()]
|
|
|
|
|
|
|
|
// Tempo osioso para a ura responder thuanny
|
2022-04-27 17:28:31 +00:00
|
|
|
//[Op.between]: [+subMinutes(new Date(), 30), +new Date()]
|
2022-04-20 19:01:58 +00:00
|
|
|
|
|
|
|
// Sub seconds
|
2022-09-22 18:34:12 +00:00
|
|
|
[Op.between]: [+subSeconds(new Date(), 0), +new Date()]
|
2022-01-06 01:26:15 +00:00
|
|
|
},
|
|
|
|
contactId: contact.id
|
|
|
|
},
|
|
|
|
order: [["updatedAt", "DESC"]]
|
|
|
|
});
|
|
|
|
|
2022-04-28 20:57:57 +00:00
|
|
|
if (ticket) {
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
await ticket.update({
|
|
|
|
status: "pending",
|
|
|
|
userId: null,
|
|
|
|
unreadMessages
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 20:57:57 +00:00
|
|
|
if (!ticket) {
|
|
|
|
|
|
|
|
let status = "pending"
|
|
|
|
|
|
|
|
if(queues.length > 1 && !botInfo.isOnQueue){
|
|
|
|
status = "queueChoice"
|
|
|
|
}
|
2022-01-18 13:47:19 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
ticket = await Ticket.create({
|
|
|
|
contactId: groupContact ? groupContact.id : contact.id,
|
2022-04-28 20:57:57 +00:00
|
|
|
status: status,
|
2022-01-06 01:26:15 +00:00
|
|
|
isGroup: !!groupContact,
|
|
|
|
unreadMessages,
|
|
|
|
whatsappId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ticket = await ShowTicketService(ticket.id);
|
|
|
|
|
|
|
|
return ticket;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FindOrCreateTicketService;
|