|
| 1 | +import { Match, check } from 'meteor/check'; |
| 2 | +import { TAPi18n } from 'meteor/rocketchat:tap-i18n'; |
| 3 | + |
| 4 | +import { API } from '../../api'; |
| 5 | +import { hasPermission } from '../../../../authorization/server'; |
| 6 | +import { Voip } from '../../../../../server/sdk'; |
| 7 | +import { ICallServerConfigData, IManagementConfigData, ServerType } from '../../../../../definition/IVoipServerConfig'; |
| 8 | + |
| 9 | +// management api(s) |
| 10 | +API.v1.addRoute('voipServerConfig.management', { authRequired: true }, { |
| 11 | + get() { |
| 12 | + if (!hasPermission(this.userId, 'manage-voip-contact-center-settings')) { |
| 13 | + return API.v1.unauthorized(TAPi18n._('error-insufficient-permission', { permission: 'manage-voip-contact-center-settings' })); |
| 14 | + } |
| 15 | + |
| 16 | + const config = Promise.await(Voip.getServerConfigData(ServerType.MANAGEMENT)); |
| 17 | + |
| 18 | + if (!config) { |
| 19 | + return API.v1.notFound(); |
| 20 | + } |
| 21 | + |
| 22 | + return API.v1.success({ ...config }); |
| 23 | + }, |
| 24 | + // NOTE: you can use this POST endpoint for both create and update operation |
| 25 | + post() { |
| 26 | + check(this.bodyParams, Match.ObjectIncluding({ |
| 27 | + host: String, |
| 28 | + port: Number, |
| 29 | + serverName: String, |
| 30 | + username: String, |
| 31 | + password: String, |
| 32 | + })); |
| 33 | + |
| 34 | + if (!hasPermission(this.userId, 'manage-voip-contact-center-settings')) { |
| 35 | + return API.v1.unauthorized(TAPi18n._('error-insufficient-permission', { permission: 'manage-voip-contact-center-settings' })); |
| 36 | + } |
| 37 | + |
| 38 | + const { host, port, serverName, username, password } = this.bodyParams; |
| 39 | + |
| 40 | + Promise.await(Voip.addServerConfigData({ |
| 41 | + type: ServerType.MANAGEMENT, |
| 42 | + host, |
| 43 | + name: serverName, |
| 44 | + active: true, |
| 45 | + configData: { |
| 46 | + port, |
| 47 | + username, |
| 48 | + password, |
| 49 | + } as IManagementConfigData, |
| 50 | + })); |
| 51 | + |
| 52 | + return API.v1.success(); |
| 53 | + }, |
| 54 | +}); |
| 55 | + |
| 56 | +// call-server api(s) |
| 57 | +API.v1.addRoute('voipServerConfig.callServer', { authRequired: true }, { |
| 58 | + get() { |
| 59 | + if (!hasPermission(this.userId, 'manage-voip-call-settings')) { |
| 60 | + return API.v1.unauthorized(TAPi18n._('error-insufficient-permission', { permission: 'manage-voip-call-settings' })); |
| 61 | + } |
| 62 | + |
| 63 | + const config = Promise.await(Voip.getServerConfigData(ServerType.CALL_SERVER)); |
| 64 | + if (!config) { |
| 65 | + return API.v1.notFound(); |
| 66 | + } |
| 67 | + |
| 68 | + return API.v1.success({ ...config }); |
| 69 | + }, |
| 70 | + // NOTE: you can use this POST endpoint for both create and update operation |
| 71 | + post() { |
| 72 | + check(this.bodyParams, Match.ObjectIncluding({ |
| 73 | + host: String, |
| 74 | + serverName: String, |
| 75 | + websocketPort: Number, |
| 76 | + websocketPath: String, |
| 77 | + })); |
| 78 | + |
| 79 | + if (!hasPermission(this.userId, 'manage-voip-call-settings')) { |
| 80 | + return API.v1.unauthorized(TAPi18n._('error-insufficient-permission', { permission: 'manage-voip-call-settings' })); |
| 81 | + } |
| 82 | + |
| 83 | + const { host, websocketPort, websocketPath, serverName } = this.bodyParams; |
| 84 | + |
| 85 | + Promise.await(Voip.addServerConfigData({ |
| 86 | + type: ServerType.CALL_SERVER, |
| 87 | + host, |
| 88 | + name: serverName, |
| 89 | + active: true, |
| 90 | + configData: { |
| 91 | + websocketPort, |
| 92 | + websocketPath, |
| 93 | + } as ICallServerConfigData, |
| 94 | + })); |
| 95 | + |
| 96 | + return API.v1.success(); |
| 97 | + }, |
| 98 | +}); |
0 commit comments