From fea7c33ab068eea93d2b05bd31f451ebf04717f4 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Wed, 8 Mar 2017 15:36:47 -0500 Subject: [PATCH] Further work on job ids. --- lib/services/wallet-api/index.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/services/wallet-api/index.js b/lib/services/wallet-api/index.js index 71b30af5..95974253 100644 --- a/lib/services/wallet-api/index.js +++ b/lib/services/wallet-api/index.js @@ -533,13 +533,13 @@ WalletService.prototype._endpointGetWalletIds = function() { WalletService.prototype._endpointRegisterWallet = function() { var self = this; return function(req, res) { - var walletId = req.walletId; + var walletId = req.params.walletId; if (!walletId) { walletId = utils.getWalletId(); } self._createWallet(walletId, function(err) { if(err) { - return utils.sendError(new Error('Create wallet operatton failed.'), res); + return utils.sendError(err, res); } res.status(201).jsonp({ walletId: walletId @@ -555,14 +555,14 @@ WalletService.prototype._endpointPostAddresses = function() { if (!addresses || !addresses.length) { return utils.sendError(new Error('addresses are required when creating a wallet.'), res); } - var walletId = req.walletId; //also can't be zero + var walletId = req.params.walletId; //also can't be zero if (!walletId) { return utils.sendError(new Error('WalletId must be given.'), res); } // TODO make imdempotent //this could be a long-running operation, so we'll return a job id var jobId = utils.generateJobId(); - self._importAddresses(walletId, addAddresses, jobId, self._jobCompletionCallback.bind(self)); + self._importAddresses(walletId, addresses, jobId, self._jobCompletionCallback.bind(self)); res.status(200).jsonp({jobId: jobId}); }; }; @@ -599,7 +599,7 @@ WalletService.prototype._endpointPutAddresses = function() { return utils.sendError(new Error('Must PUT an array'), res); } - var walletId = req.walletId; + var walletId = req.params.walletId; if (!walletId) { return utils.sendError(new Error('WalletId must be given.'), res); } @@ -811,8 +811,13 @@ WalletService.prototype._getAddresses = function(walletId, callback) { WalletService.prototype._createWallet = function(walletId, callback) { var self = this; var key = self._encoding.encodeWalletAddressesKey(walletId); - var value = self._encoding.encodeWalletAddressesValue([]); - this.store.put(key, value, callback); + self.store.get(key, function(err) { + if (err && ((/notfound/i).test(err) || err.notFound)) { + var value = self._encoding.encodeWalletAddressesValue([]); + return self.store.put(key, value, callback); + } + callback(); + }); }; WalletService.prototype._jobCompletionCallback = function(err, results) { @@ -877,7 +882,7 @@ WalletService.prototype._importAddresses = function(walletId, addresses, jobId, return callback(err); } - self._loadAllBalances(jobId, callback); + self._loadAllBalances(callback); }); }); } @@ -890,7 +895,7 @@ WalletService.prototype._getUTXOIndexOperations = function(walletId, addresses, var balance = 0; - self._getBalance(walletId, function(err, initialBalance) { + self._getBalance(walletId, {}, function(err, initialBalance) { if(err && !err.notFound) { return callback(err); } @@ -1016,6 +1021,9 @@ WalletService.prototype.setupRoutes = function(app) { app.get('/wallets/:walletId', s._endpointGetAddresses() ); + app.post('/wallets/:walletId', + s._endpointRegisterWallet() + ); app.delete('/wallets/:walletId', s._endpointRemoveWallet() ); @@ -1030,9 +1038,6 @@ WalletService.prototype.setupRoutes = function(app) { app.get('/wallets/:walletId/transactions', s._endpointGetTransactions() ); - app.post('/wallets/:walletId', - s._endpointRegisterWallet() - ); app.get('/wallets', s._endpointGetWalletIds() );