Skip to content

Commit

Permalink
[FIX] Close omnichannel conversations when agent is deactivated (#22917)
Browse files Browse the repository at this point in the history
* close omnichannel conversations when agent is deactivated

* Remove console.log

* remove unnecesary props from type

* Update app/lib/server/functions/setUserActiveStatus.js

Co-authored-by: Murtaza Patrawala <34130764+murtaza98@users.noreply.github.com>

Co-authored-by: Renato Becker <renato.augusto.becker@gmail.com>
Co-authored-by: Murtaza Patrawala <34130764+murtaza98@users.noreply.github.com>
  • Loading branch information
3 people authored and sampaiodiego committed Aug 16, 2021
1 parent d4ce52e commit 00f82fe
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
19 changes: 19 additions & 0 deletions app/lib/server/functions/closeOmnichannelConversations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';

import { LivechatRooms } from '../../../models/server';
import { IUser } from '../../../../definition/IUser';
import { settings } from '../../../settings/server';
import { Livechat } from '../../../livechat/server/lib/Livechat';

type SubscribedRooms = {
rid: string;
t: string;
}

export const closeOmnichannelConversations = (user: IUser, subscribedRooms: SubscribedRooms[]): void => {
const roomsInfo = LivechatRooms.findByIds(subscribedRooms.map(({ rid }) => rid));
const language = settings.get('Language') || 'en';
roomsInfo.map((room: any) =>
Livechat.closeRoom({ user, visitor: {}, room, comment: TAPi18n.__('Agent_deactivated', { lng: language }) }),
);
};
11 changes: 8 additions & 3 deletions app/lib/server/functions/setUserActiveStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as Mailer from '../../../mailer';
import { Users, Subscriptions, Rooms } from '../../../models';
import { settings } from '../../../settings';
import { relinquishRoomOwnerships } from './relinquishRoomOwnerships';
import { closeOmnichannelConversations } from './closeOmnichannelConversations';
import { shouldRemoveOrChangeOwner, getSubscribedRoomsForUserWithDetails } from './getRoomsWithSingleOwner';
import { getUserSingleOwnedRooms } from './getUserSingleOwnedRooms';

Expand Down Expand Up @@ -41,13 +42,17 @@ export function setUserActiveStatus(userId, active, confirmRelinquish = false) {
// Users without username can't do anything, so there is no need to check for owned rooms
if (user.username != null && !active) {
const subscribedRooms = getSubscribedRoomsForUserWithDetails(userId);
// give omnichannel rooms a special treatment :)
const chatSubscribedRooms = subscribedRooms.filter(({ t }) => t !== 'l');
const livechatSubscribedRooms = subscribedRooms.filter(({ t }) => t === 'l');

if (shouldRemoveOrChangeOwner(subscribedRooms) && !confirmRelinquish) {
const rooms = getUserSingleOwnedRooms(subscribedRooms);
if (shouldRemoveOrChangeOwner(chatSubscribedRooms) && !confirmRelinquish) {
const rooms = getUserSingleOwnedRooms(chatSubscribedRooms);
throw new Meteor.Error('user-last-owner', '', rooms);
}

relinquishRoomOwnerships(user._id, subscribedRooms, false);
closeOmnichannelConversations(user, livechatSubscribedRooms);
relinquishRoomOwnerships(user, chatSubscribedRooms, false);
}

Users.setUserActive(userId, active);
Expand Down
15 changes: 15 additions & 0 deletions app/models/server/models/LivechatRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ export class LivechatRooms extends Base {
return this.find(query, options);
}

findByIds(ids, fields) {
const options = {};

if (fields) {
options.fields = fields;
}

const query = {
t: 'l',
_id: { $in: ids },
};

return this.find(query, options);
}

findOneById(_id, fields = {}) {
const options = {};

Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@
"Agent_messages": "Agent Messages",
"Agent_Name_Placeholder": "Please enter an agent name...",
"Agent_removed": "Agent removed",
"Agent_deactivated": "Agent was deactivated",
"Agents": "Agents",
"Alerts": "Alerts",
"Alias": "Alias",
Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-i18n/i18n/es.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@
"Agent_messages": "Mensajes del agente",
"Agent_Name_Placeholder": "Por favor, introduzca el nombre de un agente...",
"Agent_removed": "Agente eliminado",
"Agent_deactivated": "Agente desactivado",
"Agents": "Agentes",
"Alerts": "Alertas",
"Alias": "Alias",
Expand Down

0 comments on commit 00f82fe

Please sign in to comment.