Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Aug 25, 2022
1 parent a5d2c52 commit 04b77de
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion apps/meteor/app/livechat/server/api/lib/visitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export async function findVisitorsByEmailOrPhoneOrNameOrUsernameOrCustomField({
const { cursor, totalCount } = await LivechatVisitors.findPaginatedVisitorsByEmailOrPhoneOrNameOrUsernameOrCustomField(
emailOrPhone,
nameOrUsername,
allowedCF,
{
sort: sort || { ts: -1 },
skip: offset,
Expand All @@ -154,7 +155,6 @@ export async function findVisitorsByEmailOrPhoneOrNameOrUsernameOrCustomField({
lastChat: 1,
},
},
allowedCF,
);

const [visitors, total] = await Promise.all([cursor.toArray(), totalCount]);
Expand Down
24 changes: 11 additions & 13 deletions apps/meteor/app/livechat/server/api/v1/contact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Match, check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import { LivechatCustomField, LivechatVisitors } from '@rocket.chat/models';
import { escapeRegExp } from '@rocket.chat/string-helpers';

import { API } from '../../../../api/server';
import { Contacts } from '../../lib/Contacts';
Expand Down Expand Up @@ -54,21 +55,18 @@ API.v1.addRoute(
if (!email && !phone && !custom) {
throw new Meteor.Error('error-invalid-params');
}
let foundCF = {};
if (custom) {
const foundCF = (async () => {
if (!custom) {
return {};
}

const customObj = Object.fromEntries(Array.from(new URLSearchParams(custom)));
const customFields = await LivechatCustomField.findMatchingCustomFields(
'visitor',
true,
{},
{ _id: { $in: Object.keys(customObj) } },
)
.map(({ _id }) => _id)
.map((id: string) => [id, customObj[id]])
.toArray();

foundCF = Object.fromEntries(customFields);
}
const customFields = await LivechatCustomField.findMatchingCustomFieldsByIds(Object.keys(customObj), 'visitor', true).toArray();

return Object.fromEntries(customFields.map(({ _id }) => [`livechatData.${_id}`, new RegExp(escapeRegExp(customObj[_id], 'i'))]));
})();

const contact = await LivechatVisitors.findOneByEmailAndPhoneAndCustomField(email, phone, foundCF);
return API.v1.success({ contact });
},
Expand Down
19 changes: 16 additions & 3 deletions apps/meteor/server/models/raw/LivechatCustomField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@ export class LivechatCustomFieldRaw extends BaseRaw<ILivechatCustomField> implem
findMatchingCustomFields(
scope: ILivechatCustomField['scope'],
searchable = true,
options: FindOptions<ILivechatCustomField> = {},
extraFilter: { [key: string]: string | string[] | { [key: string]: string | string[] } } = {},
options?: FindOptions<ILivechatCustomField>,
): FindCursor<ILivechatCustomField> {
const query = {
scope,
searchable,
...extraFilter,
};

return this.find(query, options);
}

findMatchingCustomFieldsByIds(
ids: ILivechatCustomField['_id'][],
scope: ILivechatCustomField['scope'],
searchable = true,
options?: FindOptions<ILivechatCustomField>,
): FindCursor<ILivechatCustomField> {
const query = {
_id: { $in: ids },
scope,
searchable,
};

return this.find(query, options);
Expand Down
12 changes: 2 additions & 10 deletions apps/meteor/server/models/raw/LivechatVisitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ export class LivechatVisitorsRaw extends BaseRaw<ILivechatVisitor> implements IL
async findPaginatedVisitorsByEmailOrPhoneOrNameOrUsernameOrCustomField(
emailOrPhone: string,
nameOrUsername: RegExp,
options: FindOptions<ILivechatVisitor>,
allowedCustomFields: string[] = [],
options?: FindOptions<ILivechatVisitor>,
): Promise<FindPaginated<FindCursor<ILivechatVisitor>>> {
const query = {
$or: [
Expand All @@ -180,7 +180,6 @@ export class LivechatVisitorsRaw extends BaseRaw<ILivechatVisitor> implements IL
username: nameOrUsername,
},
// nameorusername is a clean regex, so we should be good

...allowedCustomFields.map((c: string) => ({ [`livechatData.${c}`]: nameOrUsername })),
],
};
Expand All @@ -198,14 +197,7 @@ export class LivechatVisitorsRaw extends BaseRaw<ILivechatVisitor> implements IL
{
...(email && { visitorEmails: { address: email } }),
...(phone && { phone: { phoneNumber: phone } }),
...(customFields &&
Object.keys(customFields).length &&
Object.fromEntries(
Object.keys(customFields).map((fieldName: string) => [
`livechatData.${fieldName}`,
new RegExp(escapeRegExp(customFields[fieldName]), 'i'),
]),
)),
...customFields,
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ export interface ILivechatCustomFieldModel extends IBaseModel<ILivechatCustomFie
findMatchingCustomFields(
scope: ILivechatCustomField['scope'],
searchable: boolean,
options: FindOptions<ILivechatCustomField>,
extraFilter: { [key: string]: string | string[] | { [key: string]: string | string[] } },
options?: FindOptions<ILivechatCustomField>,
): FindCursor<ILivechatCustomField>;
findMatchingCustomFieldsByIds(
ids: ILivechatCustomField['_id'][],
scope: ILivechatCustomField['scope'],
searchable: boolean,
options?: FindOptions<ILivechatCustomField>,
): FindCursor<ILivechatCustomField>;
}

0 comments on commit 04b77de

Please sign in to comment.