2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var AdminDAO = function(restDao) {
|
|
|
|
this._restDao = restDao;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
* @param {Function} callback(error)
|
|
|
|
*/
|
|
|
|
AdminDAO.prototype.createUser = function(options, callback) {
|
|
|
|
var uri;
|
|
|
|
|
|
|
|
if (!options.emailAddress || !options.password || !options.phone) {
|
|
|
|
callback(new Error('Incomplete arguments!'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uri = '/user';
|
|
|
|
this._restDao.post(options, uri, function(err) {
|
|
|
|
if (err && err.code === 409) {
|
|
|
|
callback(new Error('User name is already taken!'));
|
|
|
|
return;
|
|
|
|
} else if (err) {
|
|
|
|
callback(new Error('Error creating new user!'));
|
2014-09-15 11:09:13 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param {Function} callback(error)
|
|
|
|
*/
|
|
|
|
AdminDAO.prototype.validateUser = function(options, callback) {
|
|
|
|
var uri;
|
|
|
|
|
|
|
|
if (!options.emailAddress || !options.token) {
|
|
|
|
callback(new Error('Incomplete arguments!'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uri = '/user/validate';
|
|
|
|
this._restDao.post(options, uri, function(err) {
|
|
|
|
if (!err || (err && err.code === 202)) {
|
|
|
|
// success
|
2014-09-15 11:09:13 -04:00
|
|
|
callback();
|
2014-10-02 16:05:44 -04:00
|
|
|
} else {
|
|
|
|
callback(new Error('Validation failed!'));
|
2014-09-19 12:59:13 -04:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
2014-09-19 12:59:13 -04:00
|
|
|
|
2014-10-08 06:34:34 -04:00
|
|
|
module.exports = AdminDAO;
|