public ActionResult VacancyApplications(VacancyApplicationsSearchViewModel vacancyApplicationsSearch)
        {
            var response = _applicationMediator.GetVacancyApplicationsViewModel(vacancyApplicationsSearch);

            if (response.Message != null)
            {
                SetUserMessage(response.Message.Text, response.Message.Level);
            }
            switch (response.Code)
            {
            case ApplicationMediatorCodes.GetVacancyApplicationsViewModel.Ok:
                return(View(response.ViewModel));

            default:
                throw new InvalidMediatorCodeException(response.Code);
            }
        }
        private List <ApplicationSummary> SearchCandidateApplications(VacancyApplicationsSearchViewModel vacancyApplicationsSearch, List <ApplicationSummary> applications)
        {
            if (!vacancyApplicationsSearch.IsCandidateSearch())
            {
                return(applications);
            }

            var applicantId = CandidateSearchExtensions.GetCandidateId(vacancyApplicationsSearch.ApplicantId);

            if (applicantId.HasValue)
            {
                var candidate = _candidateApplicationService.GetCandidate(applicantId.Value, false);
                if (candidate != null)
                {
                    return(applications.Where(a => a.CandidateId == candidate.EntityId).ToList());
                }
                return(new List <ApplicationSummary>());
            }

            var candidateGuidPrefix = CandidateSearchExtensions.GetCandidateGuidPrefix(vacancyApplicationsSearch.ApplicantId);

            if (!string.IsNullOrEmpty(candidateGuidPrefix))
            {
                return(applications.Where(a => a.CandidateId.ToString().StartsWith(candidateGuidPrefix, StringComparison.InvariantCultureIgnoreCase)).ToList());
            }

            if (!string.IsNullOrEmpty(vacancyApplicationsSearch.ApplicantId))
            {
                //Attempt to search by an unrecognised application id. Return no results
                return(new List <ApplicationSummary>());
            }

            return
                (applications.Where(
                     a =>
                     (string.IsNullOrEmpty(vacancyApplicationsSearch.FirstName) || a.CandidateDetails.FirstName.StartsWith(vacancyApplicationsSearch.FirstName, StringComparison.InvariantCultureIgnoreCase)) &&
                     (string.IsNullOrEmpty(vacancyApplicationsSearch.LastName) || a.CandidateDetails.LastName.StartsWith(vacancyApplicationsSearch.LastName, StringComparison.InvariantCultureIgnoreCase)) &&
                     (string.IsNullOrEmpty(vacancyApplicationsSearch.Postcode) || a.CandidateDetails.Address.Postcode.Replace(" ", "").StartsWith(vacancyApplicationsSearch.Postcode.Replace(" ", ""), StringComparison.InvariantCultureIgnoreCase))
                     ).ToList());
        }
        public VacancyApplicationsViewModel GetVacancyApplicationsViewModel(VacancyApplicationsSearchViewModel vacancyApplicationsSearch, bool applyPagination)
        {
            var vacancy = _vacancyPostingService.GetVacancyByReferenceNumber(vacancyApplicationsSearch.VacancyReferenceNumber);
            var vacancyOwnerRelationship = _providerService.GetVacancyOwnerRelationship(vacancy.VacancyOwnerRelationshipId, false);  // Closed vacancies can certainly have non-current vacancy parties
            var employer  = _employerService.GetEmployer(vacancyOwnerRelationship.EmployerId, false);
            var viewModel = _mapper.Map <Vacancy, VacancyApplicationsViewModel>(vacancy);

            viewModel.EmployerName     = employer.FullName;
            viewModel.EmployerGeoPoint = _mapper.Map <GeoPoint, GeoPointViewModel>(employer.Address.GeoPoint);

            var applications = vacancy.VacancyType == VacancyType.Traineeship
                ? _traineeshipApplicationService.GetSubmittedApplicationSummaries(vacancy.VacancyId).Select(a => (ApplicationSummary)a).ToList()
                : _apprenticeshipApplicationService.GetSubmittedApplicationSummaries(vacancy.VacancyId).Select(a => (ApplicationSummary)a).ToList();

            applications = SearchCandidateApplications(vacancyApplicationsSearch, applications);

            var @new         = applications.Where(v => v.Status == ApplicationStatuses.Submitted).ToList();
            var inProgress   = applications.Where(v => v.Status == ApplicationStatuses.InProgress).ToList();
            var successful   = applications.Where(v => v.Status == ApplicationStatuses.Successful).ToList();
            var unsuccessful = applications.Where(v => v.Status == ApplicationStatuses.Unsuccessful).ToList();

            switch (vacancyApplicationsSearch.FilterType)
            {
            case VacancyApplicationsFilterTypes.New:
                applications = @new;
                break;

            case VacancyApplicationsFilterTypes.InProgress:
                applications = inProgress;
                break;

            case VacancyApplicationsFilterTypes.Successful:
                applications = successful;
                break;

            case VacancyApplicationsFilterTypes.Unsuccessful:
                applications = unsuccessful;
                break;
            }

            //TODO: return as part of data query - probably needs migration
            viewModel.NewApplicationsCount          = @new.Count;
            viewModel.InProgressApplicationsCount   = inProgress.Count;
            viewModel.SuccessfulApplicationsCount   = successful.Count;
            viewModel.UnsuccessfulApplicationsCount = unsuccessful.Count;

            var page = GetOrderedApplicationSummaries(vacancyApplicationsSearch.OrderByField, vacancyApplicationsSearch.Order, applications);

            if (applyPagination)
            {
                page = page.Skip((vacancyApplicationsSearch.CurrentPage - 1) * vacancyApplicationsSearch.PageSize).Take(vacancyApplicationsSearch.PageSize);
            }

            viewModel.VacancyApplicationsSearch = vacancyApplicationsSearch;
            viewModel.ApplicationSummaries      = new PageableViewModel <ApplicationSummaryViewModel>
            {
                Page               = page.Select(_mapper.Map <ApplicationSummary, ApplicationSummaryViewModel>).ToList(),
                ResultsCount       = applications.Count,
                CurrentPage        = vacancyApplicationsSearch.CurrentPage,
                TotalNumberOfPages = applications.Count == 0 ? 1 : (int)Math.Ceiling((double)applications.Count / vacancyApplicationsSearch.PageSize)
            };

            IList <CandidateSummary> candidateSummaries = _candidateApplicationService.GetCandidateSummaries(viewModel.ApplicationSummaries.Page.Select(@as => @as.CandidateId));

            foreach (var application in viewModel.ApplicationSummaries.Page)
            {
                application.AnonymousLinkData =
                    _encryptionService.Encrypt(new AnonymisedApplicationLink(application.ApplicationId,
                                                                             _dateTimeService.TwoWeeksFromUtcNow));
                var candidateSummary = candidateSummaries.FirstOrDefault(cs => cs.EntityId == application.CandidateId);
                if (candidateSummary != null && candidateSummary.LegacyCandidateId != 0)
                {
                    var legacyCandidateReference = candidateSummary.LegacyCandidateId.ToString("0###-###-###").Substring(1, 11);
                    application.ApplicantID = $"{application.ApplicantID} ({legacyCandidateReference})";
                }
            }

            return(viewModel);
        }
 public VacancyApplicationsViewModel GetVacancyApplicationsViewModel(VacancyApplicationsSearchViewModel vacancyApplicationsSearch)
 {
     return(GetVacancyApplicationsViewModel(vacancyApplicationsSearch, true));
 }
        public MediatorResponse <VacancyApplicationsViewModel> GetVacancyApplicationsViewModel(VacancyApplicationsSearchViewModel vacancyApplicationsSearch)
        {
            var messages  = new List <string>();
            var viewModel = _applicationProvider.GetVacancyApplicationsViewModel(vacancyApplicationsSearch);

            if (viewModel.Status == VacancyStatus.Closed)
            {
                messages.Add(VacancyViewModelMessages.Closed);
                return(GetMediatorResponse(ApplicationMediatorCodes.GetVacancyApplicationsViewModel.Ok, viewModel, messages, UserMessageLevel.Info));
            }
            return(GetMediatorResponse(ApplicationMediatorCodes.GetVacancyApplicationsViewModel.Ok, viewModel));
        }