mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 12:19:16 +00:00
improvement: code cleanup
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import AppError from "../../errors/AppError";
|
||||
import { getIO } from "../../libs/socket";
|
||||
import Message from "../../models/Message";
|
||||
import ShowTicketService from "../TicketServices/ShowTicketService";
|
||||
import Ticket from "../../models/Ticket";
|
||||
|
||||
interface MessageData {
|
||||
id: string;
|
||||
@@ -19,17 +19,16 @@ interface Request {
|
||||
const CreateMessageService = async ({
|
||||
messageData
|
||||
}: Request): Promise<Message> => {
|
||||
const ticket = await ShowTicketService(messageData.ticketId);
|
||||
|
||||
if (!ticket) {
|
||||
throw new AppError("ERR_NO_TICKET_FOUND", 404);
|
||||
}
|
||||
|
||||
await Message.upsert(messageData);
|
||||
|
||||
const message = await Message.findByPk(messageData.id, {
|
||||
include: [
|
||||
"contact",
|
||||
{
|
||||
model: Ticket,
|
||||
as: "ticket",
|
||||
include: ["contact"]
|
||||
},
|
||||
{
|
||||
model: Message,
|
||||
as: "quotedMsg",
|
||||
@@ -39,9 +38,20 @@ const CreateMessageService = async ({
|
||||
});
|
||||
|
||||
if (!message) {
|
||||
throw new AppError("ERR_CREATING_MESSAGE", 501);
|
||||
throw new Error("ERR_CREATING_MESSAGE");
|
||||
}
|
||||
|
||||
const io = getIO();
|
||||
io.to(message.ticketId.toString())
|
||||
.to(message.ticket.status)
|
||||
.to("notification")
|
||||
.emit("appMessage", {
|
||||
action: "create",
|
||||
message,
|
||||
ticket: message.ticket,
|
||||
contact: message.ticket.contact
|
||||
});
|
||||
|
||||
return message;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user