From 47df4fe77c8d39567d5f80edd039cf9f7e5c7a49 Mon Sep 17 00:00:00 2001 From: Aleksander Nicacio da Silva Date: Tue, 30 Aug 2022 22:29:23 -0300 Subject: [PATCH 1/2] [FIX] Visitor being overwritten on call end --- .../providers/CallProvider/CallProvider.tsx | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/apps/meteor/client/providers/CallProvider/CallProvider.tsx b/apps/meteor/client/providers/CallProvider/CallProvider.tsx index a92624f3b7b2d..8db26d98d8b5d 100644 --- a/apps/meteor/client/providers/CallProvider/CallProvider.tsx +++ b/apps/meteor/client/providers/CallProvider/CallProvider.tsx @@ -12,6 +12,7 @@ import { isVoipEventCallAbandoned, UserState, ICallDetails, + ILivechatVisitor, } from '@rocket.chat/core-typings'; import { useMutableCallback } from '@rocket.chat/fuselage-hooks'; import { @@ -77,6 +78,7 @@ export const CallProvider: FC = ({ children }) => { const visitorEndpoint = useEndpoint('POST', '/v1/livechat/visitor'); const voipEndpoint = useEndpoint('GET', '/v1/voip/room'); const voipCloseRoomEndpoint = useEndpoint('POST', '/v1/voip/room.close'); + const getContactBy = useEndpoint('GET', '/v1/omnichannel/contact.search'); const setModal = useSetModal(); const t = useTranslation(); @@ -161,20 +163,36 @@ export const CallProvider: FC = ({ children }) => { roomCoordinator.openRouteLink('v', { rid }); }, []); + const findOrCreateVisitor = useCallback( + async (caller: ICallerInfo): Promise => { + const phone = parseOutboundPhoneNumber(caller.callerId); + + const { contact } = await getContactBy({ phone }); + + if (contact) { + return contact as unknown as ILivechatVisitor; + } + + const { visitor } = await visitorEndpoint({ + visitor: { + token: Random.id(), + phone, + name: caller.callerName || phone, + }, + }); + + return visitor as unknown as ILivechatVisitor; + }, + [getContactBy, visitorEndpoint], + ); + const createRoom = useCallback( async (caller: ICallerInfo, direction: IVoipRoom['direction'] = 'inbound'): Promise => { if (!user) { return ''; } try { - const phone = parseOutboundPhoneNumber(caller.callerId); - const { visitor } = await visitorEndpoint({ - visitor: { - token: Random.id(), - phone, - name: caller.callerName || phone, - }, - }); + const visitor = await findOrCreateVisitor(caller); const voipRoom = await voipEndpoint({ token: visitor.token, agentId: user._id, direction }); openRoom(voipRoom.room._id); voipRoom.room && setRoomInfo({ v: { token: voipRoom.room.v.token }, rid: voipRoom.room._id }); @@ -188,7 +206,7 @@ export const CallProvider: FC = ({ children }) => { return ''; } }, - [openRoom, result.voipClient, user, visitorEndpoint, voipEndpoint], + [openRoom, result.voipClient, user, voipEndpoint, findOrCreateVisitor], ); useEffect(() => { From 40932a8325bbff44cceee5cb30d4d54532da270e Mon Sep 17 00:00:00 2001 From: yash-rajpal Date: Wed, 31 Aug 2022 23:45:38 +0530 Subject: [PATCH 2/2] serialize the typing --- apps/meteor/client/providers/CallProvider/CallProvider.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/meteor/client/providers/CallProvider/CallProvider.tsx b/apps/meteor/client/providers/CallProvider/CallProvider.tsx index 8db26d98d8b5d..8aef3240b057c 100644 --- a/apps/meteor/client/providers/CallProvider/CallProvider.tsx +++ b/apps/meteor/client/providers/CallProvider/CallProvider.tsx @@ -13,6 +13,7 @@ import { UserState, ICallDetails, ILivechatVisitor, + Serialized, } from '@rocket.chat/core-typings'; import { useMutableCallback } from '@rocket.chat/fuselage-hooks'; import { @@ -164,13 +165,13 @@ export const CallProvider: FC = ({ children }) => { }, []); const findOrCreateVisitor = useCallback( - async (caller: ICallerInfo): Promise => { + async (caller: ICallerInfo): Promise> => { const phone = parseOutboundPhoneNumber(caller.callerId); const { contact } = await getContactBy({ phone }); if (contact) { - return contact as unknown as ILivechatVisitor; + return contact; } const { visitor } = await visitorEndpoint({ @@ -181,7 +182,7 @@ export const CallProvider: FC = ({ children }) => { }, }); - return visitor as unknown as ILivechatVisitor; + return visitor; }, [getContactBy, visitorEndpoint], );