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

[NEW] Capability to search visitors by custom fields #26312

Merged
merged 42 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
0f5fb61
[IMPROVE] Capability to search visitors by custom fields
cauefcr Jul 20, 2022
152638b
Merge branch 'develop' of ssh://github.com/RocketChat/Rocket.Chat int…
cauefcr Jul 21, 2022
76c6ca6
reverting i18n change
cauefcr Jul 21, 2022
9267b2f
Update apps/meteor/server/models/raw/LivechatVisitors.ts
cauefcr Jul 25, 2022
51fcb54
Merge branch 'develop' of ssh://github.com/RocketChat/Rocket.Chat int…
cauefcr Jul 25, 2022
181b23b
removing useless code
cauefcr Jul 25, 2022
bbaba3e
Merge branch 'fix/custom-field-search2' of ssh://github.com/RocketCha…
cauefcr Jul 25, 2022
d879b33
some extra changes to regex and queries and permissions
KevLehman Jul 26, 2022
0f92df4
fix new custom fields searchable, fix deprecated field usage, and val…
cauefcr Jul 27, 2022
fb1a23a
Merge branch 'fix/custom-field-search2' of ssh://github.com/RocketCha…
cauefcr Jul 27, 2022
8ce26b5
Merge branch 'develop' of ssh://github.com/RocketChat/Rocket.Chat int…
cauefcr Jul 27, 2022
274909a
livechat/visitor.search tests
cauefcr Jul 27, 2022
92678e2
better tests, thanks @kevin!
cauefcr Jul 29, 2022
8f15363
Merge branch 'develop' of ssh://github.com/RocketChat/Rocket.Chat int…
cauefcr Jul 29, 2022
04bebf4
Merge branch 'develop' of ssh://github.com/RocketChat/Rocket.Chat int…
cauefcr Aug 1, 2022
b8f0558
proper test end
cauefcr Aug 1, 2022
511e42b
Merge branch 'develop' into fix/custom-field-search2
KevLehman Aug 1, 2022
3b414ee
Merge branch 'develop' of ssh://github.com/RocketChat/Rocket.Chat int…
cauefcr Aug 3, 2022
b70de71
Merge branch 'develop' of ssh://github.com/RocketChat/Rocket.Chat int…
cauefcr Aug 3, 2022
f67c01c
Merge branch 'fix/custom-field-search2' of ssh://github.com/RocketCha…
cauefcr Aug 3, 2022
6097ad7
linting
cauefcr Aug 4, 2022
641da30
Merge branch 'develop' into fix/custom-field-search2
KevLehman Aug 15, 2022
6c83b8e
Merge branch 'develop' of ssh://github.com/RocketChat/Rocket.Chat int…
cauefcr Aug 15, 2022
8c35f64
Merge branch 'fix/custom-field-search2' of ssh://github.com/RocketCha…
cauefcr Aug 15, 2022
018742b
Merge branch 'develop' into fix/custom-field-search2
KevLehman Aug 18, 2022
3f97b3e
Merge branch 'develop' into fix/custom-field-search2
KevLehman Aug 18, 2022
cca0adb
Merge branch 'develop' into fix/custom-field-search2
KevLehman Aug 19, 2022
f953ae8
Merge branch 'develop' of ssh://github.com/RocketChat/Rocket.Chat int…
cauefcr Aug 23, 2022
c1d8685
Merge branch 'develop' of ssh://github.com/RocketChat/Rocket.Chat int…
cauefcr Aug 24, 2022
4eda605
refactoring queries to proper models and other fixes
cauefcr Aug 24, 2022
120332f
Merge branch 'fix/custom-field-search2' of ssh://github.com/RocketCha…
cauefcr Aug 24, 2022
7f403b5
fixing type error
cauefcr Aug 24, 2022
5f947f0
Merge branch 'develop' of ssh://github.com/RocketChat/Rocket.Chat int…
cauefcr Aug 24, 2022
9094b7b
Merge branch 'develop' into fix/custom-field-search2
casalsgh Aug 25, 2022
404f63a
Merge branch 'fix/custom-field-search2' of github.com:RocketChat/Rock…
KevLehman Aug 25, 2022
ae4ee00
small improvements
KevLehman Aug 25, 2022
a5d2c52
Merge remote-tracking branch 'origin/develop' into fix/custom-field-s…
sampaiodiego Aug 25, 2022
04b77de
small refactor
sampaiodiego Aug 25, 2022
31b73ac
remove that urlsearchparams
KevLehman Aug 25, 2022
48e26ab
forgotten changes
sampaiodiego Aug 25, 2022
9cd1fe3
Merge branch 'fix/custom-field-search2' of github.com:RocketChat/Rock…
sampaiodiego Aug 25, 2022
d142246
await but different
sampaiodiego Aug 25, 2022
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
4 changes: 2 additions & 2 deletions apps/meteor/app/livechat/imports/server/rest/visitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
findChatHistory,
searchChats,
findVisitorsToAutocomplete,
findVisitorsByEmailOrPhoneOrNameOrUsername,
findVisitorsByEmailOrPhoneOrNameOrUsernameOrCustomField,
} from '../../../server/api/lib/visitors';

