From 50fa128e44d3323592e0f5393aeb3ced7cdd6936 Mon Sep 17 00:00:00 2001 From: KobithasanVasantharajah Date: Tue, 17 Dec 2024 11:47:32 +0000 Subject: [PATCH] fix(register) : update secondary contact validation to work in edit flow --- .../Controllers/CabProviderController.cs | 155 ++++++++++-------- 1 file changed, 84 insertions(+), 71 deletions(-) diff --git a/DVSRegister/Controllers/CabProviderController.cs b/DVSRegister/Controllers/CabProviderController.cs index e833e09..f1045e7 100644 --- a/DVSRegister/Controllers/CabProviderController.cs +++ b/DVSRegister/Controllers/CabProviderController.cs @@ -676,59 +676,66 @@ public async Task EditPrimaryContact(int providerId) [HttpPost("edit-primary-contact")] public async Task UpdatePrimaryContact(PrimaryContactViewModel primaryContactViewModel) - { - ProfileSummaryViewModel profileSummary = GetProfileSummary(); - + { + int cabId = Convert.ToInt32(HttpContext?.Session.Get("CabId")); + if (cabId <= 0 || primaryContactViewModel.ProviderId <= 0) + { + return RedirectToAction("CabHandleException", "Error"); + } + + // Fetch the latest provider data from the database + ProviderProfileDto providerProfileDto = await cabService.GetProvider(primaryContactViewModel.ProviderId, cabId); + if (providerProfileDto == null) + { + return RedirectToAction("CabHandleException", "Error"); + } + ValidationHelper.ValidateDuplicateFields( - ModelState, - primaryContactViewModel.PrimaryContactEmail, - profileSummary.SecondaryContact?.SecondaryContactEmail, + ModelState, + primaryValue: primaryContactViewModel.PrimaryContactEmail, + secondaryValue: providerProfileDto.SecondaryContactEmail, new ValidationHelper.FieldComparisonConfig( - "PrimaryContactEmail", + "PrimaryContactEmail", "SecondaryContactEmail", "Email address of secondary contact cannot be the same as primary contact" - ) - ); + ) + ); ValidationHelper.ValidateDuplicateFields( - ModelState, - primaryValue: primaryContactViewModel.PrimaryContactTelephoneNumber, - secondaryValue: profileSummary.SecondaryContact?.SecondaryContactTelephoneNumber, + ModelState, + primaryValue: primaryContactViewModel.PrimaryContactTelephoneNumber, + secondaryValue: providerProfileDto.SecondaryContactTelephoneNumber, new ValidationHelper.FieldComparisonConfig( - "PrimaryContactTelephoneNumber", + "PrimaryContactTelephoneNumber", "SecondaryContactTelephoneNumber", "Telephone number of secondary contact cannot be the same as primary contact" - ) - ); - - if (ModelState.IsValid) - { - ProviderProfileDto providerProfileDto = new() - { - PrimaryContactFullName = primaryContactViewModel.PrimaryContactFullName, - PrimaryContactEmail = primaryContactViewModel.PrimaryContactEmail, - PrimaryContactJobTitle = primaryContactViewModel.PrimaryContactJobTitle, - PrimaryContactTelephoneNumber = primaryContactViewModel.PrimaryContactTelephoneNumber, - Id = primaryContactViewModel.ProviderId - }; - - GenericResponse genericResponse = await cabService.UpdatePrimaryContact(providerProfileDto, UserEmail); - if (genericResponse.Success) - { - return RedirectToAction("ProviderProfileDetails", "Cab", - new { providerId = providerProfileDto.Id }); + ) + ); + + if (ModelState.IsValid) + { + providerProfileDto.PrimaryContactFullName = primaryContactViewModel.PrimaryContactFullName; + providerProfileDto.PrimaryContactEmail = primaryContactViewModel.PrimaryContactEmail; + providerProfileDto.PrimaryContactJobTitle = primaryContactViewModel.PrimaryContactJobTitle; + providerProfileDto.PrimaryContactTelephoneNumber = primaryContactViewModel.PrimaryContactTelephoneNumber; + + GenericResponse genericResponse = await cabService.UpdatePrimaryContact(providerProfileDto, UserEmail); + if (genericResponse.Success) + { + return RedirectToAction("ProviderProfileDetails", "Cab", new { providerId = providerProfileDto.Id }); } else { - return RedirectToAction("CabHandleException", "Error"); + return RedirectToAction("CabHandleException", "Error"); } } else { - return View("EditPrimaryContact", primaryContactViewModel); + return View("EditPrimaryContact", primaryContactViewModel); } } + #endregion #region Edit secondary contact @@ -758,57 +765,63 @@ public async Task EditSecondaryContact(int providerId) } [HttpPost("edit-secondary-contact")] - public async Task UpdateSecondaryContact(SecondaryContactViewModel secondaryContactViewModel) - { - ProfileSummaryViewModel profileSummary = GetProfileSummary(); - + public async Task UpdateSecondaryContact(SecondaryContactViewModel secondaryContactViewModel) + { + int cabId = Convert.ToInt32(HttpContext?.Session.Get("CabId")); + if (cabId <= 0 || secondaryContactViewModel.ProviderId <= 0) + { + return RedirectToAction("CabHandleException", "Error"); + } + + // Fetch the latest provider data from the database + ProviderProfileDto providerProfileDto = await cabService.GetProvider(secondaryContactViewModel.ProviderId, cabId); + if (providerProfileDto == null) + { + return RedirectToAction("CabHandleException", "Error"); + } + ValidationHelper.ValidateDuplicateFields( - ModelState, - primaryValue: profileSummary.PrimaryContact?.PrimaryContactEmail, - secondaryValue: secondaryContactViewModel.SecondaryContactEmail, + ModelState, + primaryValue: providerProfileDto.PrimaryContactEmail, + secondaryValue: secondaryContactViewModel.SecondaryContactEmail, new ValidationHelper.FieldComparisonConfig( - "PrimaryContactEmail", + "PrimaryContactEmail", "SecondaryContactEmail", "Email address of secondary contact cannot be the same as primary contact" - ) - ); - + ) + ); + ValidationHelper.ValidateDuplicateFields( - ModelState, - primaryValue: profileSummary.PrimaryContact?.PrimaryContactTelephoneNumber, - secondaryValue: secondaryContactViewModel.SecondaryContactTelephoneNumber, + ModelState, + primaryValue: providerProfileDto.PrimaryContactTelephoneNumber, + secondaryValue: secondaryContactViewModel.SecondaryContactTelephoneNumber, new ValidationHelper.FieldComparisonConfig( - "PrimaryContactTelephoneNumber", + "PrimaryContactTelephoneNumber", "SecondaryContactTelephoneNumber", "Telephone number of secondary contact cannot be the same as primary contact" - ) - ); - - if (ModelState.IsValid) - { - ProviderProfileDto providerProfileDto = new() - { - SecondaryContactFullName = secondaryContactViewModel.SecondaryContactFullName, - SecondaryContactEmail = secondaryContactViewModel.SecondaryContactEmail, - SecondaryContactJobTitle = secondaryContactViewModel.SecondaryContactJobTitle, - SecondaryContactTelephoneNumber = secondaryContactViewModel.SecondaryContactTelephoneNumber, - Id = secondaryContactViewModel.ProviderId - }; - GenericResponse genericResponse = - await cabService.UpdateSecondaryContact(providerProfileDto, UserEmail); - if (genericResponse.Success) - { - return RedirectToAction("ProviderProfileDetails", "Cab", - new { providerId = providerProfileDto.Id }); + ) + ); + + if (ModelState.IsValid) + { + providerProfileDto.SecondaryContactFullName = secondaryContactViewModel.SecondaryContactFullName; + providerProfileDto.SecondaryContactEmail = secondaryContactViewModel.SecondaryContactEmail; + providerProfileDto.SecondaryContactJobTitle = secondaryContactViewModel.SecondaryContactJobTitle; + providerProfileDto.SecondaryContactTelephoneNumber = secondaryContactViewModel.SecondaryContactTelephoneNumber; + + GenericResponse genericResponse = await cabService.UpdateSecondaryContact(providerProfileDto, UserEmail); + if (genericResponse.Success) + { + return RedirectToAction("ProviderProfileDetails", "Cab", new { providerId = providerProfileDto.Id }); } - else + else { - return RedirectToAction("CabHandleException", "Error"); + return RedirectToAction("CabHandleException", "Error"); } } else { - return View("EditSecondaryContact", secondaryContactViewModel); + return View("EditSecondaryContact", secondaryContactViewModel); } }