From b60779f05c792498a089989da5e253850d384d95 Mon Sep 17 00:00:00 2001 From: Jean-Roch Maitre Date: Wed, 4 Feb 2015 14:18:20 -0800 Subject: [PATCH] UI-1129: Added automatic migration for an old Kazoo-UI user --- i18n/en-US.json | 6 ++ i18n/fr-FR.json | 6 ++ submodules/users/users.js | 146 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 153 insertions(+), 5 deletions(-) diff --git a/i18n/en-US.json b/i18n/en-US.json index bc34305..c57ca6d 100644 --- a/i18n/en-US.json +++ b/i18n/en-US.json @@ -497,6 +497,12 @@ "title": "Hot-Desking", "headline": "User Hot-Desking Settings", "help": "The Hot-Desking ID is automatically set as the user's extensions number." + }, + "__comment": "UI-1129: Migration tool from Kazoo-UI to Monster-UI", + "__version": "3.19", + "migration": { + "confirmMigration": "We found an existing callflow with the number you're trying to add, it appears we can migrate this callflow to the SmartPBX. Before we proceed, we want to make sure you understand that you won't be allowed to update this Callflow in Kazoo-UI or in the Monster-UI Callflows app after this change. Do you want to continue?", + "tooManyCallflows": "It appears the numbers you're trying to add belong to multiple Kazoo-UI Callflows. There is no way to properly migrate these callflows in Monster-UI please contact your support to fix it." } }, diff --git a/i18n/fr-FR.json b/i18n/fr-FR.json index e91d593..7736f93 100644 --- a/i18n/fr-FR.json +++ b/i18n/fr-FR.json @@ -473,6 +473,12 @@ "title": "Hot-Desking", "headline": "Réglages du Hot-Desking", "help": "Le Hot-Desking ID est automatiquement défini avec le numéro d'extension assigné." + }, + "__comment": "UI-1129: Migration tool from Kazoo-UI to Monster-UI", + "__version": "3.19", + "migration": { + "confirmMigration": "Un des numéros que vous souhaitez rajouter existe déjà dans Kazoo-UI, nous pouvons migrer le callflow vers SmartPBX pour vous permettre de l'utiliser. Vous ne pourrez plus modifier ce callflow dans Kazoo-UI ou dans l'application Callflows de Monster-UI. Continuez quand même?", + "tooManyCallflows": "Vous essayez de rajouter des numéros existant déjà sur plusieurs callflows, nous ne pouvons continuer notre opération automatique de migration. Veuillez contacter votre support pour résoudre ce problème." } }, diff --git a/submodules/users/users.js b/submodules/users/users.js index 1e4c15b..e9e31b7 100644 --- a/submodules/users/users.js +++ b/submodules/users/users.js @@ -2873,9 +2873,141 @@ define(function(require){ self.usersSmartUpdateVMBox(user, false, function(_dataVM) { callflow.flow.children['_'].data.id = _dataVM.id; - self.usersCreateCallflow(callflow, function(_dataCF) { - callback && callback(_dataCF); + self.usersCreateCallflow(callflow, + function(_dataCF) { + callback && callback(_dataCF); + }, + function(errorPayload, globalHandler) { + var errorCallback = function() { + globalHandler && globalHandler(errorPayload, { generateError: true }); + }; + + if(errorPayload.error === '400' && errorPayload.hasOwnProperty('data') && errorPayload.data.hasOwnProperty('numbers') && errorPayload.data.numbers.hasOwnProperty('unique')) { + self.usersHasKazooUICallflow(callflow, function(existingCallflow) { + self.usersMigrateKazooUIUser(callflow, existingCallflow, callback); + }, errorCallback); + } + else { + errorCallback(); + } + }, + false + ); + }); + }); + }, + + usersGetCallflowFromNumber: function(number, callback) { + var self = this, + found = false; + + self.callApi({ + resource: 'callflow.searchByNumber', + data: { + accountId: self.accountId, + value: number + }, + success: function(results) { + if(results.data.length > 0) { + _.each(results.data, function(callflow) { + _.each(callflow.numbers, function(n) { + if(n === number && found === false) { + found = true; + + self.callApi({ + resource: 'callflow.get', + data: { + accountId: self.accountId, + callflowId: callflow.id + }, + success: function(callflow) { + callback && callback(callflow.data); + } + }) + } + }); + }); + + if(found === false) { + callback && callback({}); + } + } + else { + callback && callback({}); + } + } + }); + }, + + usersHasKazooUICallflow: function(callflow, success, error) { + var self = this, + parallelRequests = {}, + kazooUICallflowFound = 0, + kazooUICallflow; + + // Check if we find a number that belong to a Kazoo-UI callflow + _.each(callflow.numbers, function(number) { + parallelRequests[number] = function(callback) { + self.usersGetCallflowFromNumber(number, function(callflow) { + if(!(callflow.hasOwnProperty('ui_metadata') && callflow.ui_metadata.hasOwnProperty('ui') && callflow.ui_metadata.ui === 'monster-ui')) { + // If we already found a callflow + if(typeof kazooUICallflow !== 'undefined') { + // If it's not the same Callflow that we found before, we increment the # of callflows found, which will trigger an error later + // If it's the same as before we do nothing + if(callflow.id !== kazooUICallflow.id) { + kazooUICallflowFound++; + } + } + else { + kazooUICallflowFound++; + kazooUICallflow = callflow; + } + } + + callback && callback(null, {}); }); + } + }); + + + monster.parallel(parallelRequests, function(err, results) { + // If we didn't find a single non-Monster-UI Callflow, then we trigger the error + if(kazooUICallflowFound === 0) { + error && error(); + } + // If we had more than 1 Kazoo UI callflow, show an error saying the migration is impossible + else if(kazooUICallflowFound > 1) { + monster.ui.alert(self.i18n.active().users.migration.tooManyCallflows); + } + // Else, we have found 1 callflow from Kazoo-UI, migration is possible, we continue with the success callback + else { + success && success(kazooUICallflow); + } + }); + }, + + usersMigrateKazooUIUser: function(callflowToCreate, existingCallflow, callback) { + var self = this, + newNumbers = []; + + // copy all the existing callflow numbers to the callflow we're about to create + callflowToCreate.numbers = existingCallflow.numbers; + + // Update the numbers of the existing callflow so that we keep a trace of the migration + _.each(existingCallflow.numbers, function(number) { + newNumbers.push('old_' + number + '_r' + monster.util.randomString(6)); + }); + existingCallflow.numbers = newNumbers; + + // Make sure the User knows what's going to happen + monster.ui.confirm(self.i18n.active().users.migration.confirmMigration, function() { + // First update the existing callflow with its new fake numbers + self.usersUpdateCallflow(existingCallflow, function(oldCallflow) { + // Now that the numbers have been changed, we can create the new Monster UI Callflow + self.usersCreateCallflow(callflowToCreate, function(newCallflow) { + // Once all this is done, continue normally to the SmartPBX normal update + callback && callback(newCallflow); + }) }); }); }, @@ -3534,17 +3666,21 @@ define(function(require){ }); }, - usersCreateCallflow: function(callflow, callback) { + usersCreateCallflow: function(callflow, success, error, generateError) { var self = this; self.callApi({ resource: 'callflow.create', data: { accountId: self.accountId, - data: callflow + data: callflow, + generateError: generateError === false ? false : true }, success: function(callflowData) { - callback && callback(callflowData.data); + success && success(callflowData.data); + }, + error: function(callflowData, junk, globalHandler) { + error && error(callflowData, globalHandler); } }); },