Exemplo n.º 1
0
        public async Task <OperationStatusUpdateResultModel> AcceptAsync(Guid id, string hash)
        {
            if (Guid.Empty == id)
            {
                _log.Warning("Operation id is empty");
                return(OperationStatusUpdateResultModel.Failed(OperationStatusUpdateError.OperationNotFound));
            }

            if (string.IsNullOrEmpty(hash))
            {
                _log.Warning("Transaction hash is empty");
                return(OperationStatusUpdateResultModel.Failed(OperationStatusUpdateError.InvalidTransactionHash));
            }

            if (!hash.IsValidTransactionHash())
            {
                _log.Warning("Transaction hash has invalid format");
                return(OperationStatusUpdateResultModel.Failed(OperationStatusUpdateError.InvalidTransactionHash));
            }

            return(await _transactionScopeHandler.WithTransactionAsync(async() =>
            {
                var operationRequest = await _operationRequestsRepository.GetByIdAsync(id);
                if (operationRequest == null)
                {
                    var operation = await _operationsRepository.GetByIdAsync(id);
                    if (operation == null)
                    {
                        _log.Warning("Operation request not found by id", context: id);
                        return OperationStatusUpdateResultModel.Failed(OperationStatusUpdateError.OperationNotFound);
                    }

                    if (operation.Status == OperationStatus.Accepted)
                    {
                        _log.Warning("Operation has already been accepted", context: id);
                    }

                    return OperationStatusUpdateResultModel.Succeeded();
                }

                if (operationRequest.Status != OperationStatus.Created)
                {
                    _log.Error(message: "Current operation request status is not expected",
                               context: new { id = operationRequest.Id, currentStatus = operationRequest.Status.ToString() });
                    return OperationStatusUpdateResultModel.Failed(OperationStatusUpdateError.InvalidStatus);
                }

                await _operationRequestsRepository.AcceptAsync(
                    operationRequest.Id,
                    operationRequest.CustomerId,
                    operationRequest.Nonce,
                    operationRequest.MasterWalletAddress,
                    operationRequest.Type,
                    operationRequest.ContextJson,
                    operationRequest.CreatedAt,
                    hash);

                return OperationStatusUpdateResultModel.Succeeded();
            }));
        }
        public async Task <IOperation> GetByIdAsync(Guid id)
        {
            if (id == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(id));
            }

            return(await _operationRequestsRepository.GetByIdAsync(id) ??
                   await _operationsRepository.GetByIdAsync(id));
        }