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
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
167 lines
4.7 KiB
TypeScript
167 lines
4.7 KiB
TypeScript
import { LivechatCustomField } from '@rocket.chat/models';
|
|
import {
|
|
isLivechatCustomFieldsProps,
|
|
isPOSTLivechatCustomFieldParams,
|
|
isPOSTLivechatCustomFieldsParams,
|
|
isPOSTLivechatRemoveCustomFields,
|
|
isPOSTLivechatSaveCustomFieldsParams,
|
|
POSTLivechatRemoveCustomFieldSuccess,
|
|
POSTLivechatSaveCustomFieldSuccess,
|
|
validateBadRequestErrorResponse,
|
|
validateForbiddenErrorResponse,
|
|
validateUnauthorizedErrorResponse,
|
|
} from '@rocket.chat/rest-typings';
|
|
|
|
import { API } from '../..';
|
|
import { setCustomFields, setMultipleCustomFields } from '../../../lib/omnichannel/custom-fields';
|
|
import type { ExtractRoutesFromAPI } from '../../ApiClass';
|
|
import { findLivechatCustomFields, findCustomFieldById } from './lib/customFields';
|
|
import { findGuest } from './lib/livechat';
|
|
import { getPaginationItems } from '../../lib/getPaginationItems';
|
|
|
|
API.v1.addRoute(
|
|
'livechat/custom.field',
|
|
{ validateParams: isPOSTLivechatCustomFieldParams },
|
|
{
|
|
async post() {
|
|
const { token, key, value, overwrite } = this.bodyParams;
|
|
const guest = await findGuest(token);
|
|
if (!guest) {
|
|
throw new Error('invalid-token');
|
|
}
|
|
|
|
await setCustomFields({ token, key, value, overwrite });
|
|
|
|
return API.v1.success({ field: { key, value, overwrite } });
|
|
},
|
|
},
|
|
);
|
|
|
|
API.v1.addRoute(
|
|
'livechat/custom.fields',
|
|
{ validateParams: isPOSTLivechatCustomFieldsParams },
|
|
{
|
|
async post() {
|
|
const { token } = this.bodyParams;
|
|
const visitor = await findGuest(token);
|
|
if (!visitor) {
|
|
throw new Error('invalid-token');
|
|
}
|
|
|
|
const result = await setMultipleCustomFields({ visitor, customFields: this.bodyParams.customFields });
|
|
|
|
return API.v1.success({
|
|
fields: result.map(({ key, value, overwrite }) => ({ Key: key, value, overwrite })),
|
|
});
|
|
},
|
|
},
|
|
);
|
|
|
|
API.v1.addRoute(
|
|
'livechat/custom-fields',
|
|
{ authRequired: true, permissionsRequired: ['view-l-room'], validateParams: isLivechatCustomFieldsProps },
|
|
{
|
|
async get() {
|
|
const { offset, count } = await getPaginationItems(this.queryParams);
|
|
const { sort } = await this.parseJsonQuery();
|
|
const { text } = this.queryParams;
|
|
|
|
const customFields = await findLivechatCustomFields({
|
|
text,
|
|
pagination: {
|
|
offset,
|
|
count,
|
|
sort,
|
|
},
|
|
});
|
|
|
|
return API.v1.success(customFields);
|
|
},
|
|
},
|
|
);
|
|
|
|
API.v1.addRoute(
|
|
'livechat/custom-fields/:_id',
|
|
{ authRequired: true, permissionsRequired: ['view-l-room'] },
|
|
{
|
|
async get() {
|
|
const { customField } = await findCustomFieldById({ customFieldId: this.urlParams._id });
|
|
|
|
return API.v1.success({
|
|
customField,
|
|
});
|
|
},
|
|
},
|
|
);
|
|
|
|
const livechatCustomFieldsEndpoints = API.v1
|
|
.post(
|
|
'livechat/custom-fields.save',
|
|
{
|
|
response: {
|
|
200: POSTLivechatSaveCustomFieldSuccess,
|
|
400: validateBadRequestErrorResponse,
|
|
401: validateUnauthorizedErrorResponse,
|
|
403: validateForbiddenErrorResponse,
|
|
},
|
|
authRequired: true,
|
|
permissionsRequired: ['view-livechat-manager'], // is this permission appropriate for the targeted action?
|
|
body: isPOSTLivechatSaveCustomFieldsParams,
|
|
},
|
|
async function action() {
|
|
const { customFieldId, customFieldData } = this.bodyParams;
|
|
|
|
if (customFieldId) {
|
|
const customField = await LivechatCustomField.findOneById(customFieldId);
|
|
if (!customField) {
|
|
return API.v1.failure('Custom Field Not found');
|
|
}
|
|
}
|
|
|
|
if (!customFieldId) {
|
|
const customField = await LivechatCustomField.findOneById(customFieldData.field);
|
|
if (customField) {
|
|
return API.v1.failure('Custom Field already exists');
|
|
}
|
|
}
|
|
|
|
const { field, label, scope, visibility, ...extraData } = customFieldData;
|
|
const result = await LivechatCustomField.createOrUpdateCustomField(customFieldId, field, label, scope, visibility, {
|
|
...extraData,
|
|
});
|
|
|
|
return API.v1.success({ customField: result });
|
|
},
|
|
)
|
|
.post(
|
|
'livechat/custom-fields.delete',
|
|
{
|
|
response: {
|
|
200: POSTLivechatRemoveCustomFieldSuccess,
|
|
400: validateBadRequestErrorResponse,
|
|
401: validateUnauthorizedErrorResponse,
|
|
403: validateForbiddenErrorResponse,
|
|
},
|
|
authRequired: true,
|
|
permissionsRequired: ['view-livechat-manager'], // is this permission appropriate for the targeted action?
|
|
body: isPOSTLivechatRemoveCustomFields,
|
|
},
|
|
async function action() {
|
|
const { customFieldId } = this.bodyParams;
|
|
|
|
const result = await LivechatCustomField.removeById(customFieldId);
|
|
if (result.deletedCount === 0) {
|
|
return API.v1.failure('Custom field not found');
|
|
}
|
|
|
|
return API.v1.success();
|
|
},
|
|
);
|
|
|
|
type LivechatCustomFieldsEndpoints = ExtractRoutesFromAPI<typeof livechatCustomFieldsEndpoints>;
|
|
|
|
declare module '@rocket.chat/rest-typings' {
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
|
|
interface Endpoints extends LivechatCustomFieldsEndpoints {}
|
|
}
|