2022-01-06 01:26:15 +00:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
import { getIO } from "../libs/socket";
|
|
|
|
|
|
|
|
import CreateTicketService from "../services/TicketServices/CreateTicketService";
|
|
|
|
import DeleteTicketService from "../services/TicketServices/DeleteTicketService";
|
2022-05-19 21:29:38 +00:00
|
|
|
import ListTicketsService from "../services/TicketServices/ListTicketsService";
|
2022-01-06 01:26:15 +00:00
|
|
|
import ShowTicketService from "../services/TicketServices/ShowTicketService";
|
|
|
|
import UpdateTicketService from "../services/TicketServices/UpdateTicketService";
|
|
|
|
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
|
|
|
|
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
2023-07-26 20:24:10 +00:00
|
|
|
import ShowStatusChatEndService from "../services/StatusChatEndService/ShowStatusChatEndService";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
import CreateSchedulingNotifyService from "../services/SchedulingNotifyServices/CreateSchedulingNotifyService";
|
|
|
|
import ListSchedulingNotifyContactService from "../services/SchedulingNotifyServices/ListSchedulingNotifyContactService";
|
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
import { isScheduling } from "../helpers/CheckSchedulingReminderNotify";
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
import ptBR from "date-fns/locale/pt-BR";
|
2022-05-16 02:48:06 +00:00
|
|
|
import { splitDateTime } from "../helpers/SplitDateTime";
|
2023-07-26 20:24:10 +00:00
|
|
|
import format from "date-fns/format";
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
import ListTicketsServiceCache from "../services/TicketServices/ListTicketServiceCache";
|
2022-05-19 21:29:38 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
import { searchTicketCache, loadTicketsCache } from "../helpers/TicketCache";
|
2024-04-02 18:33:34 +00:00
|
|
|
import { Op, where } from "sequelize";
|
2022-01-10 20:10:20 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
type IndexQuery = {
|
|
|
|
searchParam: string;
|
|
|
|
pageNumber: string;
|
|
|
|
status: string;
|
|
|
|
date: string;
|
|
|
|
showAll: string;
|
|
|
|
withUnreadMessages: string;
|
|
|
|
queueIds: string;
|
2022-04-18 18:21:28 +00:00
|
|
|
unlimited?: string;
|
2023-07-26 20:24:10 +00:00
|
|
|
searchParamContent?: string;
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
interface TicketData {
|
|
|
|
contactId: number;
|
|
|
|
status: string;
|
|
|
|
queueId: number;
|
|
|
|
userId: number;
|
2023-07-26 20:24:10 +00:00
|
|
|
whatsappId?: string | number;
|
|
|
|
msg?: string;
|
|
|
|
transfer?: boolean | undefined;
|
|
|
|
fromMe?: boolean;
|
2022-05-19 21:29:38 +00:00
|
|
|
}
|
2022-01-10 20:10:20 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
import ListStatusChatEndService from "../services/StatusChatEndService/ListStatusChatEndService";
|
2022-05-03 21:20:58 +00:00
|
|
|
import Ticket from "../models/Ticket";
|
2022-05-16 02:48:06 +00:00
|
|
|
import ShowUserServiceReport from "../services/UserServices/ShowUserServiceReport";
|
|
|
|
import TicketEmiterSumOpenClosedByUser from "../helpers/OnlineReporEmiterInfoByUser";
|
2022-08-08 16:47:28 +00:00
|
|
|
import CountTicketService from "../services/TicketServices/CountTicketService";
|
|
|
|
import CountTicketsByUserQueue from "../services/UserServices/CountTicketsByUserQueue";
|
|
|
|
import ShowUserService from "../services/UserServices/ShowUserService";
|
2023-07-12 14:54:29 +00:00
|
|
|
import axios from "axios";
|
|
|
|
import User from "../models/User";
|
|
|
|
import CheckContactOpenTickets from "../helpers/CheckContactOpenTickets";
|
|
|
|
import GetDefaultWhatsApp from "../helpers/GetDefaultWhatsApp";
|
|
|
|
import { getWbot } from "../libs/wbot";
|
|
|
|
import endPointQuery from "../helpers/old_EndPointQuery";
|
|
|
|
import Contact from "../models/Contact";
|
|
|
|
import BotIsOnQueue from "../helpers/BotIsOnQueue";
|
|
|
|
import { setMessageAsRead } from "../helpers/SetMessageAsRead";
|
2023-07-28 12:25:13 +00:00
|
|
|
import { getSettingValue } from "../helpers/WhaticketSettings";
|
2024-01-29 11:48:20 +00:00
|
|
|
import ListWhatsAppsForQueueService from "../services/WhatsappService/ListWhatsAppsForQueueService";
|
|
|
|
import ListWhatsAppsNumber from "../services/WhatsappService/ListWhatsAppsNumber";
|
|
|
|
import Whatsapp from "../models/Whatsapp";
|
|
|
|
import AppError from "../errors/AppError";
|
2024-02-21 20:47:23 +00:00
|
|
|
import CreateOrUpdateContactService from "../services/ContactServices/CreateOrUpdateContactService";
|
|
|
|
import FindOrCreateTicketService from "../services/TicketServices/FindOrCreateTicketService";
|
|
|
|
import CheckIsValidContact from "../services/WbotServices/CheckIsValidContact";
|
|
|
|
import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
|
|
|
|
import CreateContactService from "../services/ContactServices/CreateContactService";
|
|
|
|
import { botSendMessage } from "../services/WbotServices/wbotMessageListener";
|
|
|
|
import WhatsappQueue from "../models/WhatsappQueue";
|
2024-04-23 22:51:11 +00:00
|
|
|
import { del, get, set } from "../helpers/RedisClient";
|
2024-04-01 20:06:11 +00:00
|
|
|
import CountStatusChatEndService from "../services/StatusChatEndService/CountStatusChatEndService";
|
2024-04-03 21:38:56 +00:00
|
|
|
import Queue from "../models/Queue";
|
2024-04-12 21:33:15 +00:00
|
|
|
import StatusChatEnd from "../models/StatusChatEnd";
|
2024-04-23 22:51:11 +00:00
|
|
|
import controllByNumber from "../helpers/controllByNumber";
|
2024-03-13 14:19:40 +00:00
|
|
|
|
2024-04-01 20:06:11 +00:00
|
|
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
2022-01-06 01:26:15 +00:00
|
|
|
const {
|
|
|
|
pageNumber,
|
|
|
|
status,
|
|
|
|
date,
|
|
|
|
searchParam,
|
|
|
|
showAll,
|
|
|
|
queueIds: queueIdsStringified,
|
2022-04-18 18:21:28 +00:00
|
|
|
withUnreadMessages,
|
2023-07-12 14:54:29 +00:00
|
|
|
unlimited,
|
|
|
|
searchParamContent
|
2022-09-05 16:36:42 +00:00
|
|
|
} = req.query as IndexQuery;
|
2022-04-18 18:21:28 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
const userId = req.user.id;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
let queueIds: number[] = [];
|
|
|
|
|
2024-03-07 21:03:46 +00:00
|
|
|
if (queueIdsStringified && queueIdsStringified.trim().length > 0) {
|
2022-01-06 01:26:15 +00:00
|
|
|
queueIds = JSON.parse(queueIdsStringified);
|
2024-03-07 21:03:46 +00:00
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2024-04-23 22:51:11 +00:00
|
|
|
const { tickets, count, hasMore, remoteTicketsControll } =
|
|
|
|
await ListTicketsService({
|
|
|
|
searchParam,
|
|
|
|
pageNumber,
|
|
|
|
status,
|
|
|
|
date,
|
|
|
|
showAll,
|
|
|
|
userId,
|
|
|
|
queueIds,
|
|
|
|
withUnreadMessages,
|
|
|
|
unlimited,
|
|
|
|
searchParamContent
|
|
|
|
});
|
2024-04-01 20:06:11 +00:00
|
|
|
|
2024-04-23 22:51:11 +00:00
|
|
|
return res
|
|
|
|
.status(200)
|
|
|
|
.json({ tickets, count, hasMore, remoteTicketsControll });
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
2024-02-21 20:47:23 +00:00
|
|
|
export const remoteTicketCreation = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
2024-04-03 21:38:56 +00:00
|
|
|
let { queueId, contact_from, cc, contact_to, msg, contact_name }: any =
|
|
|
|
req.body;
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-02 18:33:34 +00:00
|
|
|
let whatsappId: any;
|
|
|
|
|
2024-04-03 21:38:56 +00:00
|
|
|
if (!queueId && !contact_from && !cc) {
|
|
|
|
return res.status(400).json({
|
|
|
|
error: `Property 'queueId' or 'contact_from' or 'cc' is required.`
|
|
|
|
});
|
2024-04-02 18:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const validate = ["contact_to", "msg"];
|
|
|
|
const validateOnlyNumber = ["queueId", "contact_to", "contact_from"];
|
2024-02-21 20:47:23 +00:00
|
|
|
|
|
|
|
for (let prop of validate) {
|
|
|
|
if (!req.body[prop])
|
|
|
|
return res
|
|
|
|
.status(400)
|
|
|
|
.json({ error: `Property '${prop}' is undefined.` });
|
2024-02-26 19:58:39 +00:00
|
|
|
|
|
|
|
if (validateOnlyNumber.includes(prop)) {
|
|
|
|
if (!/^\d+$/.test(req.body[prop])) {
|
2024-02-23 17:36:52 +00:00
|
|
|
return res
|
|
|
|
.status(400)
|
|
|
|
.json({ error: `The property '${prop}' must be a number` });
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 20:47:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 18:33:34 +00:00
|
|
|
if (queueId) {
|
|
|
|
const whatsapps = await ListWhatsAppsForQueueService(queueId, "CONNECTED");
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-02 18:33:34 +00:00
|
|
|
if (!whatsapps || whatsapps?.length == 0) {
|
|
|
|
return res.status(500).json({
|
|
|
|
msg: `queueId ${queueId} does not have a WhatsApp number associated with it or the number's session is disconnected.`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const { id } = whatsapps[0];
|
|
|
|
|
|
|
|
whatsappId = id;
|
|
|
|
} else if (contact_from) {
|
|
|
|
const whatsapp = await Whatsapp.findOne({
|
|
|
|
where: { number: contact_from, status: "CONNECTED" }
|
2024-04-01 20:51:45 +00:00
|
|
|
});
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-02 18:33:34 +00:00
|
|
|
if (!whatsapp) {
|
|
|
|
return res.status(404).json({
|
|
|
|
msg: `Whatsapp number ${contact_from} not found or disconnected!`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const { id } = whatsapp;
|
|
|
|
|
|
|
|
const { queues } = await ShowWhatsAppService(id);
|
|
|
|
|
|
|
|
if (!queues || queues.length == 0) {
|
|
|
|
return res.status(500).json({
|
|
|
|
msg: `The WhatsApp number ${contact_from} is not associated with any queue! `
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
queueId = queues[0].id;
|
|
|
|
whatsappId = id;
|
2024-04-03 21:38:56 +00:00
|
|
|
} else if (cc) {
|
|
|
|
const queue = await Queue.findOne({ where: { cc } });
|
|
|
|
if (!queue) {
|
|
|
|
return res.status(404).json({
|
|
|
|
msg: `Queue with cc ${cc} not found! `
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
queueId = queue.id;
|
|
|
|
|
|
|
|
const whatsapps = await ListWhatsAppsForQueueService(queueId, "CONNECTED");
|
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
if (whatsapps.length === 0) {
|
2024-04-03 21:38:56 +00:00
|
|
|
return res.status(500).json({
|
|
|
|
msg: `No WhatsApp found for this cc ${cc} or the WhatsApp number is disconnected! `
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
whatsappId = whatsapps[0].id;
|
2024-04-02 18:33:34 +00:00
|
|
|
}
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-02 14:59:44 +00:00
|
|
|
// const validNumber = await CheckIsValidContact(contact_to, true);
|
|
|
|
const validNumber = contact_to;
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-02 14:59:44 +00:00
|
|
|
if (validNumber) {
|
|
|
|
let contact = await Contact.findOne({ where: { number: validNumber } });
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-02 14:59:44 +00:00
|
|
|
if (!contact) {
|
|
|
|
// const profilePicUrl = await GetProfilePicUrl(validNumber);
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-02 14:59:44 +00:00
|
|
|
contact = await CreateContactService({
|
|
|
|
name: contact_name ? contact_name : contact_to,
|
|
|
|
number: validNumber
|
|
|
|
// profilePicUrl
|
|
|
|
});
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-02 14:59:44 +00:00
|
|
|
const io = getIO();
|
|
|
|
io.emit("contact", {
|
|
|
|
action: "create",
|
|
|
|
contact
|
|
|
|
});
|
|
|
|
}
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-02 14:59:44 +00:00
|
|
|
const { id: contactId } = contact;
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-02 14:59:44 +00:00
|
|
|
const botInfo = await BotIsOnQueue("botqueue");
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-23 22:51:11 +00:00
|
|
|
// ticket from queueChoice or bot
|
|
|
|
let ticket: any = await Ticket.findOne({
|
2024-04-02 14:59:44 +00:00
|
|
|
where: {
|
|
|
|
[Op.or]: [
|
|
|
|
{ contactId, status: "queueChoice" },
|
|
|
|
{ contactId, status: "open", userId: botInfo.userIdBot }
|
|
|
|
]
|
2024-02-21 21:10:30 +00:00
|
|
|
}
|
2024-04-02 14:59:44 +00:00
|
|
|
});
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-02 14:59:44 +00:00
|
|
|
if (getSettingValue("whatsaAppCloudApi")?.value == "enabled") {
|
|
|
|
if (ticket) {
|
|
|
|
await UpdateTicketService({
|
|
|
|
ticketData: { status: "closed" },
|
|
|
|
ticketId: ticket.id
|
|
|
|
});
|
|
|
|
ticket = null;
|
2024-02-21 20:47:23 +00:00
|
|
|
}
|
2024-04-02 14:59:44 +00:00
|
|
|
} else {
|
|
|
|
if (ticket) {
|
|
|
|
await UpdateTicketService({
|
|
|
|
ticketData: { status: "closed" },
|
|
|
|
ticketId: ticket.id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 20:47:23 +00:00
|
|
|
|
2024-04-23 22:51:11 +00:00
|
|
|
ticket = await Ticket.findOne({
|
|
|
|
where: {
|
|
|
|
[Op.or]: [
|
|
|
|
{ contactId, status: "pending" },
|
|
|
|
{ contactId, status: "open" }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ticket) {
|
|
|
|
console.log(
|
|
|
|
`THE CAMPAIGN TICKET WAS NOT CREATED BECAUSE THE TICKET IS PENDING OR OPEN`
|
2024-02-21 20:47:23 +00:00
|
|
|
);
|
2024-04-23 22:51:11 +00:00
|
|
|
|
|
|
|
return res.status(422).json({
|
|
|
|
msg: `The campaign ticket was not created because the number ${contact_to} already has a ticket open or pending`
|
|
|
|
});
|
2024-02-21 20:47:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:51:11 +00:00
|
|
|
ticket = await FindOrCreateTicketService(
|
|
|
|
contact,
|
|
|
|
whatsappId,
|
|
|
|
0,
|
|
|
|
undefined,
|
|
|
|
queueId,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
// botSendMessage(ticket, `${msg}`);
|
|
|
|
|
|
|
|
await ticket.update({
|
|
|
|
lastMessage: msg
|
|
|
|
});
|
|
|
|
|
|
|
|
await set(
|
|
|
|
`remote:ticketId:${ticket.id}`,
|
|
|
|
JSON.stringify({
|
|
|
|
id: ticket.id,
|
|
|
|
createdAt: ticket.createdAt,
|
|
|
|
updatedAt: ticket.updatedAt,
|
2024-05-07 12:49:49 +00:00
|
|
|
whatsappId: ticket.whatsappId,
|
|
|
|
status: ticket.status
|
2024-04-23 22:51:11 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2024-04-02 14:59:44 +00:00
|
|
|
const io = getIO();
|
|
|
|
io.to(ticket.status).emit("ticket", {
|
|
|
|
action: "update",
|
|
|
|
ticket
|
|
|
|
});
|
|
|
|
|
2024-04-23 22:51:11 +00:00
|
|
|
const obj = await controllByNumber();
|
|
|
|
|
|
|
|
if (obj?.tickets) {
|
|
|
|
io.emit("remoteTickesControll", {
|
|
|
|
action: "update",
|
|
|
|
tickets: obj.ticketIds
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-21 20:47:23 +00:00
|
|
|
console.log(
|
2024-04-02 14:59:44 +00:00
|
|
|
`REMOTE TICKET CREATION FROM ENDPOINT | STATUS: 200 | MSG: success`
|
2024-02-21 20:47:23 +00:00
|
|
|
);
|
2024-04-23 22:51:11 +00:00
|
|
|
|
2024-04-02 14:59:44 +00:00
|
|
|
return res.status(200).json({ msg: "success" });
|
2024-02-21 20:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log(
|
2024-04-02 14:59:44 +00:00
|
|
|
`REMOTE TICKET CREATION FROM ENDPOINT | STATUS: 500 | MSG: The number ${contact_to} does not exist on WhatsApp`
|
2024-02-21 20:47:23 +00:00
|
|
|
);
|
2024-04-02 14:59:44 +00:00
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.json({ msg: `The number ${contact_to} does not exist on WhatsApp` });
|
2024-02-21 20:47:23 +00:00
|
|
|
};
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
2023-09-08 19:50:51 +00:00
|
|
|
const { contactId, status, userId, msg, queueId, whatsappId }: TicketData =
|
|
|
|
req.body;
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2024-02-05 15:29:49 +00:00
|
|
|
const botInfo = await BotIsOnQueue("botqueue");
|
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
let ticket = await Ticket.findOne({
|
|
|
|
where: {
|
|
|
|
[Op.or]: [
|
2024-02-05 15:29:49 +00:00
|
|
|
{ contactId, status: "queueChoice" },
|
|
|
|
{ contactId, status: "open", userId: botInfo.userIdBot }
|
2023-07-12 14:54:29 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2023-09-16 14:45:44 +00:00
|
|
|
if (getSettingValue("whatsaAppCloudApi")?.value == "enabled") {
|
|
|
|
if (ticket) {
|
|
|
|
await UpdateTicketService({
|
|
|
|
ticketData: { status: "closed" },
|
|
|
|
ticketId: ticket.id
|
|
|
|
});
|
|
|
|
ticket = null;
|
|
|
|
}
|
2023-07-26 20:24:10 +00:00
|
|
|
} else {
|
2023-09-16 14:45:44 +00:00
|
|
|
if (ticket) {
|
|
|
|
await UpdateTicketService({
|
|
|
|
ticketData: { status: "open", userId: userId, queueId },
|
|
|
|
ticketId: ticket.id
|
|
|
|
});
|
2024-01-29 11:48:20 +00:00
|
|
|
}
|
2023-09-16 14:45:44 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 11:48:20 +00:00
|
|
|
if (!ticket) {
|
|
|
|
ticket = await CreateTicketService({
|
|
|
|
contactId,
|
|
|
|
status,
|
|
|
|
userId,
|
|
|
|
queueId,
|
|
|
|
whatsappId
|
|
|
|
});
|
2022-05-19 21:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.to(ticket.status).emit("ticket", {
|
|
|
|
action: "update",
|
|
|
|
ticket
|
|
|
|
});
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
return res.status(200).json(ticket);
|
|
|
|
};
|
2022-05-19 21:29:38 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
export const show = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { ticketId } = req.params;
|
|
|
|
|
|
|
|
const contact = await ShowTicketService(ticketId);
|
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
const { statusChatEnd, count, hasMore } = await ListStatusChatEndService({
|
|
|
|
searchParam: "",
|
|
|
|
pageNumber: "1"
|
|
|
|
});
|
2022-05-19 21:29:38 +00:00
|
|
|
|
2022-03-10 11:24:10 +00:00
|
|
|
//////////////////
|
2023-07-26 20:24:10 +00:00
|
|
|
const schedulesContact = await ListSchedulingNotifyContactService(
|
|
|
|
contact.contact.number
|
|
|
|
);
|
2022-03-10 11:24:10 +00:00
|
|
|
/////////////////
|
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
return res.status(200).json({ contact, statusChatEnd, schedulesContact });
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
2022-09-05 16:36:42 +00:00
|
|
|
export const count = async (req: Request, res: Response): Promise<Response> => {
|
2022-10-25 14:16:36 +00:00
|
|
|
// type indexQ = { status: string; date?: string; };
|
2024-04-29 13:00:51 +00:00
|
|
|
const { status, date, queueIds } = req.query as IndexQuery;
|
|
|
|
const ticketCount = await CountTicketService(status, date, queueIds);
|
2022-08-08 16:47:28 +00:00
|
|
|
|
2022-09-05 16:36:42 +00:00
|
|
|
return res.status(200).json(ticketCount);
|
2022-08-08 16:47:28 +00:00
|
|
|
};
|
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
export const update = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
|
|
|
console.log("ENTROU NO UPDATE TICKET CONTROLLER");
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const { ticketId } = req.params;
|
2022-02-28 13:51:11 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
const userOldInfo = await Ticket.findByPk(ticketId);
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
let ticket2 = {};
|
2022-05-19 21:29:38 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
if (req.body["status"] === "closed") {
|
2022-05-19 21:29:38 +00:00
|
|
|
const { status, userId, schedulingNotifyData } = req.body;
|
|
|
|
|
|
|
|
// lembrete
|
2023-07-26 20:24:10 +00:00
|
|
|
const scheduleData = JSON.parse(schedulingNotifyData);
|
2022-05-19 21:29:38 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
console.log("scheduleData: ", scheduleData);
|
|
|
|
|
|
|
|
const statusChatEnd = await ShowStatusChatEndService({
|
|
|
|
name: scheduleData.statusChatEndName
|
|
|
|
});
|
2022-02-28 13:51:11 +00:00
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
const { ticket } = await UpdateTicketService({
|
2023-07-26 20:24:10 +00:00
|
|
|
ticketData: {
|
|
|
|
status: status,
|
|
|
|
userId: userId,
|
2024-04-12 21:33:15 +00:00
|
|
|
statusChatEnd: statusChatEnd.name,
|
|
|
|
statusChatEndId: statusChatEnd.id
|
2023-07-26 20:24:10 +00:00
|
|
|
},
|
2022-02-28 18:17:36 +00:00
|
|
|
ticketId
|
|
|
|
});
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
let _farewellMessage;
|
|
|
|
|
|
|
|
if (getSettingValue("farewellMessageByStatusChatEnd")?.value == "enabled") {
|
|
|
|
const statusChatEndData = await get({
|
|
|
|
key: `statusChatEnd:${statusChatEnd.id}`,
|
|
|
|
parse: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (
|
|
|
|
statusChatEndData &&
|
|
|
|
statusChatEndData?.farewellMessage &&
|
|
|
|
statusChatEndData?.farewellMessage?.trim()?.length > 0
|
|
|
|
) {
|
|
|
|
const { farewellMessage } = statusChatEndData;
|
|
|
|
|
|
|
|
_farewellMessage = farewellMessage;
|
|
|
|
}
|
|
|
|
} else if (getSettingValue("farewellMessageByQueue")?.value == "enabled") {
|
|
|
|
const queueData = await get({
|
|
|
|
key: `queue:${ticket.queueId}`,
|
|
|
|
parse: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (
|
|
|
|
queueData &&
|
|
|
|
queueData?.farewellMessage &&
|
|
|
|
queueData?.farewellMessage?.trim()?.length > 0
|
|
|
|
) {
|
|
|
|
const { farewellMessage } = queueData;
|
|
|
|
|
|
|
|
_farewellMessage = farewellMessage;
|
|
|
|
}
|
|
|
|
} else if (scheduleData.farewellMessage) {
|
2022-05-19 21:29:38 +00:00
|
|
|
const whatsapp = await ShowWhatsAppService(ticket.whatsappId);
|
2022-02-28 18:17:36 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
const { farewellMessage } = whatsapp;
|
|
|
|
|
|
|
|
if (farewellMessage) {
|
2024-04-12 21:33:15 +00:00
|
|
|
_farewellMessage = farewellMessage;
|
2022-05-19 21:29:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
if (_farewellMessage) {
|
|
|
|
await SendWhatsAppMessage({ body: `\u200e${_farewellMessage}`, ticket });
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("tatusChatEnd.name: ", statusChatEnd.name);
|
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
if (
|
2024-04-12 21:33:15 +00:00
|
|
|
statusChatEnd.name === "LEMBRETE" ||
|
|
|
|
statusChatEnd.name === "AGENDAMENTO À CONFIRMAR"
|
2023-07-26 20:24:10 +00:00
|
|
|
) {
|
2024-04-12 21:33:15 +00:00
|
|
|
// lembrete // agendamento
|
2023-07-26 20:24:10 +00:00
|
|
|
if (
|
|
|
|
isScheduling(scheduleData.schedulingDate, scheduleData.schedulingTime)
|
|
|
|
) {
|
|
|
|
console.log("*** É AGENDAMENTO!");
|
|
|
|
} else {
|
|
|
|
console.log("*** É LEMBRETE!");
|
2022-03-28 19:28:35 +00:00
|
|
|
}
|
2022-02-28 18:17:36 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
const schedulingNotifyCreate = await CreateSchedulingNotifyService({
|
2024-04-23 22:51:11 +00:00
|
|
|
ticketId: scheduleData.ticketId,
|
2024-04-12 21:33:15 +00:00
|
|
|
statusChatEndId: `${statusChatEnd.id}`,
|
2023-07-26 20:24:10 +00:00
|
|
|
schedulingDate: scheduleData.schedulingDate,
|
|
|
|
schedulingTime: scheduleData.schedulingTime,
|
|
|
|
message: scheduleData.message
|
|
|
|
});
|
2022-03-06 19:37:09 +00:00
|
|
|
}
|
2022-05-19 21:29:38 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
ticket2 = ticket;
|
|
|
|
} else {
|
2023-07-12 14:54:29 +00:00
|
|
|
// Para aparecer pendente para todos usuarios que estao na fila
|
|
|
|
if (req.body.transfer) {
|
2023-07-26 20:24:10 +00:00
|
|
|
req.body.userId = null;
|
2024-02-05 15:29:49 +00:00
|
|
|
}
|
2024-01-29 11:48:20 +00:00
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
let ticketData: TicketData = req.body;
|
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
if (getSettingValue("oneContactChatWithManyWhats")?.value == "enabled") {
|
2023-07-26 20:24:10 +00:00
|
|
|
if (ticketData.transfer) {
|
2024-01-29 11:48:20 +00:00
|
|
|
const whatsappsByqueue = await ListWhatsAppsForQueueService(
|
|
|
|
ticketData.queueId
|
|
|
|
);
|
|
|
|
|
|
|
|
if (userOldInfo) {
|
|
|
|
let listTicketOpenPending: any = [];
|
|
|
|
|
|
|
|
for (const w of whatsappsByqueue) {
|
|
|
|
let whats = await ListWhatsAppsNumber(w.id);
|
|
|
|
|
|
|
|
const ticket = await Ticket.findOne({
|
|
|
|
where: {
|
|
|
|
[Op.and]: [
|
|
|
|
{ contactId: userOldInfo.contactId },
|
|
|
|
{
|
|
|
|
whatsappId: {
|
|
|
|
[Op.in]: whats.whatsapps.map((w: any) => w.id)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ status: { [Op.or]: ["open", "pending"] } }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ticket) {
|
|
|
|
listTicketOpenPending.push({
|
|
|
|
ticketId: ticket.id,
|
|
|
|
status: ticket.status,
|
|
|
|
userId: ticket.userId,
|
|
|
|
contactId: ticket.contactId,
|
|
|
|
whatsappId: ticket.whatsappId,
|
|
|
|
queueId: ticket.queueId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log("userOldInfo: ", JSON.stringify(userOldInfo, null, 6));
|
|
|
|
// console.log("##########")
|
|
|
|
// console.log(
|
|
|
|
// "listTicketOpenPending: ",
|
|
|
|
// JSON.stringify(listTicketOpenPending)
|
|
|
|
// );
|
|
|
|
|
|
|
|
if (
|
|
|
|
listTicketOpenPending.filter(
|
|
|
|
(ob: any) => userOldInfo.whatsappId != ob.whatsappId
|
|
|
|
)?.length > 0
|
|
|
|
) {
|
|
|
|
throw new AppError("ERR_OTHER_OPEN_TICKET");
|
|
|
|
}
|
|
|
|
}
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2024-01-29 11:48:20 +00:00
|
|
|
//////////////////////////////////////////////
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2024-01-29 11:48:20 +00:00
|
|
|
// const defaultWhatsapp: any = await GetDefaultWhatsApp({
|
|
|
|
// userId: ticketData.userId
|
|
|
|
// });
|
|
|
|
|
|
|
|
// console.log(
|
|
|
|
// "ticketData.userId: ",
|
|
|
|
// ticketData.userId,
|
|
|
|
// " | defaultWhatsapp: ",
|
|
|
|
// JSON.stringify(defaultWhatsapp, null, 6)
|
|
|
|
// );
|
|
|
|
|
|
|
|
// const _ticket: any = await Ticket.findByPk(ticketId);
|
|
|
|
|
|
|
|
// if (defaultWhatsapp && ticketData.status != "open") {
|
|
|
|
// await CheckContactOpenTickets(
|
|
|
|
// _ticket.dataValues.contactId,
|
|
|
|
// defaultWhatsapp.dataValues.id
|
|
|
|
// );
|
|
|
|
// }
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2024-01-29 11:48:20 +00:00
|
|
|
// ticketData.whatsappId = defaultWhatsapp.dataValues.id;
|
2023-07-26 20:24:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-28 18:17:36 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
console.log(
|
|
|
|
"--------> ticketData.status: ",
|
|
|
|
ticketData.status,
|
|
|
|
" | ticketData.fromMe: ",
|
|
|
|
ticketData.fromMe
|
|
|
|
);
|
2022-04-18 18:21:28 +00:00
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
const { ticket } = await UpdateTicketService({
|
|
|
|
ticketData,
|
2023-07-26 20:24:10 +00:00
|
|
|
ticketId
|
2022-02-28 18:17:36 +00:00
|
|
|
});
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
if (ticketData.status == "open" && !ticketData.fromMe) {
|
2023-07-12 14:54:29 +00:00
|
|
|
await setMessageAsRead(ticket);
|
|
|
|
}
|
|
|
|
|
2024-05-07 12:49:49 +00:00
|
|
|
if (ticketData.status == "open" || ticketData.status == "pending") {
|
|
|
|
let ticketRemote = await get({
|
|
|
|
key: `remote:ticketId:${ticketId}`
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ticketRemote) {
|
|
|
|
ticketRemote = JSON.parse(ticketRemote);
|
|
|
|
|
|
|
|
ticketRemote = {
|
|
|
|
...ticketRemote,
|
|
|
|
...{
|
|
|
|
status: ticketData.status == "open" ? "open" : "pending",
|
|
|
|
updatedAt: ticket.updatedAt
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
set(`remote:ticketId:${ticketId}`, JSON.stringify(ticketRemote));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:36:42 +00:00
|
|
|
if (ticketData.userId) {
|
2023-07-26 20:24:10 +00:00
|
|
|
const dateToday = splitDateTime(
|
|
|
|
new Date(format(new Date(), "yyyy-MM-dd HH:mm:ss", { locale: ptBR }))
|
|
|
|
);
|
|
|
|
TicketEmiterSumOpenClosedByUser(
|
|
|
|
ticketData.userId.toString(),
|
|
|
|
dateToday.fullDate,
|
|
|
|
dateToday.fullDate
|
|
|
|
);
|
2022-09-05 16:36:42 +00:00
|
|
|
}
|
2022-08-08 16:47:28 +00:00
|
|
|
|
2023-07-26 20:24:10 +00:00
|
|
|
ticket2 = ticket;
|
2022-05-19 21:29:38 +00:00
|
|
|
}
|
2022-02-28 18:17:36 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
if (userOldInfo) {
|
2023-07-26 20:24:10 +00:00
|
|
|
const dateToday = splitDateTime(
|
|
|
|
new Date(format(new Date(), "yyyy-MM-dd HH:mm:ss", { locale: ptBR }))
|
|
|
|
);
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
if (userOldInfo.userId) {
|
2023-07-26 20:24:10 +00:00
|
|
|
TicketEmiterSumOpenClosedByUser(
|
|
|
|
userOldInfo.userId.toString(),
|
|
|
|
dateToday.fullDate,
|
|
|
|
dateToday.fullDate
|
|
|
|
);
|
2022-05-19 21:29:38 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
return res.status(200).json(ticket2);
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
// export const update = async (
|
|
|
|
// req: Request,
|
|
|
|
// res: Response
|
|
|
|
// ): Promise<Response> => {
|
|
|
|
// const { ticketId } = req.params;
|
|
|
|
// const ticketData: TicketData = req.body;
|
|
|
|
|
|
|
|
// const { ticket } = await UpdateTicketService({
|
|
|
|
// ticketData,
|
|
|
|
// ticketId
|
|
|
|
// });
|
|
|
|
|
|
|
|
// if (ticket.status === "closed") {
|
|
|
|
// const whatsapp = await ShowWhatsAppService(ticket.whatsappId);
|
|
|
|
|
|
|
|
// const { farewellMessage } = whatsapp;
|
|
|
|
|
|
|
|
// if (farewellMessage) {
|
|
|
|
// await SendWhatsAppMessage({ body: farewellMessage, ticket });
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return res.status(200).json(ticket);
|
|
|
|
// };
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
export const remove = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
|
|
|
const { ticketId } = req.params;
|
2022-05-19 21:29:38 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const ticket = await DeleteTicketService(ticketId);
|
|
|
|
|
|
|
|
const io = getIO();
|
2023-07-26 20:24:10 +00:00
|
|
|
io.to(ticket.status).to(ticketId).to("notification").emit("ticket", {
|
|
|
|
action: "delete",
|
|
|
|
ticketId: +ticketId
|
|
|
|
});
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2024-04-23 22:51:11 +00:00
|
|
|
await del(`remote:ticketId:${ticketId}`);
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return res.status(200).json({ message: "ticket deleted" });
|
|
|
|
};
|