From d61695932014f65af4eacbb733faadb306c4dfeb Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 27 Apr 2015 15:38:33 -0300 Subject: [PATCH] add preferences --- lib/model/index.js | 1 + lib/model/preferences.js | 30 +++++++++++++++++++++++ lib/server.js | 49 ++++++++++++++++++++++++++++++++++---- lib/storage.js | 21 ++++++++++++++++ test/integration/server.js | 45 ++++++++++++++++++++++++++++++++-- 5 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 lib/model/preferences.js diff --git a/lib/model/index.js b/lib/model/index.js index 3a3fcf2..1061a30 100644 --- a/lib/model/index.js +++ b/lib/model/index.js @@ -5,5 +5,6 @@ Model.Copayer = require('./copayer'); Model.TxProposal = require('./txproposal'); Model.Address = require('./address'); Model.Notification = require('./notification'); +Model.Preferences = require('./preferences'); module.exports = Model; diff --git a/lib/model/preferences.js b/lib/model/preferences.js new file mode 100644 index 0000000..ab2fb92 --- /dev/null +++ b/lib/model/preferences.js @@ -0,0 +1,30 @@ +'use strict'; + +function Preferences() { + this.version = '1.0.0'; +}; + +Preferences.create = function(opts) { + opts = opts || {}; + + var x = new Preferences(); + + x.createdOn = Math.floor(Date.now() / 1000); + x.walletId = opts.walletId; + x.copayerId = opts.copayerId; + x.email = opts.email; + return x; +}; + +Preferences.fromObj = function(obj) { + var x = new Preferences(); + + x.createdOn = obj.createdOn; + x.walletId = obj.walletId; + x.copayerId = obj.copayerId; + x.email = obj.email; + return x; +}; + + +module.exports = Preferences; diff --git a/lib/server.js b/lib/server.js index bf71221..8df35ad 100644 --- a/lib/server.js +++ b/lib/server.js @@ -19,11 +19,13 @@ var Storage = require('./storage'); var MessageBroker = require('./messagebroker'); var BlockchainExplorer = require('./blockchainexplorer'); -var Wallet = require('./model/wallet'); -var Copayer = require('./model/copayer'); -var Address = require('./model/address'); -var TxProposal = require('./model/txproposal'); -var Notification = require('./model/notification'); +var Model = require('./model'); +var Wallet = Model.Wallet; +var Copayer = Model.Copayer; +var Address = Model.Address; +var TxProposal = Model.TxProposal; +var Notification = Model.Notification; +var Preferences = Model.Preferences; var initialized = false; var lock, storage, blockchainExplorer, blockchainExplorerOpts; @@ -412,6 +414,43 @@ 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. + */ +WalletService.prototype.savePreferences = function(opts, cb) { + var self = this; + + opts = opts || {}; + + self._runLocked(cb, function(cb) { + var preferences = Preferences.create({ + walletId: self.walletId, + copayerId: self.copayerId, + email: opts.email, + }); + self.storage.storePreferences(preferences, function(err) { + return cb(err); + }); + }); +}; + +/** + * Retrieves a preferences for the current wallet/copayer pair. + * @param {Object} opts + * @returns {Object} preferences + */ +WalletService.prototype.getPreferences = function(opts, cb) { + var self = this; + + self.storage.fetchPreferences(self.walletId, self.copayerId, function(err, preferences) { + if (err) return cb(err); + return cb(null, preferences || {}); + }); +}; + + /** * Creates a new address. * @param {Object} opts diff --git a/lib/storage.js b/lib/storage.js index 8a96663..065e897 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -18,6 +18,7 @@ var collections = { ADDRESSES: 'addresses', NOTIFICATIONS: 'notifications', COPAYERS_LOOKUP: 'copayers_lookup', + PREFERENCES: 'preferences', }; var Storage = function(opts) { @@ -353,6 +354,26 @@ Storage.prototype.fetchAddress = function(address, cb) { }); }; +Storage.prototype.fetchPreferences = function(walletId, copayerId, cb) { + this.db.collection(collections.PREFERENCES).findOne({ + walletId: walletId, + copayerId: copayerId, + }, function(err, result) { + if (err) return cb(err); + if (!result) return cb(); + return cb(null, Model.Preferences.fromObj(result)); + }); +}; + +Storage.prototype.storePreferences = function(preferences, cb) { + this.db.collection(collections.PREFERENCES).update({ + walletId: preferences.walletId, + copayerId: preferences.copayerId, + }, preferences, { + w: 1, + upsert: true, + }, cb); +}; Storage.prototype._dump = function(cb, fn) { fn = fn || console.log; diff --git a/test/integration/server.js b/test/integration/server.js index 8828674..5942a95 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -25,6 +25,7 @@ var Wallet = Model.Wallet; var TxProposal = Model.TxProposal; var Address = Model.Address; var Copayer = Model.Copayer; +var Preferences = Model.Preferences; var WalletService = require('../../lib/server'); var TestData = require('../testdata'); @@ -673,7 +674,7 @@ describe('Wallet service', function() { it('should create address', function(done) { server.createAddress({}, function(err, address) { should.not.exist(err); - address.should.exist; + should.exist(address); address.walletId.should.equal(wallet.id); address.network.should.equal('livenet'); address.address.should.equal('3KxttbKQQPWmpsnXZ3rB4mgJTuLnVR7frg'); @@ -718,7 +719,7 @@ describe('Wallet service', function() { server.storage.storeAddressAndWallet.restore(); server.createAddress({}, function(err, address) { should.not.exist(err); - address.should.exist; + should.exist(address); address.address.should.equal('3KxttbKQQPWmpsnXZ3rB4mgJTuLnVR7frg'); address.path.should.equal('m/2147483647/0/0'); done(); @@ -728,6 +729,46 @@ describe('Wallet service', function() { }); }); + describe.only('Preferences', function() { + var server, wallet; + beforeEach(function(done) { + helpers.createAndJoinWallet(2, 2, function(s, w) { + server = s; + wallet = w; + done(); + }); + }); + + it('should save & retrieve preferences', function(done) { + server.savePreferences({ + email: 'dummy@dummy.com' + }, 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'); + done(); + }); + }); + }); + it('should save preferences only for requesting copayer', function(done) { + server.savePreferences({ + email: 'dummy@dummy.com' + }, function(err) { + should.not.exist(err); + helpers.getAuthServer(wallet.copayers[1].id, function(server2) { + server2.getPreferences({}, function(err, preferences) { + should.not.exist(err); + should.not.exist(preferences.email); + done(); + }); + }); + }); + }); + it.skip('should save preferences only for requesting wallet', function(done) {}); + }); + describe('#getBalance', function() { var server, wallet; beforeEach(function(done) {