public LimitDefinition RequestToDefinition(UpdateOrCreateLimitDefinitionRequestDefinition request) { LimitDefinition def = new LimitDefinition { Path = request.Path, DefaultAmountLimit = request.DefaultAmountLimit, DefaultTimerLimit = request.DefaultTimerLimit, MaxAmountLimit = request.MaxAmountLimit, MaxAmountLimitCurrencyCode = request.MaxAmountLimitCurrencyCode, TransactionLimit = new TransactionLimitDefinition { MinimumLimit = request.TransactionMinimumLimit ?? default(int), MaximumLimit = request.TransactionMaximumLimit ?? default(int), CurrencyCode = request.CurrencyCode }, AmountLimit = new AmountLimitDefinition { Limit = request.AmountLimit ?? default(int), Utilized = 0, Remaining = request.AmountLimit ?? default(int), CurrencyCode = request.CurrencyCode }, TimerLimit = new TimerLimitDefinition { Limit = request.TimerLimit ?? default(int), Utilized = 0, Remaining = request.TimerLimit ?? default(int), MaxTimerLimit = request.MaxTimerLimit ?? default(int) }, Duration = new DurationDefinition { Span = request.Span, Renewal = request.Renewal ?? default(RenewalType) }, isActive = request.isActive ?? default(bool), alsoLook = request.alsoLook }; return(def); }
public void UpdateDefinition(UpdateOrCreateLimitDefinitionRequestDefinition data) { if (String.IsNullOrEmpty(data.Path)) { throw new NullPathException { } } ; if ( data.CurrencyCode is null && data.TransactionMinimumLimit is null && data.TransactionMaximumLimit is null && data.AmountLimit is null && data.MaxTimerLimit is null && data.TimerLimit is null && data.Span is null && data.Renewal is null && data.Availability is null && data.DefaultAmountLimit is null && data.DefaultTimerLimit is null && data.isActive is null && data.alsoLook is null) { throw new UpdateDefinitionNeedDataException { } } ; if (!_businessHelperService.CheckPathFormat(data.Path)) { throw new PathFormatException { } } ; var def = _dataService.GetDefinitions(data.Path, false).FirstOrDefault(); if ( (data.CurrencyCode is null || data.CurrencyCode == string.Empty || data.TransactionMinimumLimit is null || data.TransactionMaximumLimit is null || data.AmountLimit is null || data.TimerLimit is null || data.Span is null || data.Span == string.Empty || data.Renewal is null || data.Availability is null || data.Availability.Start is null || data.Availability.Finish is null || data.MaxAmountLimit is null || data.MaxAmountLimitCurrencyCode is null || data.MaxAmountLimitCurrencyCode == string.Empty || data.MaxTimerLimit is null || data.DefaultAmountLimit is null || data.DefaultTimerLimit is null || data.isActive is null) && def == null ) { List <string> missingFields = new List <string>(); if (data.CurrencyCode is null || data.CurrencyCode == string.Empty) { missingFields.Add("currency-code"); } if (data.TransactionMinimumLimit is null) { missingFields.Add("transaction-minimum-limit"); } if (data.TransactionMaximumLimit is null) { missingFields.Add("transaction-maximum-limit"); } if (data.AmountLimit is null) { missingFields.Add("amount-limit"); } if (data.TimerLimit is null) { missingFields.Add("timer-limit"); } if (data.Span is null || data.Span == string.Empty) { missingFields.Add("duration-span"); } if (data.Renewal is null) { missingFields.Add("duration-renewal"); } if (data.MaxAmountLimit is null) { missingFields.Add("max-amount-limit"); } if (data.MaxAmountLimitCurrencyCode is null || data.MaxAmountLimitCurrencyCode == string.Empty) { missingFields.Add("max-amount-limit-currency-code"); } if (data.DefaultAmountLimit is null) { missingFields.Add("default-amount-limit"); } if (data.DefaultTimerLimit is null) { missingFields.Add("default-timer-limit"); } if (data.MaxTimerLimit is null) { missingFields.Add("max-timer-limit"); } if (data.isActive is null) { missingFields.Add("is-active"); } if (data.Availability is null) { missingFields.Add("availability"); } else if (data.Availability.Start is null || data.Availability.Finish is null) { missingFields.Add("availabilityStartFinish"); } throw new NewDefinitionNeedDataException(missingFields); } else if (def != null) { if (data.CurrencyCode is null || data.CurrencyCode == string.Empty) { data.CurrencyCode = def.TransactionLimit.CurrencyCode; } if (data.TransactionMinimumLimit is null) { data.TransactionMinimumLimit = def.TransactionLimit.MinimumLimit; } if (data.TransactionMaximumLimit is null) { data.TransactionMaximumLimit = def.TransactionLimit.MaximumLimit; } if (data.AmountLimit is null) { data.AmountLimit = def.AmountLimit.Limit; } if (data.TimerLimit is null) { data.TimerLimit = def.TimerLimit.Limit; } if (data.Span is null || data.Span == string.Empty) { data.Span = def.Duration.Span; } if (data.Renewal is null) { data.Renewal = def.Duration.Renewal; } if (data.Availability is null) { data.Availability = def.Availability; } if (data.MaxAmountLimit is null) { data.MaxAmountLimit = def.MaxAmountLimit; } if (data.MaxAmountLimitCurrencyCode is null || data.MaxAmountLimitCurrencyCode == string.Empty) { data.MaxAmountLimitCurrencyCode = def.MaxAmountLimitCurrencyCode; } if (data.MaxTimerLimit is null) { data.MaxTimerLimit = def.TimerLimit.MaxTimerLimit; } if (data.DefaultAmountLimit is null) { data.DefaultAmountLimit = def.DefaultAmountLimit; } if (data.DefaultTimerLimit is null) { data.DefaultTimerLimit = def.DefaultTimerLimit; } if (data.isActive is null) { data.isActive = def.isActive; } if (data.alsoLook is null) { data.alsoLook = def.alsoLook; } } if (data.Availability.Exceptions != null) { List <string> missingFields = new List <string>(); foreach (var ex in data.Availability.Exceptions) { if (ex.Start is null || ex.Finish is null) { missingFields.Add("availabilityExceptionStartFinish"); } else { _businessHelperService.CheckCrons(new List <string> { ex.Start.Split('?')[0].Trim(), ex.Finish.Split('?')[0].Trim() }); if (Int32.Parse(ex.Start.Split('?')[1].Trim()) > Int32.Parse(ex.Finish.Split('?')[1].Trim())) { throw new StartFinishDateException(); } else if (Int32.Parse(ex.Start.Split('?')[1].Trim()) == Int32.Parse(ex.Finish.Split('?')[1].Trim())) { if (CrontabSchedule.Parse(ex.Start.Split('?')[0].Trim()).GetNextOccurrence(new DateTime(DateTime.Today.Year, 1, 1)) > CrontabSchedule.Parse(ex.Finish.Split('?')[0].Trim()).GetNextOccurrence(new DateTime(DateTime.Today.Year, 1, 1))) { throw new StartFinishDateException(); } } } if (ex.Descriptions != null) { foreach (var desc in ex.Descriptions) { if (desc.Description is null || desc.Language is null) { missingFields.Add("availabilityExceptionDescriptionsInfo"); } } } else { missingFields.Add("availabilityExceptionDescriptions"); } } if (missingFields.Count != 0) { throw new NewDefinitionNeedDataException(missingFields); } } if (data.alsoLook is null) { data.alsoLook = "none"; } if (data.AmountLimit < 0 && data.AmountLimit != -1) { throw new InvalidAmountLimitException { } } ; if (data.TransactionMinimumLimit < 0) { throw new InvalidMinimumLimitException { } } ; if (data.TransactionMaximumLimit < 0) { throw new InvalidMaximumLimitException { } } ; if (data.TransactionMaximumLimit < data.TransactionMinimumLimit) { throw new MinimumMaximumException { } } ; if (data.TimerLimit < 0 && data.TimerLimit != -1) { throw new InvalidTimerLimitException { } } ; if (data.DefaultAmountLimit < 0 && data.DefaultAmountLimit != -1) { throw new InvalidDefaultAmountLimitException { } } ; if (data.DefaultTimerLimit < 0 && data.DefaultTimerLimit != -1) { throw new InvalidDefaultTimerLimitException { } } ; if (!_businessHelperService.CheckCurrencyCode(data.CurrencyCode)) { throw new InvalidCurrencyCodeException { } } ; if (!_businessHelperService.CheckSpan(data.Span)) { throw new InvalidSpanException { } } ; if (data.MaxAmountLimit < 0 && data.MaxAmountLimit != -1) { throw new InvalidMaxAmountLimitException { } } ; if (data.MaxTimerLimit < 0 && data.MaxTimerLimit != -1) { throw new InvalidMaxTimerLimitException { } } ; if (!_businessHelperService.CheckCurrencyCode(data.MaxAmountLimitCurrencyCode)) { throw new InvalidMaxAmountLimitCurrencyCodeException { } } ; if (data.CurrencyCode == data.MaxAmountLimitCurrencyCode) { if (data.MaxAmountLimit < data.AmountLimit && data.MaxAmountLimit != -1) { throw new AmountLimitBiggerThanMaxException(null); } } else { if (_dataHelperService.CurrencyConverter((decimal)data.MaxAmountLimit, data.MaxAmountLimitCurrencyCode, data.CurrencyCode) < data.AmountLimit && data.MaxAmountLimit != -1) { throw new AmountLimitBiggerThanMaxException(null); } } if (data.MaxTimerLimit < data.TimerLimit && data.MaxTimerLimit != -1) { throw new TimerLimitBiggerThanMaxException(null); } _businessHelperService.CheckAmountLimits((decimal)data.AmountLimit, data.CurrencyCode, data.Path); // amount limit control for path and parent paths. _businessHelperService.CheckTimerLimits((int)data.TimerLimit, data.Path); // timer limit control for path and parent paths. _businessHelperService.CheckCrons(new List <string> { data.Availability.Start.Split('?')[0].Trim(), data.Availability.Finish.Split('?')[0].Trim() }); // availability cron controls. decimal amountRemainingLimit = 0, amountUtilizedLimit = 0; int timerRemainingLimit = 0; if (def != null) { if (def.AmountLimit.CurrencyCode != data.CurrencyCode && def.AmountLimit.Utilized != 0) { amountUtilizedLimit = _dataHelperService.CurrencyConverter(def.AmountLimit.Utilized, def.AmountLimit.CurrencyCode, data.CurrencyCode); } else { amountUtilizedLimit = def.AmountLimit.Utilized; } if (data.AmountLimit - amountUtilizedLimit >= 0 && data.AmountLimit != -1) { amountRemainingLimit = (decimal)data.AmountLimit - amountUtilizedLimit; } if (data.TimerLimit - def.TimerLimit.Utilized >= 0 && data.TimerLimit != -1) { timerRemainingLimit = (int)data.TimerLimit - def.TimerLimit.Utilized; } } _dataService.InsertDefinition( data.Path, data.CurrencyCode, data.TransactionMinimumLimit, data.TransactionMaximumLimit, data.AmountLimit, data.TimerLimit, data.Span, data.Renewal, data.Availability, amountRemainingLimit, amountUtilizedLimit, timerRemainingLimit, data.MaxAmountLimit, data.MaxAmountLimitCurrencyCode, data.DefaultAmountLimit, data.DefaultTimerLimit, data.MaxTimerLimit, data.isActive, data.alsoLook ); }