protected override async Task ValidateAndSave() { UpdateFlags(); SetDatesOnClinicalDetails(); if (ClinicalDetails.BCGVaccinationYear != null) { ValidationService.ValidateYearComparison(ClinicalDetails, nameof(ClinicalDetails.BCGVaccinationYear), (int)ClinicalDetails.BCGVaccinationYear, PatientBirthYear); } var notificationSites = CreateNotificationSitesFromModel(Notification).ToList(); // Add additional field required for date validation ClinicalDetails.Dob = Notification.PatientDetails.Dob; ClinicalDetails.SetValidationContext(Notification); OtherSite?.SetValidationContext(Notification); // Since notification has other properties which are not populated by this page but have validation rules, // validation of a whole Notification model will result in validation errors. // Therefore we need to manually validate NotificationSites and TryValidate other models if (Notification.ShouldValidateFull && !notificationSites.Any()) { ModelState.AddModelError("Notification.NotificationSites", ValidationMessages.DiseaseSiteIsRequired); } TryValidateModel(ClinicalDetails, nameof(ClinicalDetails)); if (OtherSite != null) { TryValidateModel(OtherSite, nameof(OtherSite)); } var mdrChanged = Notification.ClinicalDetails.TreatmentRegimen != ClinicalDetails.TreatmentRegimen; var nonMdrNotAllowed = !ClinicalDetails.IsMDRTreatment && Notification.MdrDetailsEntered; if (mdrChanged && nonMdrNotAllowed) { ModelState.AddModelError("ClinicalDetails.IsMDRTreatment", ValidationMessages.MDRCantChange); } AddPostMortemErrorsIfApplicable(); if (ModelState.IsValid) { await Service.UpdateClinicalDetailsAsync(Notification, ClinicalDetails); await Service.UpdateSitesAsync(Notification.NotificationId, notificationSites); if (mdrChanged) { await _enhancedSurveillanceAlertsService.CreateOrDismissMdrAlert(Notification); } } }
protected override async Task ValidateAndSave() { UpdateFlags(); new List <(string key, FormattedDate date)> { (nameof(ClinicalDetails.SymptomStartDate), FormattedSymptomDate), (nameof(ClinicalDetails.FirstPresentationDate), FormattedFirstPresentationDate), (nameof(ClinicalDetails.TBServicePresentationDate), FormattedTbServicePresentationDate), (nameof(ClinicalDetails.DiagnosisDate), FormattedDiagnosisDate), (nameof(ClinicalDetails.TreatmentStartDate), FormattedTreatmentDate), (nameof(ClinicalDetails.FirstHomeVisitDate), FormattedHomeVisitDate), (nameof(ClinicalDetails.MDRTreatmentStartDate), FormattedMdrTreatmentDate) }.ForEach(item => ValidationService.TrySetFormattedDate(ClinicalDetails, "ClinicalDetails", item.key, item.date) ); if (ClinicalDetails.BCGVaccinationYear != null) { ValidationService.ValidateYearComparison(ClinicalDetails, nameof(ClinicalDetails.BCGVaccinationYear), (int)ClinicalDetails.BCGVaccinationYear, PatientBirthYear); } var notificationSites = CreateNotificationSitesFromModel(Notification).ToList(); // Add additional field required for date validation ClinicalDetails.Dob = Notification.PatientDetails.Dob; ClinicalDetails.SetValidationContext(Notification); OtherSite?.SetValidationContext(Notification); // Since notification has other properties which are not populated by this page but have validation rules, // validation of a whole Notification model will result in validation errors. // Therefore we need to manually validate NotificationSites and TryValidate other models if (Notification.ShouldValidateFull && !notificationSites.Any()) { ModelState.AddModelError("Notification.NotificationSites", ValidationMessages.DiseaseSiteIsRequired); } TryValidateModel(ClinicalDetails, nameof(ClinicalDetails)); if (OtherSite != null) { TryValidateModel(OtherSite, nameof(OtherSite)); } var mdrChanged = Notification.ClinicalDetails.TreatmentRegimen != ClinicalDetails.TreatmentRegimen; var nonMdrNotAllowed = !ClinicalDetails.IsMDRTreatment && Notification.MDRDetails.MDRDetailsEntered; if (mdrChanged && nonMdrNotAllowed) { ModelState.AddModelError("ClinicalDetails.IsMDRTreatment", ValidationMessages.MDRCantChange); } if (ModelState.IsValid) { await Service.UpdateClinicalDetailsAsync(Notification, ClinicalDetails); await Service.UpdateSitesAsync(Notification.NotificationId, notificationSites); if (mdrChanged) { await _enhancedSurveillanceAlertsService.CreateOrDismissMdrAlert(Notification); } } }