2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-18 12:44:00 -05:00
|
|
|
var ngModule = angular.module('woServices');
|
|
|
|
ngModule.service('admin', Admin);
|
|
|
|
module.exports = Admin;
|
|
|
|
|
2014-11-25 11:46:33 -05:00
|
|
|
function Admin(adminRestDao) {
|
|
|
|
this._restDao = adminRestDao;
|
2014-11-18 12:44:00 -05:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new email account.
|
|
|
|
* @param {String} options.emailAddress The desired email address
|
|
|
|
* @param {String} options.password The password to be used for the account.
|
|
|
|
* @param {String} options.phone The user's mobile phone number (required for verification and password reset).
|
|
|
|
*/
|
2014-12-11 13:07:04 -05:00
|
|
|
Admin.prototype.createUser = function(options) {
|
|
|
|
var self = this;
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
if (!options.emailAddress || !options.password || !options.phone) {
|
|
|
|
throw new Error('Incomplete arguments!');
|
|
|
|
}
|
|
|
|
resolve();
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-11 13:07:04 -05:00
|
|
|
}).then(function() {
|
|
|
|
return self._restDao.post(options, '/user');
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-11 13:07:04 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-02 16:05:44 -04:00
|
|
|
if (err && err.code === 409) {
|
2014-12-11 13:07:04 -05:00
|
|
|
throw new Error('User name is already taken!');
|
2014-09-15 11:09:13 -04:00
|
|
|
}
|
|
|
|
|
2015-01-27 06:24:13 -05:00
|
|
|
throw new Error('Error creating new user! Reason: ' + err.message);
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify a user's phone number by confirming a token to the server.
|
|
|
|
* @param {String} options.emailAddress The desired email address
|
|
|
|
* @param {String} options.token The validation token.
|
|
|
|
*/
|
2014-12-11 13:07:04 -05:00
|
|
|
Admin.prototype.validateUser = function(options) {
|
|
|
|
var self = this;
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
if (!options.emailAddress || !options.token) {
|
|
|
|
throw new Error('Incomplete arguments!');
|
|
|
|
}
|
|
|
|
resolve();
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-11 13:07:04 -05:00
|
|
|
}).then(function() {
|
|
|
|
var uri = '/user/validate';
|
|
|
|
return self._restDao.post(options, uri);
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-11 13:07:04 -05:00
|
|
|
}).catch(function(err) {
|
|
|
|
if (err && err.code === 202) {
|
2014-10-02 16:05:44 -04:00
|
|
|
// success
|
2014-12-11 13:07:04 -05:00
|
|
|
return;
|
2014-09-19 12:59:13 -04:00
|
|
|
}
|
2014-12-11 13:07:04 -05:00
|
|
|
|
2015-01-27 06:24:13 -05:00
|
|
|
throw new Error('Validation failed! Reason: ' + err.message);
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
2014-11-18 12:44:00 -05:00
|
|
|
};
|