From 4de21b7714bdcff596e0aeab7bb608d68d1bcef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20Guti=C3=A9rrez?= Date: Mon, 22 Oct 2018 16:48:19 -0600 Subject: [PATCH] UI-3174: Create voicemail box optionally on user creation (#91) * Add checkbox to create vmbox on user creation form * Use waterfall flow for user creation * Extract user create api call to new function * Create vmbox only if requested * Rename view data property to create vmbox * Fix waterfall callbacks for user creation flow * Add translations * Refactor code and remove vmbox from callflow * Do not show VMBox row field if there is none for user * Do not create VMBox at usersSmartUpdateVMBox, if user has none * Add gittatributes to use default line ending behavior * Apply suggestions according to code review * Code refactor according to review --- .gitattributes | 2 + i18n/en-US.json | 1 + submodules/users/users.js | 165 +++++++++---- submodules/users/views/creation.html | 8 + submodules/users/views/name.html | 342 ++++++++++++++------------- 5 files changed, 297 insertions(+), 221 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..e7ec678f --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Set the default behavior, in case people don't have core.autocrlf set +* text=auto diff --git a/i18n/en-US.json b/i18n/en-US.json index 00741f16..40abaef4 100644 --- a/i18n/en-US.json +++ b/i18n/en-US.json @@ -481,6 +481,7 @@ }, "dialogCreationUser": { "createUser": "Create User", + "createVmbox": "Create a voicemail box for this user", "errorOnCreation": "An error occured while trying to create this user:", "extension": "Main Extension Number", "extensionAlreadyExist": "This Extension is already taken, please choose a different Extension Number.", diff --git a/submodules/users/users.js b/submodules/users/users.js index 4b109639..8eaa5fa6 100644 --- a/submodules/users/users.js +++ b/submodules/users/users.js @@ -1832,6 +1832,7 @@ define(function(require) { sendToSameEmail: true, nextExtension: '', listExtensions: {}, + createVmbox: true, listVMBoxes: {} }, arrayExtensions = [], @@ -3611,6 +3612,12 @@ define(function(require) { delete formattedData.user.service; } + if (!data.extra.createVmbox) { + // Remove vmbox from formatted data and user callflow + delete formattedData.vmbox; + delete formattedData.callflow.flow.children._; + } + delete formattedData.user.extra; return formattedData; @@ -3839,37 +3846,63 @@ define(function(require) { usersCreate: function(data, success, error) { var self = this; - self.callApi({ - resource: 'user.create', - data: { - accountId: self.accountId, - data: data.user + monster.waterfall([ + function(callback) { + self.usersCreateUser({ + data: { + data: data.user + }, + success: function(_dataUser) { + data.user.id = _dataUser.id; + callback(null, _dataUser); + }, + error: function(parsedError) { + callback(true); + }, + onChargesCancelled: function() { + callback(true); + } + }); }, - success: function(_dataUser) { - var userId = _dataUser.data.id; - data.user.id = userId; - data.vmbox.owner_id = userId; + function(_dataUser, callback) { + if (!data.extra.createVmbox) { + callback(null, _dataUser); + return; + } + data.vmbox.owner_id = _dataUser.id; self.usersCreateVMBox(data.vmbox, function(_dataVM) { - data.callflow.owner_id = userId; - data.callflow.type = 'mainUserCallflow'; - data.callflow.flow.data.id = userId; data.callflow.flow.children._.data.id = _dataVM.id; - - self.usersCreateCallflow(data.callflow, function(_dataCF) { - if (data.extra.includeInDirectory) { - self.usersAddUserToMainDirectory(_dataUser.data, _dataCF.id, function(dataDirectory) { - success(data); - }); - } else { - success(data); - } - }); + callback(null, _dataUser); }); }, - error: function() { + function(_dataUser, callback) { + var userId = _dataUser.id; + data.callflow.owner_id = userId; + data.callflow.type = 'mainUserCallflow'; + data.callflow.flow.data.id = userId; + + self.usersCreateCallflow(data.callflow, function(_dataCF) { + callback(null, _dataUser, _dataCF); + }); + }, + function(_dataUser, _dataCF, callback) { + if (!data.extra.includeInDirectory) { + callback(null); + return; + } + + self.usersAddUserToMainDirectory(_dataUser, _dataCF.id, function(dataDirectory) { + callback(null); + }); + } + ], + function(err) { + if (err) { error(); + return; } + success(data); }); }, @@ -3915,7 +3948,12 @@ define(function(require) { user: user, needVMUpdate: false, callback: function(_dataVM) { - callflow.flow.children._.data.id = _dataVM.id; + if (_.isEmpty(_dataVM)) { + // Remove VMBox from callflow if there is none + callflow.flow.children = {}; + } else { + callflow.flow.children._.data.id = _dataVM.id; + } self.usersCreateCallflow(callflow, function(_dataCF) { @@ -4849,39 +4887,40 @@ define(function(require) { user = args.user, needVMUpdate = args.needVMUpdate || true, callback = args.callback, - oldPresenceId = args.oldPresenceId || undefined, - userExtension = args.userExtension; + oldPresenceId = args.oldPresenceId || undefined; self.usersListVMBoxesUser(user.id, function(vmboxes) { - if (vmboxes.length > 0) { - if (needVMUpdate) { + if (_.isEmpty(vmboxes)) { + callback && callback({}); + return; + } + if (!needVMUpdate) { + callback && callback(vmboxes[0]); + return; + } + + monster.waterfall([ + function(wfCallback) { self.usersGetVMBox(vmboxes[0].id, function(vmbox) { - vmbox.name = user.first_name + ' ' + user.last_name + self.appFlags.users.smartPBXVMBoxString; - // We only want to update the vmbox number if it was already synced with the presenceId (and if the presenceId was not already set) - // This allows us to support old clients who have mailbox number != than their extension number - if (oldPresenceId === vmbox.mailbox) { - // If it's synced, then we update the vmbox number as long as the main extension is set to something different than 'unset' in which case we don't update the vmbox number value - vmbox.mailbox = (user.presence_id && user.presence_id !== 'unset') ? user.presence_id + '' : vmbox.mailbox; - } + wfCallback(null, vmbox); + }); + }, + function(vmbox, wfCallback) { + vmbox.name = user.first_name + ' ' + user.last_name + self.appFlags.users.smartPBXVMBoxString; + // We only want to update the vmbox number if it was already synced with the presenceId (and if the presenceId was not already set) + // This allows us to support old clients who have mailbox number != than their extension number + if (oldPresenceId === vmbox.mailbox) { + // If it's synced, then we update the vmbox number as long as the main extension is set to something different than 'unset' in which case we don't update the vmbox number value + vmbox.mailbox = (user.presence_id && user.presence_id !== 'unset') ? user.presence_id + '' : vmbox.mailbox; + } - self.usersUpdateVMBox(vmbox, function(vmboxSaved) { - callback && callback(vmboxSaved); - }); + self.usersUpdateVMBox(vmbox, function(vmboxSaved) { + wfCallback(null, vmboxSaved); }); - } else { - callback && callback(vmboxes[0]); } - } else { - var vmbox = { - owner_id: user.id, - mailbox: user.presence_id || userExtension || user.extra.vmbox.mailbox, - name: user.first_name + ' ' + user.last_name + self.appFlags.users.smartPBXVMBoxString - }; - - self.usersCreateVMBox(vmbox, function(vmbox) { - callback && callback(vmbox); - }); - } + ], function(err, vmboxSaved) { + callback && callback(vmboxSaved); + }); }); }, @@ -5197,6 +5236,30 @@ define(function(require) { callback && callback(data.data); } }); + }, + + usersCreateUser: function(args) { + var self = this; + + self.callApi({ + resource: 'user.create', + data: _.merge({ + accountId: self.accountId + }, args.data), + success: function(data, status) { + args.hasOwnProperty('success') && args.success(data.data); + }, + error: function(parsedError) { + if (parsedError.error === '402') { + return; + } + + args.hasOwnProperty('error') && args.error(parsedError); + }, + onChargesCancelled: function() { + args.hasOwnProperty('onChargesCancelled') && args.onChargesCancelled(); + } + }); } }; diff --git a/submodules/users/views/creation.html b/submodules/users/views/creation.html index 34751bd9..3017a8ad 100644 --- a/submodules/users/views/creation.html +++ b/submodules/users/views/creation.html @@ -53,6 +53,14 @@
+
+ +
+ {{#monsterCheckbox i18n.users.dialogCreationUser.createVmbox }} + + {{/monsterCheckbox}} +
+
diff --git a/submodules/users/views/name.html b/submodules/users/views/name.html index 5e38b691..1f8e4305 100644 --- a/submodules/users/views/name.html +++ b/submodules/users/views/name.html @@ -1,170 +1,172 @@ -
-
-
- -
-
- -
- - - -
-
- - {{extra.vmbox.mailbox}} - {{i18n.users.editionForm.changePIN}} -
- - - - {{#if extra.mainCallflowId}} -
- -
- {{/if}} -
-
-
-
- -
- -
-
- -
- -
-
- -
- -
-
- {{#unless extra.ringingTimeout}} - {{#compare extra.ringingTimeout "===" 0}} -
- {{else}} -
- {{/compare}} - {{else}} - {{#if extra.groupTimeout}} -
- {{else}} -
- {{/if}} - {{/unless}} - - - {{ i18n.users.editionForm.timeoutSec }} -
- {{#if extra.groupTimeout}} - - {{/if}} -
- -
-
- -
-
- - -
-
- -
-
- -
-
- - -
-
-
- - -
- - {{#compare id "!==" extra.adminId}} - {{ i18n.users.delete }} - {{/compare}} - - {{#if extra.canImpersonate}} - {{ i18n.users.impersonate }} - {{/if}} - -
- {{ i18n.cancel }} - -
-
-
+
+
+
+ +
+
+ +
+ + + + {{#if extra.vmbox.id}} +
+
+ + {{extra.vmbox.mailbox}} + {{i18n.users.editionForm.changePIN}} +
+ {{/if}} + + + + {{#if extra.mainCallflowId}} +
+ +
+ {{/if}} +
+
+
+
+ +
+ +
+
+ +
+ +
+
+ +
+ +
+
+ {{#unless extra.ringingTimeout}} + {{#compare extra.ringingTimeout "===" 0}} +
+ {{else}} +
+ {{/compare}} + {{else}} + {{#if extra.groupTimeout}} +
+ {{else}} +
+ {{/if}} + {{/unless}} + + + {{ i18n.users.editionForm.timeoutSec }} +
+ {{#if extra.groupTimeout}} + + {{/if}} +
+ +
+
+ +
+
+ + +
+
+ +
+
+ +
+
+ + +
+
+
+ + +
+ + {{#compare id "!==" extra.adminId}} + {{ i18n.users.delete }} + {{/compare}} + + {{#if extra.canImpersonate}} + {{ i18n.users.impersonate }} + {{/if}} + +
+ {{ i18n.cancel }} + +
+
+