diff --git a/app/voip/server/startup.ts b/app/voip/server/startup.ts new file mode 100644 index 000000000000..1220b5bb2ee2 --- /dev/null +++ b/app/voip/server/startup.ts @@ -0,0 +1,6 @@ +import { settings } from '../../settings/server'; +import { Voip } from '../../../server/sdk'; + +settings.watch('VoIP_Enabled', (value: boolean) => { + return value ? Voip.init() : Voip.stop(); +}); diff --git a/server/importPackages.ts b/server/importPackages.ts index ac23c38a1a2f..86938fc06980 100644 --- a/server/importPackages.ts +++ b/server/importPackages.ts @@ -109,3 +109,4 @@ import '../app/reactions/server'; import '../app/livechat/server'; import '../app/custom/server'; import '../app/authentication/server'; +import '../app/voip/server/startup'; diff --git a/server/sdk/types/IVoipService.ts b/server/sdk/types/IVoipService.ts index 4abc4b54eb39..44734b933d5f 100644 --- a/server/sdk/types/IVoipService.ts +++ b/server/sdk/types/IVoipService.ts @@ -16,4 +16,6 @@ export interface IVoipService { checkManagementConnection(host: string, port: string, userName: string, password: string): Promise; checkCallserverConnection(websocketUrl: string, protocol?: string): Promise; cachedQueueDetails(): () => Promise<{ name: string; members: string[] }[]>; + init(): Promise; + stop(): Promise; } diff --git a/server/services/voip/connector/asterisk/CommandHandler.ts b/server/services/voip/connector/asterisk/CommandHandler.ts index c81de3719495..7c2af2ebe427 100644 --- a/server/services/voip/connector/asterisk/CommandHandler.ts +++ b/server/services/voip/connector/asterisk/CommandHandler.ts @@ -150,5 +150,8 @@ export class CommandHandler { stop(): void { this.continuousMonitor.cleanMonitor(); + for (const connection of this.connections.values()) { + connection.closeConnection(); + } } } diff --git a/server/services/voip/service.ts b/server/services/voip/service.ts index f0c69bc9a3ab..8472fe6ec1a3 100644 --- a/server/services/voip/service.ts +++ b/server/services/voip/service.ts @@ -31,11 +31,19 @@ export class VoipService extends ServiceClassInternal implements IVoipService { this.logger = new Logger('VoIPService'); this.commandHandler = new CommandHandler(db); - try { - Promise.await(this.commandHandler.initConnection(CommandType.AMI)); - } catch (error) { - this.logger.error({ msg: `Error while initialising the connector. error = ${error}` }); - } + this.init(); + } + + async init(): Promise { + this.logger.info('Starting VoIP service'); + await this.commandHandler.initConnection(CommandType.AMI); + this.logger.info('VoIP service started'); + } + + async stop(): Promise { + this.logger.info('Stopping VoIP service'); + await this.commandHandler.stop(); + this.logger.info('VoIP service stopped'); } getServerConfigData(type: ServerType): IVoipCallServerConfig | IVoipManagementServerConfig { diff --git a/tests/end-to-end/api/02-channels.js b/tests/end-to-end/api/02-channels.js index 3e776ab2a19f..c049602aad99 100644 --- a/tests/end-to-end/api/02-channels.js +++ b/tests/end-to-end/api/02-channels.js @@ -281,6 +281,7 @@ describe('[Channels]', function () { }); describe('[/channels.files]', () => { + before(() => updateSetting('VoIP_Enabled', true)); const createVoipRoom = async () => { const testUser = await createUser({ roles: ['user', 'livechat-agent'] }); const visitor = await createVisitor();