diff --git a/LearningHub.Nhs.WebUI/Controllers/MyAccountController.cs b/LearningHub.Nhs.WebUI/Controllers/MyAccountController.cs index feebe7124..c4f549012 100644 --- a/LearningHub.Nhs.WebUI/Controllers/MyAccountController.cs +++ b/LearningHub.Nhs.WebUI/Controllers/MyAccountController.cs @@ -210,6 +210,37 @@ public async Task UpdatePersonalDetails(MyAccountPersonalDetailsV return this.View("ChangePersonalDetails", model); } + if (model.PrimaryEmailAddress.ToLower() == model.SecondaryEmailAddress?.ToLower()) + { + this.ModelState.AddModelError(nameof(model.SecondaryEmailAddress), CommonValidationErrorMessages.SecondaryEmailShouldNotBeSame); + return this.View("ChangePersonalDetails", model); + } + + bool userPrimaryEmailAddressChanged = false; + var user = await this.userService.GetUserByUserIdAsync(this.CurrentUserId); + if (user != null) + { + if (!string.IsNullOrEmpty(model.PrimaryEmailAddress) && user.EmailAddress.ToLower() != model.PrimaryEmailAddress.ToLower()) + { + userPrimaryEmailAddressChanged = true; + } + } + + if (userPrimaryEmailAddressChanged) + { + if (await this.userService.DoesEmailAlreadyExist(model.PrimaryEmailAddress)) + { + this.ModelState.AddModelError( + nameof(model.PrimaryEmailAddress), + CommonValidationErrorMessages.DuplicateEmailAddress); + return this.View("ChangePersonalDetails", model); + } + else + { + await this.MyAccountUpdatePrimaryEmail(user.EmailAddress, model.PrimaryEmailAddress); + } + } + bool isSamePrimaryEmailPendingforValidate = await this.userService.CheckSamePrimaryemailIsPendingToValidate(model.SecondaryEmailAddress); if (isSamePrimaryEmailPendingforValidate) { @@ -218,7 +249,6 @@ public async Task UpdatePersonalDetails(MyAccountPersonalDetailsV } await this.userService.UpdateMyAccountPersonalDetailsAsync(this.CurrentUserId, model); - await this.MyAccountUpdatePrimaryEmail(model.PrimaryEmailAddress); this.ViewBag.SuccessMessage = CommonValidationErrorMessages.PersonalDetailsSuccessMessage; this.ViewBag.MyAction = "Index"; return this.View("SuccessMessageMyAccount"); @@ -1507,48 +1537,26 @@ public async Task CancelEmailChangeValidationToken() return this.View("SuccessMessage"); } - private async Task MyAccountUpdatePrimaryEmail(string primaryEmailAddress) + private async Task MyAccountUpdatePrimaryEmail(string currentEmailAddress, string primaryEmailAddress) { - bool userPrimaryEmailAddressChanged = false; - var user = await this.userService.GetUserByUserIdAsync(this.CurrentUserId); - if (user != null) + var isUserRoleUpgrade = await this.userService.ValidateUserRoleUpgradeAsync(currentEmailAddress, primaryEmailAddress); + UserRoleUpgrade userRoleUpgradeModel = new UserRoleUpgrade() { - if (!string.IsNullOrEmpty(primaryEmailAddress) && user.EmailAddress.ToLower() != primaryEmailAddress.ToLower()) - { - userPrimaryEmailAddressChanged = true; - } + UserId = this.CurrentUserId, + EmailAddress = primaryEmailAddress, + }; + if (isUserRoleUpgrade) + { + userRoleUpgradeModel.UserHistoryTypeId = (int)UserHistoryType.UserRoleUpgarde; } - - if (userPrimaryEmailAddressChanged) + else { - if (await this.userService.DoesEmailAlreadyExist(primaryEmailAddress)) - { - this.ModelState.AddModelError( - nameof(primaryEmailAddress), - CommonValidationErrorMessages.DuplicateEmailAddress); - } - else - { - var isUserRoleUpgrade = await this.userService.ValidateUserRoleUpgradeAsync(user.EmailAddress, primaryEmailAddress); - UserRoleUpgrade userRoleUpgradeModel = new UserRoleUpgrade() - { - UserId = this.CurrentUserId, - EmailAddress = primaryEmailAddress, - }; - if (isUserRoleUpgrade) - { - userRoleUpgradeModel.UserHistoryTypeId = (int)UserHistoryType.UserRoleUpgarde; - } - else - { - userRoleUpgradeModel.UserHistoryTypeId = (int)UserHistoryType.UserDetails; - } - - await this.userService.GenerateEmailChangeValidationTokenAndSendEmailAsync(primaryEmailAddress, isUserRoleUpgrade); - await this.userService.UpdateUserRoleUpgradeAsync(); - await this.userService.CreateUserRoleUpgradeAsync(userRoleUpgradeModel); - } + userRoleUpgradeModel.UserHistoryTypeId = (int)UserHistoryType.UserDetails; } + + await this.userService.GenerateEmailChangeValidationTokenAndSendEmailAsync(primaryEmailAddress, isUserRoleUpgrade); + await this.userService.UpdateUserRoleUpgradeAsync(); + await this.userService.CreateUserRoleUpgradeAsync(userRoleUpgradeModel); } } } \ No newline at end of file diff --git a/LearningHub.Nhs.WebUI/Models/UserProfile/MyAccountPersonalDetailsViewModel.cs b/LearningHub.Nhs.WebUI/Models/UserProfile/MyAccountPersonalDetailsViewModel.cs index 194bcc89a..d7f6eeaf0 100644 --- a/LearningHub.Nhs.WebUI/Models/UserProfile/MyAccountPersonalDetailsViewModel.cs +++ b/LearningHub.Nhs.WebUI/Models/UserProfile/MyAccountPersonalDetailsViewModel.cs @@ -66,7 +66,6 @@ public class MyAccountPersonalDetailsViewModel /// Gets or sets the SecondaryEmailAddress. /// [DataType(DataType.EmailAddress)] - [NotEqual("PrimaryEmailAddress", ErrorMessage = CommonValidationErrorMessages.SecondaryEmailShouldNotBeSame)] [MaxLength(100, ErrorMessage = CommonValidationErrorMessages.TooLongEmail)] [EmailAddress(ErrorMessage = CommonValidationErrorMessages.InvalidEmail)] [NoWhitespace(ErrorMessage = CommonValidationErrorMessages.WhitespaceInEmail)] diff --git a/LearningHub.Nhs.WebUI/Services/UserService.cs b/LearningHub.Nhs.WebUI/Services/UserService.cs index bf432e9c4..b7390de5d 100644 --- a/LearningHub.Nhs.WebUI/Services/UserService.cs +++ b/LearningHub.Nhs.WebUI/Services/UserService.cs @@ -1942,9 +1942,9 @@ public async Task UpdateMyAccountPersonalDetailsAsync(int userId, MyAccountPerso PersonalDetailsViewModel personalDetailsViewModel = new PersonalDetailsViewModel { UserId = userId, - FirstName = model.FirstName, - LastName = model.LastName, - PreferredName = model.PreferredName, + FirstName = model.FirstName.Trim(), + LastName = model.LastName.Trim(), + PreferredName = model.PreferredName?.Trim(), SecondaryEmailAddress = model.SecondaryEmailAddress, }; diff --git a/LearningHub.Nhs.WebUI/Views/MyAccount/ChangePersonalDetails.cshtml b/LearningHub.Nhs.WebUI/Views/MyAccount/ChangePersonalDetails.cshtml index 1855c2766..a4b76cbb7 100644 --- a/LearningHub.Nhs.WebUI/Views/MyAccount/ChangePersonalDetails.cshtml +++ b/LearningHub.Nhs.WebUI/Views/MyAccount/ChangePersonalDetails.cshtml @@ -56,7 +56,7 @@
} -
+
@@ -83,7 +83,7 @@
- Primary email address + Work email
@if (Model.NewPrimaryEmailAddress != "" && Model.NewPrimaryEmailAddress != null) { @@ -102,7 +102,7 @@
- Secondary email address + Secondary email
@Model.SecondaryEmailAddress diff --git a/LearningHub.Nhs.WebUI/Views/MyAccount/MyAccountSecurity.cshtml b/LearningHub.Nhs.WebUI/Views/MyAccount/MyAccountSecurity.cshtml index 00c498b14..9c4c371ce 100644 --- a/LearningHub.Nhs.WebUI/Views/MyAccount/MyAccountSecurity.cshtml +++ b/LearningHub.Nhs.WebUI/Views/MyAccount/MyAccountSecurity.cshtml @@ -22,7 +22,7 @@
} -
+
diff --git a/LearningHub.Nhs.WebUI/Views/MyAccount/MyEmployment.cshtml b/LearningHub.Nhs.WebUI/Views/MyAccount/MyEmployment.cshtml index 2f9fa11e9..602eaf144 100644 --- a/LearningHub.Nhs.WebUI/Views/MyAccount/MyEmployment.cshtml +++ b/LearningHub.Nhs.WebUI/Views/MyAccount/MyEmployment.cshtml @@ -23,7 +23,7 @@
} -
+
diff --git a/LearningHub.Nhs.WebUI/Views/Notification/Index.cshtml b/LearningHub.Nhs.WebUI/Views/Notification/Index.cshtml index c88c723cf..56fc3fa59 100644 --- a/LearningHub.Nhs.WebUI/Views/Notification/Index.cshtml +++ b/LearningHub.Nhs.WebUI/Views/Notification/Index.cshtml @@ -20,7 +20,7 @@
} -
+
diff --git a/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/Messaging/EmailTemplateService.cs b/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/Messaging/EmailTemplateService.cs index 3f47fe35e..dd4851149 100644 --- a/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/Messaging/EmailTemplateService.cs +++ b/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/Messaging/EmailTemplateService.cs @@ -154,7 +154,7 @@ public EmailDetails GetEmailChangeConfirmation(SendEmailModel { ["UserFirstName"] = model.UserFirstName,