diff --git a/ee/app/api-enterprise/server/lib/canned-responses.js b/ee/app/api-enterprise/server/lib/canned-responses.js index 821dc819a2c0e..e5b326a1e42d8 100644 --- a/ee/app/api-enterprise/server/lib/canned-responses.js +++ b/ee/app/api-enterprise/server/lib/canned-responses.js @@ -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({ @@ -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', @@ -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, diff --git a/ee/client/omnichannel/cannedResponses/CannedResponseFilter.tsx b/ee/client/omnichannel/cannedResponses/CannedResponseFilter.tsx index ca84db714ace2..44c21e7d57c33 100644 --- a/ee/client/omnichannel/cannedResponses/CannedResponseFilter.tsx +++ b/ee/client/omnichannel/cannedResponses/CannedResponseFilter.tsx @@ -30,8 +30,8 @@ const CannedResponsesFilter: FC = ({ const t = useTranslation(); const sharingList: SelectOptions = [ ['', t('All')], - ['private', t('Private')], - ['public', t('Public')], + ['user', t('Private')], + ['global', t('Public')], ['department', t('Department')], ]; diff --git a/ee/client/omnichannel/hooks/useCannedResponseList.ts b/ee/client/omnichannel/hooks/useCannedResponseList.ts index 34cbc808e80e1..4c9520281c5de 100644 --- a/ee/client/omnichannel/hooks/useCannedResponseList.ts +++ b/ee/client/omnichannel/hooks/useCannedResponseList.ts @@ -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,