projeto-hit/backend/src/services/TicketServices/FindOrCreateTicketService.ts

131 lines
3.3 KiB
TypeScript
Raw Normal View History

import { subHours, subMinutes, subSeconds } from "date-fns";
import { Op } from "sequelize";
import BotIsOnQueue from "../../helpers/BotIsOnQueue";
import Contact from "../../models/Contact";
import Ticket from "../../models/Ticket";
import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService";
import ShowTicketService from "./ShowTicketService";
import AppError from "../../errors/AppError";
2023-07-26 20:24:10 +00:00
import ListWhatsAppsNumber from "../WhatsappService/ListWhatsAppsNumber";
import { getSettingValue } from "../../helpers/WhaticketSettings";
const FindOrCreateTicketService = async (
contact: Contact,
whatsappId: number,
unreadMessages: number,
groupContact?: Contact,
queueId?: number | string,
isRemote?: boolean
): Promise<Ticket> => {
try {
2023-07-26 20:24:10 +00:00
let ticket;
if (getSettingValue("oneContactChatWithManyWhats")?.value == "enabled") {
let whats = await ListWhatsAppsNumber(whatsappId);
2023-07-26 20:24:10 +00:00
ticket = await Ticket.findOne({
where: {
status: {
[Op.or]: ["open", "pending", "queueChoice"]
},
contactId: groupContact ? groupContact.id : contact.id,
whatsappId: { [Op.in]: whats.whatsapps.map((w: any) => w.id) }
2023-07-26 20:24:10 +00:00
}
});
} else {
ticket = await Ticket.findOne({
where: {
status: {
[Op.or]: ["open", "pending", "queueChoice"]
},
contactId: groupContact ? groupContact.id : contact.id
2023-07-26 20:24:10 +00:00
}
});
}
const { queues, greetingMessage, phoneNumberId } =
await ShowWhatsAppService(whatsappId);
2023-07-26 20:24:10 +00:00
const botInfo = { isOnQueue: false };
if (ticket) {
await ticket.update({ unreadMessages });
}
if (!ticket && groupContact) {
ticket = await Ticket.findOne({
where: {
contactId: groupContact.id
},
order: [["updatedAt", "DESC"]]
});
2024-06-13 20:50:30 +00:00
if (ticket) {
await ticket.update({
status: "pending",
userId: null,
2024-06-13 20:50:30 +00:00
queueId: null,
unreadMessages
});
}
}
if (!ticket && !groupContact) {
ticket = await Ticket.findOne({
where: {
updatedAt: {
// Tempo osioso para a ura responder thuanny
//[Op.between]: [+subMinutes(new Date(), 30), +new Date()]
// Sub seconds
[Op.between]: [+subSeconds(new Date(), 0), +new Date()]
},
contactId: contact.id
},
order: [["updatedAt", "DESC"]]
});
if (ticket) {
await ticket.update({
status: "pending",
userId: null,
unreadMessages
});
}
}
if (!ticket) {
2023-07-26 20:24:10 +00:00
let status = "pending";
2024-06-13 20:50:30 +00:00
if (
queues.length > 1 &&
!botInfo.isOnQueue &&
!queueId &&
!groupContact
) {
2023-07-26 20:24:10 +00:00
status = "queueChoice";
}
ticket = await Ticket.create({
contactId: groupContact ? groupContact.id : contact.id,
status: status,
isGroup: !!groupContact,
queueId,
unreadMessages,
2023-09-08 19:50:51 +00:00
whatsappId,
phoneNumberId,
isRemote
});
}
ticket = await ShowTicketService(ticket.id);
return ticket;
} catch (error: any) {
2023-07-26 20:24:10 +00:00
console.error("===> Error on FindOrCreateTicketService.ts file: \n", error);
throw new AppError(error.message);
}
};
export default FindOrCreateTicketService;