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";
|
2022-05-19 21:29:38 +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";
|
|
|
|
|
|
|
|
import { isScheduling } from "../helpers/CheckSchedulingReminderNotify"
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
import ptBR from 'date-fns/locale/pt-BR';
|
|
|
|
import { splitDateTime } from "../helpers/SplitDateTime";
|
|
|
|
import format from 'date-fns/format';
|
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
import ListTicketsServiceCache from "../services/TicketServices/ListTicketServiceCache";
|
2022-05-19 21:29:38 +00:00
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
import { searchTicketCache, loadTicketsCache, } from '../helpers/TicketCache'
|
2023-07-12 14:54:29 +00:00
|
|
|
import { Op } from "sequelize";
|
2022-01-10 20:10:20 +00:00
|
|
|
|
2022-08-08 16:47:28 +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-12 14:54:29 +00:00
|
|
|
searchParamContent?: string
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
interface TicketData {
|
|
|
|
contactId: number;
|
|
|
|
status: string;
|
|
|
|
queueId: number;
|
|
|
|
userId: number;
|
2023-07-12 14:54:29 +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 QueuesByUser from "../services/UserServices/ShowQueuesByUser";
|
|
|
|
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";
|
2022-08-08 16:47:28 +00:00
|
|
|
|
2022-09-05 16:36:42 +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[] = [];
|
|
|
|
|
|
|
|
if (queueIdsStringified) {
|
|
|
|
queueIds = JSON.parse(queueIdsStringified);
|
2022-05-19 21:29:38 +00:00
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const { tickets, count, hasMore } = await ListTicketsService({
|
|
|
|
searchParam,
|
|
|
|
pageNumber,
|
|
|
|
status,
|
|
|
|
date,
|
|
|
|
showAll,
|
|
|
|
userId,
|
|
|
|
queueIds,
|
2022-04-18 18:21:28 +00:00
|
|
|
withUnreadMessages,
|
2023-07-12 14:54:29 +00:00
|
|
|
unlimited,
|
|
|
|
searchParamContent
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
2022-05-19 21:29:38 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return res.status(200).json({ tickets, count, hasMore });
|
|
|
|
};
|
|
|
|
|
|
|
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
2023-07-12 14:54:29 +00:00
|
|
|
const { contactId, status, userId, msg, queueId }: TicketData = req.body;
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
const botInfo = await BotIsOnQueue('botqueue')
|
|
|
|
|
|
|
|
let ticket = await Ticket.findOne({
|
|
|
|
where: {
|
|
|
|
[Op.or]: [
|
|
|
|
{ contactId, status: 'queueChoice' },
|
|
|
|
{ contactId, status: 'open', userId: botInfo.userIdBot }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
if (ticket) {
|
2023-07-12 14:54:29 +00:00
|
|
|
await UpdateTicketService({ ticketData: { status: 'open', userId: userId, queueId }, ticketId: ticket.id });
|
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-07-12 14:54:29 +00:00
|
|
|
ticket = await CreateTicketService({ contactId, status, userId, queueId });
|
2022-05-19 21:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.to(ticket.status).emit("ticket", {
|
|
|
|
action: "update",
|
|
|
|
ticket
|
|
|
|
});
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
// const ticket = await CreateTicketService({ contactId, status, userId });
|
2022-05-03 21:20:58 +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);
|
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
const { statusChatEnd, count, hasMore } = await ListStatusChatEndService({ searchParam: "", pageNumber: "1" });
|
|
|
|
|
2022-03-10 11:24:10 +00:00
|
|
|
//////////////////
|
2022-05-19 21:29:38 +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-08-08 16:47:28 +00:00
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
// type indexQ = { status: string; date?: string; };
|
2022-09-05 16:36:42 +00:00
|
|
|
const { status, date } = req.query as IndexQuery
|
2022-08-08 16:47:28 +00:00
|
|
|
|
2022-09-05 16:36:42 +00:00
|
|
|
const ticketCount = await CountTicketService(status, date);
|
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
|
|
|
};
|
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
export const update = async (req: Request, res: Response): Promise<Response> => {
|
2022-02-28 13:51:11 +00:00
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
console.log('ENTROU NO UPDATE TICKET CONTROLLER')
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const { ticketId } = req.params;
|
2022-02-28 13:51:11 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
const userOldInfo = await Ticket.findByPk(ticketId)
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
let ticket2 = {}
|
2022-02-28 13:51:11 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
if (req.body['status'] === "closed") {
|
|
|
|
|
|
|
|
const { status, userId, schedulingNotifyData } = req.body;
|
|
|
|
|
|
|
|
// lembrete
|
|
|
|
const scheduleData = JSON.parse(schedulingNotifyData)
|
|
|
|
|
|
|
|
const statusChatEndName = await ShowStatusChatEndService(scheduleData.statusChatEndId)
|
2022-02-28 13:51:11 +00:00
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
const { ticket } = await UpdateTicketService({
|
2022-05-19 21:29:38 +00:00
|
|
|
ticketData: { 'status': status, 'userId': userId, 'statusChatEnd': statusChatEndName.name },
|
2022-02-28 18:17:36 +00:00
|
|
|
ticketId
|
|
|
|
});
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
if (scheduleData.farewellMessage) {
|
|
|
|
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) {
|
|
|
|
await SendWhatsAppMessage({ body: farewellMessage, ticket });
|
|
|
|
}
|
|
|
|
}
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
|
|
|
|
// lembrete // agendamento
|
|
|
|
if (scheduleData.statusChatEndId === '2' || scheduleData.statusChatEndId === '3') {
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2022-03-28 19:28:35 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
if (isScheduling(scheduleData.schedulingDate, scheduleData.schedulingTime)) {
|
2022-03-28 19:28:35 +00:00
|
|
|
console.log('*** É AGENDAMENTO!')
|
|
|
|
}
|
2022-05-19 21:29:38 +00:00
|
|
|
else {
|
2022-03-28 19:28:35 +00:00
|
|
|
console.log('*** É LEMBRETE!')
|
|
|
|
}
|
2022-02-28 18:17:36 +00:00
|
|
|
|
2022-03-06 19:37:09 +00:00
|
|
|
const schedulingNotifyCreate = await CreateSchedulingNotifyService(
|
|
|
|
{
|
|
|
|
ticketId: scheduleData.ticketId,
|
2022-05-19 21:29:38 +00:00
|
|
|
statusChatEndId: scheduleData.statusChatEndId,
|
|
|
|
schedulingDate: scheduleData.schedulingDate,
|
2022-03-12 05:13:15 +00:00
|
|
|
schedulingTime: scheduleData.schedulingTime,
|
2022-05-19 21:29:38 +00:00
|
|
|
message: scheduleData.message
|
2022-03-06 19:37:09 +00:00
|
|
|
}
|
2022-05-19 21:29:38 +00:00
|
|
|
)
|
2022-03-06 19:37:09 +00:00
|
|
|
}
|
2022-05-19 21:29:38 +00:00
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
ticket2 = ticket
|
|
|
|
|
|
|
|
}
|
2022-05-19 21:29:38 +00:00
|
|
|
else {
|
2022-02-28 18:17:36 +00:00
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
// Para aparecer pendente para todos usuarios que estao na fila
|
|
|
|
if (req.body.transfer) {
|
|
|
|
req.body.userId = null
|
|
|
|
}
|
|
|
|
|
|
|
|
let ticketData: TicketData = req.body;
|
|
|
|
|
|
|
|
// console.log('ticketData: ', ticketData)
|
|
|
|
// console.log('ticketData.transfer', ticketData.transfer)
|
|
|
|
|
|
|
|
// return res.send()
|
|
|
|
|
|
|
|
|
|
|
|
// if (ticketData.transfer) {
|
|
|
|
|
|
|
|
// const defaultWhatsapp: any = await GetDefaultWhatsApp(ticketData.userId);
|
|
|
|
|
|
|
|
// const _ticket: any = await Ticket.findByPk(ticketId)
|
|
|
|
|
|
|
|
// if (defaultWhatsapp && ticketData.status != 'open') {
|
|
|
|
|
|
|
|
// await CheckContactOpenTickets(_ticket.dataValues.contactId, defaultWhatsapp.dataValues.id)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// ticketData.whatsappId = defaultWhatsapp.dataValues.id
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
console.log('--------> ticketData.status: ', ticketData.status, ' | ticketData.fromMe: ', ticketData.fromMe)
|
2022-02-28 18:17:36 +00:00
|
|
|
|
2022-04-18 18:21:28 +00:00
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
const { ticket } = await UpdateTicketService({
|
|
|
|
ticketData,
|
2023-07-12 14:54:29 +00:00
|
|
|
ticketId,
|
2022-02-28 18:17:36 +00:00
|
|
|
});
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
|
|
|
|
if (ticketData.status == 'open' && !ticketData.fromMe) {
|
|
|
|
|
|
|
|
await setMessageAsRead(ticket);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('ticket.unreadMessages: ', ticket.unreadMessages)
|
|
|
|
|
2022-09-05 16:36:42 +00:00
|
|
|
if (ticketData.userId) {
|
|
|
|
|
|
|
|
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-08-08 16:47:28 +00:00
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
ticket2 = ticket
|
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
}
|
2022-02-28 18:17:36 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-09-05 16:36:42 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
if (userOldInfo) {
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-05-19 21:29:38 +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) {
|
2022-08-08 16:47:28 +00:00
|
|
|
|
2022-09-05 16:36:42 +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-09-05 16:36:42 +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();
|
|
|
|
io.to(ticket.status)
|
|
|
|
.to(ticketId)
|
|
|
|
.to("notification")
|
|
|
|
.emit("ticket", {
|
|
|
|
action: "delete",
|
|
|
|
ticketId: +ticketId
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).json({ message: "ticket deleted" });
|
|
|
|
};
|
2023-07-12 14:54:29 +00:00
|
|
|
// export async function setMessageAsRead(ticket: Ticket) {
|
|
|
|
// const wbot_url = await getWbot(ticket.whatsappId);
|
|
|
|
|
|
|
|
// console.log('wbot_url: ', wbot_url, ' | ticket.contact.number: ', ticket.contact.number);
|
|
|
|
|
|
|
|
// await endPointQuery(`${wbot_url}/api/sendSeen`, { number: `${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us` });
|
|
|
|
// }
|
|
|
|
|