Skip to content

Commit

Permalink
Add Editar mensagens
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh committed Oct 27, 2024
1 parent d2a110c commit fb4e326
Show file tree
Hide file tree
Showing 19 changed files with 420 additions and 97 deletions.
16 changes: 16 additions & 0 deletions backend/src/controllers/MessageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Message from "../models/Message";
import ListMessagesService from "../services/MessageServices/ListMessagesService";
import ShowTicketService from "../services/TicketServices/ShowTicketService";
import DeleteWhatsAppMessage from "../services/WbotServices/DeleteWhatsAppMessage";
import EditWhatsAppMessage from "../services/WbotServices/EditWhatsAppMessage";
import SendWhatsAppMedia from "../services/WbotServices/SendWhatsAppMedia";
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";

Expand Down Expand Up @@ -57,6 +58,21 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
return res.send();
};

export const edit = async (req: Request, res: Response): Promise<Response> => {
const { messageId } = req.params;
const { body }: MessageData = req.body;

const message = await EditWhatsAppMessage(messageId, body);

const io = getIO();
io.to(message.ticketId.toString()).emit("appMessage", {
action: "update",
message
});

return res.send();
};

export const remove = async (
req: Request,
res: Response
Expand Down
20 changes: 11 additions & 9 deletions backend/src/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { Sequelize } from "sequelize-typescript";
import User from "../models/User";
import Setting from "../models/Setting";
import Contact from "../models/Contact";
import Ticket from "../models/Ticket";
import Whatsapp from "../models/Whatsapp";
import ContactCustomField from "../models/ContactCustomField";
import ContactTag from "../models/ContactTag";
import Integration from "../models/Integration";
import Message from "../models/Message";
import OldMessage from "../models/OldMessage";
import Queue from "../models/Queue";
import WhatsappQueue from "../models/WhatsappQueue";
import UserQueue from "../models/UserQueue";
import QuickAnswer from "../models/QuickAnswer";
import Setting from "../models/Setting";
import Tag from "../models/Tag";
import ContactTag from "../models/ContactTag";
import Integration from "../models/Integration";
import Ticket from "../models/Ticket";
import User from "../models/User";
import UserQueue from "../models/UserQueue";
import Whatsapp from "../models/Whatsapp";
import WhatsappQueue from "../models/WhatsappQueue";

// eslint-disable-next-line
const dbConfig = require("../config/database");
Expand All @@ -34,7 +35,8 @@ const models = [
QuickAnswer,
Tag,
ContactTag,
Integration
Integration,
OldMessage
];

sequelize.addModels(models);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DataTypes, QueryInterface } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.addColumn("Messages", "isEdited", {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.removeColumn("Messages", "isEdited");
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { DataTypes, QueryInterface } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.createTable("OldMessages", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
body: {
type: DataTypes.TEXT,
allowNull: false
},
messageId: {
type: DataTypes.STRING,
references: {
model: "Messages",
key: "id"
},
onUpdate: "CASCADE",
onDelete: "SET NULL"
},
createdAt: {
type: DataTypes.DATE(6),
allowNull: false
},
updatedAt: {
type: DataTypes.DATE(6),
allowNull: false
}
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.dropTable("OldMessages");
}
};
26 changes: 17 additions & 9 deletions backend/src/models/Message.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import {
Table,
BelongsTo,
Column,
CreatedAt,
UpdatedAt,
Model,
DataType,
PrimaryKey,
Default,
BelongsTo,
ForeignKey
ForeignKey,
HasMany,
Model,
PrimaryKey,
Table,
UpdatedAt
} from "sequelize-typescript";
import Contact from "./Contact";
import OldMessage from "./OldMessage";
import Ticket from "./Ticket";

@Table
Expand All @@ -37,9 +39,8 @@ class Message extends Model<Message> {
@Column(DataType.STRING)
get mediaUrl(): string | null {
if (this.getDataValue("mediaUrl")) {
return `${process.env.BACKEND_URL}:${
process.env.PROXY_PORT
}/public/${this.getDataValue("mediaUrl")}`;
return `${process.env.BACKEND_URL}:${process.env.PROXY_PORT
}/public/${this.getDataValue("mediaUrl")}`;
}
return null;
}
Expand All @@ -51,6 +52,10 @@ class Message extends Model<Message> {
@Column
isDeleted: boolean;

@Default(false)
@Column
isEdited: boolean;

@CreatedAt
@Column(DataType.DATE(6))
createdAt: Date;
Expand All @@ -73,6 +78,9 @@ class Message extends Model<Message> {
@BelongsTo(() => Ticket)
ticket: Ticket;

@HasMany(() => OldMessage)
oldMessages: OldMessage[];

@ForeignKey(() => Contact)
@Column
contactId: number;
Expand Down
42 changes: 42 additions & 0 deletions backend/src/models/OldMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
AutoIncrement,
BelongsTo,
Column,
CreatedAt,
DataType,
ForeignKey,
Model,
PrimaryKey,
Table,
UpdatedAt
} from "sequelize-typescript";

import Message from "./Message";

@Table
class OldMessage extends Model<OldMessage> {
@PrimaryKey
@AutoIncrement
@Column
id: number;

@Column(DataType.TEXT)
body: string;

@CreatedAt
@Column(DataType.DATE(6))
createdAt: Date;

@UpdatedAt
@Column(DataType.DATE(6))
updatedAt: Date;

@ForeignKey(() => Message)
@Column
messageId: string;

@BelongsTo(() => Message, "messageId")
message: Message;
}

export default OldMessage;
4 changes: 2 additions & 2 deletions backend/src/routes/messageRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router } from "express";
import multer from "multer";
import isAuth from "../middleware/isAuth";
import uploadConfig from "../config/upload";
import isAuth from "../middleware/isAuth";

import * as MessageController from "../controllers/MessageController";

Expand All @@ -17,7 +17,7 @@ messageRoutes.post(
upload.array("medias"),
MessageController.store
);

messageRoutes.post("/messages/edit/:messageId", isAuth, MessageController.edit);
messageRoutes.delete("/messages/:messageId", isAuth, MessageController.remove);

export default messageRoutes;
5 changes: 5 additions & 0 deletions backend/src/services/MessageServices/CreateMessageService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getIO } from "../../libs/socket";
import Message from "../../models/Message";
import OldMessage from "../../models/OldMessage";
import Ticket from "../../models/Ticket";
import Whatsapp from "../../models/Whatsapp";

Expand Down Expand Up @@ -42,6 +43,10 @@ const CreateMessageService = async ({
model: Message,
as: "quotedMsg",
include: ["contact"]
},
{
model: OldMessage,
as: "oldMessages"
}
]
});
Expand Down
17 changes: 11 additions & 6 deletions backend/src/services/MessageServices/ListMessagesService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Op } from "sequelize";
import AppError from "../../errors/AppError";
import Message from "../../models/Message";
import OldMessage from "../../models/OldMessage";
import Ticket from "../../models/Ticket";
import ShowTicketService from "../TicketServices/ShowTicketService";
import { Op } from "sequelize";

