diff --git a/apps/meteor/app/livechat/imports/server/rest/sms.ts b/apps/meteor/app/livechat/imports/server/rest/sms.ts index 8d0d7670269e8..27f11a6c9808e 100644 --- a/apps/meteor/app/livechat/imports/server/rest/sms.ts +++ b/apps/meteor/app/livechat/imports/server/rest/sms.ts @@ -73,7 +73,7 @@ const defineVisitor = async (smsNumber: string, targetDepartment?: string) => { throw new Meteor.Error('error-invalid-visitor', 'Invalid visitor'); } - return LivechatVisitors.findOneEnabledById(livechatVisitor._id); + return livechatVisitor; }; const normalizeLocationSharing = (payload: ServiceData) => { diff --git a/apps/meteor/app/livechat/server/api/v1/message.ts b/apps/meteor/app/livechat/server/api/v1/message.ts index 1200cdbe6315d..921d48ba04615 100644 --- a/apps/meteor/app/livechat/server/api/v1/message.ts +++ b/apps/meteor/app/livechat/server/api/v1/message.ts @@ -251,7 +251,7 @@ API.v1.addRoute( async post() { const visitorToken = this.bodyParams.visitor.token; - let visitor = await LivechatVisitors.getVisitorByToken(visitorToken, {}); + const visitor = await LivechatVisitors.getVisitorByToken(visitorToken, {}); let rid: string; if (visitor) { const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {}); @@ -267,11 +267,10 @@ API.v1.addRoute( const guest: typeof this.bodyParams.visitor & { connectionData?: unknown } = this.bodyParams.visitor; guest.connectionData = normalizeHttpHeaderData(this.request.headers); - const livechatVisitor = await LivechatTyped.registerGuest(guest); - if (!livechatVisitor) { + const visitor = await LivechatTyped.registerGuest(guest); + if (!visitor) { throw new Error('error-livechat-visitor-registration'); } - visitor = await LivechatVisitors.findOneEnabledById(livechatVisitor?._id); } const guest = visitor; diff --git a/apps/meteor/app/livechat/server/api/v1/visitor.ts b/apps/meteor/app/livechat/server/api/v1/visitor.ts index a40cfdeab3c6d..a5b3f2de35b10 100644 --- a/apps/meteor/app/livechat/server/api/v1/visitor.ts +++ b/apps/meteor/app/livechat/server/api/v1/visitor.ts @@ -1,4 +1,4 @@ -import type { ILivechatCustomField, ILivechatVisitor, IRoom } from '@rocket.chat/core-typings'; +import type { ILivechatCustomField, IRoom } from '@rocket.chat/core-typings'; import { LivechatVisitors as VisitorsRaw, LivechatCustomField, LivechatRooms } from '@rocket.chat/models'; import { Match, check } from 'meteor/check'; import { Meteor } from 'meteor/meteor'; @@ -47,31 +47,28 @@ API.v1.addRoute('livechat/visitor', { connectionData: normalizeHttpHeaderData(this.request.headers), }; - const livechatVisitor = await LivechatTyped.registerGuest(guest); - if (!livechatVisitor) { + const visitor = await LivechatTyped.registerGuest(guest); + if (!visitor) { throw new Meteor.Error('error-livechat-visitor-registration', 'Error registering visitor', { method: 'livechat/visitor', }); } - let visitor: ILivechatVisitor | null = await VisitorsRaw.findOneEnabledById(livechatVisitor._id, {}); - if (visitor) { - const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {}); - // If it's updating an existing visitor, it must also update the roomInfo - const rooms = await LivechatRooms.findOpenByVisitorToken(visitor?.token, {}, extraQuery).toArray(); - await Promise.all( - rooms.map( - (room: IRoom) => - visitor && - LivechatTyped.saveRoomInfo(room, { - _id: visitor._id, - name: visitor.name, - phone: visitor.phone?.[0]?.phoneNumber, - livechatData: visitor.livechatData as { [k: string]: string }, - }), - ), - ); - } + const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {}); + // If it's updating an existing visitor, it must also update the roomInfo + const rooms = await LivechatRooms.findOpenByVisitorToken(visitor?.token, {}, extraQuery).toArray(); + await Promise.all( + rooms.map( + (room: IRoom) => + visitor && + LivechatTyped.saveRoomInfo(room, { + _id: visitor._id, + name: visitor.name, + phone: visitor.phone?.[0]?.phoneNumber, + livechatData: visitor.livechatData as { [k: string]: string }, + }), + ), + ); if (customFields && Array.isArray(customFields) && customFields.length > 0) { const keys = customFields.map((field) => field.key); @@ -101,7 +98,7 @@ API.v1.addRoute('livechat/visitor', { if (processedKeys.length !== keys.length) { LivechatTyped.logger.warn({ msg: 'Some custom fields were not processed', - visitorId: livechatVisitor._id, + visitorId: visitor._id, missingKeys: keys.filter((key) => !processedKeys.includes(key)), }); } @@ -109,13 +106,13 @@ API.v1.addRoute('livechat/visitor', { if (errors.length > 0) { LivechatTyped.logger.error({ msg: 'Error updating custom fields', - visitorId: livechatVisitor._id, + visitorId: visitor._id, errors, }); throw new Error('error-updating-custom-fields'); } - visitor = await VisitorsRaw.findOneEnabledById(livechatVisitor._id); + return API.v1.success({ visitor: await VisitorsRaw.findOneEnabledById(visitor._id) }); } if (!visitor) { diff --git a/apps/meteor/app/livechat/server/methods/registerGuest.ts b/apps/meteor/app/livechat/server/methods/registerGuest.ts index 89b4f7f44e755..4a531d0c89e52 100644 --- a/apps/meteor/app/livechat/server/methods/registerGuest.ts +++ b/apps/meteor/app/livechat/server/methods/registerGuest.ts @@ -23,16 +23,16 @@ declare module '@rocket.chat/ui-contexts' { department?: string; customFields?: Array<{ key: string; value: string; overwrite: boolean; scope?: unknown }>; }): { - userId: string; - visitor: ILivechatVisitor | null; + userId: ILivechatVisitor['_id']; + visitor: Pick; }; } } Meteor.methods({ async 'livechat:registerGuest'({ token, name, email, department, customFields } = {}): Promise<{ - userId: string; - visitor: ILivechatVisitor | null; + userId: ILivechatVisitor['_id']; + visitor: Pick; }> { methodDeprecationLogger.method('livechat:registerGuest', '7.0.0'); @@ -40,7 +40,7 @@ Meteor.methods({ throw new Meteor.Error('error-invalid-token', 'Invalid token', { method: 'livechat:registerGuest' }); } - const livechatVisitor = await LivechatTyped.registerGuest.call(this, { + const visitor = await LivechatTyped.registerGuest.call(this, { token, name, email, @@ -50,17 +50,6 @@ Meteor.methods({ // update visited page history to not expire await Messages.keepHistoryForToken(token); - // TODO: remove unneeded find - registerGuest returns created record - const visitor = await LivechatVisitors.getVisitorByToken(token, { - projection: { - token: 1, - name: 1, - username: 1, - visitorEmails: 1, - department: 1, - }, - }); - if (!visitor) { throw new Meteor.Error('error-invalid-visitor', 'Invalid visitor', { method: 'livechat:registerGuest' }); } @@ -93,8 +82,15 @@ Meteor.methods({ } return { - userId: livechatVisitor?._id ?? '', - visitor, + userId: visitor._id, + visitor: { + _id: visitor._id, + token: visitor.token, + name: visitor.name, + username: visitor.username, + visitorEmails: visitor.visitorEmails, + department: visitor.department, + }, }; }, });