public async Task <JsonResult> ChangeAccountEmail([FromHeader] int accountId, [FromBody] EmailUpdateVM emailUpdateData) { var newEmailValidation = emailUpdateData.VerifyNewEmail(); if (newEmailValidation.Count != 0) { var errorMessages = emailUpdateData.GenerateErrorMessages(newEmailValidation); return(new JsonResult(new JsonResponse { Result = SharedEnums.RequestResults.Failed, Data = errorMessages, Error = SharedEnums.HttpStatusCodes.Conflict })); } var userAccount = await _accountService.GetUserAccountById(accountId); if (userAccount == null) { return(new JsonResult(new JsonResponse { Result = SharedEnums.RequestResults.Failed, Message = "Failed to find your account." })); } if (!_assistantService.IsHashMatchesPlainText(userAccount.PasswordHash, emailUpdateData.Password)) { return(new JsonResult(new JsonResponse { Result = SharedEnums.RequestResults.Denied, Message = "Password is incorrect.", Error = SharedEnums.HttpStatusCodes.Forbidden })); } userAccount.Email = emailUpdateData.NewEmail; userAccount.EmailConfirmed = false; var confirmationToken = Helpers.GenerateRandomString(SharedConstants.AccountActivationTokenLength); userAccount.RecoveryToken = confirmationToken; userAccount.TokenSetOn = DateTime.UtcNow; await _accountService.StartTransaction(); var updateResult = await _accountService.UpdateUserAccount(userAccount); if (!updateResult) { await _accountService.RevertTransaction(); return(new JsonResult(new JsonResponse { Result = SharedEnums.RequestResults.Failed, Message = "An error occurred while updating your account.", Error = SharedEnums.HttpStatusCodes.InternalServerError })); } var emailUpdateLog = new EmailUpdateLog { Activity = nameof(ChangeAccountEmail), AccountId = userAccount.Id, EmailBeingReplaced = userAccount.Email }; if (!await _accountLogService.InsertRoutinizeAccountLog(emailUpdateLog)) { await _accountService.RevertTransaction(); return(new JsonResult(new JsonResponse { Result = SharedEnums.RequestResults.Failed, Message = "An error occurred while updating your email." })); } using var fileReader = System.IO.File.OpenText($"{ SharedConstants.EmailTemplatesDirectory }EmailUpdateNotificationEmail.html"); var emailUpdateNotificationContent = await fileReader.ReadToEndAsync(); emailUpdateNotificationContent = emailUpdateNotificationContent.Replace("[USER_NAME]", userAccount.Username); emailUpdateNotificationContent = emailUpdateNotificationContent.Replace("[ACTIVATION_TOKEN]", confirmationToken); emailUpdateNotificationContent = emailUpdateNotificationContent.Replace("[VALIDITY_DURATION]", SharedConstants.AccountActivationEmailValidityDuration.ToString()); emailUpdateNotificationContent = emailUpdateNotificationContent.Replace("[USER_EMAIL]", userAccount.Email); var emailUpdateEmail = new EmailContent { Subject = "Activate your account", Body = emailUpdateNotificationContent, ReceiverName = userAccount.Username, ReceiverAddress = userAccount.Email }; fileReader.Close(); if (!await _emailSenderService.SendEmailSingle(emailUpdateEmail)) { await _accountLogService.RemoveAccountLogEntry(emailUpdateLog); await _accountService.RevertTransaction(); return(new JsonResult(new JsonResponse { Result = SharedEnums.RequestResults.Partial, Message = "Failed to send the activation email." })); } await _accountService.CommitTransaction(); return(new JsonResult(new JsonResponse { Result = SharedEnums.RequestResults.Success })); }