interface Request {
ticketId: string;
Expand Down Expand Up @@ -31,8 +32,8 @@ const ListMessagesService = async ({
const offset = limit * (+pageNumber - 1);

const { count, rows: messages } = await Message.findAndCountAll({
//where: { ticketId },
//where: {contactid : ticket.contactId},
// where: { ticketId },
// where: {contactid : ticket.contactId},
limit,
include: [
"contact",
Expand All @@ -41,16 +42,20 @@ const ListMessagesService = async ({
as: "quotedMsg",
include: ["contact"]
},
{
model: OldMessage,
as: "oldMessages"
},
{
model: Ticket,
where: {
contactId: ticket.contactId,
whatsappId: ticket.whatsappId,
queueId: {
[Op.or]: [ticket.queueId, null],
},
[Op.or]: [ticket.queueId, null]
}
},
required: true,
required: true
}
],
offset,
Expand Down
53 changes: 53 additions & 0 deletions backend/src/services/WbotServices/EditWhatsAppMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import AppError from "../../errors/AppError";
import GetWbotMessage from "../../helpers/GetWbotMessage";
import Message from "../../models/Message";
import Ticket from "../../models/Ticket";

const EditWhatsAppMessage = async (
messageId: string,
newBody: 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 hasBody = newBody
// ? formatBody(newBody as string, message.ticket)
// : undefined;

// if (!hasBody) {
// throw new AppError("No message found with this newBody.");
// }
const { ticket } = message;

const messageToEdit = await GetWbotMessage(ticket, messageId);

try {
const res = await messageToEdit.edit(newBody);
if (res === null) throw new Error("Can't edit");
} catch (err) {
throw new AppError("ERR_EDITING_WAPP_MSG");
}

// const mostRecentMessage = await Message.findOne({
// where: { ticketId: ticket.id },
// order: [["updatedAt", "DESC"]]
// });

// if (mostRecentMessage && mostRecentMessage.id === messageId) {
// await ticket.update({ lastMessage: newBody });
// }

return message;
};

export default EditWhatsAppMessage;
Loading

0 comments on commit fb4e326

Please sign in to comment.