Regression: Remove model observe that was used to control the status of the Omnichannel agents (#17078)
parent
a84edc77b0
commit
0758bb7ba3
@ -0,0 +1,105 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
|
||||
import { Livechat } from '../Livechat'; |
||||
import { settings } from '../../../../settings/server'; |
||||
import { Users } from '../../../../models/server'; |
||||
|
||||
let monitorAgents = false; |
||||
let actionTimeout = 60000; |
||||
let action = 'none'; |
||||
let comment = ''; |
||||
|
||||
settings.get('Livechat_agent_leave_action_timeout', function(_key: string, value: number) { |
||||
actionTimeout = value * 1000; |
||||
}); |
||||
|
||||
settings.get('Livechat_agent_leave_action', function(_key: string, value: boolean) { |
||||
monitorAgents = value; |
||||
}); |
||||
|
||||
settings.get('Livechat_agent_leave_action', function(_key: string, value: string) { |
||||
action = value; |
||||
}); |
||||
|
||||
settings.get('Livechat_agent_leave_comment', function(_key: string, value: string) { |
||||
comment = value; |
||||
}); |
||||
|
||||
const onlineAgents = { |
||||
users: new Set(), |
||||
queue: new Map(), |
||||
|
||||
add(userId: string): void { |
||||
if (this.exists(userId)) { |
||||
return; |
||||
} |
||||
|
||||
if (this.queue.has(userId)) { |
||||
clearTimeout(this.queue.get(userId)); |
||||
this.queue.delete(userId); |
||||
} |
||||
this.users.add(userId); |
||||
}, |
||||
|
||||
remove(userId: string): void { |
||||
if (!this.exists(userId)) { |
||||
return; |
||||
} |
||||
|
||||
if (this.queue.has(userId)) { |
||||
clearTimeout(this.queue.get(userId)); |
||||
} |
||||
|
||||
this.queue.set(userId, setTimeout(this.runAgentLeaveAction, actionTimeout, userId)); |
||||
}, |
||||
|
||||
exists(userId: string): boolean { |
||||
return this.users.has(userId); |
||||
}, |
||||
|
||||
runAgentLeaveAction: Meteor.bindEnvironment((userId: string) => { |
||||
onlineAgents.users.delete(userId); |
||||
onlineAgents.queue.delete(userId); |
||||
|
||||
if (action === 'close') { |
||||
return Livechat.closeOpenChats(userId, comment); |
||||
} |
||||
|
||||
if (action === 'forward') { |
||||
return Livechat.forwardOpenChats(userId); |
||||
} |
||||
}), |
||||
}; |
||||
|
||||
Users.on('change', ({ clientAction, id, diff }) => { |
||||
if (!monitorAgents) { |
||||
return; |
||||
} |
||||
|
||||
if (clientAction !== 'removed' && diff && !diff.status && !diff.statusLivechat) { |
||||
return; |
||||
} |
||||
|
||||
switch (clientAction) { |
||||
case 'updated': |
||||
case 'inserted': |
||||
const agent = Users.findOneAgentById(id, { |
||||
fields: { |
||||
status: 1, |
||||
statusLivechat: 1, |
||||
}, |
||||
}); |
||||
const serviceOnline = agent && agent.status !== 'offline' && agent.statusLivechat === 'available'; |
||||
|
||||
if (serviceOnline) { |
||||
return onlineAgents.add(id); |
||||
} |
||||
|
||||
onlineAgents.remove(id); |
||||
|
||||
break; |
||||
case 'removed': |
||||
onlineAgents.remove(id); |
||||
break; |
||||
} |
||||
}); |
@ -1,95 +0,0 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
import { UserPresenceMonitor } from 'meteor/konecty:user-presence'; |
||||
|
||||
import { Livechat } from './lib/Livechat'; |
||||
import { settings } from '../../settings'; |
||||
import { Users } from '../../models'; |
||||
|
||||
let agentsHandler; |
||||
let monitorAgents = false; |
||||
let actionTimeout = 60000; |
||||
|
||||
const onlineAgents = { |
||||
users: {}, |
||||
queue: {}, |
||||
|
||||
add(userId) { |
||||
if (this.queue[userId]) { |
||||
clearTimeout(this.queue[userId]); |
||||
delete this.queue[userId]; |
||||
} |
||||
this.users[userId] = 1; |
||||
}, |
||||
|
||||
remove(userId, callback) { |
||||
if (this.queue[userId]) { |
||||
clearTimeout(this.queue[userId]); |
||||
} |
||||
this.queue[userId] = setTimeout(Meteor.bindEnvironment(() => { |
||||
callback(); |
||||
|
||||
delete this.users[userId]; |
||||
delete this.queue[userId]; |
||||
}), actionTimeout); |
||||
}, |
||||
|
||||
exists(userId) { |
||||
return !!this.users[userId]; |
||||
}, |
||||
}; |
||||
|
||||
function runAgentLeaveAction(userId) { |
||||
const action = settings.get('Livechat_agent_leave_action'); |
||||
if (action === 'close') { |
||||
return Livechat.closeOpenChats(userId, settings.get('Livechat_agent_leave_comment')); |
||||
} if (action === 'forward') { |
||||
return Livechat.forwardOpenChats(userId); |
||||
} |
||||
} |
||||
|
||||
settings.get('Livechat_agent_leave_action_timeout', function(key, value) { |
||||
actionTimeout = value * 1000; |
||||
}); |
||||
|
||||
settings.get('Livechat_agent_leave_action', function(key, value) { |
||||
monitorAgents = value; |
||||
if (value !== 'none') { |
||||
if (!agentsHandler) { |
||||
agentsHandler = Users.findOnlineAgents().observeChanges({ |
||||
added(id) { |
||||
onlineAgents.add(id); |
||||
}, |
||||
changed(id, fields) { |
||||
if (fields.statusLivechat && fields.statusLivechat === 'not-available') { |
||||
onlineAgents.remove(id, () => { |
||||
runAgentLeaveAction(id); |
||||
}); |
||||
} else { |
||||
onlineAgents.add(id); |
||||
} |
||||
}, |
||||
removed(id) { |
||||
onlineAgents.remove(id, () => { |
||||
runAgentLeaveAction(id); |
||||
}); |
||||
}, |
||||
}); |
||||
} |
||||
} else if (agentsHandler) { |
||||
agentsHandler.stop(); |
||||
agentsHandler = null; |
||||
} |
||||
}); |
||||
|
||||
UserPresenceMonitor.onSetUserStatus((user, status/* , statusConnection*/) => { |
||||
if (!monitorAgents) { |
||||
return; |
||||
} |
||||
if (onlineAgents.exists(user._id)) { |
||||
if (status === 'offline' || user.statusLivechat === 'not-available') { |
||||
onlineAgents.remove(user._id, () => { |
||||
runAgentLeaveAction(user._id); |
||||
}); |
||||
} |
||||
} |
||||
}); |
@ -0,0 +1,3 @@ |
||||
export namespace settings { |
||||
export function get(name: string, callback: (key: string, value: any) => void): string; |
||||
} |
Loading…
Reference in new issue