Skip to content

Commit

Permalink
[NEW] Add endpoints to list inquiries and take a inquiry (#14779)
Browse files Browse the repository at this point in the history
* Add endpoints to list inquiries and take a inquiry

* Fix inquiries endpoints

* Add department query parameter in inquiries.list endpoint
  • Loading branch information
MarcosSpessatto authored and renatobecker committed Jun 11, 2019
1 parent 1d217f0 commit ab6a0f3
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
69 changes: 69 additions & 0 deletions app/livechat/imports/server/rest/inquiries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';

import { API } from '../../../../api';
import { hasPermission } from '../../../../authorization';
import { Users, LivechatDepartment } from '../../../../models';
import { LivechatInquiry } from '../../../lib/LivechatInquiry';

API.v1.addRoute('livechat/inquiries.list', { authRequired: true }, {
get() {
if (!hasPermission(this.userId, 'view-livechat-manager')) {
return API.v1.unauthorized();
}
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();
const { department } = this.requestParams();
const ourQuery = Object.assign({}, { status: 'open' });
if (department) {
const departmentFromDB = LivechatDepartment.findOneByIdOrName(department);
if (departmentFromDB) {
ourQuery.department = departmentFromDB._id;
}
}
const cursor = LivechatInquiry.find(ourQuery, {
sort: sort || { ts: -1 },
skip: offset,
limit: count,
fields: {
rid: 1,
name: 1,
ts: 1,
status: 1,
department: 1,
},
});
const totalCount = cursor.count();
const inquiries = cursor.fetch();


return API.v1.success({
inquiries,
offset,
count: inquiries.length,
total: totalCount,
});
},
});

API.v1.addRoute('livechat/inquiries.take', { authRequired: true }, {
post() {
if (!hasPermission(this.userId, 'view-livechat-manager')) {
return API.v1.unauthorized();
}
try {
check(this.bodyParams, {
inquiryId: String,
userId: Match.Maybe(String),
});
if (this.bodyParams.userId && !Users.findOneById(this.bodyParams.userId, { fields: { _id: 1 } })) {
return API.v1.failure('The user is invalid');
}
return API.v1.success({
inquiry: Meteor.runAsUser(this.bodyParams.userId || this.userId, () => Meteor.call('livechat:takeInquiry', this.bodyParams.inquiryId)),
});
} catch (e) {
return API.v1.failure(e);
}
},
});
1 change: 1 addition & 0 deletions app/livechat/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ import '../imports/server/rest/facebook.js';
import '../imports/server/rest/sms.js';
import '../imports/server/rest/users.js';
import '../imports/server/rest/upload.js';
import '../imports/server/rest/inquiries.js';

0 comments on commit ab6a0f3

Please sign in to comment.