Skip to content
26 changes: 12 additions & 14 deletions app/livechat/client/views/app/livechatReadOnly.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
<template name="livechatReadOnly">
{{#if roomOpen}}
{{#if showPreview}}
{{#if isPreparing}}
{{> loading}}
{{#if isPreparing}}
{{> loading}}
{{else}}
{{#if isOnHold}}
<div class="rc-message-box__join">
{{{_ "chat_on_hold_due_to_inactivity"}}}
<button class="rc-button rc-button--primary rc-button--small rc-message-box__resume-it-button js-resume-it">{{_ "Resume"}}</button>
</div>
{{else}}
{{#if isOnHold}}
{{#if inquiryOpen}}
<div class="rc-message-box__join">
{{{_ "chat_on_hold_due_to_inactivity"}}}
<button class="rc-button rc-button--primary rc-button--small rc-message-box__resume-it-button js-resume-it">{{_ "Resume"}}</button>
{{{_ "you_are_in_preview_mode_of_incoming_livechat"}}}
<button class="rc-button rc-button--primary rc-button--small rc-message-box__take-it-button js-take-it">{{_ "Take_it"}}</button>
</div>
{{else}}
{{#if inquiryOpen}}
<div class="rc-message-box__join">
{{{_ "you_are_in_preview_mode_of_incoming_livechat"}}}
<button class="rc-button rc-button--primary rc-button--small rc-message-box__take-it-button js-take-it">{{_ "Take_it"}}</button>
</div>
{{else}}
{{_ "room_is_read_only"}}
{{/if}}
{{_ "room_is_read_only"}}
{{/if}}
{{/if}}
{{/if}}
Expand Down
5 changes: 3 additions & 2 deletions app/livechat/client/views/app/livechatReadOnly.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { inquiryDataStream } from '../../lib/stream/inquiry';
Template.livechatReadOnly.helpers({
inquiryOpen() {
const inquiry = Template.instance().inquiry.get();
return inquiry && inquiry.status === 'queued';
const room = Template.instance().room.get();
return (inquiry && inquiry.status === 'queued') || !room.servedBy;
Copy link
Member

@KevLehman KevLehman Dec 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Murtaza, I know this is an old PR, but, would you mind explaining me this change? 👀

I just want to know what's fixing, since I'm looking at a related issue and wanted to understand why this was needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, @KevLehman This change was needed to ensure that we show the Take It button on the chat if there are no agents assigned to a chat yet.
Before this change, we were only showing that button if the "Manual Selection" algorithm was on. However, we ran into a situation (on servers running other algorithms than manual) where LiveChat managers were using the "Current Chats" panel to send messages to unassigned chats - this resulted in chats that were actually answered but not assigned to any agents - since the manually answered those chats and didn't get the routing algorithm involved.

Hence we added a new rule here where we decided that if any manager wants to answer an unassigned chat, then they'd first have to "Take it" ..... Hence I added this new condition here - !room.servedBy.

Let me know if you need any further help with this 😄

},

roomOpen() {
Expand Down Expand Up @@ -88,7 +89,7 @@ Template.livechatReadOnly.onCreated(function() {

this.autorun(() => this.loadInquiry(this.rid));
this.autorun(() => {
this.room.set(ChatRoom.findOne({ _id: Template.currentData().rid }, { fields: { open: 1 } }));
this.room.set(ChatRoom.findOne({ _id: Template.currentData().rid }, { fields: { open: 1, servedBy: 1 } }));
});
});

Expand Down
4 changes: 2 additions & 2 deletions app/livechat/lib/LivechatRoomType.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default class LivechatRoomType extends RoomTypeConfig {
}
}

readOnly(rid, user) {
readOnly(rid) {
const room = ChatRoom.findOne({ _id: rid }, { fields: { open: 1, servedBy: 1 } });
if (!room || !room.open) {
return true;
Expand All @@ -111,7 +111,7 @@ export default class LivechatRoomType extends RoomTypeConfig {
return true;
}

return (!room.servedBy || room.servedBy._id !== user._id) && !hasPermission('view-livechat-rooms');
return !room.servedBy;
}

getAvatarPath(roomData) {
Expand Down
9 changes: 7 additions & 2 deletions app/livechat/server/lib/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ export const createLivechatSubscription = (rid, name, guest, agent, department)
username: String,
}));

const existingSubscription = Subscriptions.findOneByRoomIdAndUserId(rid, agent.agentId);
if (existingSubscription?._id) {
return existingSubscription;
}

const { _id, username, token, status = 'online' } = guest;

const subscriptionData = {
Expand Down Expand Up @@ -306,7 +311,7 @@ export const forwardRoomToAgent = async (room, transferData) => {
const { servedBy } = roomTaken;
if (servedBy) {
if (oldServedBy && servedBy._id !== oldServedBy._id) {
removeAgentFromSubscription(rid, oldServedBy);
RoutingManager.removeAllRoomSubscriptions(room, servedBy);
}
Messages.createUserJoinWithRoomIdAndUser(rid, { _id: servedBy._id, username: servedBy.username });

Expand Down Expand Up @@ -393,7 +398,7 @@ export const forwardRoomToDepartment = async (room, guest, transferData) => {

Livechat.saveTransferHistory(room, transferData);
if (oldServedBy) {
removeAgentFromSubscription(rid, oldServedBy);
RoutingManager.removeAllRoomSubscriptions(room, servedBy);
}
if (!chatQueued && servedBy) {
Messages.createUserJoinWithRoomIdAndUser(rid, servedBy);
Expand Down
14 changes: 13 additions & 1 deletion app/livechat/server/lib/RoutingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ export const RoutingManager = {
const { servedBy } = room;

if (servedBy) {
removeAgentFromSubscription(rid, servedBy);
LivechatRooms.removeAgentByRoomId(rid);
this.removeAllRoomSubscriptions(room);
dispatchAgentDelegated(rid, null);
}

Expand Down Expand Up @@ -170,6 +170,18 @@ export const RoutingManager = {
dispatchInquiryQueued(inquiry, defaultAgent);
return defaultAgent;
},

removeAllRoomSubscriptions(room, ignoreUser) {
const { _id: roomId } = room;

const subscriptions = Subscriptions.findByRoomId(roomId).fetch();
subscriptions?.forEach(({ u }) => {
if (ignoreUser && ignoreUser._id === u._id) {
return;
}
removeAgentFromSubscription(roomId, u);
});
},
};

settings.get('Livechat_Routing_Method', function(key, value) {
Expand Down