Skip to content

Commit

Permalink
Merge branch 'feat/single-contact-id' into feat/search-create-edit
Browse files Browse the repository at this point in the history
  • Loading branch information
dougfabris authored Oct 15, 2024
2 parents 5c221e7 + 8617215 commit dbc0ef0
Show file tree
Hide file tree
Showing 45 changed files with 1,407 additions and 278 deletions.
6 changes: 6 additions & 0 deletions .changeset/calm-worms-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rocket.chat/model-typings': minor
'@rocket.chat/meteor': minor
---

Added on-demand data migration for omnichannel visitors to be converted into the new contacts
5 changes: 5 additions & 0 deletions .changeset/purple-papayas-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixes a logical error when updating the `responseBy` property of a room while `waitingResponse` property was still defined.
6 changes: 6 additions & 0 deletions .changeset/two-geckos-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": major
"@rocket.chat/i18n": major
---

Adds new empty states for the marketplace view
2 changes: 0 additions & 2 deletions apps/meteor/app/apps/server/converters/visitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class AppVisitorsConverter {
visitorEmails: 'visitorEmails',
livechatData: 'livechatData',
status: 'status',
contactId: 'contactId',
};

return transformMappedData(visitor, map);
Expand All @@ -55,7 +54,6 @@ export class AppVisitorsConverter {
phone: visitor.phone,
livechatData: visitor.livechatData,
status: visitor.status || 'online',
contactId: visitor.contactId,
...(visitor.visitorEmails && { visitorEmails: visitor.visitorEmails }),
...(visitor.department && { department: visitor.department }),
};
Expand Down
12 changes: 1 addition & 11 deletions apps/meteor/app/livechat/server/api/lib/livechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,7 @@ async function findDepartments(
}

export function findGuest(token: string): Promise<ILivechatVisitor | null> {
return LivechatVisitors.getVisitorByToken(token, {
projection: {
name: 1,
username: 1,
token: 1,
visitorEmails: 1,
department: 1,
activity: 1,
contactId: 1,
},
});
return LivechatVisitors.getVisitorByToken(token);
}

export function findGuestWithoutActivity(token: string): Promise<ILivechatVisitor | null> {
Expand Down
17 changes: 16 additions & 1 deletion apps/meteor/app/livechat/server/api/lib/visitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { FindOptions } from 'mongodb';

import { callbacks } from '../../../../../lib/callbacks';
import { canAccessRoomAsync } from '../../../../authorization/server/functions/canAccessRoom';
import { isSingleContactEnabled, migrateVisitorToContactId, getContactIdByVisitorId } from '../../lib/Contacts';

export async function findVisitorInfo({ visitorId }: { visitorId: IVisitor['_id'] }) {
const visitor = await LivechatVisitors.findOneEnabledById(visitorId);
Expand All @@ -12,7 +13,21 @@ export async function findVisitorInfo({ visitorId }: { visitorId: IVisitor['_id'
}

return {
visitor,
visitor: await addContactIdToVisitor(visitor),
};
}

export async function addContactIdToVisitor(visitor: ILivechatVisitor): Promise<ILivechatVisitor & { contactId?: string }> {
if (!isSingleContactEnabled()) {
return visitor;
}

const contactId = await getContactIdByVisitorId(visitor._id);

// If the visitor doesn't have a contactId yet, create a new contact for it using the same _id
return {
...visitor,
contactId: contactId || (await migrateVisitorToContactId(visitor, undefined, true)) || undefined,
};
}

Expand Down
27 changes: 25 additions & 2 deletions apps/meteor/app/livechat/server/api/v1/contact.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ILivechatContact } from '@rocket.chat/core-typings';
import { LivechatContacts, LivechatCustomField, LivechatVisitors } from '@rocket.chat/models';
import {
isPOSTOmnichannelContactsProps,
Expand All @@ -12,7 +13,15 @@ import { Meteor } from 'meteor/meteor';

import { API } from '../../../../api/server';
import { getPaginationItems } from '../../../../api/server/helpers/getPaginationItems';
import { getContactHistory, Contacts, createContact, updateContact, getContacts, isSingleContactEnabled } from '../../lib/Contacts';
import {
getContactHistory,
Contacts,
createContact,
getContact,
updateContact,
getContacts,
isSingleContactEnabled,
} from '../../lib/Contacts';

API.v1.addRoute(
'omnichannel/contact',
Expand Down Expand Up @@ -136,7 +145,21 @@ API.v1.addRoute(
if (!isSingleContactEnabled()) {
return API.v1.unauthorized();
}
const contact = await LivechatContacts.findOneById(this.queryParams.contactId);

const { contactId, email, phone } = this.queryParams;
let contact: ILivechatContact | null = null;

if (contactId) {
contact = await getContact(contactId);
}

if (email) {
contact = await LivechatContacts.findOne({ 'emails.address': email });
}

if (phone) {
contact = await LivechatContacts.findOne({ 'phones.phoneNumber': phone });
}

return API.v1.success({ contact });
},
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/app/livechat/server/api/v1/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { API } from '../../../../api/server';
import { settings } from '../../../../settings/server';
import { Livechat as LivechatTyped } from '../../lib/LivechatTyped';
import { findGuest, normalizeHttpHeaderData } from '../lib/livechat';
import { addContactIdToVisitor } from '../lib/visitors';

API.v1.addRoute(
'livechat/visitor',
Expand Down Expand Up @@ -144,7 +145,7 @@ API.v1.addRoute('livechat/visitor/:token', {
throw new Meteor.Error('invalid-token');
}

return API.v1.success({ visitor });
return API.v1.success({ visitor: await addContactIdToVisitor(visitor) });
},
async delete() {
check(this.urlParams, {
Expand Down
15 changes: 7 additions & 8 deletions apps/meteor/app/livechat/server/hooks/markRoomResponded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ export async function markRoomResponded(
}
}

if (room.responseBy) {
LivechatRooms.getAgentLastMessageTsUpdateQuery(roomUpdater);
}

if (!room.waitingResponse) {
// case where agent sends second message or any subsequent message in a room before visitor responds to the first message
// in this case, we just need to update the lastMessageTs of the responseBy object
Expand All @@ -47,10 +43,13 @@ export async function markRoomResponded(
return room.responseBy;
}

const responseBy: IOmnichannelRoom['responseBy'] = room.responseBy || {
_id: message.u._id,
username: message.u.username,
firstResponseTs: new Date(message.ts),
// Since we're updating the whole object anyways, we re-use the same values from object (or from message if not present)
// And then we update the lastMessageTs, which is the only thing that should be updating here
const { responseBy: { _id, username, firstResponseTs } = {} } = room;
const responseBy: IOmnichannelRoom['responseBy'] = {
_id: _id || message.u._id,
username: username || message.u.username,
firstResponseTs: firstResponseTs || new Date(message.ts),
lastMessageTs: new Date(message.ts),
};

Expand Down
Loading

0 comments on commit dbc0ef0

Please sign in to comment.