Skip to content
Snippets Groups Projects
Unverified Commit 733af429 authored by Kevin Aleman's avatar Kevin Aleman Committed by GitHub
Browse files

[NEW] REST endpoints to manage Omnichannel Business Units (#23750)

* add new endpoints to manage business units

* change the way deprecations were being returned

* change endpoints to be ts

* Change to omnichannel

* add deprecation warning to old endpoints
parent d9ccd564
No related branches found
No related tags found
No related merge requests found
import { API } from '../api';
import { apiDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
(API as any).helperMethods.set('deprecationWarning', function _deprecationWarning({ endpoint, versionWillBeRemoved, response }: { endpoint: string; versionWillBeRemoved: string; response: any }) {
export function deprecationWarning<T>({ endpoint, versionWillBeRemoved = '5.0', response }: { endpoint: string; versionWillBeRemoved?: string; response: T }): T {
const warningMessage = `The endpoint "${ endpoint }" is deprecated and will be removed after version ${ versionWillBeRemoved }`;
apiDeprecationLogger.warn(warningMessage);
if (process.env.NODE_ENV === 'development') {
......@@ -12,4 +12,6 @@ import { apiDeprecationLogger } from '../../../lib/server/lib/deprecationWarning
}
return response;
});
}
(API as any).helperMethods.set('deprecationWarning', deprecationWarning);
export interface IOmnichannelBusinessUnit {
_id: string;
name: string;
visibility: 'public' | 'private';
type: string;
numMonitors: number;
numDepartments: number;
_updatedAt: Date;
}
......@@ -7,7 +7,6 @@ API.v1.addRoute('livechat/business-hours.list', { authRequired: true }, {
const { sort } = this.parseJsonQuery();
const { name } = this.queryParams;
// @ts-ignore
return API.v1.success(await findBusinessHours(
this.userId,
{
......
......@@ -17,7 +17,7 @@ export async function findMonitors({ userId, text, pagination: { offset, count,
sort: sort || { name: 1 },
skip: offset,
limit: count,
fields: {
projection: {
username: 1,
name: 1,
status: 1,
......@@ -44,7 +44,7 @@ export async function findMonitorByUsername({ userId, username }) {
throw new Error('error-not-authorized');
}
const user = await Users.findOne({ username }, {
fields: {
projection: {
username: 1,
name: 1,
status: 1,
......
......@@ -45,5 +45,9 @@ export async function findUnitById({ userId, unitId }) {
if (!await hasPermissionAsync(userId, 'manage-livechat-units')) {
throw new Error('error-not-authorized');
}
return LivechatUnit.findOneById(unitId);
const unit = LivechatUnit.findOneById(unitId);
return {
unit,
};
}
import { API } from '../../../../../app/api/server';
import { findUnits, findUnitById, findUnitMonitors } from './lib/units';
API.v1.addRoute('livechat/units.list', { authRequired: true }, {
get() {
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();
const { text } = this.queryParams;
return API.v1.success(Promise.await(findUnits({
userId: this.userId,
text,
pagination: {
offset,
count,
sort,
},
})));
},
});
API.v1.addRoute('livechat/units.getOne', { authRequired: true }, {
get() {
const { unitId } = this.queryParams;
return API.v1.success(Promise.await(findUnitById({
userId: this.userId,
unitId,
})));
},
});
API.v1.addRoute('livechat/unitMonitors.list', { authRequired: true }, {
get() {
const { unitId } = this.queryParams;
return API.v1.success(Promise.await(findUnitMonitors({
userId: this.userId,
unitId,
})));
},
});
import { API } from '../../../../../app/api/server';
import { deprecationWarning } from '../../../../../app/api/server/helpers/deprecationWarning';
import { findUnits, findUnitById, findUnitMonitors } from './lib/units';
import { LivechatEnterprise } from '../lib/LivechatEnterprise';
import { IOmnichannelBusinessUnit } from '../../../../../definition/IOmnichannelBusinessUnit';
API.v1.addRoute('livechat/units.list', { authRequired: true }, {
async get() {
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();
const { text } = this.queryParams;
const response = await findUnits({
userId: this.userId,
text,
pagination: {
offset,
count,
sort,
},
});
return API.v1.success(deprecationWarning({ response, endpoint: 'livechat/units.list' }));
},
});
API.v1.addRoute('livechat/units.getOne', { authRequired: true }, {
async get() {
const { id } = this.urlParams;
const { unit } = await findUnitById({
userId: this.userId,
unitId: id,
}) as { unit: IOmnichannelBusinessUnit };
return API.v1.success(deprecationWarning({ response: unit, endpoint: 'livechat/units.getOne' }));
},
});
API.v1.addRoute('livechat/unitMonitors.list', { authRequired: true }, {
async get() {
const { unitId } = this.queryParams;
return API.v1.success(await findUnitMonitors({
userId: this.userId,
unitId,
}));
},
});
API.v1.addRoute('livechat/units', { authRequired: true, permissionsRequired: ['manage-livechat-units'] }, {
async get() {
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();
const { text } = this.queryParams;
return API.v1.success(Promise.await(findUnits({
userId: this.userId,
text,
pagination: {
offset,
count,
sort,
},
})));
},
async post() {
const { unitData, unitMonitors, unitDepartments } = this.bodyParams?.();
return LivechatEnterprise.saveUnit(null, unitData, unitMonitors, unitDepartments);
},
});
API.v1.addRoute('livechat/units/:id', { authRequired: true, permissionsRequired: ['manage-livechat-units'] }, {
async get() {
const { id } = this.urlParams;
const { unit } = await findUnitById({
userId: this.userId,
unitId: id,
}) as { unit: IOmnichannelBusinessUnit };
return API.v1.success(unit);
},
async post() {
const { unitData, unitMonitors, unitDepartments } = this.bodyParams?.();
const { id } = this.urlParams;
return LivechatEnterprise.saveUnit(id, unitData, unitMonitors, unitDepartments);
},
async delete() {
const { id } = this.urlParams;
return LivechatEnterprise.removeUnit(id);
},
});
import type { EngagementDashboardEndpoints } from './v1/engagementDashboard';
import type { OmnichannelBusinessHoursEndpoints } from './v1/omnichannel/businessHours';
import type { OmnichannelEndpoints } from './v1/omnichannel';
export type EnterpriseEndpoints = EngagementDashboardEndpoints & OmnichannelBusinessHoursEndpoints;
export type EnterpriseEndpoints = EngagementDashboardEndpoints & OmnichannelEndpoints;
import { IOmnichannelBusinessUnit } from '../../../../../definition/IOmnichannelBusinessUnit';
import { ILivechatMonitor } from '../../../../../definition/ILivechatMonitor';
type WithPagination<T> = {
units: T;
count: number;
offset: number;
total: number;
}
export type OmnichannelBusinessUnitsEndpoints = {
'livechat/units.list': {
GET: () => (WithPagination<IOmnichannelBusinessUnit[]>);
};
'livechat/units.getOne': {
GET: () => (IOmnichannelBusinessUnit);
};
'livechat/unitMonitors.list': {
GET: () => ({ monitors: ILivechatMonitor[] });
};
'livechat/units': {
GET: () => (WithPagination<IOmnichannelBusinessUnit[]>);
POST: () => IOmnichannelBusinessUnit;
};
'livechat/units/:id': {
GET: () => IOmnichannelBusinessUnit;
POST: () => IOmnichannelBusinessUnit;
DELETE: () => number;
};
}
import type { OmnichannelBusinessHoursEndpoints } from './businessHours';
import type { OmnichannelBusinessUnitsEndpoints } from './businessUnits';
export type OmnichannelEndpoints = OmnichannelBusinessHoursEndpoints & OmnichannelBusinessUnitsEndpoints;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment