private async Task <string> CheckLimitAsync( double cashPeriodValue, double transferPeriodValue, CashOperationLimitation limit, LimitationPeriod period, string clientId, string asset, double amount) { double currentValue = cashPeriodValue; if (limit.LimitationType == LimitationType.CardAndSwiftCashIn) { currentValue += transferPeriodValue; } double limitValue = (await _currencyConverter.ConvertAsync(limit.Asset, _currencyConverter.DefaultAsset, limit.Limit)).Item2; if (limitValue < currentValue + amount) { return(GetPeriodLimitFailMessage(period)); } double antiFraudValue = await _antiFraudCollector.GetAttemptsValueAsync( clientId, asset, limit.LimitationType); if (limitValue < currentValue + amount + antiFraudValue) { var forbidDuration = limit.LimitationType == LimitationType.CardCashIn ? CashOperationsTimeoutInMinutes : _attemptRetainInMinutes; return($"Please wait {forbidDuration} minute(s) after previous payment attempt"); } return(null); }
private async Task <RemainingLimitation> CalculateRemainingAsync( IEnumerable <CashOperation> operations, IEnumerable <CurrencyOperationAttempt> attempts, List <CurrencyOperationType> operationTypes, CashOperationLimitation limitation) { bool checkForAllCryptoCashouts = limitation.Asset == _currencyConverter.DefaultAsset && operationTypes.Contains(CurrencyOperationType.CryptoCashOut); double sum = 0; foreach (var assetCashin in operations.Where(c => operationTypes.Contains(c.OperationType.Value))) { if (limitation.Asset == _currencyConverter.DefaultAsset) { var converted = await _currencyConverter.ConvertAsync( assetCashin.Asset, _currencyConverter.DefaultAsset, assetCashin.Volume, checkForAllCryptoCashouts&& _currencyConverter.IsNotConvertible(assetCashin.Asset)); if (converted.Item1 == limitation.Asset) { sum += Math.Abs(converted.Item2); } } else if (limitation.Asset == assetCashin.Asset) { sum += Math.Abs(assetCashin.Volume); } } foreach (var attempt in attempts.Where(a => operationTypes.Contains(a.OperationType))) { if (limitation.Asset == _currencyConverter.DefaultAsset) { var converted = await _currencyConverter.ConvertAsync( attempt.Asset, _currencyConverter.DefaultAsset, attempt.Amount, checkForAllCryptoCashouts&& _currencyConverter.IsNotConvertible(attempt.Asset)); if (converted.Item1 == limitation.Asset) { sum += Math.Abs(converted.Item2); } } else if (limitation.Asset == attempt.Asset) { sum += Math.Abs(attempt.Amount); } } return(new RemainingLimitation { Asset = limitation.Asset, LimitationType = limitation.LimitationType, RemainingAmount = Math.Max(0, limitation.Limit - sum), }); }
internal static bool IsValid(this CashOperationLimitation limit) { return(!string.IsNullOrWhiteSpace(limit.Asset) && limit.Limit > 0); }