public async Task <OrganizationUser> SaveUserAsync(OrganizationUser user, IEnumerable <SubvaultUser> subvaults) { if (user.Id.Equals(default(Guid))) { throw new BadRequestException("Invite the user first."); } await _organizationUserRepository.ReplaceAsync(user); var orgSubvaults = await _subvaultRepository.GetManyByOrganizationIdAsync(user.OrganizationId); var currentUserSubvaults = await _subvaultUserRepository.GetManyByOrganizationUserIdAsync(user.Id); // Let's make sure all these belong to this user and organization. var filteredSubvaults = subvaults.Where(s => orgSubvaults.Any(os => os.Id == s.SubvaultId) && (s.Id == default(Guid) || currentUserSubvaults.Any(cs => cs.Id == s.Id))); var subvaultsToDelete = currentUserSubvaults.Where(cs => !subvaults.Any(s => s.Id == cs.Id)); foreach (var subvault in filteredSubvaults) { await _subvaultUserRepository.UpsertAsync(subvault); } foreach (var subvault in subvaultsToDelete) { await _subvaultUserRepository.DeleteAsync(subvault); } return(user); }
private async Task SaveUserSubvaultsAsync(OrganizationUser user, IEnumerable <SubvaultUser> subvaults, bool newUser) { if (subvaults == null) { subvaults = new List <SubvaultUser>(); } var orgSubvaults = await _subvaultRepository.GetManyByOrganizationIdAsync(user.OrganizationId); var currentUserSubvaults = newUser ? null : await _subvaultUserRepository.GetManyByOrganizationUserIdAsync(user.Id); // Let's make sure all these belong to this user and organization. var filteredSubvaults = subvaults.Where(s => orgSubvaults.Any(os => os.Id == s.SubvaultId)); foreach (var subvault in filteredSubvaults) { var existingSubvaultUser = currentUserSubvaults?.FirstOrDefault(cs => cs.SubvaultId == subvault.SubvaultId); if (existingSubvaultUser != null) { subvault.Id = existingSubvaultUser.Id; subvault.CreationDate = existingSubvaultUser.CreationDate; } subvault.OrganizationUserId = user.Id; await _subvaultUserRepository.UpsertAsync(subvault); } if (!newUser) { var subvaultsToDelete = currentUserSubvaults.Where(cs => !filteredSubvaults.Any(s => s.SubvaultId == cs.SubvaultId)); foreach (var subvault in subvaultsToDelete) { await _subvaultUserRepository.DeleteAsync(subvault); } } }