112 lines
3.5 KiB
TypeScript
112 lines
3.5 KiB
TypeScript
import { Request, Response } from "express";
|
|
import whatsappOfficialAPI from "../helpers/WhatsappOfficialAPI";
|
|
|
|
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";
|
|
import axios from "axios";
|
|
import Contact from "../models/Contact";
|
|
import {
|
|
isValidMsg,
|
|
verifyMessage
|
|
} from "../services/WbotServices/wbotMessageListener";
|
|
import CreateOrUpdateContactService from "../services/ContactServices/CreateOrUpdateContactService";
|
|
import sendWhatsAppMessageOfficialAPI from "../helpers/sendWhatsAppMessageOfficialAPI";
|
|
import Whatsapp from "../models/Whatsapp";
|
|
|
|
type IndexQuery = {
|
|
pageNumber: string;
|
|
};
|
|
|
|
type MessageData = {
|
|
body: string;
|
|
fromMe: boolean;
|
|
read: boolean;
|
|
quotedMsg?: Message;
|
|
mic_audio?: boolean
|
|
};
|
|
|
|
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
|
|
});
|
|
|
|
return res.json({ count, messages, ticket, hasMore });
|
|
};
|
|
|
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
|
const { ticketId } = req.params;
|
|
const { body, quotedMsg, mic_audio }: MessageData = req.body;
|
|
const medias = req.files as Express.Multer.File[];
|
|
const ticket = await ShowTicketService(ticketId);
|
|
|
|
const { queueId } = ticket;
|
|
console.log("-----------> queueId: ", queueId, " | quotedMsg: ", quotedMsg);
|
|
|
|
if (medias) {
|
|
await Promise.all(
|
|
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"
|
|
);
|
|
await SendWhatsAppMedia({ media, ticket, mic_audio });
|
|
})
|
|
);
|
|
} else {
|
|
console.log(`\n >>>>>>>>>> SENDING MESSAGE
|
|
Parcial ticket info:
|
|
ticket.id: ${ticket.id}
|
|
ticket.status: ${ticket.status}
|
|
ticket.whatsapp.id: ${ticket.whatsappId}
|
|
ticket.contact.number: ${ticket.contact.number}
|
|
message: ${body}
|
|
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`);
|
|
|
|
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();
|
|
};
|