/// <summary> /// Replaces the user in the database and updates Azure Active Directory if properties have changed /// </summary> /// <param name="oldUser">The user to change</param> /// <param name="updatedUser">The parameters to update the user with</param> /// <returns>The updated user from the database</returns> private async Task <AdminUser> ReplaceUserAsync(AdminUser oldUser, AdminUser updatedUser) { // Sanity check input oldUser = Ensure.IsNotNull(() => oldUser); updatedUser = Ensure.IsNotNull(() => updatedUser); // Create replaced user var replacedUser = AdminUser.Replace(oldUser, updatedUser); // Replace user in database var userFromDatabase = await this.userRepository.ReplaceUserAsync(replacedUser, replacedUser.Id); // Update email in AAD B2C if needed if (oldUser.Email != replacedUser.Email) { var oldEmail = Ensure.IsNotNullOrWhitespace(() => oldUser.Email); var newEmail = Ensure.IsNotNullOrWhitespace(() => userFromDatabase.Email); await this.azureActiveDirectoryRepository.UpdateUserEmailAsync(oldEmail, newEmail); } // Sanity check output userFromDatabase = Ensure.IsNotNull(() => userFromDatabase); userFromDatabase.Validate(); this.Logger.LogDebug("Updated 1 user in the database"); return(userFromDatabase); }