projeto-hit/backend/src/controllers/TicketController.ts

225 lines
5.3 KiB
TypeScript
Raw Normal View History

import { Request, Response } from "express";
import { getIO } from "../libs/socket";
import CreateTicketService from "../services/TicketServices/CreateTicketService";
import DeleteTicketService from "../services/TicketServices/DeleteTicketService";
import ListTicketsService from "../services/TicketServices/ListTicketsService";
import ShowTicketService from "../services/TicketServices/ShowTicketService";
import UpdateTicketService from "../services/TicketServices/UpdateTicketService";
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
import CreateSchedulingNotifyService from "../services/SchedulingNotifyServices/CreateSchedulingNotifyService";
import ListSchedulingNotifyContactService from "../services/SchedulingNotifyServices/ListSchedulingNotifyContactService";
type IndexQuery = {
searchParam: string;
pageNumber: string;
status: string;
date: string;
showAll: string;
withUnreadMessages: string;
queueIds: string;
};
interface TicketData {
contactId: number;
status: string;
queueId: number;
userId: number;
}
import ListScheduleService from "../services/ScheduleService/ListScheduleService";
export const index = async (req: Request, res: Response): Promise<Response> => {
const {
pageNumber,
status,
date,
searchParam,
showAll,
queueIds: queueIdsStringified,
withUnreadMessages
} = req.query as IndexQuery;
const userId = req.user.id;
let queueIds: number[] = [];
if (queueIdsStringified) {
queueIds = JSON.parse(queueIdsStringified);
}
const { tickets, count, hasMore } = await ListTicketsService({
searchParam,
pageNumber,
status,
date,
showAll,
userId,
queueIds,
withUnreadMessages
});
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;
// naty
// console.log(
// `contactId: ${contactId} \n| status: ${status} \n| userId: ${userId}`
// )
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);
};
export const show = async (req: Request, res: Response): Promise<Response> => {
const { ticketId } = req.params;
const contact = await ShowTicketService(ticketId);
const { schedules, count, hasMore } = await ListScheduleService({ searchParam: "", pageNumber: "1" });
//////////////////
const schedulesContact = await ListSchedulingNotifyContactService(contact.contact.number);
/////////////////
// console.log('############### schedulesContact: ', schedulesContact)
// console.log('############### contact.contact.number: ',contact.contact.number)
// return res.status(200).json(contact);
return res.status(200).json({contact, schedules, schedulesContact});
};
export const update = async ( req: Request, res: Response ): Promise<Response> => {
const { ticketId } = req.params;
let ticket2 = {}
if(req.body['status'] === "closed"){
const {status, userId, schedulingNotifyData} = req.body;
const { ticket } = await UpdateTicketService({
ticketData:{'status': status, 'userId': userId},
ticketId
});
///////////////////////////////
const whatsapp = await ShowWhatsAppService(ticket.whatsappId);
const { farewellMessage } = whatsapp;
if (farewellMessage) {
await SendWhatsAppMessage({ body: farewellMessage, ticket });
}
///////////////////////////////
// agendamento
const scheduleData = JSON.parse(schedulingNotifyData)
if( scheduleData.scheduleId != '1'){
const schedulingNotifyCreate = await CreateSchedulingNotifyService(
{
ticketId: scheduleData.ticketId,
scheduleId: scheduleData.scheduleId,
schedulingDate: scheduleData.schedulingDate,
message: scheduleData.message
}
)
}
ticket2 = ticket
}
else{
const ticketData: TicketData = req.body;
const { ticket } = await UpdateTicketService({
ticketData,
ticketId
});
ticket2 = ticket
}
return res.status(200).json(ticket2);
};
// 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);
// };
export const remove = async (
req: Request,
res: Response
): Promise<Response> => {
const { ticketId } = req.params;
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" });
};