node-eventstore-client/src/clientOperations/commitTransactionOperation.js

67 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-03-09 20:46:15 +00:00
var util = require('util');
var uuid = require('uuid');
var TcpCommand = require('../systemData/tcpCommand');
var InspectionDecision = require('../systemData/inspectionDecision');
var InspectionResult = require('./../systemData/inspectionResult');
var ClientMessage = require('../messages/clientMessage');
var results = require('../results');
2016-03-18 21:04:07 +00:00
var WrongExpectedVersionError = require('../errors/wrongExpectedVersionError');
var StreamDeletedError = require('../errors/streamDeletedError');
var AccessDeniedError = require('../errors/accessDeniedError');
2016-03-09 20:46:15 +00:00
var OperationBase = require('../clientOperations/operationBase');
function CommitTransactionOperation(log, cb, requireMaster, transactionId, userCredentials) {
OperationBase.call(this, log, cb, TcpCommand.TransactionCommit, TcpCommand.TransactionCommitCompleted, userCredentials);
this._responseType = ClientMessage.TransactionCommitCompleted;
this._requireMaster = requireMaster;
this._transactionId = transactionId;
}
util.inherits(CommitTransactionOperation, OperationBase);
CommitTransactionOperation.prototype._createRequestDto = function() {
return new ClientMessage.TransactionCommit(this._transactionId, this._requireMaster);
};
CommitTransactionOperation.prototype._inspectResponse = function(response) {
switch (response.result)
{
case ClientMessage.OperationResult.Success:
this._succeed();
return new InspectionResult(InspectionDecision.EndOperation, "Success");
case ClientMessage.OperationResult.PrepareTimeout:
return new InspectionResult(InspectionDecision.Retry, "PrepareTimeout");
case ClientMessage.OperationResult.CommitTimeout:
return new InspectionResult(InspectionDecision.Retry, "CommitTimeout");
case ClientMessage.OperationResult.ForwardTimeout:
return new InspectionResult(InspectionDecision.Retry, "ForwardTimeout");
case ClientMessage.OperationResult.WrongExpectedVersion:
2016-03-18 21:04:07 +00:00
this.fail(new WrongExpectedVersionError("Commit", this._transactionId));
2016-03-09 20:46:15 +00:00
return new InspectionResult(InspectionDecision.EndOperation, "WrongExpectedVersion");
case ClientMessage.OperationResult.StreamDeleted:
2016-03-18 21:04:07 +00:00
this.fail(new StreamDeletedError(this._transactionId));
2016-03-09 20:46:15 +00:00
return new InspectionResult(InspectionDecision.EndOperation, "StreamDeleted");
case ClientMessage.OperationResult.InvalidTransaction:
this.fail(new Error("Invalid transaction."));
return new InspectionResult(InspectionDecision.EndOperation, "InvalidTransaction");
case ClientMessage.OperationResult.AccessDenied:
2016-03-18 21:04:07 +00:00
this.fail(new AccessDeniedError("Write", this._transactionId));
2016-03-09 20:46:15 +00:00
return new InspectionResult(InspectionDecision.EndOperation, "AccessDenied");
default:
throw new Error(util.format("Unexpected OperationResult: %s.", response.result));
}
};
CommitTransactionOperation.prototype._transformResponse = function(response) {
var logPosition = new results.Position(response.prepare_position || -1, response.commit_position || -1);
return new results.WriteResult(response.last_event_number, logPosition);
};
CommitTransactionOperation.prototype.toString = function() {
return util.format("TransactionId: %s", this._transactionId);
};
module.exports = CommitTransactionOperation;