From d6b74c3da867f4c791dd90d7eee194bee28e2135 Mon Sep 17 00:00:00 2001 From: Hoan Luu Huu <110280845+xquanluu@users.noreply.github.com> Date: Wed, 25 Oct 2023 07:16:01 +0700 Subject: [PATCH] Fix/dynamic apps (#499) * refactor utilization jambonz app for queue and user * refactor utilization jambonz app for queue and user * refactor utilization jambonz app for queue and user * refactor utilization jambonz app for queue and user * refactor utilization jambonz app for queue and user --- lib/dynamic-apps.js | 54 +++++++++++++++++++++++++++++++++++++++++++++ lib/middleware.js | 40 ++++----------------------------- 2 files changed, 58 insertions(+), 36 deletions(-) create mode 100644 lib/dynamic-apps.js diff --git a/lib/dynamic-apps.js b/lib/dynamic-apps.js new file mode 100644 index 00000000..cacf8442 --- /dev/null +++ b/lib/dynamic-apps.js @@ -0,0 +1,54 @@ +const appsMap = { + queue: { + // Dummy hook to follow later feature server logic. + call_hook: { + url: 'https://jambonz.org', + method: 'GET' + }, + account_sid: '', + app_json: [{ + verb: 'dequeue', + name: '', + timeout: 5 + }] + }, + user: { + // Dummy hook to follow later feature server logic. + call_hook: { + url: 'https://jambonz.org', + method: 'GET' + }, + account_sid: '', + app_json: [{ + verb: 'dial', + callerId: '', + answerOnBridge: true, + target: [ + { + type: 'user', + name: '' + } + ] + }] + } +}; + +const createJambonzApp = (type, {account_sid, name, caller_id}) => { + const app = {...appsMap[type]}; + app.account_sid = account_sid; + switch (type) { + case 'queue': + app.app_json[0].name = name; + break; + case 'user': + app.app_json[0].callerId = caller_id; + app.app_json[0].target[0].name = name; + break; + } + app.app_json = JSON.stringify(app.app_json); + return app; +}; + +module.exports = { + createJambonzApp +}; diff --git a/lib/middleware.js b/lib/middleware.js index aa444396..edcb8a89 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -13,6 +13,7 @@ const listTaskNames = require('./utils/summarize-tasks'); const { JAMBONES_MYSQL_REFRESH_TTL } = require('./config'); +const { createJambonzApp } = require('./dynamic-apps'); module.exports = function(srf, logger) { const { @@ -215,44 +216,11 @@ module.exports = function(srf, logger) { let app; if (req.locals.queue_name) { logger.debug(`calling to queue ${req.locals.queue_name}, generating queue app`); - app = { - // Dummy hook to follow later feature server logic. - call_hook: { - url: 'https://jambonz.org', - method: 'GET' - }, - account_sid, - app_json: JSON.stringify( - [{ - verb: 'dequeue', - name: req.locals.queue_name, - timeout: 5 - }] - ) - }; + app = createJambonzApp('queue', {account_sid, name: req.locals.queue_name}); } else if (req.locals.called_user) { logger.debug(`calling to registered user ${req.locals.called_user}, generating dial app`); - app = { - // Dummy hook to follow later feature server logic. - call_hook: { - url: 'https://jambonz.org', - method: 'GET' - }, - account_sid, - app_json: JSON.stringify( - [{ - verb: 'dial', - callerId: req.locals.callingNumber, - answerOnBridge: true, - target: [ - { - type: 'user', - name: req.locals.called_user - } - ] - }] - ) - }; + app = createJambonzApp('user', + {account_sid, name: req.locals.called_user, caller_id: req.locals.callingNumber}); } else if (req.locals.application_sid) { app = await lookupAppBySid(req.locals.application_sid); } else if (req.locals.originatingUser) {