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] Add Livechat inquiries endpoints #14779

Merged
merged 3 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
49 changes: 49 additions & 0 deletions app/livechat/imports/server/rest/inquiries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Meteor } from 'meteor/meteor';

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

API.v1.addRoute('livechat/inquiries', { authRequired: true }, {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better renaming this route to livechat/inquiries.list, ok?

get() {
if (!hasPermission(this.userId, 'view-livechat-manager')) {
return API.v1.unauthorized();
}
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();
const cursor = LivechatInquiry.find({ status: 'open' }, {
Copy link

@renatobecker-zz renatobecker-zz Jun 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we decided to return the entire inquiry object, but due to the large number of agents available, it would be better to return only a few fields, such as:

   {
            "_id": "jz9WE69S9zmvpW2Fd",
            "rid": "Q3ZLHdXYmPk8PxfPm",
            "name": "Renato Becker",
            "ts": "2019-06-11T02:15:54.761Z",
            "status": "open"
    }

sort: sort || { ts: -1 },
skip: offset,
limit: count,
});
const totalCount = cursor.count();
const inquiries = cursor.fetch();


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

API.v1.addRoute('livechat/inquiry.take', { authRequired: true }, {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better renaming this route to livechat/inquiries.take, ok?

post() {
if (!hasPermission(this.userId, 'view-livechat-manager')) {
return API.v1.unauthorized();
}
const { inquiryId, userId } = this.bodyParams;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about using the check approach? Like this:

{
    try {
	check(this.bodyParams, {
		inquiriId: string,
		userId: Match.Maybe(String),
	});
     ...
     } catch (e) {
	  return API.v1.failure(e);
     }
}

if (!inquiryId) {
return API.v1.failure('The bodyParam "inquiryId" is required');
}
if (userId && !Users.findOneById(userId, { fields: { _id: 1 } })) {
return API.v1.failure('The user is invalid');
}
return API.v1.success({
inquiry: Meteor.runAsUser(userId || this.userId, () => Meteor.call('livechat:takeInquiry', inquiryId)),
});
},
});
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';