private async Task <int> ValidateCandidateMatch(SpecimenPotentialMatchSelection selection) { // Prefix ModelState endsWith substring with '.' to avoid catching x.ManualNotificationId var candidateMatchModelStateKey = ModelState.Keys.SingleOrDefault(key => key.EndsWith("." + nameof(SpecimenPotentialMatchSelection.NotificationId))); var notificationId = selection.NotificationId.GetValueOrDefault(); var notification = await _notificationRepository.GetNotifiedNotificationAsync(notificationId); if (notification == null) { throw new DataException( "When performing a specimen match via `/LabResults`, a candidate match was not found in the ntbs database."); } var(permissionLevel, _) = await _authorizationService.GetPermissionLevelAsync(User, notification); if (permissionLevel != PermissionLevel.Edit) { ModelState.AddModelError(candidateMatchModelStateKey, ValidationMessages.LabResultNotificationMatchNoPermission); } return(notificationId); }
private async Task <int> ValidateManualMatch(SpecimenPotentialMatchSelection value) { var manualMatchModelStateKey = ModelState.Keys.SingleOrDefault(key => key.EndsWith(nameof(SpecimenPotentialMatchSelection.ManualNotificationId))); var notificationId = int.Parse(value.ManualNotificationId); var notification = await _notificationRepository.GetNotifiedNotificationAsync(notificationId); if (notification == null) { ModelState.AddModelError(manualMatchModelStateKey, ValidationMessages.LabResultNotificationDoesNotExist); } else { var(permissionLevel, _) = await _authorizationService.GetPermissionLevelAsync(User, notification); if (permissionLevel != PermissionLevel.Edit) { ModelState.AddModelError(manualMatchModelStateKey, ValidationMessages.LabResultNotificationMatchNoPermission); } } return(notificationId); }
public async Task <IActionResult> OnGetPotentialMatchDataAsync(string manualNotificationId) { var dynamicModel = new SpecimenPotentialMatchSelection { ManualNotificationId = manualNotificationId }; const string dynamicValidationModelName = "DynamicModel"; const string propertyName = nameof(SpecimenPotentialMatchSelection.ManualNotificationId); var combinedModelPropertyKey = $"{dynamicValidationModelName}.{propertyName}"; ValidationService.ValidateProperty(dynamicModel, dynamicValidationModelName, manualNotificationId, propertyName); if (!ValidationService.IsValid(combinedModelPropertyKey)) { var errorMessage = ModelState[combinedModelPropertyKey].Errors.First().ErrorMessage; return(new JsonResult(new { errorMessage })); } if (string.IsNullOrWhiteSpace(manualNotificationId)) { return(new JsonResult(new {})); } var parsedManualNotificationId = int.Parse(manualNotificationId); var notification = await _notificationRepository.GetNotifiedNotificationAsync(parsedManualNotificationId); if (notification == null) { return(new JsonResult(new { errorMessage = ValidationMessages.LabResultNotificationDoesNotExist })); } var(permissionLevel, _) = await _authorizationService.GetPermissionLevelAsync(User, notification); if (permissionLevel != PermissionLevel.Edit) { return(new JsonResult(new { errorMessage = ValidationMessages.LabResultNotificationMatchNoPermission })); } var potentialMatch = MapNotificationToSpecimenPotentialMatch(notification); return(new PartialViewResult { ViewName = "_labResultsDemographics", ViewData = new ViewDataDictionary <SpecimenPotentialMatch>(ViewData, potentialMatch) }); }