mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
improvement: remove tickets logic from useTickets to improve notifications
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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" });
|
||||
};
|
||||
|
||||
@@ -24,7 +24,7 @@ const SetTicketMessagesAsRead = async (ticket: Ticket): Promise<void> => {
|
||||
}
|
||||
|
||||
const io = getIO();
|
||||
io.to("notification").emit("ticket", {
|
||||
io.to(ticket.status).to("notification").emit("ticket", {
|
||||
action: "updateUnread",
|
||||
ticketId: ticket.id
|
||||
});
|
||||
|
||||
@@ -18,6 +18,11 @@ export const initIO = (httpServer: Server): SocketIO => {
|
||||
socket.join("notification");
|
||||
});
|
||||
|
||||
socket.on("joinTickets", status => {
|
||||
console.log(`A client joined to ${status} tickets channel.`);
|
||||
socket.join(status);
|
||||
});
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
console.log("Client disconnected");
|
||||
});
|
||||
|
||||
@@ -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"] });
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
Reference in New Issue
Block a user