mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 03:39:29 +00:00
feat: start delete whatsapp messages backend
This commit is contained in:
@@ -6,7 +6,9 @@ import { getIO } from "../libs/socket";
|
||||
|
||||
import CreateMessageService from "../services/MessageServices/CreateMessageService";
|
||||
import ListMessagesService from "../services/MessageServices/ListMessagesService";
|
||||
import UpdateMessageService from "../services/MessageServices/UpdateMessageService";
|
||||
import ShowTicketService from "../services/TicketServices/ShowTicketService";
|
||||
import DeleteWhatsAppMessage from "../services/WbotServices/DeleteWhatsAppMessage";
|
||||
import SendWhatsAppMedia from "../services/WbotServices/SendWhatsAppMedia";
|
||||
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
|
||||
|
||||
@@ -43,8 +45,6 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
|
||||
const ticket = await ShowTicketService(ticketId);
|
||||
|
||||
console.log(body);
|
||||
|
||||
let sentMessage: WbotMessage;
|
||||
|
||||
if (media) {
|
||||
@@ -79,3 +79,18 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
|
||||
return res.status(200).json(message);
|
||||
};
|
||||
|
||||
export const remove = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<Response> => {
|
||||
const { messageId } = req.params;
|
||||
|
||||
const messageDeleted = await DeleteWhatsAppMessage(messageId);
|
||||
|
||||
await UpdateMessageService({ messageData: { isDeleted: true }, messageId });
|
||||
|
||||
console.log(messageDeleted);
|
||||
|
||||
return res.json({ ok: true });
|
||||
};
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { QueryInterface, DataTypes } from "sequelize";
|
||||
|
||||
module.exports = {
|
||||
up: (queryInterface: QueryInterface) => {
|
||||
return queryInterface.addColumn("Messages", "isDeleted", {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: false
|
||||
});
|
||||
},
|
||||
|
||||
down: (queryInterface: QueryInterface) => {
|
||||
return queryInterface.removeColumn("Messages", "isDeleted");
|
||||
}
|
||||
};
|
||||
37
backend/src/helpers/GetWbotMessage.ts
Normal file
37
backend/src/helpers/GetWbotMessage.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { Message as WbotMessage } from "whatsapp-web.js";
|
||||
import Ticket from "../models/Ticket";
|
||||
import GetTicketWbot from "./GetTicketWbot";
|
||||
import AppError from "../errors/AppError";
|
||||
|
||||
export const GetWbotMessage = async (
|
||||
ticket: Ticket,
|
||||
messageId: string
|
||||
): Promise<WbotMessage> => {
|
||||
const wbot = await GetTicketWbot(ticket);
|
||||
|
||||
const wbotChat = await wbot.getChatById(`${ticket.contact.number}@c.us`);
|
||||
|
||||
try {
|
||||
const chatMessages = await wbotChat.fetchMessages({ limit: 20 });
|
||||
|
||||
const msgToDelete = chatMessages.find(msg => msg.id.id === messageId);
|
||||
|
||||
if (!msgToDelete) {
|
||||
throw new AppError("msgNotFound");
|
||||
}
|
||||
|
||||
return msgToDelete;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
if (err.message === "msgNotFound") {
|
||||
throw new AppError(
|
||||
"Could not find a message witht this ID in WhatsApp chat."
|
||||
);
|
||||
}
|
||||
throw new AppError(
|
||||
"Could not valid WhatsApp contact. Check connections page"
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default GetWbotMessage;
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
Model,
|
||||
DataType,
|
||||
PrimaryKey,
|
||||
AutoIncrement,
|
||||
Default,
|
||||
BelongsTo,
|
||||
ForeignKey
|
||||
@@ -47,6 +46,10 @@ class Message extends Model<Message> {
|
||||
@Column
|
||||
mediaType: string;
|
||||
|
||||
@Default(false)
|
||||
@Column
|
||||
isDeleted: boolean;
|
||||
|
||||
@CreatedAt
|
||||
@Column(DataType.DATE(6))
|
||||
createdAt: Date;
|
||||
|
||||
@@ -9,4 +9,6 @@ messageRoutes.get("/messages/:ticketId", isAuth, MessageController.index);
|
||||
|
||||
messageRoutes.post("/messages/:ticketId", isAuth, MessageController.store);
|
||||
|
||||
messageRoutes.delete("/messages/:messageId", isAuth, MessageController.remove);
|
||||
|
||||
export default messageRoutes;
|
||||
|
||||
20
backend/src/services/MessageServices/UpdateMessageService.ts
Normal file
20
backend/src/services/MessageServices/UpdateMessageService.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import Message from "../../models/Message";
|
||||
|
||||
interface MessageData {
|
||||
isDeleted?: boolean;
|
||||
}
|
||||
|
||||
interface Request {
|
||||
messageData: MessageData;
|
||||
messageId: string;
|
||||
}
|
||||
|
||||
const UpdateMessageService = async ({
|
||||
messageData,
|
||||
messageId
|
||||
}: Request): Promise<Response> => {
|
||||
const { isDeleted } = messageData;
|
||||
const message = Message.update({ where: {messageId}, { isDeleted: true } } );
|
||||
};
|
||||
|
||||
export default UpdateMessageService;
|
||||
36
backend/src/services/WbotServices/DeleteWhatsAppMessage.ts
Normal file
36
backend/src/services/WbotServices/DeleteWhatsAppMessage.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
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;
|
||||
@@ -1,12 +1,12 @@
|
||||
import React, { useState } from "react";
|
||||
|
||||
// import { toast } from "react-toastify";
|
||||
import { toast } from "react-toastify";
|
||||
|
||||
import MenuItem from "@material-ui/core/MenuItem";
|
||||
import Menu from "@material-ui/core/Menu";
|
||||
|
||||
// import { i18n } from "../../translate/i18n";
|
||||
// import api from "../../services/api";
|
||||
import api from "../../services/api";
|
||||
import ConfirmationModal from "../ConfirmationModal";
|
||||
|
||||
const MessageOptionsMenu = ({ messageId, menuOpen, handleClose, anchorEl }) => {
|
||||
@@ -15,11 +15,11 @@ const MessageOptionsMenu = ({ messageId, menuOpen, handleClose, anchorEl }) => {
|
||||
const handleDeleteMessage = async () => {
|
||||
console.log("message deleted", messageId);
|
||||
|
||||
// try {
|
||||
// await api.delete(`/messages/${message.id}`);
|
||||
// } catch (err) {
|
||||
// toast.error("Erro ao deletar o message");
|
||||
// }
|
||||
try {
|
||||
await api.delete(`/messages/${messageId}`);
|
||||
} catch (err) {
|
||||
toast.error("Erro ao deletar o message");
|
||||
}
|
||||
};
|
||||
|
||||
const handleOpenConfirmationModal = e => {
|
||||
|
||||
Reference in New Issue
Block a user