[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
pull/23757/head
Kevin Aleman 4 years ago committed by GitHub
parent d9ccd56426
commit 733af4291c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/api/server/helpers/deprecationWarning.ts
  2. 9
      definition/IOmnichannelBusinessUnit.ts
  3. 1
      ee/app/livechat-enterprise/server/api/business-hours.ts
  4. 4
      ee/app/livechat-enterprise/server/api/lib/monitors.js
  5. 6
      ee/app/livechat-enterprise/server/api/lib/units.js
  6. 42
      ee/app/livechat-enterprise/server/api/units.js
  7. 93
      ee/app/livechat-enterprise/server/api/units.ts
  8. 4
      ee/definition/rest/index.ts
  9. 30
      ee/definition/rest/v1/omnichannel/businessUnits.ts
  10. 4
      ee/definition/rest/v1/omnichannel/index.ts

@ -1,7 +1,7 @@
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);

@ -0,0 +1,9 @@
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,
};
}

@ -1,42 +0,0 @@
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,
})));
},
});

@ -0,0 +1,93 @@
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);
},
});

@ -1,4 +1,4 @@
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;

@ -0,0 +1,30 @@
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;
};
}

@ -0,0 +1,4 @@
import type { OmnichannelBusinessHoursEndpoints } from './businessHours';
import type { OmnichannelBusinessUnitsEndpoints } from './businessUnits';
export type OmnichannelEndpoints = OmnichannelBusinessHoursEndpoints & OmnichannelBusinessUnitsEndpoints;
Loading…
Cancel
Save