Skip to content

Commit

Permalink
[NEW][APPS-ENGINE] Implement new IPostLivechatRoomTransferred event (#…
Browse files Browse the repository at this point in the history
…18625)

* Implement new IPostLivechatRoomTransferred event

* Move event trigger to correct place

* Update Apps-Engine version
  • Loading branch information
d-gubert authored Aug 21, 2020
1 parent 06467a6 commit 40520f4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
12 changes: 12 additions & 0 deletions app/apps/server/bridges/listeners.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AppInterface } from '@rocket.chat/apps-engine/definition/metadata';
import { LivechatTransferEventType } from '@rocket.chat/apps-engine/definition/livechat';

export class AppListenerBridge {
constructor(orch) {
Expand Down Expand Up @@ -39,6 +40,7 @@ export class AppListenerBridge {
case AppInterface.IPostLivechatRoomClosed:
case AppInterface.IPostLivechatAgentAssigned:
case AppInterface.IPostLivechatAgentUnassigned:
case AppInterface.IPostLivechatRoomTransferred:
return 'livechatEvent';
case AppInterface.IUIKitInteractionHandler:
return 'uiKitInteractionEvent';
Expand Down Expand Up @@ -100,6 +102,16 @@ export class AppListenerBridge {
room: this.orch.getConverters().get('rooms').convertRoom(data.room),
agent: this.orch.getConverters().get('users').convertToApp(data.user),
});
case AppInterface.IPostLivechatRoomTransferred:
const converter = data.type === LivechatTransferEventType.AGENT ? 'users' : 'departments';

return this.orch.getManager().getListenerManager().executeListener(inte, {
type: data.type,
room: this.orch.getConverters().get('rooms').convertById(data.room),
from: this.orch.getConverters().get(converter).convertById(data.from),
to: this.orch.getConverters().get(converter).convertById(data.to),
});

default:
const room = this.orch.getConverters().get('rooms').convertRoom(data);

Expand Down
30 changes: 27 additions & 3 deletions app/livechat/server/lib/Helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { MongoInternals } from 'meteor/mongo';
import { LivechatTransferEventType } from '@rocket.chat/apps-engine/definition/livechat';

import { Messages, LivechatRooms, Rooms, Subscriptions, Users, LivechatInquiry, LivechatDepartment, LivechatDepartmentAgents } from '../../../models/server';
import { Livechat } from './Livechat';
Expand Down Expand Up @@ -44,8 +45,11 @@ export const createLivechatRoom = (rid, name, guest, roomInfo = {}, extraData =

const roomId = Rooms.insert(room);

Apps.getBridges().getListenerBridge().livechatEvent(AppEvents.IPostLivechatRoomStarted, room);
callbacks.run('livechat.newRoom', room);
Meteor.defer(() => {
Apps.triggerEvent(AppEvents.IPostLivechatRoomStarted, room);
callbacks.run('livechat.newRoom', room);
});

return roomId;
};

Expand Down Expand Up @@ -166,7 +170,9 @@ export const removeAgentFromSubscription = (rid, { _id, username }) => {
Subscriptions.removeByRoomIdAndUserId(rid, _id);
Messages.createUserLeaveWithRoomIdAndUser(rid, { _id, username });

Apps.getBridges().getListenerBridge().livechatEvent(AppEvents.IPostLivechatAgentUnassigned, { room, user });
Meteor.defer(() => {
Apps.triggerEvent(AppEvents.IPostLivechatAgentUnassigned, { room, user });
});
};

export const parseAgentCustomFields = (customFields) => {
Expand Down Expand Up @@ -257,6 +263,15 @@ export const forwardRoomToAgent = async (room, transferData) => {
removeAgentFromSubscription(rid, oldServedBy);
}
Messages.createUserJoinWithRoomIdAndUser(rid, { _id: servedBy._id, username: servedBy.username });

Meteor.defer(() => {
Apps.triggerEvent(AppEvents.IPostLivechatRoomTransferred, {
type: LivechatTransferEventType.AGENT,
room: rid,
from: oldServedBy?._id,
to: servedBy._id,
});
});
}

callbacks.run('livechat.afterForwardChatToAgent', { rid, servedBy, oldServedBy });
Expand All @@ -267,6 +282,15 @@ export const updateChatDepartment = ({ rid, newDepartmentId, oldDepartmentId })
LivechatRooms.changeDepartmentIdByRoomId(rid, newDepartmentId);
LivechatInquiry.changeDepartmentIdByRoomId(rid, newDepartmentId);

Meteor.defer(() => {
Apps.triggerEvent(AppEvents.IPostLivechatRoomTransferred, {
type: LivechatTransferEventType.DEPARTMENT,
room: rid,
from: oldDepartmentId,
to: newDepartmentId,
});
});

return callbacks.run('livechat.afterForwardChatToDepartment', { rid, newDepartmentId, oldDepartmentId });
};

Expand Down
11 changes: 3 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"@nivo/heatmap": "^0.61.0",
"@nivo/line": "^0.61.1",
"@nivo/pie": "^0.61.1",
"@rocket.chat/apps-engine": "1.16.0",
"@rocket.chat/apps-engine": "1.17.0-beta.3607",
"@rocket.chat/css-in-js": "^0.14.0",
"@rocket.chat/fuselage": "^0.14.0",
"@rocket.chat/fuselage-hooks": "^0.14.0",
Expand Down

0 comments on commit 40520f4

Please sign in to comment.