Browse Source

add language & unit to preferences

Signed-off-by: Ivan Socolsky <jungans@gmail.com>
activeAddress
Ivan Socolsky 10 years ago
parent
commit
1460bf2128
  1. 4
      lib/model/preferences.js
  2. 36
      lib/server.js
  3. 48
      test/integration/server.js

4
lib/model/preferences.js

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

36
lib/server.js

@ -454,16 +454,42 @@ WalletService.prototype.joinWallet = function(opts, cb) {
* Save copayer preferences for the current wallet/copayer pair. * Save copayer preferences for the current wallet/copayer pair.
* @param {Object} opts * @param {Object} opts
* @param {string} opts.email - Email address for notifications. * @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) { WalletService.prototype.savePreferences = function(opts, cb) {
var self = this; var self = this;
opts = opts || {}; opts = opts || {};
if (opts.email) { var preferences = [{
if (!EmailValidator.validate(opts.email)) { name: 'email',
return cb(new ClientError('Invalid email address')); 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) { self._runLocked(cb, function(cb) {
@ -471,6 +497,8 @@ WalletService.prototype.savePreferences = function(opts, cb) {
walletId: self.walletId, walletId: self.walletId,
copayerId: self.copayerId, copayerId: self.copayerId,
email: opts.email, email: opts.email,
language: opts.language,
unit: opts.unit,
}); });
self.storage.storePreferences(preferences, function(err) { self.storage.storePreferences(preferences, function(err) {
return cb(err); return cb(err);

48
test/integration/server.js

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

Loading…
Cancel
Save