add language & unit to preferences

Signed-off-by: Ivan Socolsky <jungans@gmail.com>
This commit is contained in:
Ivan Socolsky 2015-06-22 16:52:01 -03:00
parent a9d1a3c06a
commit 1460bf2128
3 changed files with 72 additions and 16 deletions

View File

@ -13,6 +13,8 @@ Preferences.create = function(opts) {
x.walletId = opts.walletId;
x.copayerId = opts.copayerId;
x.email = opts.email;
x.language = opts.language;
x.unit = opts.unit;
return x;
};
@ -23,6 +25,8 @@ Preferences.fromObj = function(obj) {
x.walletId = obj.walletId;
x.copayerId = obj.copayerId;
x.email = obj.email;
x.language = obj.language;
x.unit = obj.unit;
return x;
};

View File

@ -454,16 +454,42 @@ WalletService.prototype.joinWallet = function(opts, cb) {
* Save copayer preferences for the current wallet/copayer pair.
* @param {Object} opts
* @param {string} opts.email - Email address for notifications.
* @param {string} opts.language - Language used for notifications.
* @param {string} opts.unit - Bitcoin unit used to format amounts in notifications.
*/
WalletService.prototype.savePreferences = function(opts, cb) {
var self = this;
opts = opts || {};
if (opts.email) {
if (!EmailValidator.validate(opts.email)) {
return cb(new ClientError('Invalid email address'));
}
var preferences = [{
name: 'email',
isValid: function(value) {
return EmailValidator.validate(value);
},
}, {
name: 'language',
isValid: function(value) {
return _.isString(value) && value.length == 2;
},
}, {
name: 'unit',
isValid: function(value) {
return _.isString(value) && _.contains(['btc', 'bit'], value.toLowerCase());
},
}];
try {
_.each(preferences, function(preference) {
var value = opts[preference.name];
if (!value) return;
if (!preference.isValid(value)) {
throw 'Invalid ' + preference.name;
return false;
}
});
} catch (ex) {
return cb(new ClientError(ex));
}
self._runLocked(cb, function(cb) {
@ -471,6 +497,8 @@ WalletService.prototype.savePreferences = function(opts, cb) {
walletId: self.walletId,
copayerId: self.copayerId,
email: opts.email,
language: opts.language,
unit: opts.unit,
});
self.storage.storePreferences(preferences, function(err) {
return cb(err);

View File

@ -1099,13 +1099,17 @@ describe('Wallet service', function() {
it('should save & retrieve preferences', function(done) {
server.savePreferences({
email: 'dummy@dummy.com'
email: 'dummy@dummy.com',
language: 'es',
unit: 'bit',
}, function(err) {
should.not.exist(err);
server.getPreferences({}, function(err, preferences) {
should.not.exist(err);
should.exist(preferences);
preferences.email.should.equal('dummy@dummy.com');
preferences.language.should.equal('es');
preferences.unit.should.equal('bit');
done();
});
});
@ -1125,20 +1129,40 @@ describe('Wallet service', function() {
});
});
it.skip('should save preferences only for requesting wallet', function(done) {});
it('should validate email address', function(done) {
server.savePreferences({
email: ' '
}, function(err) {
should.exist(err);
err.message.should.contain('email');
server.savePreferences({
it('should validate entries', function(done) {
var invalid = [{
preferences: {
email: ' ',
},
expected: 'email'
}, {
preferences: {
email: 'dummy@' + _.repeat('domain', 50),
}, function(err) {
},
expected: 'email'
}, {
preferences: {
language: 'xxxxx',
},
expected: 'language'
}, {
preferences: {
language: 123,
},
expected: 'language'
}, {
preferences: {
unit: 'xxxxx',
},
expected: 'unit'
}, ];
async.each(invalid, function(item, next) {
server.savePreferences(item.preferences, function(err) {
should.exist(err);
err.message.should.contain('email');
done();
err.message.should.contain(item.expected);
next();
});
});
}, done);
});
});