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() {
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()
);