Skip to content

Commit

Permalink
[NEW]Add new endpoint to change Omnichannel room's visitor (#18528)
Browse files Browse the repository at this point in the history
* add new endpoint to change room visitor

* Apply suggestions from code review

Co-authored-by: Renato Becker <[email protected]>

* fix errors in previous commit

* modify livechat.config endpoint to support new param - roomId

* Apply suggestions from code review

Co-authored-by: Renato Becker <[email protected]>

* remove changes to livechat-config endpoint

* move permission check into Livechat lib

* refactor code

* Apply suggestions from code review

Co-authored-by: Renato Becker <[email protected]>

* query optimization and fix return value

* return whole room object

* limit room fields from while loading from DB

* Remove unecessary promise statement.

Co-authored-by: Renato Becker <[email protected]>
  • Loading branch information
murtaza98 and renatobecker authored Aug 20, 2020
1 parent a0a4948 commit bce223a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
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
36 changes: 36 additions & 0 deletions app/livechat/server/api/v1/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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';

API.v1.addRoute('livechat/room', {
get() {
Expand Down Expand Up @@ -179,3 +180,38 @@ 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,
newVisitorId: String,
});

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

const { visitor } = Promise.await(findVisitorInfo({ userId: this.userId, visitorId: newVisitorId }));
if (!visitor) {
throw new Meteor.Error('invalid-visitor');
}

let room = LivechatRooms.findOneById(rid, { _id: 1 });
if (!room) {
throw new Meteor.Error('invalid-room');
}

const { v: { _id: roomVisitorId } = {} } = room;
if (roomVisitorId !== oldVisitorId) {
throw new Meteor.Error('invalid-room-visitor');
}

room = Livechat.changeRoomVisitor(this.userId, rid, visitor);

return API.v1.success({ room });
} catch (e) {
return API.v1.failure(e);
}
},
});
35 changes: 34 additions & 1 deletion app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
LivechatInquiry,
} from '../../../models';
import { Logger } from '../../../logger';
import { addUserRoles, hasPermission, hasRole, removeUserFromRoles } from '../../../authorization';
import { addUserRoles, hasPermission, hasRole, removeUserFromRoles, canAccessRoom } from '../../../authorization';
import * as Mailer from '../../../mailer';
import { sendMessage } from '../../../lib/server/functions/sendMessage';
import { updateMessage } from '../../../lib/server/functions/updateMessage';
Expand Down Expand Up @@ -1119,6 +1119,39 @@ export const Livechat = {

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

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

changeRoomVisitor(userId, roomId, visitor) {
const user = Promise.await(Users.findOneById(userId));
if (!user) {
throw new Error('error-user-not-found');
}

if (!hasPermission(userId, 'change-livechat-room-visitor')) {
throw new Error('error-not-authorized');
}

const room = Promise.await(LivechatRooms.findOneById(roomId, { _id: 1, t: 1 }));
if (!room) {
throw new Meteor.Error('invalid-room');
}

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

LivechatRooms.changeVisitorByRoomId(room._id, visitor);

Livechat.notifyRoomVisitorChange(room._id, visitor);

return LivechatRooms.findOneById(roomId);
},
};

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);

0 comments on commit bce223a

Please sign in to comment.