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 1 commit
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
36 changes: 27 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,7 @@ import {
isVoipEventCallAbandoned,
UserState,
ICallDetails,
ILivechatVisitor,
} from '@rocket.chat/core-typings';
import { useMutableCallback } from '@rocket.chat/fuselage-hooks';
import {
Expand Down Expand Up @@ -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();

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

const findOrCreateVisitor = useCallback(
async (caller: ICallerInfo): Promise<ILivechatVisitor> => {
yash-rajpal marked this conversation as resolved.
Show resolved Hide resolved
const phone = parseOutboundPhoneNumber(caller.callerId);

const { contact } = await getContactBy({ phone });

if (contact) {
return contact as unknown as ILivechatVisitor;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type returned by useEndpoint is not compatible with the same type on core-typings. If any of you know how to solve this, please let me know.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is because of Types Serialization, when we send some data over HTTP requests, the objects and types are converted to strings, in this case Date type is converted to string type which creates this problem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something that is not exclusive to this PR, I seen this happen in other places, we need to investigate if this is an inconsistency issue between our types.

This is a work that will go over the scope of this PR so we can do this separately

}

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<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 +206,7 @@ export const CallProvider: FC = ({ children }) => {
return '';
}
},
[openRoom, result.voipClient, user, visitorEndpoint, voipEndpoint],
[openRoom, result.voipClient, user, voipEndpoint, findOrCreateVisitor],
);

useEffect(() => {
Expand Down