diff --git a/lib/storage.js b/lib/storage.js index 0132aa0..fc7b474 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -25,6 +25,7 @@ var collections = { TX_NOTES: 'tx_notes', SESSIONS: 'sessions', PUSH_NOTIFICATION_SUBS: 'push_notification_subs', + TX_CONFIRMATION_SUBS: 'tx_confirmation_subs', }; var Storage = function(opts) { @@ -78,6 +79,10 @@ Storage.prototype._createIndexes = function() { this.db.collection(collections.PUSH_NOTIFICATION_SUBS).createIndex({ copayerId: 1, }); + this.db.collection(collections.TX_CONFIRMATION_SUBS).createIndex({ + copayerId: 1, + txid: 1, + }); }; Storage.prototype.connect = function(opts, cb) { @@ -935,6 +940,42 @@ Storage.prototype.removePushNotificationSub = function(copayerId, token, cb) { }, cb); }; +Storage.prototype.fetchActiveTxConfirmationSubs = function(copayerId, cb) { + this.db.collection(collections.TX_CONFIRMATION_SUBS).find({ + copayerId: copayerId, + isActive: true, + }).toArray(function(err, result) { + if (err) return cb(err); + + if (!result) return cb(); + + var subs = _.map([].concat(result), function(r) { + return Model.TxConfirmationSub.fromObj(r); + }); + return cb(null, subs); + }); +}; + +Storage.prototype.storeTxConfirmationSub = function(txConfirmationSub, cb) { + this.db.collection(collections.TX_CONFIRMATION_SUBS).update({ + copayerId: txConfirmationSub.copayerId, + txid: txConfirmationSub.txid, + }, txConfirmationSub, { + w: 1, + upsert: true, + }, cb); +}; + +Storage.prototype.removeTxConfirmationSub = function(copayerId, txid, cb) { + this.db.collection(collections.TX_CONFIRMATION_SUBS).remove({ + copayerId: copayerId, + txid: txid, + }, { + w: 1 + }, cb); +}; + + Storage.prototype._dump = function(cb, fn) { fn = fn || console.log; cb = cb || function() {};