diff --git a/apps/api/src/workspace/service/workspace.service.ts b/apps/api/src/workspace/service/workspace.service.ts index 2a7ec59d..5f14183c 100644 --- a/apps/api/src/workspace/service/workspace.service.ts +++ b/apps/api/src/workspace/service/workspace.service.ts @@ -434,16 +434,12 @@ export class WorkspaceService { searchTerm: string ): Promise[]> { // Fetch projects where user has READ_PROJECT authority and match search term - return this.prisma.project.findMany({ - where: { - id: { in: projectIds }, - OR: [ - { name: { contains: searchTerm, mode: 'insensitive' } }, - { description: { contains: searchTerm, mode: 'insensitive' } } - ] - }, - select: { slug: true, name: true, description: true } - }) + return this.prisma.project.findMany( + this.createSearchQuery(projectIds, searchTerm, [ + 'name', + 'description' + ]) + ) } /** @@ -457,18 +453,12 @@ export class WorkspaceService { projectIds: string[], searchTerm: string ): Promise[]> { - return this.prisma.environment.findMany({ - where: { - project: { - id: { in: projectIds } - }, - OR: [ - { name: { contains: searchTerm, mode: 'insensitive' } }, - { description: { contains: searchTerm, mode: 'insensitive' } } - ] - }, - select: { slug: true, name: true, description: true } - }) + return this.prisma.environment.findMany( + this.createSearchQuery(projectIds, searchTerm, [ + 'name', + 'description' + ]) + ) } /** @@ -483,18 +473,9 @@ export class WorkspaceService { searchTerm: string ): Promise[]> { // Fetch secrets associated with projects user has READ_SECRET authority on - return await this.prisma.secret.findMany({ - where: { - project: { - id: { in: projectIds } - }, - OR: [ - { name: { contains: searchTerm, mode: 'insensitive' } }, - { note: { contains: searchTerm, mode: 'insensitive' } } - ] - }, - select: { slug: true, name: true, note: true } - }) + return await this.prisma.secret.findMany( + this.createSearchQuery(projectIds, searchTerm, ['name', 'note']) + ) } /** @@ -508,18 +489,9 @@ export class WorkspaceService { projectIds: string[], searchTerm: string ): Promise[]> { - return this.prisma.variable.findMany({ - where: { - project: { - id: { in: projectIds } - }, - OR: [ - { name: { contains: searchTerm, mode: 'insensitive' } }, - { note: { contains: searchTerm, mode: 'insensitive' } } - ] - }, - select: { slug: true, name: true, note: true } - }) + return this.prisma.variable.findMany( + this.createSearchQuery(projectIds, searchTerm, ['name', 'note']) + ) } /** @@ -542,4 +514,31 @@ export class WorkspaceService { })) > 0 ) } + + /** + * Creates a search query object based on provided project IDs and search term. + * @param projectIds The IDs of projects to query + * @param searchTerm The search term to query by + * @param fields The fields to apply the search term in the query + * @returns The prisma query object + * @private + */ + private createSearchQuery( + projectIds: string[], + searchTerm: string, + fields: Array> + ) { + return { + where: { + id: { in: projectIds }, + OR: fields.map((field) => ({ + [field]: { contains: searchTerm, mode: 'insensitive' } + })) + }, + select: Object.fromEntries([ + ['slug', true], + ...fields.map((field) => [field, true]) + ]) + } + } }