Skip to content

Commit

Permalink
Regression: Visitor being overwritten on call end (#26756)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Aleman <kaleman960@gmail.com>
Co-authored-by: yash-rajpal <rajpal.yash03@gmail.com>
  • Loading branch information
3 people authored Sep 1, 2022
1 parent e0b7e02 commit 4d340d9
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions apps/meteor/client/providers/CallProvider/CallProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
isVoipEventCallAbandoned,
UserState,
ICallDetails,
ILivechatVisitor,
Serialized,
} from '@rocket.chat/core-typings';
import { useMutableCallback } from '@rocket.chat/fuselage-hooks';
import {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -161,20 +164,36 @@ export const CallProvider: FC = ({ children }) => {
roomCoordinator.openRouteLink('v', { rid });
}, []);

const findOrCreateVisitor = useCallback(
async (caller: ICallerInfo): Promise<Serialized<ILivechatVisitor>> => {
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<IVoipRoom['_id']> => {
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 });
Expand All @@ -188,7 +207,7 @@ export const CallProvider: FC = ({ children }) => {
return '';
}
},
[openRoom, result.voipClient, user, visitorEndpoint, voipEndpoint],
[openRoom, result.voipClient, user, voipEndpoint, findOrCreateVisitor],
);

useEffect(() => {
Expand Down

0 comments on commit 4d340d9

Please sign in to comment.