Skip to content

Commit

Permalink
[NEW] Add REST API endpoint to search Livechat visitors (#18514)
Browse files Browse the repository at this point in the history
* create a new livechat-api to search for visitor

* updated the api structure

* change endpoint path

* limit output  fields

Co-authored-by: Renato Becker <renato.augusto.becker@gmail.com>
  • Loading branch information
murtaza98 and renatobecker authored Aug 10, 2020
1 parent 27a2e09 commit 1005ab6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
25 changes: 23 additions & 2 deletions app/livechat/imports/server/rest/visitors.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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,
},
})));
},
});
31 changes: 31 additions & 0 deletions app/livechat/server/api/lib/visitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
20 changes: 20 additions & 0 deletions app/models/server/raw/LivechatVisitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 1005ab6

Please sign in to comment.