diff --git a/backend/src/controllers/MessageController.ts b/backend/src/controllers/MessageController.ts index bff0869..321fa33 100644 --- a/backend/src/controllers/MessageController.ts +++ b/backend/src/controllers/MessageController.ts @@ -25,6 +25,8 @@ export const index = async (req: Request, res: Response): Promise => { const { ticketId } = req.params; const { pageNumber } = req.query as IndexQuery; + // console.log(':::::::::::::> TICKET ID: ', ticketId) + const { count, messages, ticket, hasMore } = await ListMessagesService({ pageNumber, ticketId diff --git a/backend/src/controllers/TicketController.ts b/backend/src/controllers/TicketController.ts index 60ef621..6d201f0 100644 --- a/backend/src/controllers/TicketController.ts +++ b/backend/src/controllers/TicketController.ts @@ -128,8 +128,8 @@ export const store = async (req: Request, res: Response): Promise => { export const show = async (req: Request, res: Response): Promise => { - const { ticketId } = req.params; - + const { ticketId } = req.params; + const contact = await ShowTicketService(ticketId); const { statusChatEnd, count, hasMore } = await ListStatusChatEndService({ searchParam: "", pageNumber: "1" }); diff --git a/backend/src/libs/socket.ts b/backend/src/libs/socket.ts index 50ee860..78d00ac 100644 --- a/backend/src/libs/socket.ts +++ b/backend/src/libs/socket.ts @@ -39,7 +39,7 @@ export const initIO = (httpServer: Server): SocketIO => { io = new SocketIO(httpServer, { cors: { origin: process.env.FRONTEND_URL - }, + }, maxHttpBufferSize: 1e8 }); @@ -59,18 +59,18 @@ export const initIO = (httpServer: Server): SocketIO => { }); socket.on("message_from_client", () => { - + socket.emit('message_from_server', 'Sent an event from the server!'); }) socket.on("message_create", async (data: any) => { - handleMessage(data.msg, data); + handleMessage(data.msg, data); }); - socket.on("media_uploaded", async (data: any) => { + socket.on("media_uploaded", async (data: any) => { handleMessage(data.msg, data); @@ -220,11 +220,11 @@ export const initIO = (httpServer: Server): SocketIO => { }); socket.on("joinChatBox", (ticketId: string) => { - logger.info("A client joined a ticket channel"); + logger.info("A client joined a ticket channel"); const regex = /^[0-9\b]+$/; // Regular expression to match only numbers - if (ticketId && !ticketId.match(regex)) return - + if (ticketId && !ticketId.match(regex) || !ticketId) return + socket.join(ticketId); }); @@ -247,18 +247,18 @@ export const initIO = (httpServer: Server): SocketIO => { let rooms = socket.rooms - console.log('rooms: ', rooms, ' | rooms.size: ', rooms.size) + console.log('rooms: ', rooms, ' | rooms.size: ', rooms.size) - if(rooms && rooms.size==1) return - if(rooms && rooms.size==2 && !([...rooms][1].startsWith('session_'))) return + if (rooms && rooms.size == 1) return + if (rooms && rooms.size == 2 && !([...rooms][1].startsWith('session_'))) return - let whatsappIds: any = await Whatsapp.findAll({ attributes: ['id'], raw: true }) + let whatsappIds: any = await Whatsapp.findAll({ attributes: ['id'], raw: true }) if (whatsappIds && whatsappIds.length > 0) { whatsappIds = whatsappIds.map((e: any) => `${e.id}`) - console.log('whatsappIds whatsappIds whatsappIds whatsappIds whatsappIds: ',whatsappIds) + console.log('whatsappIds whatsappIds whatsappIds whatsappIds whatsappIds: ', whatsappIds) if (rooms && rooms.size == 2 && [...rooms][1].startsWith('session_') && diff --git a/backend/src/services/MessageServices/ListMessagesService.ts b/backend/src/services/MessageServices/ListMessagesService.ts index 4f50ce1..a6261f2 100644 --- a/backend/src/services/MessageServices/ListMessagesService.ts +++ b/backend/src/services/MessageServices/ListMessagesService.ts @@ -18,11 +18,9 @@ interface Response { const ListMessagesService = async ({ pageNumber = "1", ticketId -}: Request): Promise => { - - console.log('>>>>>>>>>>>>>>>>>>>> ticketId: ', ticketId) - - if (ticketId && ticketId.length > 9) { +}: Request): Promise => { + + if ((ticketId && ticketId.length > 9) || !ticketId || (ticketId && ticketId === 'undefined')) { return { messages: [], ticket: new Ticket, diff --git a/backend/src/services/TicketServices/CreateTicketService.ts b/backend/src/services/TicketServices/CreateTicketService.ts index ae80bd0..be7ff80 100644 --- a/backend/src/services/TicketServices/CreateTicketService.ts +++ b/backend/src/services/TicketServices/CreateTicketService.ts @@ -70,13 +70,9 @@ const CreateTicketService = async ({ if (!ticket) { throw new AppError("ERR_CREATING_TICKET"); - } - - // console.log('CONTACT ticket.id: ', ticket.id) - - // console.log('>>>>>>>>>>>>>>>>> msg: ', msg) + } - if (msg.length > 0) { + if (msg.length > 0) { sendWhatsAppMessageSocket(ticket, msg) } diff --git a/backend/src/services/TicketServices/FindOrCreateTicketService.ts b/backend/src/services/TicketServices/FindOrCreateTicketService.ts index 601a99f..de661f0 100644 --- a/backend/src/services/TicketServices/FindOrCreateTicketService.ts +++ b/backend/src/services/TicketServices/FindOrCreateTicketService.ts @@ -15,8 +15,7 @@ const FindOrCreateTicketService = async ( groupContact?: Contact ): Promise => { - try { - + try { let ticket = await Ticket.findOne({ where: { status: { diff --git a/frontend/src/components/Ticket/index.js b/frontend/src/components/Ticket/index.js index de0e3d4..28a7d0a 100644 --- a/frontend/src/components/Ticket/index.js +++ b/frontend/src/components/Ticket/index.js @@ -115,9 +115,9 @@ const Ticket = () => { }, [ticketId, history]); useEffect(() => { - + const regex = /^[0-9\b]+$/; // Regular expression to match only numbers - if (ticketId && !ticketId.match(regex) || ticketId && ticketId.length > 9 ) return + if (ticketId && !ticketId.match(regex) || ticketId && ticketId.length > 9 || !ticketId) return const socket = openSocket(process.env.REACT_APP_BACKEND_URL); diff --git a/frontend/src/components/TicketsList/index.js b/frontend/src/components/TicketsList/index.js index 13d9994..90e2e01 100644 --- a/frontend/src/components/TicketsList/index.js +++ b/frontend/src/components/TicketsList/index.js @@ -183,17 +183,14 @@ const TicketsList = (props) => { const [ticketsList, dispatch] = useReducer(reducer, []); const { user } = useContext(AuthContext); - const { searchTicket } = useContext(SearchTicketContext) - + const { searchTicket } = useContext(SearchTicketContext) useEffect(() => { dispatch({ type: "RESET" }); setPageNumber(1); - }, [status, searchParam, showAll, selectedQueueIds, searchTicket]); - - + }, [status, searchParam, showAll, selectedQueueIds, searchTicket]); const { tickets, hasMore, loading } = useTickets({ pageNumber, diff --git a/frontend/src/pages/Tickets/WhatsLink/index.js b/frontend/src/pages/Tickets/WhatsLink/index.js index 273435c..3b47905 100644 --- a/frontend/src/pages/Tickets/WhatsLink/index.js +++ b/frontend/src/pages/Tickets/WhatsLink/index.js @@ -94,25 +94,46 @@ const Ticket = () => { useEffect(() => { setLoading(true); const delayDebounceFn = setTimeout(() => { - const fetchTicket = async () => { - - console.log('+++++++++++ ticketId: ', ticketId) + const fetchTicket = async () => { try { let number - let msg + let msg = '' let contactId + let link - console.log('ticketId: ', ticketId) - console.log('window.location.href): ', window.location.href) + link = window.location.href - const link = window.location.href.split('/https://wa.me/') + if (link.includes('/https://wa.me/')) { - number = link[1].split('?text=')[0] - msg = link[1].split('?text=')[1] || '' + link = window.location.href.split('/https://wa.me/') - msg = decodeURIComponent(msg); + number = link[1].split('?text=')[0] + msg = link[1].split('?text=')[1] || '' + } + else if (link.includes('https://web.whatsapp.com/send/?phone')) { + + let aux = link.split('?phone=') + + number = aux[1].split('&text')[0] + + if (link.includes('&text=')) { + + msg = aux[1].split('&text=')[1] + + msg = msg.replaceAll("+", " "); + + msg = msg.split('&type=')[0] + } + + } + + if (msg && msg.trim().length > 0) { + msg = decodeURIComponent(msg); + } + + console.log('NUMBER: ', number, ' | MESSAGE: ', msg) const { data: data0 } = await api.get("/contacts/", { params: { searchParam: number, pageNumber: "1" }, }); @@ -120,14 +141,14 @@ const Ticket = () => { console.log('-----> data: ', data0.contacts[0].id) contactId = data0.contacts[0].id } - else { + else { const values = { name: number, number: number, }; - const { data: data1 } = await api.post("/contacts", values); + const { data: data1 } = await api.post("/contacts", values); contactId = data1.id @@ -159,10 +180,10 @@ const Ticket = () => { return () => clearTimeout(delayDebounceFn); }, [ticketId, history, user.id]); - useEffect(() => { + useEffect(() => { const regex = /^[0-9\b]+$/; // Regular expression to match only numbers - if (ticketId && !ticketId.match(regex) || ticketId && ticketId.length > 9 ) return + if (ticketId && !ticketId.match(regex) || ticketId && ticketId.length > 9 || !ticketId) return const socket = openSocket(process.env.REACT_APP_BACKEND_URL); diff --git a/frontend/src/routes/index.js b/frontend/src/routes/index.js index 08dd6c1..76008dd 100644 --- a/frontend/src/routes/index.js +++ b/frontend/src/routes/index.js @@ -37,6 +37,8 @@ const Routes = () => { + +