diff --git a/apps/meteor/client/providers/CallProvider/CallProvider.tsx b/apps/meteor/client/providers/CallProvider/CallProvider.tsx index a92624f3b7b2..8aef3240b057 100644 --- a/apps/meteor/client/providers/CallProvider/CallProvider.tsx +++ b/apps/meteor/client/providers/CallProvider/CallProvider.tsx @@ -12,6 +12,8 @@ import { isVoipEventCallAbandoned, UserState, ICallDetails, + ILivechatVisitor, + Serialized, } from '@rocket.chat/core-typings'; import { useMutableCallback } from '@rocket.chat/fuselage-hooks'; import { @@ -77,6 +79,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 +164,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; + } + + const { visitor } = await visitorEndpoint({ + visitor: { + token: Random.id(), + phone, + name: caller.callerName || phone, + }, + }); + + return visitor; + }, + [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 +207,7 @@ export const CallProvider: FC = ({ children }) => { return ''; } }, - [openRoom, result.voipClient, user, visitorEndpoint, voipEndpoint], + [openRoom, result.voipClient, user, voipEndpoint, findOrCreateVisitor], ); useEffect(() => {