using DigitalOps.Authority.Services; using DigitalOps.Dal; using Microsoft.EntityFrameworkCore; using PoweredSoft.DynamicLinq; namespace DigitalOps.CQRS.Queries.Client; public class ClientItem { public long Id { get; set; } public IEnumerable OrganizationIds { get; set; } public required string Name { get; set; } public DateTime CreatedAt { get; set; } public DateTime? UpdatedAt { get; set; } } public class ClientQueryableProvider(MainDbContext dbContext, UserIdentityService userIdentityService): IQueryableProviderOverride { public async Task> GetQueryableAsync(object query, CancellationToken cancellationToken = default) { var user = await userIdentityService.GetUserOrDefaultAsync(cancellationToken); var queryable = dbContext.Clients .AsNoTracking() .Where(client => client.OrganizationClients.Any(organizationClient => organizationClient.Organization.OrganizationUsers.Any(organizationUser => organizationUser.UserId == user!.Id))) .Select(client => new ClientItem { Id = client.Id, OrganizationIds = client.OrganizationClients.Select(organizationClient => organizationClient.OrganizationId), Name = client.Name, CreatedAt = client.CreatedAt, UpdatedAt = client.UpdatedAt }); return queryable; } }