[NEW][EE] Allow to filter departments by Business Units on Livechat (#24162)

Co-authored-by: Renato Becker <renato.augusto.becker@gmail.com>
Co-authored-by: Kevin Aleman <kevin.aleman@rocket.chat>
pull/24231/head
Murtaza Patrawala 4 years ago committed by GitHub
parent 1a5d28896a
commit c09cf332e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/livechat/server/api/lib/livechat.js
  2. 10
      app/livechat/server/api/v1/config.js
  3. 8
      app/models/server/models/LivechatDepartment.js
  4. 15
      ee/app/models/server/models/LivechatDepartment.js

@ -22,8 +22,8 @@ async function findTriggers() {
}));
}
export function findDepartments() {
return LivechatDepartment.findEnabledWithAgents({
export function findDepartments(businessUnit) {
return LivechatDepartment.findEnabledWithAgentsAndBusinessUnit(businessUnit, {
_id: 1,
name: 1,
showOnRegistration: 1,
@ -107,10 +107,10 @@ export function normalizeHttpHeaderData(headers = {}) {
const httpHeaders = Object.assign({}, headers);
return { httpHeaders };
}
export async function settings() {
export async function settings({ businessUnit = '' }) {
const initSettings = Livechat.getInitSettings();
const triggers = await findTriggers();
const departments = findDepartments();
const departments = findDepartments(businessUnit);
const sound = `${Meteor.absoluteUrl()}sounds/chime.mp3`;
const emojis = await EmojiCustom.find().toArray();
return {

@ -5,11 +5,12 @@ import { Livechat } from '../../lib/Livechat';
import { settings, findOpenRoom, getExtraConfigInfo, findAgent } from '../lib/livechat';
API.v1.addRoute('livechat/config', {
get() {
async get() {
try {
check(this.queryParams, {
token: Match.Maybe(String),
department: Match.Maybe(String),
businessUnit: Match.Maybe(String),
});
const enabled = Livechat.enabled();
@ -17,16 +18,17 @@ API.v1.addRoute('livechat/config', {
return API.v1.success({ config: { enabled: false } });
}
const config = Promise.await(settings());
const { token, department, businessUnit } = this.queryParams;
const config = await settings({ businessUnit });
const { token, department } = this.queryParams;
const status = Livechat.online(department);
const guest = token && Livechat.findGuest(token);
const room = guest && findOpenRoom(token);
const agent = guest && room && room.servedBy && findAgent(room.servedBy._id);
const extra = Promise.await(getExtraConfigInfo(room));
const extra = await getExtraConfigInfo(room);
return API.v1.success({
config: { ...config, online: status, guest, room, agent, ...extra },
});

@ -104,6 +104,14 @@ export class LivechatDepartment extends Base {
return this.find(query, fields && { fields });
}
findEnabledWithAgentsAndBusinessUnit(_, fields = undefined) {
const query = {
numAgents: { $gt: 0 },
enabled: true,
};
return this.find(query, fields && { fields });
}
findOneByIdOrName(_idOrName, options) {
const query = {
$or: [

@ -1,4 +1,8 @@
import { Meteor } from 'meteor/meteor';
import { LivechatDepartment } from '../../../../../app/models/server/models/LivechatDepartment';
import { LivechatDepartment as LivechatDepartmentModel } from '../../../../../app/models/server';
import { LivechatUnit } from '../index';
import { overwriteClassOnLicense } from '../../../license/server';
const { find, findOne, update, remove } = LivechatDepartment.prototype;
@ -38,6 +42,17 @@ overwriteClassOnLicense('livechat-enterprise', LivechatDepartment, {
return this.update(query, update, { multi: true });
},
findEnabledWithAgentsAndBusinessUnit(originalFn, businessUnit, fields = undefined) {
if (!businessUnit) {
return LivechatDepartmentModel.findEnabledWithAgents(fields);
}
const unit = LivechatUnit.findOneById(businessUnit, { fields: { _id: 1 } });
if (!unit) {
throw new Meteor.Error('error-unit-not-found', `Error! No Active Business Unit found with id: ${businessUnit}`);
}
return LivechatDepartmentModel.findByUnitIds([businessUnit], { fields });
},
});
LivechatDepartment.prototype.removeDepartmentFromForwardListById = function (_id) {

Loading…
Cancel
Save