Beispiel #1
0
        public async Task <OneOf <ModelWithErrors <ViewModel>, ViewModel> > Handle(
            Query request,
            CancellationToken cancellationToken)
        {
            ValidateFlowState();

            var providerId = _providerContextProvider.GetProviderId();

            ViewModel vm = await CreateViewModel(providerId);

            var validator        = new CompleteValidator();
            var validationResult = await validator.ValidateAsync(_flow.State);

            if (!validationResult.IsValid)
            {
                return(new ModelWithErrors <ViewModel>(vm, validationResult));
            }
            else
            {
                return(vm);
            }
        }
Beispiel #2
0
        public async Task <OneOf <ModelWithErrors <ViewModel>, Success> > Handle(
            CompleteCommand request,
            CancellationToken cancellationToken)
        {
            ValidateFlowState();

            var providerId = request.ProviderId;
            var provider   = await _providerInfoCache.GetProviderInfo(providerId);

            var validator        = new CompleteValidator();
            var validationResult = await validator.ValidateAsync(_flow.State);

            if (!validationResult.IsValid)
            {
                var vm = await CreateViewModel(provider);

                return(new ModelWithErrors <ViewModel>(vm, validationResult));
            }

            var currentUser = _currentUserProvider.GetCurrentUser();
            var now         = _clock.UtcNow;

            var apprenticeshipId = _flow.State.ApprenticeshipId.GetValueOrDefault(Guid.NewGuid());
            var providerUkprn    = provider.Ukprn;

            var providerVenues = await _cosmosDbQueryDispatcher.ExecuteQuery(
                new GetVenuesByProvider()
            {
                ProviderUkprn = providerUkprn
            });

            var locations = CreateLocations(providerVenues, providerUkprn);

            if (_flow.State.ApprenticeshipId.HasValue)
            {
                // Update existing apprenticeship
                await _cosmosDbQueryDispatcher.ExecuteQuery(
                    new UpdateApprenticeship()
                {
                    ApprenticeshipLocations = locations,
                    ApprenticeshipTitle     = _flow.State.ApprenticeshipStandardOrFramework.StandardOrFrameworkTitle,
                    ApprenticeshipType      = _flow.State.ApprenticeshipStandardOrFramework.IsStandard ?
                                              ApprenticeshipType.StandardCode :
                                              ApprenticeshipType.FrameworkCode,
                    ContactEmail     = _flow.State.ApprenticeshipContactEmail,
                    ContactTelephone = _flow.State.ApprenticeshipContactTelephone,
                    ContactWebsite   = _flow.State.ApprenticeshipContactWebsite,
                    UpdatedBy        = currentUser,
                    UpdatedDate      = now,
                    Id = _flow.State.ApprenticeshipId.Value,
                    MarketingInformation = _flow.State.ApprenticeshipMarketingInformation,
                    ProviderUkprn        = providerUkprn,
                    StandardOrFramework  = _flow.State.ApprenticeshipStandardOrFramework,
                    Url = _flow.State.ApprenticeshipWebsite
                });
            }
            else
            {
                // Create apprenticeship
                await _cosmosDbQueryDispatcher.ExecuteQuery(
                    new CreateApprenticeship()
                {
                    ApprenticeshipLocations = locations,
                    ApprenticeshipTitle     = _flow.State.ApprenticeshipStandardOrFramework.StandardOrFrameworkTitle,
                    ApprenticeshipType      = _flow.State.ApprenticeshipStandardOrFramework.IsStandard ?
                                              ApprenticeshipType.StandardCode :
                                              ApprenticeshipType.FrameworkCode,
                    ContactEmail     = _flow.State.ApprenticeshipContactEmail,
                    ContactTelephone = _flow.State.ApprenticeshipContactTelephone,
                    ContactWebsite   = _flow.State.ApprenticeshipContactWebsite,
                    CreatedByUser    = currentUser,
                    CreatedDate      = now,
                    Id = apprenticeshipId,
                    MarketingInformation = _flow.State.ApprenticeshipMarketingInformation,
                    ProviderId           = request.ProviderId,
                    ProviderUkprn        = providerUkprn,
                    StandardOrFramework  = _flow.State.ApprenticeshipStandardOrFramework,
                    Url    = _flow.State.ApprenticeshipWebsite,
                    Status = 2      // Pending
                });
            }

            // Create QA submission
            await _sqlQueryDispatcher.ExecuteQuery(
                new CreateApprenticeshipQASubmission()
            {
                ProviderId      = request.ProviderId,
                Apprenticeships = new List <CreateApprenticeshipQASubmissionApprenticeship>()
                {
                    new CreateApprenticeshipQASubmissionApprenticeship()
                    {
                        ApprenticeshipId = apprenticeshipId,
                        ApprenticeshipMarketingInformation = _flow.State.ApprenticeshipMarketingInformation,
                        ApprenticeshipTitle = _flow.State.ApprenticeshipStandardOrFramework.StandardOrFrameworkTitle,
                        Locations           = locations.Select(l => l.ApprenticeshipLocationType switch
                        {
                            ApprenticeshipLocationType.ClassroomBased => new CreateApprenticeshipQASubmissionApprenticeshipLocation(
                                new CreateApprenticeshipQASubmissionApprenticeshipClassroomLocation()
                            {
                                DeliveryModes = l.DeliveryModes,
                                Radius        = l.Radius.Value,
                                VenueName     = providerVenues.Single(v => v.Id == l.VenueId).VenueName
                            }),
                            ApprenticeshipLocationType.EmployerBased => new CreateApprenticeshipQASubmissionApprenticeshipLocation(
                                l.National == true ?
                                new CreateApprenticeshipQASubmissionApprenticeshipEmployerLocation(new National()) :
                                new CreateApprenticeshipQASubmissionApprenticeshipEmployerLocation(
                                    new CreateApprenticeshipQASubmissionApprenticeshipEmployerLocationRegions()
                            {
                                SubRegionIds = l.Regions.ToList()
                            })),
                            _ => throw new NotSupportedException()
                        }).ToList()