From 71f00b41bd7117f976044a3c28784bec3e9a1925 Mon Sep 17 00:00:00 2001 From: Mathias Beaulieu-Duncan Date: Sun, 5 Jan 2025 00:34:00 -0500 Subject: [PATCH] fix client query --- DigitalOps.Api/Program.cs | 2 +- DigitalOps.Api/appsettings.Development.json | 2 +- DigitalOps.CQRS/Queries/Client/ClientQuery.cs | 27 +++++++++++++++++-- .../RegisterOrganizationQueriesModule.cs | 2 +- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/DigitalOps.Api/Program.cs b/DigitalOps.Api/Program.cs index 650b5e1..4e4fc1d 100644 --- a/DigitalOps.Api/Program.cs +++ b/DigitalOps.Api/Program.cs @@ -111,7 +111,7 @@ app.UseCors(options => var origins = new List {"https://hoppscotch.io"}; if (builder.Environment.IsDevelopment()) { - origins.Add("http://localhost:8100"); + origins.Add("http://localhost:4200"); } options.WithOrigins(origins.ToArray()); diff --git a/DigitalOps.Api/appsettings.Development.json b/DigitalOps.Api/appsettings.Development.json index fb9716d..c6fae80 100644 --- a/DigitalOps.Api/appsettings.Development.json +++ b/DigitalOps.Api/appsettings.Development.json @@ -9,6 +9,6 @@ "ConnectionString": "Host=workstation;Port=5432;Database=digital-operations;Username=planb;Password=-mbd2018-" }, "JwtBearer": { - "Authority": "https://login-planb.openharbor.io/realms/plan-b" + "Authority": "https://login-planb.openharbor.io/realms/digital-ops" } } diff --git a/DigitalOps.CQRS/Queries/Client/ClientQuery.cs b/DigitalOps.CQRS/Queries/Client/ClientQuery.cs index 4f880e9..1244be8 100644 --- a/DigitalOps.CQRS/Queries/Client/ClientQuery.cs +++ b/DigitalOps.CQRS/Queries/Client/ClientQuery.cs @@ -1,10 +1,15 @@ using DigitalOps.Authority.Services; using DigitalOps.Dal; using Microsoft.EntityFrameworkCore; -using PoweredSoft.DynamicLinq; +using OpenHarbor.CQRS.DynamicQuery.Abstractions; namespace DigitalOps.CQRS.Queries.Client; +public class ClientParams +{ + public long OrganizationId { get; set; } +} + public class ClientItem { public long Id { get; set; } @@ -18,10 +23,28 @@ public class ClientQueryableProvider(MainDbContext dbContext, UserIdentityServic { public async Task> GetQueryableAsync(object query, CancellationToken cancellationToken = default) { + long? organizationId = null; + + if (query is IDynamicQueryParams dynamicQuery) + { + var queryParams = dynamicQuery.GetParams(); + organizationId = queryParams?.OrganizationId; + } + + if (organizationId is null) + { + // don't bother to call the database if organizationId is not set + return Enumerable.Empty().AsQueryable(); + } + var user = await userIdentityService.GetUserOrDefaultAsync(cancellationToken); var queryable = dbContext.Clients + .AsNoTracking(); + + var result = dbContext.Clients .AsNoTracking() + .Where(client => client.OrganizationClients.Any(organizationClient => organizationClient.OrganizationId == organizationId)) .Where(client => client.OrganizationClients.Any(organizationClient => organizationClient.Organization.OrganizationUsers.Any(organizationUser => organizationUser.UserId == user!.Id))) .Select(client => new ClientItem @@ -33,6 +56,6 @@ public class ClientQueryableProvider(MainDbContext dbContext, UserIdentityServic UpdatedAt = client.UpdatedAt }); - return queryable; + return result; } } \ No newline at end of file diff --git a/DigitalOps.CQRS/Queries/Client/RegisterOrganizationQueriesModule.cs b/DigitalOps.CQRS/Queries/Client/RegisterOrganizationQueriesModule.cs index 3e941ac..dbb0ce1 100644 --- a/DigitalOps.CQRS/Queries/Client/RegisterOrganizationQueriesModule.cs +++ b/DigitalOps.CQRS/Queries/Client/RegisterOrganizationQueriesModule.cs @@ -9,7 +9,7 @@ public class RegisterClientQueriesModule : IModule { public IServiceCollection ConfigureServices(IServiceCollection services) { - services.AddDynamicQuery() + services.AddDynamicQueryWithParams() .AddQueryableProviderOverride(); return services;