Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions ee/app/api-enterprise/server/lib/canned-responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ export async function findAllCannedResponses({ userId }) {
}).toArray();
}

export async function findAllCannedResponsesFilter({ userId, shortcut, text, scope, createdBy, tags = [], options = {} }) {
export async function findAllCannedResponsesFilter({ userId, shortcut, text, departmentId, scope, createdBy, tags = [], options = {} }) {
if (!await hasPermissionAsync(userId, 'view-canned-responses')) {
throw new Error('error-not-authorized');
}

let extraFilter = {};
let extraFilter = [];
// if user cannot see all, filter to private + public + departments user is in
if (!await hasPermissionAsync(userId, 'view-all-canned-responses')) {
const departments = await LivechatDepartmentAgents.find({
Expand All @@ -81,9 +81,9 @@ export async function findAllCannedResponsesFilter({ userId, shortcut, text, sco
},
}).toArray();

const departmentIds = departments.map((department) => department.departmentId);
const departmentIds = departmentId ? [departmentId] : departments.map((department) => department.departmentId);

extraFilter = {
extraFilter = [{
$or: [
{
scope: 'user',
Expand All @@ -99,23 +99,37 @@ export async function findAllCannedResponsesFilter({ userId, shortcut, text, sco
scope: 'global',
},
],
};
}];
}

const filter = new RegExp(escapeRegExp(text), 'i');
if (departmentId) {
extraFilter = [{
departmentId,
}];
}

const cursor = CannedResponse.find({
...shortcut && { shortcut },
...text && { $or: [{ shortcut: filter }, { text: filter }] },
...scope && { scope },
...createdBy && { 'createdBy.username': createdBy },
...tags.length && {
tags: {
$in: tags,
},
},
...extraFilter,
}, {
const textFilter = new RegExp(escapeRegExp(text), 'i');

let filter = {
$and: [
...shortcut ? [{ shortcut }] : [],
...text ? [{ $or: [{ shortcut: textFilter }, { text: textFilter }] }] : [],
...scope ? [{ scope }] : [],
...createdBy ? [{ 'createdBy.username': createdBy }] : [],
...tags.length ? [{
tags: {
$in: tags,
},
}] : [],
...extraFilter,
],
};

if (!filter.$and.length) {
filter = {};
}

const cursor = CannedResponse.find(filter, {
sort: options.sort || { shortcut: 1 },
skip: options.offset,
limit: options.count,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const CannedResponsesFilter: FC<CannedResponsesFilterProps> = ({
const t = useTranslation();
const sharingList: SelectOptions = [
['', t('All')],
['private', t('Private')],
['public', t('Public')],
['user', t('Private')],
['global', t('Public')],
['department', t('Department')],
];

Expand Down
1 change: 1 addition & 0 deletions ee/client/omnichannel/hooks/useCannedResponseList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const useCannedResponseList = (
['global', 'user'].find((option) => option === options.type) && { scope: options.type }),
...(options.type &&
!['global', 'user', 'all'].find((option) => option === options.type) && {
scope: 'department',
departmentId: options.type,
}),
offset: start,
Expand Down