add preferences
This commit is contained in:
parent
ebc779fb02
commit
d616959320
|
@ -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;
|
||||
|
|
|
@ -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;
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue