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][Omnichannel/API] Endpoint livechat/room.visitor to change Omnichannel room's visitor #18528

Merged
merged 21 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
36deaf3
add new endpoint to change room visitor
murtaza98 Aug 12, 2020
e74d83d
Merge branch 'develop' into omnichannel/replace-guest-endpoint
murtaza98 Aug 12, 2020
81c6244
Apply suggestions from code review
murtaza98 Aug 13, 2020
e524bd8
fix errors in previous commit
murtaza98 Aug 13, 2020
cd3bb02
Merge branch 'develop' into omnichannel/replace-guest-endpoint
renatobecker Aug 13, 2020
15f0f16
modify livechat.config endpoint to support new param - roomId
murtaza98 Aug 14, 2020
0b9b835
Merge branch 'omnichannel/replace-guest-endpoint' of https://github.c…
murtaza98 Aug 14, 2020
23e2f53
Merge branch 'develop' into omnichannel/replace-guest-endpoint
murtaza98 Aug 14, 2020
b98c7bf
Apply suggestions from code review
murtaza98 Aug 17, 2020
9e242e0
Merge branch 'develop' into omnichannel/replace-guest-endpoint
renatobecker Aug 17, 2020
a99f6cb
remove changes to livechat-config endpoint
murtaza98 Aug 18, 2020
098b599
move permission check into Livechat lib
murtaza98 Aug 19, 2020
022390a
refactor code
murtaza98 Aug 19, 2020
ef18815
Merge branch 'develop' into omnichannel/replace-guest-endpoint
murtaza98 Aug 19, 2020
ecb9761
Merge branch 'develop' into omnichannel/replace-guest-endpoint
renatobecker Aug 20, 2020
4947149
Apply suggestions from code review
murtaza98 Aug 20, 2020
416a51a
query optimization and fix return value
murtaza98 Aug 20, 2020
3d238f5
return whole room object
murtaza98 Aug 20, 2020
87307e3
limit room fields from while loading from DB
murtaza98 Aug 20, 2020
3c71b36
Remove unecessary promise statement.
renatobecker Aug 20, 2020
ece6b31
Merge branch 'develop' into omnichannel/replace-guest-endpoint
renatobecker Aug 20, 2020
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
1 change: 1 addition & 0 deletions app/authorization/server/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Meteor.startup(function() {
{ _id: 'assign-roles', roles: ['admin'] },
{ _id: 'ban-user', roles: ['admin', 'owner', 'moderator'] },
{ _id: 'bulk-register-user', roles: ['admin'] },
{ _id: 'change-livechat-room-visitor', roles: ['admin', 'livechat-manager', 'livechat-agent'] },
{ _id: 'create-c', roles: ['admin', 'user', 'bot', 'app'] },
{ _id: 'create-d', roles: ['admin', 'user', 'bot', 'app'] },
{ _id: 'create-p', roles: ['admin', 'user', 'bot', 'app'] },
Expand Down
60 changes: 59 additions & 1 deletion app/livechat/server/api/v1/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { Random } from 'meteor/random';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';

import { settings as rcSettings } from '../../../../settings';
import { Messages, LivechatRooms } from '../../../../models';
import { Messages, LivechatRooms, Users } from '../../../../models';
import { API } from '../../../../api/server';
import { findGuest, findRoom, getRoom, settings, findAgent, onCheckRoomParams } from '../lib/livechat';
import { Livechat } from '../../lib/Livechat';
import { normalizeTransferredByData } from '../../lib/Helper';
import { findVisitorInfo } from '../lib/visitors';
import { hasPermissionAsync } from '../../../../authorization/server/functions/hasPermission';
import { canAccessRoom } from '../../../../authorization/server/functions/canAccessRoom';

API.v1.addRoute('livechat/room', {
get() {
Expand Down Expand Up @@ -179,3 +182,58 @@ API.v1.addRoute('livechat/room.forward', { authRequired: true }, {
API.v1.success(Meteor.runAsUser(this.userId, () => Meteor.call('livechat:transfer', this.bodyParams)));
},
});

API.v1.addRoute('livechat/room.visitor', { authRequired: true }, {
put() {
try {
check(this.bodyParams, {
rid: String,
oldVisitorId: String,
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
newVisitorId: String,
});

murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
const user = Promise.await(Users.findOneById(this.userId));
if (!user) {
throw new Error('error-user-not-found');
}

if (!Promise.await(hasPermissionAsync(this.userId, 'change-livechat-room-visitor'))) {
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('error-not-authorized');
}

const { rid, newVisitorId, oldVisitorId } = this.bodyParams;

const { visitor } = Promise.await(findVisitorInfo({ userId: this.userId, visitorId: newVisitorId }));
if (!visitor) {
throw new Meteor.Error('invalid-new-visitor-id');
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
}
const { _id, username, token } = visitor;


const room = Promise.await(LivechatRooms.findOneById(rid));
if (!room) {
throw new Meteor.Error('invalid-room-id');
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
}

const { v: { _id: roomVisitorId } = {} } = room;
if (roomVisitorId !== oldVisitorId) {
throw new Meteor.Error('invalid-room-visitor-id');
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
}

if (!canAccessRoom(room, user)) {
throw new Error('error-not-allowed');
}

Promise.await(Livechat.changeRoomVisitor(rid, { _id, username, token }));

return API.v1.success({
room: {
rid,
v: { _id, username, token },
},
});
} catch (e) {
return API.v1.failure(e);
}
},
});
12 changes: 12 additions & 0 deletions app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,18 @@ export const Livechat = {

return Promise.await(businessHourManager.allowAgentChangeServiceStatus(agentId));
},

notifyRoomVisitorChange(roomId, visitor) {
Livechat.stream.emit(roomId, {
type: 'visitorData',
visitor,
});
},

changeRoomVisitor(roomId, visitor) {
Promise.await(LivechatRooms.changeVisitorByRoomId(roomId, visitor));
Promise.await(Livechat.notifyRoomVisitorChange(roomId, visitor));
},
};

Livechat.stream = new Meteor.Streamer('livechat-room');
Expand Down
16 changes: 16 additions & 0 deletions app/models/server/models/LivechatRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,22 @@ export class LivechatRooms extends Base {

return this.update(query, update);
}

changeVisitorByRoomId(roomId, { _id, username, token }) {
const query = {
_id: roomId,
t: 'l',
};
const update = {
$set: {
'v._id': _id,
'v.username': username,
'v.token': token,
},
};

return this.update(query, update);
}
}

export default new LivechatRooms(Rooms.model, true);