Skip to content

Commit

Permalink
Chore: Omnichannel endpoints e2e tests (#26376)
Browse files Browse the repository at this point in the history
Co-authored-by: Murtaza Patrawala <34130764+murtaza98@users.noreply.github.com>
Co-authored-by: Guilherme Gazzo <5263975+ggazzo@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 1, 2022
1 parent 355819e commit d4deb4e
Show file tree
Hide file tree
Showing 27 changed files with 1,483 additions and 575 deletions.
7 changes: 6 additions & 1 deletion apps/meteor/app/livechat/imports/server/rest/departments.ts
Expand Up @@ -190,6 +190,10 @@ API.v1.addRoute<'livechat/department/:departmentId/agents', { authRequired: true
{ authRequired: true },
{
async get() {
if (!hasAtLeastOnePermission(this.userId, ['view-livechat-departments', 'view-l-room'])) {
return API.v1.unauthorized();
}

check(this.urlParams, {
departmentId: String,
});
Expand All @@ -210,9 +214,10 @@ API.v1.addRoute<'livechat/department/:departmentId/agents', { authRequired: true
return API.v1.success(agents);
},
post() {
if (!hasPermission(this.userId, 'manage-livechat-departments') || !hasPermission(this.userId, 'add-livechat-department-agents')) {
if (!hasAtLeastOnePermission(this.userId, ['manage-livechat-departments', 'add-livechat-department-agents'])) {
return API.v1.unauthorized();
}

check(this.urlParams, {
departmentId: String,
});
Expand Down
45 changes: 19 additions & 26 deletions apps/meteor/app/livechat/imports/server/rest/inquiries.js
Expand Up @@ -53,32 +53,28 @@ API.v1.addRoute(

API.v1.addRoute(
'livechat/inquiries.take',
{ authRequired: true },
{ authRequired: true, permissionsRequired: ['view-l-room'] },
{
post() {
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);
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),
),
});
},
},
);

API.v1.addRoute(
'livechat/inquiries.queued',
{ authRequired: true },
{ authRequired: true, permissionsRequired: ['view-l-room'] },
{
get() {
const { offset, count } = this.getPaginationItems();
Expand All @@ -105,7 +101,7 @@ API.v1.addRoute(

API.v1.addRoute(
'livechat/inquiries.queuedForUser',
{ authRequired: true },
{ authRequired: true, permissionsRequired: ['view-l-room'] },
{
async get() {
const { offset, count } = this.getPaginationItems();
Expand All @@ -130,21 +126,18 @@ API.v1.addRoute(

API.v1.addRoute(
'livechat/inquiries.getOne',
{ authRequired: true },
{ authRequired: true, permissionsRequired: ['view-l-room'] },
{
get() {
async get() {
const { roomId } = this.queryParams;
if (!roomId) {
return API.v1.failure("The 'roomId' param is required");
}

return API.v1.success(
Promise.await(
findOneInquiryByRoomId({
userId: this.userId,
roomId,
}),
),
await findOneInquiryByRoomId({
roomId,
}),
);
},
},
Expand Down
4 changes: 4 additions & 0 deletions apps/meteor/app/livechat/imports/server/rest/sms.js
Expand Up @@ -72,6 +72,10 @@ const normalizeLocationSharing = (payload) => {

API.v1.addRoute('livechat/sms-incoming/:service', {
async post() {
if (!SMS.isConfiguredService(this.urlParams.service)) {
return API.v1.failure('Invalid service');
}

const SMSService = SMS.getService(this.urlParams.service);
const sms = SMSService.parse(this.bodyParams);
const { department } = this.queryParams;
Expand Down
132 changes: 60 additions & 72 deletions apps/meteor/app/livechat/imports/server/rest/users.js
Expand Up @@ -55,33 +55,29 @@ API.v1.addRoute(
if (!hasPermission(this.userId, 'view-livechat-manager')) {
return API.v1.unauthorized();
}
try {
check(this.urlParams, {
type: String,
});
check(this.urlParams, {
type: String,
});

check(this.bodyParams, {
username: String,
});
check(this.bodyParams, {
username: String,
});

if (this.urlParams.type === 'agent') {
const user = Livechat.addAgent(this.bodyParams.username);
if (user) {
return API.v1.success({ user });
}
} else if (this.urlParams.type === 'manager') {
const user = Livechat.addManager(this.bodyParams.username);
if (user) {
return API.v1.success({ user });
}
} else {
throw new Error('Invalid type');
if (this.urlParams.type === 'agent') {
const user = Livechat.addAgent(this.bodyParams.username);
if (user) {
return API.v1.success({ user });
}

return API.v1.failure();
} catch (e) {
return API.v1.failure(e.error);
} else if (this.urlParams.type === 'manager') {
const user = Livechat.addManager(this.bodyParams.username);
if (user) {
return API.v1.success({ user });
}
} else {
throw new Error('Invalid type');
}

return API.v1.failure();
},
},
);
Expand All @@ -95,74 +91,66 @@ API.v1.addRoute(
return API.v1.unauthorized();
}

try {
check(this.urlParams, {
type: String,
_id: String,
});

const user = Users.findOneById(this.urlParams._id);
check(this.urlParams, {
type: String,
_id: String,
});

if (!user) {
return API.v1.failure('User not found');
}
const user = Users.findOneById(this.urlParams._id);

let role;
if (!user) {
return API.v1.failure('User not found');
}

if (this.urlParams.type === 'agent') {
role = 'livechat-agent';
} else if (this.urlParams.type === 'manager') {
role = 'livechat-manager';
} else {
throw new Error('Invalid type');
}
let role;

if (user.roles.indexOf(role) !== -1) {
return API.v1.success({
user: _.pick(user, '_id', 'username', 'name', 'status', 'statusLivechat', 'emails', 'livechat'),
});
}
if (this.urlParams.type === 'agent') {
role = 'livechat-agent';
} else if (this.urlParams.type === 'manager') {
role = 'livechat-manager';
} else {
throw new Error('Invalid type');
}

if (user.roles.indexOf(role) !== -1) {
return API.v1.success({
user: null,
user: _.pick(user, '_id', 'username', 'name', 'status', 'statusLivechat', 'emails', 'livechat'),
});
} catch (e) {
return API.v1.failure(e.error);
}

return API.v1.success({
user: null,
});
},
delete() {
if (!hasPermission(this.userId, 'view-livechat-manager')) {
return API.v1.unauthorized();
}

try {
check(this.urlParams, {
type: String,
_id: String,
});
check(this.urlParams, {
type: String,
_id: String,
});

const user = Users.findOneById(this.urlParams._id);
const user = Users.findOneById(this.urlParams._id);

if (!user) {
return API.v1.failure();
}
if (!user) {
return API.v1.failure();
}

if (this.urlParams.type === 'agent') {
if (Livechat.removeAgent(user.username)) {
return API.v1.success();
}
} else if (this.urlParams.type === 'manager') {
if (Livechat.removeManager(user.username)) {
return API.v1.success();
}
} else {
throw new Error('Invalid type');
if (this.urlParams.type === 'agent') {
if (Livechat.removeAgent(user.username)) {
return API.v1.success();
}

return API.v1.failure();
} catch (e) {
return API.v1.failure(e.error);
} else if (this.urlParams.type === 'manager') {
if (Livechat.removeManager(user.username)) {
return API.v1.success();
}
} else {
throw new Error('Invalid type');
}

return API.v1.failure();
},
},
);
46 changes: 21 additions & 25 deletions apps/meteor/app/livechat/imports/server/rest/visitors.js
Expand Up @@ -86,9 +86,9 @@ API.v1.addRoute(

API.v1.addRoute(
'livechat/visitors.searchChats/room/:roomId/visitor/:visitorId',
{ authRequired: true },
{ authRequired: true, permissionsRequired: ['view-l-room'] },
{
get() {
async get() {
check(this.urlParams, {
visitorId: String,
roomId: String,
Expand All @@ -97,43 +97,39 @@ API.v1.addRoute(
const { searchText, closedChatsOnly, servedChatsOnly } = this.queryParams;
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();
const history = Promise.await(
searchChats({
userId: this.userId,
roomId,
visitorId,
searchText,
closedChatsOnly,
servedChatsOnly,
pagination: {
offset,
count,
sort,
},
}),
);
const history = await searchChats({
userId: this.userId,
roomId,
visitorId,
searchText,
closedChatsOnly,
servedChatsOnly,
pagination: {
offset,
count,
sort,
},
});
return API.v1.success(history);
},
},
);

API.v1.addRoute(
'livechat/visitors.autocomplete',
{ authRequired: true },
{ authRequired: true, permissionsRequired: ['view-l-room'] },
{
get() {
async get() {
const { selector } = this.queryParams;
if (!selector) {
return API.v1.failure("The 'selector' param is required");
}

return API.v1.success(
Promise.await(
findVisitorsToAutocomplete({
userId: this.userId,
selector: JSON.parse(selector),
}),
),
await findVisitorsToAutocomplete({
userId: this.userId,
selector: JSON.parse(selector),
}),
);
},
},
Expand Down
5 changes: 0 additions & 5 deletions apps/meteor/app/livechat/server/api/lib/departments.ts
Expand Up @@ -132,14 +132,9 @@ export async function findDepartmentsToAutocomplete({
}

export async function findDepartmentAgents({
userId,
departmentId,
pagination: { offset, count, sort },
}: FindDepartmentAgentsParams): Promise<PaginatedResult<{ agents: ILivechatDepartmentAgents[] }>> {
if (!(await hasPermissionAsync(userId, 'view-livechat-departments')) && !(await hasPermissionAsync(userId, 'view-l-room'))) {
throw new Error('error-not-authorized');
}

const { cursor, totalCount } = LivechatDepartmentAgents.findAgentsByDepartmentId<ILivechatDepartmentAgents>(departmentId, {
sort: sort || { username: 1 },
skip: offset,
Expand Down

0 comments on commit d4deb4e

Please sign in to comment.