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

195 lines
5.5 KiB
TypeScript
Raw Normal View History

import { Request, Response } from "express";
2023-08-28 17:05:53 +00:00
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";
2023-08-28 17:05:53 +00:00
import axios from "axios";
import Contact from "../models/Contact";
import {
isValidMsg,
verifyMessage
} from "../services/WbotServices/wbotMessageListener";
import CreateOrUpdateContactService from "../services/ContactServices/CreateOrUpdateContactService";
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
});
// SetTicketMessagesAsRead(ticket);
return res.json({ count, messages, ticket, hasMore });
};
2023-08-28 17:05:53 +00:00
export const store = async (req: Request, res: Response): Promise<Response> => {
const { ticketId } = req.params;
const { body, quotedMsg }: MessageData = req.body;
const medias = req.files as Express.Multer.File[];
const ticket = await ShowTicketService(ticketId);
2023-08-28 17:05:53 +00:00
const { queueId } = ticket;
console.log("-----------> queueId: ", queueId);
2023-01-20 19:38:24 +00:00
2023-08-28 17:05:53 +00:00
// TEST FILA DE ATENDIMENTO 42 WHATSAPP OFFICIAL
if (queueId == 42) {
const { contactId } = ticket;
const contact: any = await Contact.findByPk(contactId);
const { number } = contact;
console.log("NUMBER: ", number);
console.log("CONTACT ID: ", contactId);
const data = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: number,
type: "text",
text: {
preview_url: true,
body
}
};
if (!isValidMsg({ type: data.type })) {
return res.status(400).json({ message: "Wrong message type" });
}
whatsappOfficialAPI
.post("/v17.0/105394365522185/messagess", data)
.then(response => {
console.log("Response:", response.data);
if (response.status == 200) {
console.log("STATUS 200");
}
let msg = {};
msg = {
...msg,
id: { id: response.data.messages[0].id },
fromMe: true,
type: "chat",
read: false,
body
};
verifyMessage(msg, ticket, contact, quotedMsg);
2023-08-28 17:05:53 +00:00
})
.catch(error => {
console.log(
"Error on try request: ",
error.response.data.error.message
);
// return res
// .status(500)
// .json({ error: error.response.data.error.message });
if (error?.response?.data?.error?.message && error.response?.status) {
}
if (error.response) {
// The request was made and the server responded with a non-2xx status code
throw new Error(
`Request failed with status ${error.response.status}`
);
} else if (error.request) {
// The request was made but no response was received (e.g., network error)
throw new Error("No response received from the server");
} else {
// Something happened in setting up the request that triggered an error
throw new Error("Request configuration error");
}
});
// return res.status(500).json({ error: "Internal server error" });
return res.send();
}
// ORIGINAL
if (medias) {
await Promise.all(
2023-08-28 17:05:53 +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}
2023-08-28 17:05:53 +00:00
media:`,
media,
"\n"
);
await SendWhatsAppMedia({ media, ticket });
})
);
} 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}
2023-08-28 17:05:53 +00:00
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();
};