From 1005ab64eeebec7e4198e62ec3e0a6c4d34c34f3 Mon Sep 17 00:00:00 2001 From: Murtaza Patrawala <34130764+murtaza98@users.noreply.github.com> Date: Tue, 11 Aug 2020 00:37:41 +0530 Subject: [PATCH] [NEW] Add REST API endpoint to search Livechat visitors (#18514) * create a new livechat-api to search for visitor * updated the api structure * change endpoint path * limit output fields Co-authored-by: Renato Becker --- app/livechat/imports/server/rest/visitors.js | 25 ++++++++++++++-- app/livechat/server/api/lib/visitors.js | 31 ++++++++++++++++++++ app/models/server/raw/LivechatVisitors.js | 20 +++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/app/livechat/imports/server/rest/visitors.js b/app/livechat/imports/server/rest/visitors.js index 423950ff3d02..fe60b53e10c8 100644 --- a/app/livechat/imports/server/rest/visitors.js +++ b/app/livechat/imports/server/rest/visitors.js @@ -1,8 +1,8 @@ -import { check } from 'meteor/check'; +import { Match, check } from 'meteor/check'; import { API } from '../../../../api/server'; -import { findVisitorInfo, findVisitedPages, findChatHistory, searchChats, findVisitorsToAutocomplete } from '../../../server/api/lib/visitors'; +import { findVisitorInfo, findVisitedPages, findChatHistory, searchChats, findVisitorsToAutocomplete, findVisitorsByEmailOrPhoneOrNameOrUsername } from '../../../server/api/lib/visitors'; API.v1.addRoute('livechat/visitors.info', { authRequired: true }, { get() { @@ -102,3 +102,24 @@ API.v1.addRoute('livechat/visitors.autocomplete', { authRequired: true }, { }))); }, }); + +API.v1.addRoute('livechat/visitors.search', { authRequired: true }, { + get() { + const { term } = this.requestParams(); + + check(term, Match.Maybe(String)); + + const { offset, count } = this.getPaginationItems(); + const { sort } = this.parseJsonQuery(); + + return API.v1.success(Promise.await(findVisitorsByEmailOrPhoneOrNameOrUsername({ + userId: this.userId, + term, + pagination: { + offset, + count, + sort, + }, + }))); + }, +}); diff --git a/app/livechat/server/api/lib/visitors.js b/app/livechat/server/api/lib/visitors.js index 39fca9d90c8a..d8cf2763571d 100644 --- a/app/livechat/server/api/lib/visitors.js +++ b/app/livechat/server/api/lib/visitors.js @@ -127,3 +127,34 @@ export async function findVisitorsToAutocomplete({ userId, selector }) { items, }; } + +export async function findVisitorsByEmailOrPhoneOrNameOrUsername({ userId, term, pagination: { offset, count, sort } }) { + if (!await hasPermissionAsync(userId, 'view-l-room')) { + throw new Error('error-not-authorized'); + } + + const cursor = LivechatVisitors.findVisitorsByEmailOrPhoneOrNameOrUsername(term, { + sort: sort || { ts: -1 }, + skip: offset, + limit: count, + fields: { + _id: 1, + username: 1, + name: 1, + phone: 1, + livechatData: 1, + visitorEmails: 1, + }, + }); + + const total = await cursor.count(); + + const visitors = await cursor.toArray(); + + return { + visitors, + count: visitors.length, + offset, + total, + }; +} diff --git a/app/models/server/raw/LivechatVisitors.js b/app/models/server/raw/LivechatVisitors.js index 8be6eccd5d49..36da8a934a82 100644 --- a/app/models/server/raw/LivechatVisitors.js +++ b/app/models/server/raw/LivechatVisitors.js @@ -53,4 +53,24 @@ export class LivechatVisitorsRaw extends BaseRaw { return this.col.aggregate(params); } + + /** + * Find visitors by their email or phone or username or name + * @return [{object}] List of Visitors from db + */ + findVisitorsByEmailOrPhoneOrNameOrUsername(_emailOrPhoneOrNameOrUsername, options) { + const query = { + $or: [{ + 'visitorEmails.address': new RegExp(`^${ s.escapeRegExp(_emailOrPhoneOrNameOrUsername) }$`, 'i'), + }, { + 'phone.phoneNumber': _emailOrPhoneOrNameOrUsername, + }, { + name: new RegExp(`^${ s.escapeRegExp(_emailOrPhoneOrNameOrUsername).trim() }`, 'i'), + }, { + username: _emailOrPhoneOrNameOrUsername, + }], + }; + + return this.find(query, options); + } }