public ActionResult Create(Guid patientId) { var baseObject = GetBaseObject(); Application.Entities.Patient patient = _patientService.Get(patientId); IList <Application.EntityViews.MeasurementTool> measurementTools = _visitService.GetMeasurementToolsForVisit(patientId); IList <Application.EntityViews.TeamMember> teamMembers = _patientService.ListTeamMembers(patientId); IList <Application.EntityViews.CompletedMeasurementTool> completedMeasurementTools = _patientService.ListCompletedMeasurementTools(patient.Id, DateTime.Now.AddYears(-1), DateTime.Now); IList <Application.EntityViews.EpisodeOfCare> episodeOfCares = _patientService.ListEpisodesOfCare(patientId, baseObject.GetCurrentFacilityId().Value); if (!patient.CanAccess(baseObject.User, teamMembers, baseObject.User.Permissions)) { throw new BusinessRuleException("You are not autorized to view this patient"); } foreach (var measurementTool in measurementTools) { var temp = completedMeasurementTools.OrderByDescending(x => x.EndDate).FirstOrDefault(x => x.Id == measurementTool.Id); measurementTool.LastCompletedTimestamp = temp == null ? (DateTime?)null : temp.EndDate; } if (!patient.CanAccess(baseObject.User, teamMembers, baseObject.User.Permissions)) { throw new BusinessRuleException("You are not autorized to view this patient"); } return(View(new ViewObjects.Visit.Create(baseObject) { Visit = new Application.Entities.Visit() { PatientId = patientId, Timestamp = DateTime.Now, ImpairmentGroups = _listRepository.GetImpairmentGroups() }, EpisodeOfCare = episodeOfCares.FirstOrDefault(), Patient = patient, MeasurementTools = measurementTools, Surveys = _patientService.ListSurveys(patientId) })); }