<commit_before>﻿using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.EntityFrameworkCore;
using SFA.DAS.Authorization.Features.Models;
using SFA.DAS.Authorization.Features.Services;
using SFA.DAS.CommitmentsV2.Data;
using SFA.DAS.CommitmentsV2.Domain;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.CommitmentsV2.Types.Dtos;

namespace SFA.DAS.CommitmentsV2.Application.Queries.GetDraftApprenticeships
{
    public class GetDraftApprenticeshipsQueryHandler : IRequestHandler<GetDraftApprenticeshipsQuery, GetDraftApprenticeshipsQueryResult>
    {
        private readonly Lazy<ProviderCommitmentsDbContext> _dbContext;
        private readonly IFeatureTogglesService<FeatureToggle> _featureTogglesService;

        public GetDraftApprenticeshipsQueryHandler(Lazy<ProviderCommitmentsDbContext> dbContext, IFeatureTogglesService<FeatureToggle> featureTogglesService)
        {
            _dbContext = dbContext;
            _featureTogglesService = featureTogglesService;
        }

        public Task<GetDraftApprenticeshipsQueryResult> Handle(GetDraftApprenticeshipsQuery query, CancellationToken cancellationToken)
        {
            var cohort = _dbContext.Value.Cohorts
                .Include(x => x.Apprenticeships).ThenInclude(x => x.FlexibleEmployment)
                .Include(x => x.Apprenticeships).ThenInclude(x => x.PriorLearning)
                .Where(x => x.Id == query.CohortId)
                .Select(x => new { DraftApprenticeships = x.Apprenticeships})
                .SingleOrDefault();

            var isRplRequired = _featureTogglesService.GetFeatureToggle(Constants.RecognitionOfPriorLearningFeature).IsEnabled;

            return Task.FromResult(new GetDraftApprenticeshipsQueryResult
            {
                DraftApprenticeships = cohort?.DraftApprenticeships.Select(a => new DraftApprenticeshipDto
                {
                    Id = a.Id,
                    FirstName = a.FirstName,
                    LastName = a.LastName,
                    Email = a.Email,
                    Cost = (int?) a.Cost,
                    CourseCode = a.CourseCode,
                    CourseName = a.CourseName,
                    DeliveryModel = a.DeliveryModel ?? DeliveryModel.Regular,
                    DateOfBirth =  a.DateOfBirth,
                    StartDate = a.StartDate,
                    EndDate = a.EndDate,
                    Uln = a.Uln,
                    OriginalStartDate = a.OriginalStartDate,
                    EmploymentEndDate = a.FlexibleEmployment?.EmploymentEndDate,
                    EmploymentPrice = a.FlexibleEmployment?.EmploymentPrice,
                    RecognisePriorLearning = a.RecognisePriorLearning,
                    DurationReducedBy = a.PriorLearning?.DurationReducedBy,
                    PriceReducedBy = a.PriorLearning?.PriceReducedBy,
                    RecognisingPriorLearningStillNeedsToBeConsidered = !isRplRequired && a.RecognisingPriorLearningStillNeedsToBeConsidered
                }).ToList()
            });
        }
    }
}<commit_msg>fix bug to ensure the RPL doesn't need to be considered<commit_after>﻿using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.EntityFrameworkCore;
using SFA.DAS.Authorization.Features.Models;
using SFA.DAS.Authorization.Features.Services;
using SFA.DAS.CommitmentsV2.Data;
using SFA.DAS.CommitmentsV2.Domain;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.CommitmentsV2.Types.Dtos;

namespace SFA.DAS.CommitmentsV2.Application.Queries.GetDraftApprenticeships
{
    public class GetDraftApprenticeshipsQueryHandler : IRequestHandler<GetDraftApprenticeshipsQuery, GetDraftApprenticeshipsQueryResult>
    {
        private readonly Lazy<ProviderCommitmentsDbContext> _dbContext;
        private readonly IFeatureTogglesService<FeatureToggle> _featureTogglesService;

        public GetDraftApprenticeshipsQueryHandler(Lazy<ProviderCommitmentsDbContext> dbContext, IFeatureTogglesService<FeatureToggle> featureTogglesService)
        {
            _dbContext = dbContext;
            _featureTogglesService = featureTogglesService;
        }

        public Task<GetDraftApprenticeshipsQueryResult> Handle(GetDraftApprenticeshipsQuery query, CancellationToken cancellationToken)
        {
            var cohort = _dbContext.Value.Cohorts
                .Include(x => x.Apprenticeships).ThenInclude(x => x.FlexibleEmployment)
                .Include(x => x.Apprenticeships).ThenInclude(x => x.PriorLearning)
                .Where(x => x.Id == query.CohortId)
                .Select(x => new { DraftApprenticeships = x.Apprenticeships})
                .SingleOrDefault();

            var isRplRequired = _featureTogglesService.GetFeatureToggle(Constants.RecognitionOfPriorLearningFeature).IsEnabled;

            return Task.FromResult(new GetDraftApprenticeshipsQueryResult
            {
                DraftApprenticeships = cohort?.DraftApprenticeships.Select(a => new DraftApprenticeshipDto
                {
                    Id = a.Id,
                    FirstName = a.FirstName,
                    LastName = a.LastName,
                    Email = a.Email,
                    Cost = (int?) a.Cost,
                    CourseCode = a.CourseCode,
                    CourseName = a.CourseName,
                    DeliveryModel = a.DeliveryModel ?? DeliveryModel.Regular,
                    DateOfBirth =  a.DateOfBirth,
                    StartDate = a.StartDate,
                    EndDate = a.EndDate,
                    Uln = a.Uln,
                    OriginalStartDate = a.OriginalStartDate,
                    EmploymentEndDate = a.FlexibleEmployment?.EmploymentEndDate,
                    EmploymentPrice = a.FlexibleEmployment?.EmploymentPrice,
                    RecognisePriorLearning = a.RecognisePriorLearning,
                    DurationReducedBy = a.PriorLearning?.DurationReducedBy,
                    PriceReducedBy = a.PriorLearning?.PriceReducedBy,
                    RecognisingPriorLearningStillNeedsToBeConsidered = isRplRequired && a.RecognisingPriorLearningStillNeedsToBeConsidered
                }).ToList()
            });
        }
    }
}
{'fraction_non_alphanumeric': 0.07308121908409128, 'fraction_numerical': 0.0015956598053295037, 'mean_word_length': 2.378975741239892, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5634571', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1680, 'n_words': 256}