From d38251e65315d4134e8a03a754c5ff5dda871f17 Mon Sep 17 00:00:00 2001 From: Ricardo Merino Date: Wed, 26 Dec 2018 12:49:15 -0600 Subject: [PATCH] UI-3275: Feature codes configured as patterns instead of single numbers (#110) * Flatten and use declarative style to refactor feature code creation * Fix incorrect path to get/set feature code * Remove unnecessary feature code defaults and callback * feature codes configured as patterns instead of single numbers * Update feature codes patterns * Code style * Verify if needed pattern is given by feature codes * Fix wrong pattern --- submodules/strategy/strategy.js | 153 +++++++++++++++++++++----------- 1 file changed, 101 insertions(+), 52 deletions(-) diff --git a/submodules/strategy/strategy.js b/submodules/strategy/strategy.js index 0dda43a..b4ada89 100644 --- a/submodules/strategy/strategy.js +++ b/submodules/strategy/strategy.js @@ -94,14 +94,14 @@ define(function(require) { { name: 'voicemail[action=check]', number: '97', - callflowNumber: '*97', + pattern: '^\\*97([0-9]*)$', moduleName: 'voicemail', actionName: 'check' }, { name: 'voicemail[single_mailbox_login]', number: '98', - callflowNumber: '*98', + pattern: '^\\*98([0-9]*)$', moduleName: 'voicemail', actionName: 'check', extraData: { @@ -242,7 +242,7 @@ define(function(require) { callback && callback(); }); - self.strategyCreateFeatureCodes(); + self.strategyHandleFeatureCodes(); }, strategyCheckFirstWalkthrough: function() { @@ -3203,61 +3203,96 @@ define(function(require) { }); }, - strategyCreateFeatureCodes: function(callback) { - var self = this; - - /* To complete with all feature codes */ - self.strategyGetFeatureCodes(function(listFeatureCodes) { - var existingFeatureCodes = $.map(listFeatureCodes, function(val) { return val.featurecode.name; }), - listRequests = []; - - _.each(self.featureCodes, function(featureCode) { - if (existingFeatureCodes.indexOf(featureCode.name) === -1) { - var callflow = { - flow: { - children: {}, - data: featureCode.extraData || {}, - module: featureCode.moduleName - }, - featurecode: { - name: featureCode.name, - number: featureCode.number - } - }; - - if (featureCode.hasOwnProperty('actionName')) { - callflow.flow.data = $.extend(callflow.flow.data, { - action: featureCode.actionName - }); - } - - if ('pattern' in featureCode) { - callflow.patterns = [ featureCode.pattern ]; - } else { - callflow.numbers = [ featureCode.callflowNumber ]; - } + strategyHandleFeatureCodes: function() { + var self = this, + featureCodesToUpdate = [ + 'voicemail[action=check]', + 'voicemail[single_mailbox_login]' + ], + expectedFeaturedCodes = _.keyBy(self.featureCodes, 'name'); - listRequests.push(function(localCallback) { - self.strategyCreateCallflow({ - data: { - data: callflow + monster.waterfall([ + function(callback) { + self.strategyGetFeatureCodes(function(featureCodes) { + callback(null, featureCodes); + }); + }, + function(featureCodes, callback) { + var featureCodeNames = _.map(featureCodes, 'featurecode.name'); + + monster.parallel(_ + .chain(self.featureCodes) + .reject(function(featureCode) { + return _.includes(featureCodeNames, featureCode.name); + }) + .map(function(featureCode) { + var newCallflow = { + flow: { + children: {}, + data: _.get(featureCode, 'extraData', {}), + module: featureCode.moduleName }, - success: function(data) { - localCallback && localCallback(null, data); + featurecode: { + name: featureCode.name, + number: featureCode.number } - }); - }); - } - }); + }; - if (listRequests.length > 0) { - monster.parallel(listRequests, function(err, results) { - callback && callback(); + if (_.has(featureCode, 'actionName')) { + _.set(newCallflow, 'flow.data.action', featureCode.actionName); + } + if (_.has(featureCode, 'pattern')) { + _.set(newCallflow, 'patterns', [featureCode.pattern]); + } else { + _.set(newCallflow, 'numbers', [featureCode.callflowNumber]); + } + + return function(parallelCallback) { + self.strategyCreateCallflow({ + data: { + data: newCallflow + }, + success: function() { + parallelCallback(null); + } + }); + }; + }) + .value() + , function() { + callback(null, featureCodes); }); - } else { - callback && callback(); + }, + function(featureCodes, callback) { + monster.parallel( + _.chain(featureCodes) + .filter(function(featureCode) { + return !_.includes( + _.get(featureCode, 'patterns', []), + _.get(expectedFeaturedCodes, [featureCode.featurecode.name, 'pattern']) + ) && _.includes(featureCodesToUpdate, featureCode.featurecode.name); + }) + .map(function(featureCode) { + return function(parallelCallback) { + self.strategyPatchCallflow({ + data: { + callflowId: featureCode.id, + data: { + patterns: [_.get(expectedFeaturedCodes, [featureCode.featurecode.name, 'pattern'])], + numbers: [] + } + }, + callback: function() { + parallelCallback(null); + } + }); + }; + }) + .value(), + callback + ); } - }); + ]); }, strategyGetFeatureCodes: function(callback) { @@ -3761,6 +3796,20 @@ define(function(require) { }); }, + strategyPatchCallflow: function(args) { + var self = this; + + self.callApi({ + resource: 'callflow.patch', + data: _.merge({ + accountId: self.accountId + }, args.data), + success: function(data) { + args.callback(data.data); + } + }); + }, + strategyListDirectories: function(callbackSuccess, callbackError) { var self = this;