improvement: code cleanup

This commit is contained in:
canove
2021-01-07 22:01:13 -03:00
parent 896f122cf7
commit c8c8dc43e9
9 changed files with 297 additions and 250 deletions

View File

@@ -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;
};