Esempio n. 1
0
        public async Task <TimestampDao> GenerateTimestamp(TimestampDao timestamp, CancellationToken cancellationToken)
        {
            Guard.Argument(timestamp, nameof(timestamp)).NotNull();
            Guard.Argument(timestamp.UserId, nameof(timestamp.UserId)).NotNull().NotEmpty().NotWhiteSpace();

            var user = await _userRepository.GetUserById(timestamp.UserId, cancellationToken);

            if (user == null)
            {
                var message = $"Unable to find the user with user identifier '{timestamp.UserId}'.";
                _logger.Error(message);
                throw new UserException(message);
            }

            if (user.RemainingTimeStamps < 1)
            {
                var message = $"Not sufficient stamps left for the user '{user.Id}' with price plan '{user.CurrentPricePlanId}'.";
                _logger.Error(message);
                throw new TimestampException(message);
            }

            var pricePlan = await _pricePlanRepository.GetPricePlanById(user.CurrentPricePlanId, cancellationToken);

            if (pricePlan == null)
            {
                var message = $"Unable to find the current price plan with user identifier '{user.CurrentPricePlanId}' for an user '{user.Id}'.";
                _logger.Error(message);
                throw new UserException(message);
            }

            try
            {
                await SendTransaction(timestamp, pricePlan.GasPrice, pricePlan.Price <= 0, cancellationToken);

                var newTimestamp = await _timestampRepository.CreateTimestamp(timestamp, cancellationToken);

                await _timestampQueueService.AddTimestampMessage(new TimestampQueueMessage { TimestampId = newTimestamp.Id, TransactionId = newTimestamp.TransactionId, Created = DateTime.UtcNow, IsPremiumPlan = pricePlan.Price > 0 }, cancellationToken);

                user.RemainingTimeStamps--;
                await _userRepository.UpdateUser(user, cancellationToken);

                _logger.Information($"Successfully created time stamp for user '{user.Id}' with transaction '{newTimestamp.TransactionId}'");

                return(newTimestamp);
            }
            catch (TimestampException ex)
            {
                _logger.Error(ex.Message);
                throw ex;
            }
            catch (RpcClientUnknownException ex)
            {
                var message = $"{nameof(RpcClientUnknownException)} : {ex.Message}";
                _logger.Error(message);
                throw new RpcClientException(message);
            }
            catch (RpcClientTimeoutException ex)
            {
                var message = $"{nameof(RpcClientTimeoutException)} : {ex.Message}";
                _logger.Error(message);
                throw new RpcClientException(message);
            }
            catch (RpcClientNonceException ex)
            {
                var message = $"{nameof(RpcClientNonceException)} : {ex.Message}";
                _logger.Error(message);
                throw new RpcClientException(message);
            }
            catch (RpcClientUnderpricedException ex)
            {
                var message = $"{nameof(RpcClientUnderpricedException)} : {ex.Message}";
                _logger.Error(message);
                throw new RpcClientException(message);
            }
        }