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';
|
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
|
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;
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
interface TicketData {
|
|
|
|
contactId: number;
|
|
|
|
status: string;
|
|
|
|
queueId: number;
|
|
|
|
userId: number;
|
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";
|
|
|
|
|
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,
|
|
|
|
unlimited
|
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,
|
|
|
|
unlimited
|
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> => {
|
|
|
|
const { contactId, status, userId }: TicketData = req.body;
|
|
|
|
|
2022-03-01 03:27:17 +00:00
|
|
|
// naty
|
|
|
|
// console.log(
|
|
|
|
// `contactId: ${contactId} \n| status: ${status} \n| userId: ${userId}`
|
|
|
|
// )
|
2022-05-03 21:20:58 +00:00
|
|
|
|
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
// test del
|
|
|
|
let ticket = await Ticket.findOne({ where: { contactId, status: 'queueChoice' } });
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
if (ticket) {
|
|
|
|
await UpdateTicketService({ ticketData: { status: 'open', userId: userId, }, ticketId: ticket.id });
|
|
|
|
console.log('TICKET QUEUE CHOICE !!!!!!!')
|
|
|
|
}
|
|
|
|
else {
|
2022-09-05 16:36:42 +00:00
|
|
|
ticket = await CreateTicketService({ contactId, status, userId });
|
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-02-28 18:17:36 +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-09-05 16:36:42 +00:00
|
|
|
type indexQ = { status: string; date?: string; };
|
|
|
|
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
|
|
|
|
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-02-28 18:17:36 +00:00
|
|
|
///////////////////////////////
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2022-05-19 21:36:04 +00:00
|
|
|
//console.log('------- scheduleData.farewellMessage: ', scheduleData.farewellMessage)
|
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 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
// lembrete // agendamento
|
|
|
|
if (scheduleData.statusChatEndId === '2' || scheduleData.statusChatEndId === '3') {
|
2022-03-28 19:28:35 +00:00
|
|
|
|
|
|
|
console.log('*** schedulingDate: ', scheduleData.schedulingDate)
|
|
|
|
console.log('*** schedulingTime: ', scheduleData.schedulingTime)
|
|
|
|
|
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
|
|
|
|
|
|
|
const ticketData: TicketData = req.body;
|
|
|
|
|
2022-08-08 16:47:28 +00:00
|
|
|
//ticketData: { status: 'open', userId: 4 } , ticketId
|
2022-04-18 18:21:28 +00:00
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
const { ticket } = await UpdateTicketService({
|
|
|
|
ticketData,
|
|
|
|
ticketId
|
|
|
|
});
|
2022-01-06 01:26:15 +00:00
|
|
|
|
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
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
if (userOldInfo.userId) {
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-08-08 16:47:28 +00:00
|
|
|
// console.log('FECHOU...')
|
|
|
|
|
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" });
|
|
|
|
};
|