public async Task <FlowModel> Initialize(Guid apprenticeshipId)
        {
            var maybeProviderId = await _providerOwnershipCache.GetProviderForApprenticeship(apprenticeshipId);

            if (!maybeProviderId.HasValue)
            {
                throw new InvalidStateException(InvalidStateReason.ApprenticeshipDoesNotExist);
            }

            var providerId = maybeProviderId.Value;

            var qaStatus = await _sqlQueryDispatcher.ExecuteQuery(
                new GetProviderApprenticeshipQAStatus()
            {
                ProviderId = providerId
            });

            var maybeLatestSubmission = await _sqlQueryDispatcher.ExecuteQuery(
                new GetLatestApprenticeshipQASubmissionForProvider()
            {
                ProviderId = providerId
            });

            if (maybeLatestSubmission.Value is None)
            {
                throw new InvalidStateException(InvalidStateReason.NoValidApprenticeshipQASubmission);
            }

            var latestSubmission = maybeLatestSubmission.AsT1;

            var submissionApprenticeship = latestSubmission.Apprenticeships
                                           .SingleOrDefault(a => a.ApprenticeshipId == apprenticeshipId);

            if (submissionApprenticeship == null)
            {
                throw new InvalidStateException(InvalidStateReason.NoValidApprenticeshipQASubmission);
            }

            var latestAssessment = await _sqlQueryDispatcher.ExecuteQuery(
                new GetLatestApprenticeshipQAApprenticeshipAssessmentForSubmission()
            {
                ApprenticeshipQASubmissionId = latestSubmission.ApprenticeshipQASubmissionId,
                ApprenticeshipId             = apprenticeshipId
            });

            return(new FlowModel()
            {
                ApprenticeshipId = apprenticeshipId,
                ProviderId = providerId,
                ComplianceComments = latestAssessment.Match(_ => string.Empty, v => v.ComplianceComments),
                ComplianceFailedReasons = latestAssessment.Match(_ => ApprenticeshipQAApprenticeshipComplianceFailedReasons.None, v => v.ComplianceFailedReasons),
                CompliancePassed = latestAssessment.Match(_ => null, v => v.CompliancePassed),
                StyleComments = latestAssessment.Match(_ => string.Empty, v => v.StyleComments),
                StyleFailedReasons = latestAssessment.Match(_ => ApprenticeshipQAApprenticeshipStyleFailedReasons.None, v => v.StyleFailedReasons),
                StylePassed = latestAssessment.Match(_ => null, v => v.StylePassed),
                IsReadOnly = !(qaStatus == ApprenticeshipQAStatus.Submitted || qaStatus == ApprenticeshipQAStatus.InProgress),
            });
        }
Example #2
0
        public async Task <JourneyModel> Initialize(Guid apprenticeshipId)
        {
            var maybeProviderId = await _providerOwnershipCache.GetProviderForApprenticeship(apprenticeshipId);

            if (!maybeProviderId.HasValue)
            {
                throw new ResourceDoesNotExistException(ResourceType.Apprenticeship, apprenticeshipId);
            }

            var providerId = maybeProviderId.Value;

            var qaStatus = await _sqlQueryDispatcher.ExecuteQuery(
                new GetProviderApprenticeshipQAStatus()
            {
                ProviderId = providerId
            });

            var latestSubmission = await _sqlQueryDispatcher.ExecuteQuery(
                new GetLatestApprenticeshipQASubmissionForProvider()
            {
                ProviderId = providerId
            });

            if (latestSubmission == null)
            {
                throw new InvalidStateException(InvalidStateReason.NoValidApprenticeshipQASubmission);
            }

            var submissionApprenticeship = latestSubmission.Apprenticeships
                                           .SingleOrDefault(a => a.ApprenticeshipId == apprenticeshipId);

            if (submissionApprenticeship == null)
            {
                throw new InvalidStateException(InvalidStateReason.NoValidApprenticeshipQASubmission);
            }

            var latestAssessment = await _sqlQueryDispatcher.ExecuteQuery(
                new GetLatestApprenticeshipQAApprenticeshipAssessmentForSubmission()
            {
                ApprenticeshipQASubmissionId = latestSubmission.ApprenticeshipQASubmissionId,
                ApprenticeshipId             = apprenticeshipId
            });

            return(new JourneyModel()
            {
                ApprenticeshipId = apprenticeshipId,
                ProviderId = providerId,
                ComplianceComments = latestAssessment?.ComplianceComments,
                ComplianceFailedReasons = latestAssessment?.ComplianceFailedReasons,
                CompliancePassed = latestAssessment?.CompliancePassed,
                StyleComments = latestAssessment?.StyleComments,
                StyleFailedReasons = latestAssessment?.StyleFailedReasons,
                StylePassed = latestAssessment?.StylePassed,
                IsReadOnly = !(qaStatus == ApprenticeshipQAStatus.Submitted || qaStatus == ApprenticeshipQAStatus.InProgress),
            });
        }