2022-01-06 01:26:15 +00:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
|
|
|
|
import SetTicketMessagesAsRead from "../helpers/SetTicketMessagesAsRead";
|
|
|
|
import { getIO } from "../libs/socket";
|
|
|
|
import Message from "../models/Message";
|
|
|
|
|
|
|
|
import ListMessagesService from "../services/MessageServices/ListMessagesService";
|
|
|
|
import ShowTicketService from "../services/TicketServices/ShowTicketService";
|
|
|
|
import DeleteWhatsAppMessage from "../services/WbotServices/DeleteWhatsAppMessage";
|
|
|
|
import SendWhatsAppMedia from "../services/WbotServices/SendWhatsAppMedia";
|
|
|
|
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
|
|
|
|
|
|
|
|
type IndexQuery = {
|
|
|
|
pageNumber: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type MessageData = {
|
|
|
|
body: string;
|
|
|
|
fromMe: boolean;
|
|
|
|
read: boolean;
|
|
|
|
quotedMsg?: Message;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { ticketId } = req.params;
|
|
|
|
const { pageNumber } = req.query as IndexQuery;
|
|
|
|
|
|
|
|
const { count, messages, ticket, hasMore } = await ListMessagesService({
|
|
|
|
pageNumber,
|
|
|
|
ticketId
|
|
|
|
});
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
// SetTicketMessagesAsRead(ticket);
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
return res.json({ count, messages, ticket, hasMore });
|
|
|
|
};
|
|
|
|
|
2023-01-20 19:38:24 +00:00
|
|
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const { ticketId } = req.params;
|
|
|
|
const { body, quotedMsg }: MessageData = req.body;
|
|
|
|
const medias = req.files as Express.Multer.File[];
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const ticket = await ShowTicketService(ticketId);
|
|
|
|
|
2023-01-20 19:38:24 +00:00
|
|
|
console.log('TICKET ID: ', ticketId)
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
// SetTicketMessagesAsRead(ticket);
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
if (medias) {
|
|
|
|
await Promise.all(
|
2022-07-20 15:34:12 +00:00
|
|
|
medias.map(async (media: Express.Multer.File) => {
|
|
|
|
|
|
|
|
console.log(`\n >>>>>>>>>> SENDING MESSAGE MEDIA
|
|
|
|
Parcial ticket info and media:
|
|
|
|
ticket.id: ${ticket.id}
|
|
|
|
ticket.status: ${ticket.status}
|
|
|
|
ticket.whatsapp.id: ${ticket.whatsappId}
|
|
|
|
ticket.contact.number: ${ticket.contact.number}
|
|
|
|
ticket.contact.name: ${ticket.contact.name}
|
|
|
|
ticket.contact.profilePicUrl: ${ticket.contact.profilePicUrl}
|
|
|
|
ticket.user.id: ${ticket.user.id}
|
|
|
|
ticket.user.name: ${ticket.user.name}
|
|
|
|
media:`, media,'\n')
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
await SendWhatsAppMedia({ media, ticket });
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2022-07-20 15:34:12 +00:00
|
|
|
console.log(`\n >>>>>>>>>> SENDING MESSAGE
|
|
|
|
Parcial ticket info:
|
|
|
|
ticket.id: ${ticket.id}
|
|
|
|
ticket.status: ${ticket.status}
|
2022-11-16 18:29:20 +00:00
|
|
|
ticket.whatsapp.id: ${ticket.whatsappId}
|
2022-07-20 15:34:12 +00:00
|
|
|
ticket.contact.number: ${ticket.contact.number}
|
2022-11-16 18:29:20 +00:00
|
|
|
message: ${body}
|
2022-07-20 15:34:12 +00:00
|
|
|
ticket.contact.name: ${ticket.contact.name}
|
|
|
|
ticket.contact.profilePicUrl: ${ticket.contact.profilePicUrl}
|
|
|
|
ticket.user.id: ${ticket.user.id}
|
|
|
|
ticket.user.name: ${ticket.user.name}\n`)
|
|
|
|
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
await SendWhatsAppMessage({ body, ticket, quotedMsg });
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.send();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const remove = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
|
|
|
const { messageId } = req.params;
|
|
|
|
|
|
|
|
const message = await DeleteWhatsAppMessage(messageId);
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.to(message.ticketId.toString()).emit("appMessage", {
|
|
|
|
action: "update",
|
|
|
|
message
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.send();
|
|
|
|
};
|