Skip to content

Commit

Permalink
UI-3174: Create voicemail box optionally on user creation (#91)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
guillegr123 authored Oct 22, 2018
1 parent 46c29dd commit 4de21b7
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 221 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Set the default behavior, in case people don't have core.autocrlf set
* text=auto
1 change: 1 addition & 0 deletions i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
165 changes: 114 additions & 51 deletions submodules/users/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,7 @@ define(function(require) {
sendToSameEmail: true,
nextExtension: '',
listExtensions: {},
createVmbox: true,
listVMBoxes: {}
},
arrayExtensions = [],
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
},

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
});
},

Expand Down Expand Up @@ -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();
}
});
}
};

Expand Down
8 changes: 8 additions & 0 deletions submodules/users/views/creation.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
</div>

<div>
<div class="control-group hack-left">
<label class="control-label" for="create_vmbox"></label>
<div class="controls">
{{#monsterCheckbox i18n.users.dialogCreationUser.createVmbox }}
<input id="create_vmbox" type="checkbox" name="extra.createVmbox"{{#if createVmbox}} checked="checked"{{/if}}></input>
{{/monsterCheckbox}}
</div>
</div>
<div class="control-group hack-left">
<label class="control-label" for="include_directory"></label>
<div class="controls">
Expand Down
Loading

0 comments on commit 4de21b7

Please sign in to comment.