Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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