2022-01-06 01:26:15 +00:00
|
|
|
import { Request, Response } from "express";
|
2023-08-28 17:05:53 +00:00
|
|
|
import whatsappOfficialAPI from "../helpers/WhatsappOfficialAPI";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
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";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
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-08-28 17:05:53 +00:00
|
|
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
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[];
|
|
|
|
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-02-07 15:47:40 +00:00
|
|
|
|
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
|
2022-01-06 01:26:15 +00:00
|
|
|
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
|
2022-07-20 15:34:12 +00:00
|
|
|
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"
|
|
|
|
);
|
2022-07-20 15:34:12 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
await SendWhatsAppMedia({ media, ticket });
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
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}
|
2023-08-28 17:05:53 +00:00
|
|
|
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();
|
|
|
|
};
|