-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
420 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
backend/src/database/migrations/20241026162841-add-isEdited-column-to-messages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}; |
39 changes: 39 additions & 0 deletions
39
backend/src/database/migrations/20241026164855-create-oldmessages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.