Ejemplo n.º 1
0
        public async Task <IActionResult> SelectProgram(ProgramViewModel model)
        {
            var settings = await _performerSchedulingService.GetSettingsAsync();

            var schedulingStage = _performerSchedulingService.GetSchedulingStage(settings);

            if (schedulingStage != PsSchedulingStage.SchedulingOpen)
            {
                return(RedirectToAction(nameof(Index)));
            }

            PsProgram program;

            try
            {
                program = await _performerSchedulingService.GetProgramByIdAsync(
                    model.BranchSelection.ProgramId.Value, onlyApproved : true);
            }
            catch (GraException gex)
            {
                ShowAlertDanger("Unable to select program: ", gex);
                return(RedirectToAction(nameof(Programs)));
            }

            var ageAlreadySelected = await _performerSchedulingService
                                     .BranchAgeGroupAlreadySelectedAsync(model.BranchSelection.AgeGroupId,
                                                                         model.BranchSelection.BranchId);

            if (ageAlreadySelected)
            {
                ShowAlertDanger("Branch already has a selection for that age group.");
                return(RedirectToAction(nameof(Program), new { id = program.Id }));
            }

            var programAvailableAtBranch = await _performerSchedulingService
                                           .ProgramAvailableAtBranchAsync(model.BranchSelection.ProgramId.Value,
                                                                          model.BranchSelection.BranchId);

            if (!programAvailableAtBranch)
            {
                ShowAlertDanger("The performer does not performer at that branch.");
                return(RedirectToAction(nameof(Program), new { id = program.Id }));
            }

            var viewModel = new SelectProgramViewModel
            {
                BranchSelection = model.BranchSelection,
                BlackoutDates   = await _performerSchedulingService.GetBlackoutDatesAsync(),
                ScheduleDates   = await _performerSchedulingService.GetPerformerScheduleAsync(
                    program.PerformerId),
                BookedDates = (await _performerSchedulingService
                               .GetPerformerBranchSelectionsAsync(program.PerformerId))
                              .Select(_ => _.RequestedStartTime),
                Settings = settings
            };

            if (await _performerSchedulingService.BranchAgeGroupHasBackToBackAsync(
                    model.BranchSelection.AgeGroupId, model.BranchSelection.BranchId))
            {
                viewModel.BranchSelection.BackToBackProgram = true;
            }

            viewModel.BranchSelection.AgeGroup = await _performerSchedulingService
                                                 .GetAgeGroupByIdAsync(viewModel.BranchSelection.AgeGroupId);

            viewModel.BranchSelection.Branch = await _performerSchedulingService
                                               .GetNonExcludedBranch(model.BranchSelection.BranchId);

            viewModel.BranchSelection.Program = program;

            return(View(viewModel));
        }