improvement: remove tickets logic from useTickets to improve notifications

This commit is contained in:
canove
2020-09-25 18:53:14 -03:00
parent c7d3807219
commit 5396837db4
10 changed files with 190 additions and 163 deletions

View File

@@ -4,12 +4,14 @@ import Ticket from "../../models/Ticket";
interface Request {
contactId: number;
status?: string;
status: string;
userId: number;
}
const CreateTicketService = async ({
contactId,
status
status,
userId
}: Request): Promise<Ticket> => {
const defaultWhatsapp = await GetDefaultWhatsApp();
@@ -19,7 +21,8 @@ const CreateTicketService = async ({
const { id }: Ticket = await defaultWhatsapp.$create("ticket", {
contactId,
status
status,
userId
});
const ticket = await Ticket.findByPk(id, { include: ["contact"] });

View File

@@ -1,7 +1,7 @@
import Ticket from "../../models/Ticket";
import AppError from "../../errors/AppError";
const DeleteTicketService = async (id: string): Promise<void> => {
const DeleteTicketService = async (id: string): Promise<Ticket> => {
const ticket = await Ticket.findOne({
where: { id }
});
@@ -11,6 +11,8 @@ const DeleteTicketService = async (id: string): Promise<void> => {
}
await ticket.destroy();
return ticket;
};
export default DeleteTicketService;

View File

@@ -12,10 +12,15 @@ interface Request {
ticketId: string;
}
interface Response {
ticket: Ticket;
oldStatus: string;
}
const UpdateTicketService = async ({
ticketData,
ticketId
}: Request): Promise<Ticket> => {
}: Request): Promise<Response> => {
const { status, userId } = ticketData;
const ticket = await Ticket.findOne({
@@ -33,12 +38,14 @@ const UpdateTicketService = async ({
throw new AppError("No ticket found with this ID.", 404);
}
const oldStatus = ticket.status;
await ticket.update({
status,
userId
});
return ticket;
return { ticket, oldStatus };
};
export default UpdateTicketService;

View File

@@ -138,12 +138,15 @@ const handleMessage = async (
}
const io = getIO();
io.to(ticket.id.toString()).to("notification").emit("appMessage", {
action: "create",
message: newMessage,
ticket,
contact
});
io.to(ticket.id.toString())
.to(ticket.status)
.to("notification")
.emit("appMessage", {
action: "create",
message: newMessage,
ticket,
contact
});
};
const isValidMsg = (msg: WbotMessage): boolean => {