public async Task <OmbiIdentityResult> DeleteUser(string userId) { var userToDelete = await UserManager.Users.FirstOrDefaultAsync(x => x.Id == userId); if (userToDelete != null) { // Can we delete this user? var userRoles = await UserManager.GetRolesAsync(userToDelete); if (!CanModifyUser(userRoles)) { return(Error("You do not have the correct permissions to delete this user")); } // We need to delete all the requests first var moviesUserRequested = MovieRepo.GetAll().Where(x => x.RequestedUserId == userId); var tvUserRequested = TvRepo.GetChild().Where(x => x.RequestedUserId == userId); var musicRequested = MusicRepo.GetAll().Where(x => x.RequestedUserId == userId); var notificationPreferences = _userNotificationPreferences.GetAll().Where(x => x.UserId == userId); var userQuality = await _userQualityProfiles.GetAll().FirstOrDefaultAsync(x => x.UserId == userId); if (moviesUserRequested.Any()) { await MovieRepo.DeleteRange(moviesUserRequested); } if (tvUserRequested.Any()) { await TvRepo.DeleteChildRange(tvUserRequested); } if (musicRequested.Any()) { await MusicRepo.DeleteRange(musicRequested); } if (notificationPreferences.Any()) { await _userNotificationPreferences.DeleteRange(notificationPreferences); } if (userQuality != null) { await _userQualityProfiles.Delete(userQuality); } // Delete any issues and request logs var issues = _issuesRepository.GetAll().Where(x => x.UserReportedId == userId); var issueComments = _issueCommentsRepository.GetAll().Where(x => x.UserId == userId); var requestLog = _requestLogRepository.GetAll().Where(x => x.UserId == userId); if (issues.Any()) { await _issuesRepository.DeleteRange(issues); } if (requestLog.Any()) { await _requestLogRepository.DeleteRange(requestLog); } if (issueComments.Any()) { await _issueCommentsRepository.DeleteRange(issueComments); } // Delete the Subscriptions and mobile notification ids var subs = _requestSubscriptionRepository.GetAll().Where(x => x.UserId == userId); var mobileIds = _notificationRepository.GetAll().Where(x => x.UserId == userId); if (subs.Any()) { await _requestSubscriptionRepository.DeleteRange(subs); } if (mobileIds.Any()) { await _notificationRepository.DeleteRange(mobileIds); } var result = await UserManager.DeleteAsync(userToDelete); if (result.Succeeded) { return(new OmbiIdentityResult { Successful = true }); } return(new OmbiIdentityResult { Errors = result.Errors.Select(x => x.Description).ToList() }); } return(Error("Could not find user to delete.")); }