diff --git a/lib/server.js b/lib/server.js index 8aaf92e..c956306 100644 --- a/lib/server.js +++ b/lib/server.js @@ -480,6 +480,7 @@ WalletService.prototype.savePreferences = function(opts, cb) { }, }]; + opts = _.pick(opts, _.pluck(preferences, 'name')); try { _.each(preferences, function(preference) { var value = opts[preference.name]; @@ -494,15 +495,17 @@ WalletService.prototype.savePreferences = function(opts, cb) { } self._runLocked(cb, function(cb) { - var preferences = Model.Preferences.create({ - walletId: self.walletId, - copayerId: self.copayerId, - email: opts.email, - language: opts.language, - unit: opts.unit, - }); - self.storage.storePreferences(preferences, function(err) { - return cb(err); + self.storage.fetchPreferences(self.walletId, self.copayerId, function(err, oldPref) { + if (err) return cb(err); + + var newPref = Model.Preferences.create({ + walletId: self.walletId, + copayerId: self.copayerId, + }); + var preferences = Model.Preferences.fromObj(_.defaults(newPref, opts, oldPref)); + self.storage.storePreferences(preferences, function(err) { + return cb(err); + }); }); }); }; diff --git a/test/integration/server.js b/test/integration/server.js index 1654708..aa6d56e 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -1160,6 +1160,7 @@ describe('Wallet service', function() { email: 'dummy@dummy.com', language: 'es', unit: 'bit', + dummy: 'ignored', }, function(err) { should.not.exist(err); server.getPreferences({}, function(err, preferences) { @@ -1168,6 +1169,7 @@ describe('Wallet service', function() { preferences.email.should.equal('dummy@dummy.com'); preferences.language.should.equal('es'); preferences.unit.should.equal('bit'); + should.not.exist(preferences.dummy); done(); }); }); @@ -1186,6 +1188,58 @@ describe('Wallet service', function() { }); }); }); + it('should save preferences incrementally', function(done) { + async.series([ + + function(next) { + server.savePreferences({ + email: 'dummy@dummy.com', + }, next); + }, + function(next) { + server.getPreferences({}, function(err, preferences) { + should.not.exist(err); + should.exist(preferences); + preferences.email.should.equal('dummy@dummy.com'); + should.not.exist(preferences.language); + next(); + }); + }, + function(next) { + server.savePreferences({ + language: 'es', + }, next); + }, + function(next) { + server.getPreferences({}, function(err, preferences) { + should.not.exist(err); + should.exist(preferences); + preferences.language.should.equal('es'); + preferences.email.should.equal('dummy@dummy.com'); + next(); + }); + }, + function(next) { + server.savePreferences({ + language: null, + unit: 'bit', + }, next); + }, + function(next) { + server.getPreferences({}, function(err, preferences) { + should.not.exist(err); + should.exist(preferences); + preferences.unit.should.equal('bit'); + should.not.exist(preferences.language); + preferences.email.should.equal('dummy@dummy.com'); + next(); + }); + }, + ], function(err) { + should.not.exist(err); + done(); + }); + }); it.skip('should save preferences only for requesting wallet', function(done) {}); it('should validate entries', function(done) { var invalid = [{