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

@@ -66,7 +66,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
});
const io = getIO();
io.to(ticketId).to("notification").emit("appMessage", {
io.to(ticketId).to("notification").to(ticket.status).emit("appMessage", {
action: "create",
message,
ticket,

View File

@@ -18,6 +18,7 @@ type IndexQuery = {
interface TicketData {
contactId: number;
status: string;
userId: number;
}
export const index = async (req: Request, res: Response): Promise<Response> => {
@@ -46,12 +47,12 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
};
export const store = async (req: Request, res: Response): Promise<Response> => {
const { contactId, status }: TicketData = req.body;
const { contactId, status, userId }: TicketData = req.body;
const ticket = await CreateTicketService({ contactId, status });
const ticket = await CreateTicketService({ contactId, status, userId });
const io = getIO();
io.to("notification").emit("ticket", {
io.to(ticket.status).emit("ticket", {
action: "create",
ticket
});
@@ -66,10 +67,21 @@ export const update = async (
const { ticketId } = req.params;
const ticketData: TicketData = req.body;
const ticket = await UpdateTicketService({ ticketData, ticketId });
const { ticket, oldStatus } = await UpdateTicketService({
ticketData,
ticketId
});
const io = getIO();
io.to("notification").emit("ticket", {
if (ticket.status !== oldStatus) {
io.to(oldStatus).emit("ticket", {
action: "delete",
ticketId: ticket.id
});
}
io.to(ticket.status).emit("ticket", {
action: "updateStatus",
ticket
});
@@ -83,13 +95,15 @@ export const remove = async (
): Promise<Response> => {
const { ticketId } = req.params;
await DeleteTicketService(ticketId);
const ticket = await DeleteTicketService(ticketId);
const io = getIO();
io.to("notification").emit("ticket", {
action: "delete",
ticketId: +ticketId
});
io.to(ticket.status)
.to("notification")
.emit("ticket", {
action: "delete",
ticketId: +ticketId
});
return res.status(200).json({ message: "ticket deleted" });
};