From ceb21fbe519118860a31fd316992764a3e7dd238 Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Thu, 26 Sep 2024 11:27:59 +0200 Subject: [PATCH] feat: get projects by ids (#8269) --- src/lib/features/project/project-read-model-type.ts | 4 ++-- src/lib/features/project/project-read-model.ts | 7 +++++-- src/lib/features/project/project-service.e2e.test.ts | 5 +++++ src/lib/features/project/project-service.ts | 11 ++++++----- src/lib/features/project/project-store-type.ts | 4 ++++ 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/lib/features/project/project-read-model-type.ts b/src/lib/features/project/project-read-model-type.ts index cd9b47028999..a3d2270edc02 100644 --- a/src/lib/features/project/project-read-model-type.ts +++ b/src/lib/features/project/project-read-model-type.ts @@ -1,5 +1,5 @@ import type { ProjectMode } from '../../types'; -import type { IProjectQuery } from './project-store-type'; +import type { IProjectQuery, IProjectsQuery } from './project-store-type'; export type ProjectForUi = { id: string; @@ -28,7 +28,7 @@ export type ProjectForInsights = { export interface IProjectReadModel { getProjectsForAdminUi( - query?: IProjectQuery, + query?: IProjectQuery & IProjectsQuery, userId?: number, ): Promise; getProjectsForInsights( diff --git a/src/lib/features/project/project-read-model.ts b/src/lib/features/project/project-read-model.ts index 66977d94e296..b15ab461f501 100644 --- a/src/lib/features/project/project-read-model.ts +++ b/src/lib/features/project/project-read-model.ts @@ -6,7 +6,7 @@ import type { ProjectForInsights, ProjectForUi, } from './project-read-model-type'; -import type { IProjectQuery } from './project-store-type'; +import type { IProjectQuery, IProjectsQuery } from './project-store-type'; import metricsHelper from '../../util/metrics-helper'; import type EventEmitter from 'events'; import type { IProjectMembersCount } from './project-store'; @@ -79,7 +79,7 @@ export class ProjectReadModel implements IProjectReadModel { } async getProjectsForAdminUi( - query?: IProjectQuery, + query?: IProjectQuery & IProjectsQuery, userId?: number, ): Promise { const projectTimer = this.timer('getProjectsForAdminUi'); @@ -113,6 +113,9 @@ export class ProjectReadModel implements IProjectReadModel { if (query?.id) { projects = projects.where(`${TABLE}.id`, query.id); } + if (query?.ids) { + projects = projects.whereIn(`${TABLE}.id`, query.ids); + } let selectColumns = [ this.db.raw( diff --git a/src/lib/features/project/project-service.e2e.test.ts b/src/lib/features/project/project-service.e2e.test.ts index 1171f78ee5cf..c7e5cf9c44b8 100644 --- a/src/lib/features/project/project-service.e2e.test.ts +++ b/src/lib/features/project/project-service.e2e.test.ts @@ -152,6 +152,11 @@ test('should create new project', async () => { expect(project.name).toEqual(ret.name); expect(project.description).toEqual(ret.description); expect(ret.createdAt).toBeTruthy(); + + const projectsById = await projectService.getProjects({ id: 'test' }); + const projectsByIds = await projectService.getProjects({ ids: ['test'] }); + expect(projectsById).toMatchObject([{ id: 'test' }]); + expect(projectsByIds).toMatchObject([{ id: 'test' }]); }); test('should create new private project', async () => { diff --git a/src/lib/features/project/project-service.ts b/src/lib/features/project/project-service.ts index ac5f16b38621..4c8272a032e0 100644 --- a/src/lib/features/project/project-service.ts +++ b/src/lib/features/project/project-service.ts @@ -83,6 +83,7 @@ import type { IProjectApplicationsSearchParams, IProjectEnterpriseSettingsUpdate, IProjectQuery, + IProjectsQuery, } from './project-store-type'; import type { IProjectFlagCreatorsReadModel } from './project-flag-creators-read-model.type'; import { throwExceedsLimitError } from '../../error/exceeds-limit-error'; @@ -230,13 +231,13 @@ export default class ProjectService { } async getProjects( - query?: IProjectQuery, + query?: IProjectQuery & IProjectsQuery, userId?: number, ): Promise { - const getProjects = () => - this.projectReadModel.getProjectsForAdminUi(query, userId); - - const projects = await getProjects(); + const projects = await this.projectReadModel.getProjectsForAdminUi( + query, + userId, + ); if (userId) { const projectAccess = diff --git a/src/lib/features/project/project-store-type.ts b/src/lib/features/project/project-store-type.ts index b4374f9b5cdf..9f3a190a27c4 100644 --- a/src/lib/features/project/project-store-type.ts +++ b/src/lib/features/project/project-store-type.ts @@ -48,6 +48,10 @@ export interface IProjectQuery { archived?: boolean; } +export interface IProjectsQuery { + ids?: string[]; +} + export type ProjectEnvironment = { environment: string; changeRequestEnabled?: boolean;