Further work on job ids.

This commit is contained in:
Chris Kleeschulte 2017-03-08 15:36:47 -05:00
parent 841530705e
commit fea7c33ab0

View File

@ -533,13 +533,13 @@ WalletService.prototype._endpointGetWalletIds = function() {
WalletService.prototype._endpointRegisterWallet = function() { WalletService.prototype._endpointRegisterWallet = function() {
var self = this; var self = this;
return function(req, res) { return function(req, res) {
var walletId = req.walletId; var walletId = req.params.walletId;
if (!walletId) { if (!walletId) {
walletId = utils.getWalletId(); walletId = utils.getWalletId();
} }
self._createWallet(walletId, function(err) { self._createWallet(walletId, function(err) {
if(err) { if(err) {
return utils.sendError(new Error('Create wallet operatton failed.'), res); return utils.sendError(err, res);
} }
res.status(201).jsonp({ res.status(201).jsonp({
walletId: walletId walletId: walletId
@ -555,14 +555,14 @@ WalletService.prototype._endpointPostAddresses = function() {
if (!addresses || !addresses.length) { if (!addresses || !addresses.length) {
return utils.sendError(new Error('addresses are required when creating a wallet.'), res); 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) { if (!walletId) {
return utils.sendError(new Error('WalletId must be given.'), res); return utils.sendError(new Error('WalletId must be given.'), res);
} }
// TODO make imdempotent // TODO make imdempotent
//this could be a long-running operation, so we'll return a job id //this could be a long-running operation, so we'll return a job id
var jobId = utils.generateJobId(); 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}); res.status(200).jsonp({jobId: jobId});
}; };
}; };
@ -599,7 +599,7 @@ WalletService.prototype._endpointPutAddresses = function() {
return utils.sendError(new Error('Must PUT an array'), res); return utils.sendError(new Error('Must PUT an array'), res);
} }
var walletId = req.walletId; var walletId = req.params.walletId;
if (!walletId) { if (!walletId) {
return utils.sendError(new Error('WalletId must be given.'), res); 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) { WalletService.prototype._createWallet = function(walletId, callback) {
var self = this; var self = this;
var key = self._encoding.encodeWalletAddressesKey(walletId); var key = self._encoding.encodeWalletAddressesKey(walletId);
var value = self._encoding.encodeWalletAddressesValue([]); self.store.get(key, function(err) {
this.store.put(key, value, callback); 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) { WalletService.prototype._jobCompletionCallback = function(err, results) {
@ -877,7 +882,7 @@ WalletService.prototype._importAddresses = function(walletId, addresses, jobId,
return callback(err); return callback(err);
} }
self._loadAllBalances(jobId, callback); self._loadAllBalances(callback);
}); });
}); });
} }
@ -890,7 +895,7 @@ WalletService.prototype._getUTXOIndexOperations = function(walletId, addresses,
var balance = 0; var balance = 0;
self._getBalance(walletId, function(err, initialBalance) { self._getBalance(walletId, {}, function(err, initialBalance) {
if(err && !err.notFound) { if(err && !err.notFound) {
return callback(err); return callback(err);
} }
@ -1016,6 +1021,9 @@ WalletService.prototype.setupRoutes = function(app) {
app.get('/wallets/:walletId', app.get('/wallets/:walletId',
s._endpointGetAddresses() s._endpointGetAddresses()
); );
app.post('/wallets/:walletId',
s._endpointRegisterWallet()
);
app.delete('/wallets/:walletId', app.delete('/wallets/:walletId',
s._endpointRemoveWallet() s._endpointRemoveWallet()
); );
@ -1030,9 +1038,6 @@ WalletService.prototype.setupRoutes = function(app) {
app.get('/wallets/:walletId/transactions', app.get('/wallets/:walletId/transactions',
s._endpointGetTransactions() s._endpointGetTransactions()
); );
app.post('/wallets/:walletId',
s._endpointRegisterWallet()
);
app.get('/wallets', app.get('/wallets',
s._endpointGetWalletIds() s._endpointGetWalletIds()
); );