API.v1.addRoute(
Expand Down Expand Up @@ -154,7 +154,7 @@ API.v1.addRoute(
const nameOrUsername = new RegExp(escapeRegExp(term), 'i');

return API.v1.success(
await findVisitorsByEmailOrPhoneOrNameOrUsername({
await findVisitorsByEmailOrPhoneOrNameOrUsernameOrCustomField({
userId: this.userId,
emailOrPhone: term,
nameOrUsername,
Expand Down
30 changes: 17 additions & 13 deletions apps/meteor/app/livechat/server/api/lib/visitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export async function findVisitorsToAutocomplete({ userId, selector }) {
};
}

export async function findVisitorsByEmailOrPhoneOrNameOrUsername({
export async function findVisitorsByEmailOrPhoneOrNameOrUsernameOrCustomField({
userId,
emailOrPhone,
nameOrUsername,
Expand All @@ -156,19 +156,23 @@ export async function findVisitorsByEmailOrPhoneOrNameOrUsername({
throw new Error('error-not-authorized');
}

const { cursor, totalCount } = LivechatVisitors.findPaginatedVisitorsByEmailOrPhoneOrNameOrUsername(emailOrPhone, nameOrUsername, {
sort: sort || { ts: -1 },
skip: offset,
limit: count,
projection: {
username: 1,
name: 1,
phone: 1,
livechatData: 1,
visitorEmails: 1,
lastChat: 1,
const { cursor, totalCount } = await LivechatVisitors.findPaginatedVisitorsByEmailOrPhoneOrNameOrUsernameOrCustomField(
emailOrPhone,
nameOrUsername,
{
sort: sort || { ts: -1 },
skip: offset,
limit: count,
projection: {
username: 1,
name: 1,
phone: 1,
livechatData: 1,
visitorEmails: 1,
lastChat: 1,
},
},
});
);

const [visitors, total] = await Promise.all([cursor.toArray(), totalCount]);

Expand Down
28 changes: 24 additions & 4 deletions apps/meteor/app/livechat/server/api/v1/contact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Match, check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import { LivechatVisitors } from '@rocket.chat/models';
import { LivechatCustomField, LivechatVisitors } from '@rocket.chat/models';

import { API } from '../../../../api/server';
import { Contacts } from '../../lib/Contacts';
Expand Down Expand Up @@ -47,19 +47,39 @@ API.v1.addRoute(
check(this.queryParams, {
email: Match.Maybe(String),
phone: Match.Maybe(String),
custom: Match.Maybe(String),
});
const { email, phone, custom } = this.queryParams;

const { email, phone } = this.queryParams;

if (!email && !phone) {
if (!email && !phone && !custom) {
throw new Meteor.Error('error-invalid-params');
}
let foundCF = {};
if (custom) {
const customObj = Object.fromEntries(
Array.from(new URLSearchParams(custom)).filter(([k, v]) => typeof k === 'string' && typeof v === 'string'),
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
);

foundCF = Object.fromEntries(
cauefcr marked this conversation as resolved.
Show resolved Hide resolved
(
await LivechatCustomField.find(
{ scope: 'visitor', searchable: true, _id: { $in: Object.keys(customObj) } },
cauefcr marked this conversation as resolved.
Show resolved Hide resolved
{
projection: {
_id: 1,
},
},
).toArray()
).map(({ _id }) => [_id, customObj[_id]]),
);
}

const query = Object.assign(
{},
{
...(email && { visitorEmails: { address: email } }),
...(phone && { phone: { phoneNumber: phone } }),
...(custom && { livechatData: foundCF }),
},
);

Expand Down
1 change: 1 addition & 0 deletions apps/meteor/app/models/server/models/LivechatRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class LivechatRooms extends Base {
},
},
);
this.tryEnsureIndex({ 'livechatData.$**': 1 });
}

findOneByIdOrName(_idOrName, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import React, { useMemo } from 'react';
const CustomFieldsForm = ({ values = {}, handlers = {}, className }) => {
const t = useTranslation();

const { id, field, label, scope, visibility, regexp } = values;
const { id, field, label, scope, visibility, searchable, regexp } = values;

const { handleField, handleLabel, handleScope, handleVisibility, handleRegexp } = handlers;
const { handleField, handleLabel, handleScope, handleVisibility, handleSearchable, handleRegexp } = handlers;

const scopeOptions = useMemo(
() => [
Expand Down Expand Up @@ -45,6 +45,14 @@ const CustomFieldsForm = ({ values = {}, handlers = {}, className }) => {
</Field.Row>
</Box>
</Field>
<Field className={className}>
<Box display='flex' flexDirection='row'>
<Field.Label htmlFor='searchable'>{t('Searchable')}</Field.Label>
<Field.Row>
<ToggleSwitch id='searchable' checked={searchable} onChange={handleSearchable} />
</Field.Row>
</Box>
</Field>
<Field className={className}>
<Field.Label>{t('Validation')}</Field.Label>
<Field.Row>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const getInitialValues = (cf) => ({
label: cf.label,
scope: cf.scope,
visibility: cf.visibility === 'visible',
searchable: !!cf.searchable,
regexp: cf.regexp,
});

Expand Down
1 change: 1 addition & 0 deletions apps/meteor/packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -4008,6 +4008,7 @@
"Screen_Share": "Screen Share",
"Script_Enabled": "Script Enabled",
"Search": "Search",
"Searchable": "Searchable",
"Search_Apps": "Search Apps",
"Search_by_file_name": "Search by file name",
"Search_by_username": "Search by username",
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/packages/rocketchat-i18n/i18n/pt-BR.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -3837,6 +3837,7 @@
"Screen_Share": "Compartilhamento de tela",
"Script_Enabled": "Script ativado",
"Search": "Pesquisar",
"Searchable": "Pesquisável",
"Search_Apps": "Pesquisar aplicativos",
"Search_by_file_name": "Pesquisar por nome de arquivo",
"Search_by_username": "Pesquisar por nome de usuário",
Expand Down
13 changes: 10 additions & 3 deletions apps/meteor/server/models/raw/LivechatVisitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
DeleteResult,
UpdateFilter,
} from 'mongodb';
import { Settings } from '@rocket.chat/models';
import { Settings, LivechatCustomField } from '@rocket.chat/models';
import { escapeRegExp } from '@rocket.chat/string-helpers';

import { BaseRaw } from './BaseRaw';
Expand All @@ -31,6 +31,7 @@ export class LivechatVisitorsRaw extends BaseRaw<ILivechatVisitor> implements IL
{ key: { name: 1 }, sparse: true },
{ key: { username: 1 } },
{ key: { 'contactMananger.username': 1 }, sparse: true },
{ key: { 'livechatData.$**': 1 } },
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
];
}

Expand Down Expand Up @@ -159,11 +160,16 @@ export class LivechatVisitorsRaw extends BaseRaw<ILivechatVisitor> implements IL
/**
* Find visitors by their email or phone or username or name
*/
findPaginatedVisitorsByEmailOrPhoneOrNameOrUsername(
async findPaginatedVisitorsByEmailOrPhoneOrNameOrUsernameOrCustomField(
emailOrPhone: string,
nameOrUsername: RegExp,
options: FindOptions<ILivechatVisitor>,
): FindPaginated<FindCursor<ILivechatVisitor>> {
): Promise<FindPaginated<FindCursor<ILivechatVisitor>>> {
const allowedCF = LivechatCustomField.find({ scope: 'visitor', searchable: true }, { projection: { fields: { _id: 1 } } }).map(
cauefcr marked this conversation as resolved.
Show resolved Hide resolved
({ _id }) => _id,
);

const filter = new RegExp(emailOrPhone, 'i');
const query = {
$or: [
{
Expand All @@ -178,6 +184,7 @@ export class LivechatVisitorsRaw extends BaseRaw<ILivechatVisitor> implements IL
{
username: nameOrUsername,
},
...(await allowedCF.toArray().then((cf) => cf.map((c: string) => ({ [`livechatData.${c}`]: filter })))),
],
};

Expand Down
1 change: 1 addition & 0 deletions packages/core-typings/src/ILivechatCustomField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface ILivechatCustomField extends IRocketChatRecord {
defaultValue?: string;
options?: string;
public?: boolean;
searchable?: boolean;
}
4 changes: 2 additions & 2 deletions packages/model-typings/src/models/ILivechatVisitorsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export interface ILivechatVisitorsModel extends IBaseModel<ILivechatVisitor> {
custom_name: string;
}
>;
findPaginatedVisitorsByEmailOrPhoneOrNameOrUsername(
findPaginatedVisitorsByEmailOrPhoneOrNameOrUsernameOrCustomField(
emailOrPhone: string,
nameOrUsername: RegExp,
options: FindOptions<ILivechatVisitor>,
): FindPaginated<FindCursor<ILivechatVisitor>>;
): Promise<FindPaginated<FindCursor<ILivechatVisitor>>>;

removeContactManagerByUsername(manager: string): Promise<UpdateResult | Document>;

Expand Down