public async Task ThenRulesShouldBeReturned()
        {
            //Act
            var result = await _service.GetFundingRules();

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(_expectedResevationRules, result.Rules);
            Assert.AreEqual(_expectedGlobalRules, result.GlobalRules);
        }
示例#2
0
        public async Task <GetNextUnreadGlobalFundingRuleResult> Handle(GetNextUnreadGlobalFundingRuleQuery request, CancellationToken cancellationToken)
        {
            var response = await _service.GetFundingRules();

            var globalRules =
                response?.GlobalRules?.Where(rule => rule.ActiveFrom.HasValue && rule.ActiveFrom >= DateTime.Now);


            if (Guid.TryParse(request.Id, out var userId))
            {
                globalRules = globalRules?.Where(rule => rule.UserRuleAcknowledgements == null ||
                                                 !rule.UserRuleAcknowledgements.Any() ||
                                                 rule.UserRuleAcknowledgements.Where(a => a.UserId.HasValue).All(a => !a.UserId.Value.Equals(userId)));
            }
            else if (int.TryParse(request.Id, out var ukPrn))
            {
                globalRules = globalRules?.Where(rule => rule.UserRuleAcknowledgements == null ||
                                                 !rule.UserRuleAcknowledgements.Any() ||
                                                 rule.UserRuleAcknowledgements.Where(a => a.UkPrn.HasValue).All(a => !a.UkPrn.Value.Equals(ukPrn)));
            }

            var nextRule = globalRules?.OrderBy(r => r.ActiveFrom).FirstOrDefault();

            return(new GetNextUnreadGlobalFundingRuleResult {
                Rule = nextRule
            });
        }
        public async Task <GetFundingRulesResult> Handle(GetFundingRulesQuery request, CancellationToken cancellationToken)
        {
            var response = await _service.GetFundingRules();

            return(new GetFundingRulesResult
            {
                AccountRules = response?.Rules?.ToArray() ?? new ReservationRule[0],
                GlobalRules = response?.GlobalRules?.ToArray() ?? new GlobalRule[0]
            });
        }
        public async Task <ValidationResult> ValidateAsync(CacheReservationEmployerCommand command)
        {
            var result = new ValidationResult();

            if (command.Id == Guid.Empty)
            {
                result.AddError(nameof(command.Id));
            }

            if (command.AccountId == default(long))
            {
                result.AddError(nameof(command.AccountId));
            }
            else
            {
                var accountFundingRulesApiResponse = await _rulesService.GetAccountFundingRules(command.AccountId);

                if (accountFundingRulesApiResponse.GlobalRules.Any(c => c != null && c.RuleType == GlobalRuleType.ReservationLimit) &&
                    accountFundingRulesApiResponse.GlobalRules.Count(c => c.RuleType == GlobalRuleType.ReservationLimit) > 0)
                {
                    result.FailedRuleValidation = true;
                }

                var globalRulesApiResponse = await _rulesService.GetFundingRules();

                if (globalRulesApiResponse.GlobalRules != null && globalRulesApiResponse.GlobalRules.Any(c => c != null && c.RuleType == GlobalRuleType.FundingPaused) &&
                    globalRulesApiResponse.GlobalRules.Count(c => c.RuleType == GlobalRuleType.FundingPaused) > 0)
                {
                    result.FailedGlobalRuleValidation = true;
                }

                // eoi
                var queryResult = await _mediator.Send(new GetLegalEntitiesQuery
                {
                    AccountId = command.AccountId
                });

                if (queryResult.AccountLegalEntities.Any(entity =>
                                                         !entity.IsLevy &&
                                                         entity.AgreementType != AgreementType.NonLevyExpressionOfInterest))
                {
                    result.FailedEoiCheck = true;
                    return(result);
                }
            }

            if (command.AccountLegalEntityId == default(long))
            {
                result.AddError(nameof(command.AccountLegalEntityId));
            }

            if (string.IsNullOrWhiteSpace(command.AccountLegalEntityName))
            {
                result.AddError(nameof(command.AccountLegalEntityName));
            }

            if (string.IsNullOrWhiteSpace(command.AccountLegalEntityPublicHashedId))
            {
                result.AddError(nameof(command.AccountLegalEntityPublicHashedId));
            }

            if (command.UkPrn.HasValue && !command.IsEmptyCohortFromSelect)
            {
                var accounts = await _mediator.Send(
                    new GetTrustedEmployersQuery { UkPrn = command.UkPrn.Value });

                var matchedAccount = accounts?.Employers?.SingleOrDefault(employer =>
                                                                          employer.AccountLegalEntityPublicHashedId == command.AccountLegalEntityPublicHashedId);

                result.FailedAuthorisationValidation = matchedAccount == null;
            }

            return(result);
        }