Files
whaticket-community/backend/src/services/WbotServices/DeleteWhatsAppMessage.ts
2020-09-27 20:10:52 -03:00

37 lines
856 B
TypeScript

import AppError from "../../errors/AppError";
import GetWbotMessage from "../../helpers/GetWbotMessage";
import Message from "../../models/Message";
import Ticket from "../../models/Ticket";
const DeleteWhatsAppMessage = async (messageId: string): Promise<Message> => {
const message = await Message.findByPk(messageId, {
include: [
{
model: Ticket,
as: "ticket",
include: ["contact"]
}
]
});
if (!message) {
throw new AppError("No message found with this ID.");
}
const { ticket } = message;
const messageToDelete = await GetWbotMessage(ticket, messageId);
try {
await messageToDelete.delete(true);
} catch (err) {
throw new AppError(
"Couldn't delete message from WhatsApp. Check connections page."
);
}
return message;
};
export default DeleteWhatsAppMessage;