public BatchCertificateRequestValidator(IStringLocalizer <BatchCertificateRequestValidator> localiser, IOrganisationQueryRepository organisationQueryRepository, IIlrRepository ilrRepository, ICertificateRepository certificateRepository, IStandardService standardService)
        {
            RuleFor(m => m.UkPrn).InclusiveBetween(10000000, 99999999).WithMessage("The UKPRN should contain exactly 8 numbers");

            RuleFor(m => m.FamilyName).NotEmpty().WithMessage("Provide apprentice family name");
            RuleFor(m => m.StandardCode).GreaterThan(0).WithMessage("Provide a valid Standard").DependentRules(() =>
            {
                RuleFor(m => m).CustomAsync(async(m, context, cancellation) =>
                {
                    bool sameStandard = true;

                    if (!string.IsNullOrEmpty(m.StandardReference))
                    {
                        var collatedStandard = await standardService.GetStandard(m.StandardReference);
                        if (m.StandardCode != collatedStandard?.StandardId)
                        {
                            sameStandard = false;
                            context.AddFailure("StandardReference and StandardCode must be for the same Standard");
                        }
                    }

                    // NOTE: This is not a nice way to do this BUT we cannot use another DependantRules()
                    if (sameStandard)
                    {
                        var courseOptions = await certificateRepository.GetOptions(m.StandardCode);

                        if (!courseOptions.Any() && !string.IsNullOrEmpty(m.CertificateData?.CourseOption))
                        {
                            context.AddFailure(new ValidationFailure("CourseOption", $"No course option available for this Standard. Must be empty"));
                        }
                        else if (courseOptions.Any() && !courseOptions.Any(o => o.OptionName.Equals(m.CertificateData?.CourseOption, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            string courseOptionsString = string.Join(", ", courseOptions.Select(o => o.OptionName));
                            context.AddFailure(new ValidationFailure("CourseOption", $"Invalid course option for this Standard. Must be one of the following: {courseOptionsString}"));
                        }
                    }
                });
            });

            RuleFor(m => m.Uln).InclusiveBetween(1000000000, 9999999999).WithMessage("ULN should contain exactly 10 numbers").DependentRules(() =>
            {
                When(m => m.StandardCode > 0 && !string.IsNullOrEmpty(m.FamilyName), () =>
                {
                    RuleFor(m => m).CustomAsync(async(m, context, canellation) =>
                    {
                        var requestedIlr   = await ilrRepository.Get(m.Uln, m.StandardCode);
                        var sumbittingEpao = await organisationQueryRepository.GetByUkPrn(m.UkPrn);

                        if (requestedIlr is null || !string.Equals(requestedIlr.FamilyName, m.FamilyName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            context.AddFailure(new ValidationFailure("Uln", "ULN, FamilyName and Standard not found."));
                        }
                        else if (sumbittingEpao is null)
                        {
                            context.AddFailure(new ValidationFailure("UkPrn", "Specified UKPRN not found"));
                        }
                        else
                        {
                            var providedStandards = await standardService.GetEpaoRegisteredStandards(sumbittingEpao.EndPointAssessorOrganisationId);

                            if (!providedStandards.Any(s => s.StandardCode == m.StandardCode))
                            {
                                context.AddFailure(new ValidationFailure("StandardCode", "Your organisation is not approved to assess this Standard"));
                            }
                        }
                    });
                });
Exemplo n.º 2
0
 public async Task <List <Option> > Handle(GetOptionsRequest request, CancellationToken cancellationToken)
 {
     return(await _repository.GetOptions(request.StdCode));
 }