private DateTimeViewModel DetermineChangeDate(ChangeStatusType changeType, Apprenticeship apprenticeship, WhenToMakeChangeOptions whenToMakeChange, DateTime?dateOfChange)
        {
            if (changeType == ChangeStatusType.Pause || changeType == ChangeStatusType.Resume)
            {
                return(new DateTimeViewModel(_currentDateTime.Now.Date));
            }

            if (apprenticeship.IsWaitingToStart(_currentDateTime))
            {
                return(new DateTimeViewModel(apprenticeship.StartDate));
            }

            if (whenToMakeChange == WhenToMakeChangeOptions.Immediately)
            {
                return(new DateTimeViewModel(_currentDateTime.Now.Date));
            }

            return(new DateTimeViewModel(dateOfChange));
        }
        public async Task <OrchestratorResponse <WhenToMakeChangeViewModel> > GetChangeStatusDateOfChangeViewModel(
            string hashedAccountId, string hashedApprenticeshipId,
            ChangeStatusType changeType, string externalUserId)
        {
            var accountId        = HashingService.DecodeValue(hashedAccountId);
            var apprenticeshipId = HashingService.DecodeValue(hashedApprenticeshipId);

            Logger.Info(
                $"Determining navigation for type of change status selection. AccountId: {accountId}, ApprenticeshipId: {apprenticeshipId}");

            return(await CheckUserAuthorization(async() =>
            {
                var data =
                    await
                    Mediator.SendAsync(new GetApprenticeshipQueryRequest
                {
                    AccountId = accountId,
                    ApprenticeshipId = apprenticeshipId
                });

                CheckApprenticeshipStateValidForChange(data.Apprenticeship);

                return new OrchestratorResponse <WhenToMakeChangeViewModel>
                {
                    Data = new WhenToMakeChangeViewModel
                    {
                        ApprenticeStartDate = data.Apprenticeship.StartDate.Value,
                        SkipToConfirmationPage = CanSkipToConfirmationPage(changeType, data),
                        SkipToMadeRedundantQuestion = CanSkipToAskRedundancyQuestion(changeType, data),
                        ChangeStatusViewModel = new ChangeStatusViewModel
                        {
                            ChangeType = changeType
                        },
                        ApprenticeshipULN = data.Apprenticeship.ULN,
                        ApprenticeshipName = data.Apprenticeship.ApprenticeshipName,
                        TrainingName = data.Apprenticeship.TrainingName
                    }
                };
            }, hashedAccountId, externalUserId));
        }
        public async Task <OrchestratorResponse <RedundantApprenticeViewModel> > GetRedundantViewModel(
            string hashedAccountId, string hashedApprenticeshipId, ChangeStatusType changeType, DateTime?dateOfChange, WhenToMakeChangeOptions whenToMakeChange, string externalUserId, bool?madeRedundant)
        {
            var accountId        = HashingService.DecodeValue(hashedAccountId);
            var apprenticeshipId = HashingService.DecodeValue(hashedApprenticeshipId);

            Logger.Info(
                $"Determining navigation for type of change status selection. AccountId: {accountId}, ApprenticeshipId: {apprenticeshipId}");

            return(await CheckUserAuthorization(async() =>
            {
                var data =
                    await
                    Mediator.SendAsync(new GetApprenticeshipQueryRequest
                {
                    AccountId = accountId,
                    ApprenticeshipId = apprenticeshipId
                });

                CheckApprenticeshipStateValidForChange(data.Apprenticeship);

                return new OrchestratorResponse <RedundantApprenticeViewModel>
                {
                    Data = new RedundantApprenticeViewModel
                    {
                        ApprenticeshipName = data.Apprenticeship.ApprenticeshipName,
                        ChangeStatusViewModel = new ChangeStatusViewModel()
                        {
                            DateOfChange = DetermineChangeDate(changeType, data.Apprenticeship, whenToMakeChange, dateOfChange),
                            ChangeType = changeType,
                            WhenToMakeChange = whenToMakeChange,
                            ChangeConfirmed = false,
                            StartDate = data.Apprenticeship.StartDate,
                            MadeRedundant = madeRedundant
                        }
                    }
                };
            }, hashedAccountId, externalUserId));
        }
        public async Task <OrchestratorResponse <ConfirmationStateChangeViewModel> > GetChangeStatusConfirmationViewModel(string hashedAccountId, string hashedApprenticeshipId, ChangeStatusType changeType, WhenToMakeChangeOptions whenToMakeChange, DateTime?dateOfChange, bool?madeRedundant, string externalUserId)
        {
            var accountId        = HashingService.DecodeValue(hashedAccountId);
            var apprenticeshipId = HashingService.DecodeValue(hashedApprenticeshipId);

            Logger.Info(
                $"Getting Change Status Confirmation ViewModel. AccountId: {accountId}, ApprenticeshipId: {apprenticeshipId}, ChangeType: {changeType}");

            return(await CheckUserAuthorization(async() =>
            {
                var data =
                    await
                    Mediator.SendAsync(new GetApprenticeshipQueryRequest
                {
                    AccountId = accountId,
                    ApprenticeshipId = apprenticeshipId
                });

                CheckApprenticeshipStateValidForChange(data.Apprenticeship);

                var result = new OrchestratorResponse <ConfirmationStateChangeViewModel>
                {
                    Data = new ConfirmationStateChangeViewModel
                    {
                        ApprenticeName = data.Apprenticeship.ApprenticeshipName,
                        ApprenticeULN = data.Apprenticeship.ULN,
                        DateOfBirth = data.Apprenticeship.DateOfBirth.Value,
                        ApprenticeCourse = data.Apprenticeship.TrainingName,
                        ViewTransactionsLink = _linkGenerator.FinanceLink($"accounts/{hashedAccountId}/finance/{_currentDateTime.Now.Year}/{_currentDateTime.Now.Month}"),

                        ChangeStatusViewModel = new ChangeStatusViewModel
                        {
                            DateOfChange = DetermineChangeDate(changeType, data.Apprenticeship, whenToMakeChange, dateOfChange),
                            ChangeType = changeType,
                            WhenToMakeChange = whenToMakeChange,
                            ChangeConfirmed = false,
                            StartDate = data.Apprenticeship.StartDate,
                            MadeRedundant = madeRedundant
                        }
                    }
                };

                var notResuming = changeType != ChangeStatusType.Resume;

                if (notResuming)
                {
                    return result;
                }

                result.Data.ChangeStatusViewModel.PauseDate = new DateTimeViewModel(data.Apprenticeship.PauseDate, 90);


                if (data.Apprenticeship.IsWaitingToStart(_currentDateTime))
                {
                    result.Data.ChangeStatusViewModel.DateOfChange = new DateTimeViewModel(_currentDateTime.Now.Date, 90);
                    return result;
                }

                result.Data.ChangeStatusViewModel.DateOfChange = new DateTimeViewModel(data.Apprenticeship.PauseDate.Value, 90);

                var mustInvokeAcademicYearFundingRule = _academicYearValidator.Validate(data.Apprenticeship.PauseDate.Value) == AcademicYearValidationResult.NotWithinFundingPeriod;

                if (!mustInvokeAcademicYearFundingRule)
                {
                    return result;
                }

                result.Data.ChangeStatusViewModel.AcademicYearBreakInTraining = true;

                result.Data.ChangeStatusViewModel.DateOfChange = new DateTimeViewModel(_academicYearDateProvider.CurrentAcademicYearStartDate, 90);


                return result;
            }, hashedAccountId, externalUserId));
        }
 private bool CanSkipToAskRedundancyQuestion(ChangeStatusType changeType, GetApprenticeshipQueryResponse data)
 {
     return(changeType == ChangeStatusType.Stop && data.Apprenticeship.IsWaitingToStart(_currentDateTime));
 }
 private bool CanSkipToConfirmationPage(ChangeStatusType changeType, GetApprenticeshipQueryResponse data)
 {
     return(data.Apprenticeship.PaymentStatus == PaymentStatus.Paused && changeType == ChangeStatusType.Resume || // Resuming
            data.Apprenticeship.PaymentStatus == PaymentStatus.Active && changeType == ChangeStatusType.Pause); // Pausing
 }