/**
* @file Volt Core Library - user module
*
* @license
* (c) 2017 NS BASIC Corporation. All rights reserved.
*/
$volt.register('user', function (core, state) {
'use strict';
/**
* Volt User Library
*
* Provides functions for user management.
*
* @namespace
* @alias $volt.user
*/
var ns = {
/**
* Get user details
*
* @param {voltCallback} [callback] - upon success the data param will contain an API user object (see the response section of the [API spec](https://docs.voltcloud.io/api/#users-current-user-get)) - if not passed, a promise is returned
*/
get: function (userId, callback) {
if (typeof userId !== 'string') {
callback = userId;
userId = state.userId;
}
return core.api('GET', '/user/{{userId}}', {
userId: userId
}, undefined, callback);
},
/**
* Update user details
*
* @param {object} data - the properties to be updated (see the [API Spec](https://docs.voltcloud.io/api/#users-current-user-put) for details)
* @param {voltCallback} [callback] - upon success the data param will contain an API user object (see the response section of the [API spec](https://docs.voltcloud.io/api/#users-current-user-get)) - if not passed, a promise is returned
*/
update: function (data, userId, callback) {
if (typeof userId !== 'string') {
callback = userId;
userId = state.userId;
}
callback = core.methodAsPromised(callback);
core.api('PUT', '/user/{{userId}}', {
userId: userId
}, data, function (error, data) {
if (!error && state.userId === userId) {
// if the user changed their own details, make sure the
// admin attribute is kept in sync
state.admin = data.admin;
state.save();
}
callback(error, data);
});
return callback.promise;
},
/**
* Delete a user (or your own account)
*
* @param {voltCallback} [callback] - no data is returned on success - if not passed, a promise is returned
*/
delete: function (userId, callback) {
if (typeof userId !== 'string') {
callback = userId;
userId = state.userId;
}
callback = core.methodAsPromised(callback);
core.api('DELETE', '/user/{{userId}}', {
userId: userId
}, undefined, function (error, data) {
if (!error && state.userId === userId) {
// if the user deleted their own account, logout
core.auth.logout();
}
callback(error, data);
});
return callback.promise;
}
};
return ns;
});