From 661bf5877e502dde4e17eb46b4f7c694ac9bfced Mon Sep 17 00:00:00 2001 From: Ben Chidgey Date: Wed, 22 Jan 2025 16:36:07 +0000 Subject: [PATCH] 469941: Sort deployments by teamId If teamId's are provided via the avouriteTeamIds query array, then sort by services that are owned by these teams --- .../Endpoints/DeploymentsEndpointV2.cs | 2 ++ .../Services/Deployments/DeploymentsServiceV2.cs | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Defra.Cdp.Backend.Api/Endpoints/DeploymentsEndpointV2.cs b/Defra.Cdp.Backend.Api/Endpoints/DeploymentsEndpointV2.cs index 34b9c1c..2c50b08 100644 --- a/Defra.Cdp.Backend.Api/Endpoints/DeploymentsEndpointV2.cs +++ b/Defra.Cdp.Backend.Api/Endpoints/DeploymentsEndpointV2.cs @@ -24,6 +24,7 @@ public static void MapDeploymentsEndpointV2(this IEndpointRouteBuilder app) // GET /v2/deployments or with query params GET /v2/deployments?environment=dev&service=forms-runner&user=jeff&status=running&page=1&offset=0&size=50 private static async Task FindLatestDeployments(IDeploymentsServiceV2 deploymentsService, + [FromQuery(Name = "favouriteTeamIds")] string[]? favouriteTeamIds, [FromQuery(Name = "environment")] string? environment, [FromQuery(Name = "service")] string? service, [FromQuery(Name = "user")] string? user, @@ -35,6 +36,7 @@ private static async Task FindLatestDeployments(IDeploymentsServiceV2 d CancellationToken cancellationToken) { var deploymentsPage = await deploymentsService.FindLatest( + favouriteTeamIds, environment, service, user, diff --git a/Defra.Cdp.Backend.Api/Services/Deployments/DeploymentsServiceV2.cs b/Defra.Cdp.Backend.Api/Services/Deployments/DeploymentsServiceV2.cs index 094eb3b..c65ab3c 100644 --- a/Defra.Cdp.Backend.Api/Services/Deployments/DeploymentsServiceV2.cs +++ b/Defra.Cdp.Backend.Api/Services/Deployments/DeploymentsServiceV2.cs @@ -16,6 +16,7 @@ public interface IDeploymentsServiceV2 Task UpdateDeploymentStatus(string lambdaId, string eventName, string reason, CancellationToken ct); Task> FindLatest( + string[]? favouriteTeamIds, string? environment, string? service, string? user, @@ -181,7 +182,8 @@ public async Task> FindWhatsRunningWhere(string serviceName, .ToListAsync(ct); } - public async Task> FindLatest(string? environment, string? service, string? user, + public async Task> FindLatest(string[]? favouriteTeamIds, string? environment, + string? service, string? user, string? status, string? team, int offset = 0, @@ -237,6 +239,18 @@ public async Task> FindLatest(string? environment, strin .SortByDescending(d => d.Created) .ToListAsync(ct); + if (favouriteTeamIds?.Length > 0) + { + var repos = (await Task.WhenAll(favouriteTeamIds.Select(teamId => + _repositoryService.FindRepositoriesByTeamId(teamId, true, ct)))) + .SelectMany(r => r) + .ToList(); + + var servicesOwnedByTeam = repos.Select(r => r.Id); + + deployments = deployments.OrderByDescending(d => servicesOwnedByTeam.Contains(d.Service)).ToList(); + } + var totalDeployments = await Collection.CountDocumentsAsync(filter, cancellationToken: ct); var totalPages = Math.Max(1, (int)Math.Ceiling((double)totalDeployments / size));