Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: Visitor being overwritten on call end #26756

Merged
merged 6 commits into from
Sep 1, 2022
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