Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
116 lines
3.8 KiB
TypeScript
116 lines
3.8 KiB
TypeScript
import type { IOmnichannelCannedResponse, ILivechatDepartment } from '@rocket.chat/core-typings';
|
|
import { LivechatDepartment, CannedResponse, Users } from '@rocket.chat/models';
|
|
import { Match, check } from 'meteor/check';
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { hasPermissionAsync } from '../../../server/lib/authorization/hasPermission';
|
|
import notifications from '../../../server/lib/notifications/core/lib/Notifications';
|
|
|
|
type ResponseData = {
|
|
shortcut: string;
|
|
text: string;
|
|
scope: string;
|
|
tags?: string[];
|
|
departmentId?: string;
|
|
};
|
|
|
|
export const saveCannedResponse = async (
|
|
userId: string,
|
|
responseData: ResponseData,
|
|
_id?: string,
|
|
): Promise<Omit<IOmnichannelCannedResponse, '_updatedAt' | '_createdAt'> & { _createdAt?: Date }> => {
|
|
if (!(await hasPermissionAsync(userId, 'save-canned-responses'))) {
|
|
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'saveCannedResponse' });
|
|
}
|
|
|
|
check(_id, Match.Maybe(String));
|
|
|
|
check(responseData, {
|
|
shortcut: String,
|
|
text: String,
|
|
scope: String,
|
|
tags: Match.Maybe([String]),
|
|
departmentId: Match.Maybe(String),
|
|
});
|
|
|
|
const canSaveAll = await hasPermissionAsync(userId, 'save-all-canned-responses');
|
|
if (!canSaveAll && ['global'].includes(responseData.scope)) {
|
|
throw new Meteor.Error('error-not-allowed', 'Not allowed to modify canned responses on *global* scope', {
|
|
method: 'saveCannedResponse',
|
|
});
|
|
}
|
|
|
|
const canSaveDepartment = await hasPermissionAsync(userId, 'save-department-canned-responses');
|
|
if (!canSaveAll && !canSaveDepartment && ['department'].includes(responseData.scope)) {
|
|
throw new Meteor.Error('error-not-allowed', 'Not allowed to modify canned responses on *department* scope', {
|
|
method: 'saveCannedResponse',
|
|
});
|
|
}
|
|
|
|
// to avoid inconsistencies
|
|
if (responseData.scope === 'user') {
|
|
delete responseData.departmentId;
|
|
}
|
|
// TODO: check if the department i'm trying to save is a department i can interact with
|
|
|
|
// check if the response already exists and we're not updating one
|
|
const duplicateShortcut = await CannedResponse.findOneByShortcut(responseData.shortcut, {
|
|
projection: { _id: 1 },
|
|
});
|
|
if ((!_id && duplicateShortcut) || (_id && duplicateShortcut && duplicateShortcut._id !== _id)) {
|
|
throw new Meteor.Error('error-invalid-shortcut', 'Shortcut provided already exists', {
|
|
method: 'saveCannedResponse',
|
|
});
|
|
}
|
|
|
|
if (responseData.scope === 'department' && !responseData.departmentId) {
|
|
throw new Meteor.Error('error-invalid-department', 'Invalid department', {
|
|
method: 'saveCannedResponse',
|
|
});
|
|
}
|
|
|
|
if (
|
|
responseData.departmentId &&
|
|
!(await LivechatDepartment.findOneById<Pick<ILivechatDepartment, '_id'>>(responseData.departmentId, { projection: { _id: 1 } }))
|
|
) {
|
|
throw new Meteor.Error('error-invalid-department', 'Invalid department', {
|
|
method: 'saveCannedResponse',
|
|
});
|
|
}
|
|
|
|
let result: Omit<IOmnichannelCannedResponse, '_updatedAt' | '_createdAt'> & { _createdAt?: Date };
|
|
|
|
if (_id) {
|
|
const cannedResponse = await CannedResponse.findOneById(_id);
|
|
if (!cannedResponse) {
|
|
throw new Meteor.Error('error-canned-response-not-found', 'Canned Response not found', {
|
|
method: 'saveCannedResponse',
|
|
});
|
|
}
|
|
|
|
result = await CannedResponse.updateCannedResponse(_id, {
|
|
...responseData,
|
|
...(cannedResponse.scope === 'user' && { userId: cannedResponse.userId }),
|
|
createdBy: cannedResponse.createdBy,
|
|
});
|
|
} else {
|
|
const user = await Users.findOneById(userId);
|
|
|
|
const data = {
|
|
...responseData,
|
|
...(responseData.scope === 'user' && { userId: user?._id }),
|
|
createdBy: { _id: user?._id || '', username: user?.username || '' },
|
|
_createdAt: new Date(),
|
|
};
|
|
|
|
result = await CannedResponse.createCannedResponse(data);
|
|
}
|
|
|
|
notifications.streamCannedResponses.emit('canned-responses', {
|
|
type: 'changed',
|
|
...result,
|
|
});
|
|
|
|
return result;
|
|
};
|