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-01-15 18:32:46 +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-02-28 18:17:36 +00:00
|
|
|
import CreateSchedulingNotifyService from "../services/SchedulingNotifyServices/CreateSchedulingNotifyService";
|
2022-03-10 11:24:10 +00:00
|
|
|
import ListSchedulingNotifyContactService from "../services/SchedulingNotifyServices/ListSchedulingNotifyContactService";
|
2022-03-28 19:28:35 +00:00
|
|
|
|
|
|
|
import {isScheduling} from "../helpers/CheckSchedulingReminderNotify"
|
2022-02-28 13:51:11 +00:00
|
|
|
|
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;
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
interface TicketData {
|
|
|
|
contactId: number;
|
|
|
|
status: string;
|
|
|
|
queueId: number;
|
|
|
|
userId: number;
|
2022-01-10 20:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-23 02:59:01 +00:00
|
|
|
import ListStatusChatEndService from "../services/StatusChatEndService/ListStatusChatEndService";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const {
|
|
|
|
pageNumber,
|
|
|
|
status,
|
|
|
|
date,
|
|
|
|
searchParam,
|
|
|
|
showAll,
|
|
|
|
queueIds: queueIdsStringified,
|
2022-04-18 18:21:28 +00:00
|
|
|
withUnreadMessages,
|
|
|
|
unlimited
|
2022-01-06 01:26:15 +00:00
|
|
|
} = req.query as IndexQuery;
|
|
|
|
|
2022-04-18 18:21:28 +00:00
|
|
|
|
|
|
|
|
2022-01-10 20:10:20 +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-01-10 20:10:20 +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-01-10 20:10:20 +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-01-06 01:26:15 +00:00
|
|
|
const ticket = await CreateTicketService({ contactId, status, userId });
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.to(ticket.status).emit("ticket", {
|
|
|
|
action: "update",
|
|
|
|
ticket
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).json(ticket);
|
|
|
|
};
|
2022-01-15 18:32:46 +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-03-23 02:59:01 +00:00
|
|
|
const { statusChatEnd, count, hasMore } = await ListStatusChatEndService({ searchParam: "", pageNumber: "1" });
|
|
|
|
|
2022-03-10 11:24:10 +00:00
|
|
|
//////////////////
|
|
|
|
const schedulesContact = await ListSchedulingNotifyContactService(contact.contact.number);
|
|
|
|
/////////////////
|
2022-03-23 02:59:01 +00:00
|
|
|
|
2022-03-10 11:24:10 +00:00
|
|
|
|
2022-03-23 02:59:01 +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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-02-28 18:17:36 +00:00
|
|
|
let ticket2 = {}
|
|
|
|
|
|
|
|
if(req.body['status'] === "closed"){
|
2022-02-28 13:51:11 +00:00
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
const {status, userId, schedulingNotifyData} = req.body;
|
2022-02-28 13:51:11 +00:00
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
const { ticket } = await UpdateTicketService({
|
|
|
|
ticketData:{'status': status, 'userId': userId},
|
|
|
|
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
|
|
|
const whatsapp = await ShowWhatsAppService(ticket.whatsappId);
|
|
|
|
|
|
|
|
const { farewellMessage } = whatsapp;
|
|
|
|
|
|
|
|
if (farewellMessage) {
|
2022-02-22 20:36:11 +00:00
|
|
|
await SendWhatsAppMessage({ body: farewellMessage, ticket });
|
2022-02-28 18:17:36 +00:00
|
|
|
}
|
|
|
|
///////////////////////////////
|
|
|
|
|
2022-03-28 19:28:35 +00:00
|
|
|
|
|
|
|
|
2022-03-17 11:58:22 +00:00
|
|
|
// lembrete
|
2022-02-28 18:17:36 +00:00
|
|
|
const scheduleData = JSON.parse(schedulingNotifyData)
|
2022-03-28 19:28:35 +00:00
|
|
|
// lembrete // agendamento
|
|
|
|
if( scheduleData.statusChatEndId === '2' || scheduleData.statusChatEndId === '3'){
|
|
|
|
|
|
|
|
console.log('*** schedulingDate: ', scheduleData.schedulingDate)
|
|
|
|
console.log('*** schedulingTime: ', scheduleData.schedulingTime)
|
|
|
|
|
|
|
|
if(isScheduling(scheduleData.schedulingDate, scheduleData.schedulingTime)){
|
|
|
|
console.log('*** É AGENDAMENTO!')
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
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-03-23 02:59:01 +00:00
|
|
|
statusChatEndId: scheduleData.statusChatEndId,
|
2022-03-06 19:37:09 +00:00
|
|
|
schedulingDate: scheduleData.schedulingDate,
|
2022-03-12 05:13:15 +00:00
|
|
|
schedulingTime: scheduleData.schedulingTime,
|
2022-03-06 19:37:09 +00:00
|
|
|
message: scheduleData.message
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
ticket2 = ticket
|
|
|
|
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
|
|
|
|
const ticketData: TicketData = req.body;
|
|
|
|
|
2022-04-18 18:21:28 +00:00
|
|
|
//ticketData: { status: 'open', userId: 4 } , ticketId
|
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
const { ticket } = await UpdateTicketService({
|
|
|
|
ticketData,
|
|
|
|
ticketId
|
|
|
|
});
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2022-02-28 18:17:36 +00:00
|
|
|
ticket2 = ticket
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-10 20:10:20 +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" });
|
|
|
|
};
|