From 95c6dcb32f0ae7a4a58c8437d5c7496724ae38c4 Mon Sep 17 00:00:00 2001 From: canove Date: Sun, 20 Sep 2020 12:00:30 -0300 Subject: [PATCH] enabled socket io on tickets controller --- backend/src/controllers/TicketController.ts | 82 ++++--------------- .../TicketServices/UpdateTicketService.ts | 2 +- frontend/src/pages/Connections/index.js | 42 +++++----- 3 files changed, 39 insertions(+), 87 deletions(-) diff --git a/backend/src/controllers/TicketController.ts b/backend/src/controllers/TicketController.ts index 7684d47..0ae36da 100644 --- a/backend/src/controllers/TicketController.ts +++ b/backend/src/controllers/TicketController.ts @@ -1,11 +1,11 @@ import { Request, Response } from "express"; +import { getIO } from "../libs/socket"; + import CreateTicketService from "../services/TicketServices/CreateTicketService"; import DeleteTicketService from "../services/TicketServices/DeleteTicketService"; import ListTicketsService from "../services/TicketServices/ListTicketsService"; import UpdateTicketService from "../services/TicketServices/UpdateTicketService"; -// const { getIO } = require("../libs/socket"); - type IndexQuery = { searchParam: string; pageNumber: string; @@ -30,54 +30,6 @@ export const index = async (req: Request, res: Response): Promise => { const userId = req.user.id; - // let includeCondition = [ - // { - // model: Contact, - // as: "contact", - // attributes: ["name", "number", "profilePicUrl"] - // } - // ]; - - // if (searchParam) { - // includeCondition = [ - // ...includeCondition, - // { - // model: Message, - // as: "messages", - // attributes: ["id", "body"], - // where: { - // body: Sequelize.where( - // Sequelize.fn("LOWER", Sequelize.col("body")), - // "LIKE", - // "%" + searchParam.toLowerCase() + "%" - // ) - // }, - // required: false, - // duplicating: false - // } - // ]; - - // whereCondition = { - // [Sequelize.Op.or]: [ - // { - // "$contact.name$": Sequelize.where( - // Sequelize.fn("LOWER", Sequelize.col("name")), - // "LIKE", - // "%" + searchParam.toLowerCase() + "%" - // ) - // }, - // { "$contact.number$": { [Sequelize.Op.like]: `%${searchParam}%` } }, - // { - // "$message.body$": Sequelize.where( - // Sequelize.fn("LOWER", Sequelize.col("body")), - // "LIKE", - // "%" + searchParam.toLowerCase() + "%" - // ) - // } - // ] - // }; - // } - const { tickets, count, hasMore } = await ListTicketsService({ searchParam, pageNumber, @@ -95,11 +47,11 @@ export const store = async (req: Request, res: Response): Promise => { const ticket = await CreateTicketService({ contactId, status }); - // const io = getIO(); - // io.to("notification").emit("ticket", { - // action: "create", - // ticket: serializaedTicket - // }); + const io = getIO(); + io.to("notification").emit("ticket", { + action: "create", + ticket + }); return res.status(200).json(ticket); }; @@ -113,11 +65,11 @@ export const update = async ( const ticket = await UpdateTicketService({ ticketData, ticketId }); - // const io = getIO(); - // io.to("notification").emit("ticket", { - // action: "updateStatus", - // ticket: ticket - // }); + const io = getIO(); + io.to("notification").emit("ticket", { + action: "updateStatus", + ticket + }); return res.status(200).json(ticket); }; @@ -130,11 +82,11 @@ export const remove = async ( await DeleteTicketService(ticketId); - // const io = getIO(); - // io.to("notification").emit("ticket", { - // action: "delete", - // ticketId: ticket.id - // }); + const io = getIO(); + io.to("notification").emit("ticket", { + action: "delete", + ticketId: +ticketId + }); return res.status(200).json({ message: "ticket deleted" }); }; diff --git a/backend/src/services/TicketServices/UpdateTicketService.ts b/backend/src/services/TicketServices/UpdateTicketService.ts index 303a91d..f7b893d 100644 --- a/backend/src/services/TicketServices/UpdateTicketService.ts +++ b/backend/src/services/TicketServices/UpdateTicketService.ts @@ -24,7 +24,7 @@ const UpdateTicketService = async ({ { model: Contact, as: "contact", - attributes: ["name", "number", "profilePicUrl"] + attributes: ["id", "name", "number", "profilePicUrl"] } ] }); diff --git a/frontend/src/pages/Connections/index.js b/frontend/src/pages/Connections/index.js index 3df1a65..800b478 100644 --- a/frontend/src/pages/Connections/index.js +++ b/frontend/src/pages/Connections/index.js @@ -111,31 +111,31 @@ const Connections = () => { fetchSession(); }, []); - // useEffect(() => { - // const socket = openSocket(process.env.REACT_APP_BACKEND_URL); + useEffect(() => { + const socket = openSocket(process.env.REACT_APP_BACKEND_URL); - // socket.on("whatsapp", data => { - // if (data.action === "update") { - // dispatch({ type: "UPDATE_WHATSAPPS", payload: data.whatsapp }); - // } - // }); + socket.on("whatsapp", data => { + if (data.action === "update") { + dispatch({ type: "UPDATE_WHATSAPPS", payload: data.whatsapp }); + } + }); - // socket.on("whatsapp", data => { - // if (data.action === "delete") { - // dispatch({ type: "DELETE_WHATSAPPS", payload: data.whatsappId }); - // } - // }); + socket.on("whatsapp", data => { + if (data.action === "delete") { + dispatch({ type: "DELETE_WHATSAPPS", payload: data.whatsappId }); + } + }); - // socket.on("whatsappSession", data => { - // if (data.action === "update") { - // dispatch({ type: "UPDATE_SESSION", payload: data.session }); - // } - // }); + socket.on("whatsappSession", data => { + if (data.action === "update") { + dispatch({ type: "UPDATE_SESSION", payload: data.session }); + } + }); - // return () => { - // socket.disconnect(); - // }; - // }, []); + return () => { + socket.disconnect(); + }; + }, []); // const handleDisconnectSession = async whatsAppId => { // try {