From bd31e9a6c5908a65c03ea7fc4f92b46ccd8e33bd Mon Sep 17 00:00:00 2001 From: Priyobroto Kar Date: Fri, 20 Sep 2024 01:04:14 +0530 Subject: [PATCH 1/5] fix(api): replace the id with slug in the global-search service --- apps/api/src/workspace/service/workspace.service.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/api/src/workspace/service/workspace.service.ts b/apps/api/src/workspace/service/workspace.service.ts index 5fffa44a..2a7ec59d 100644 --- a/apps/api/src/workspace/service/workspace.service.ts +++ b/apps/api/src/workspace/service/workspace.service.ts @@ -442,7 +442,7 @@ export class WorkspaceService { { description: { contains: searchTerm, mode: 'insensitive' } } ] }, - select: { id: true, name: true, description: true } + select: { slug: true, name: true, description: true } }) } @@ -467,7 +467,7 @@ export class WorkspaceService { { description: { contains: searchTerm, mode: 'insensitive' } } ] }, - select: { id: true, name: true, description: true } + select: { slug: true, name: true, description: true } }) } @@ -493,7 +493,7 @@ export class WorkspaceService { { note: { contains: searchTerm, mode: 'insensitive' } } ] }, - select: { id: true, name: true, note: true } + select: { slug: true, name: true, note: true } }) } @@ -518,7 +518,7 @@ export class WorkspaceService { { note: { contains: searchTerm, mode: 'insensitive' } } ] }, - select: { id: true, name: true, note: true } + select: { slug: true, name: true, note: true } }) } From 3c9659233c6c7ddd6a1b0247d27421f56e7ceb20 Mon Sep 17 00:00:00 2001 From: Priyobroto Kar Date: Sat, 21 Sep 2024 17:36:18 +0530 Subject: [PATCH 2/5] fix(api-client): Replace IDs with slugs in workspace types --- packages/api-client/src/types/workspace.types.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/api-client/src/types/workspace.types.d.ts b/packages/api-client/src/types/workspace.types.d.ts index c1d60b61..8737417f 100644 --- a/packages/api-client/src/types/workspace.types.d.ts +++ b/packages/api-client/src/types/workspace.types.d.ts @@ -96,22 +96,22 @@ export interface GlobalSearchRequest { export interface GlobalSearchResponse { projects: { - id: string + slug: string name: string description: string }[] environments: { - id: string + slug: string name: string description: string }[] secrets: { - id: string + slug: string name: string note: string }[] variables: { - id: string + slug: string name: string note: string }[] From 327abe0c8a9a39f03c4507862fb183baa8c4501b Mon Sep 17 00:00:00 2001 From: Priyobroto Kar Date: Sun, 22 Sep 2024 01:18:55 +0530 Subject: [PATCH 3/5] refactor(api): Refactor search queries in WorkspaceService --- .../workspace/service/workspace.service.ts | 91 +++++++++---------- 1 file changed, 45 insertions(+), 46 deletions(-) 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]) + ]) + } + } } From 2be4930500c3baf1dd2a489a7bfec1413fec40e2 Mon Sep 17 00:00:00 2001 From: Priyobroto Kar Date: Sun, 22 Sep 2024 10:08:36 +0530 Subject: [PATCH 4/5] fix(api): implement dynamic id query filtering based on the model to search --- .../workspace/service/workspace.service.ts | 68 +++++++++++-------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/apps/api/src/workspace/service/workspace.service.ts b/apps/api/src/workspace/service/workspace.service.ts index 5f14183c..8137c299 100644 --- a/apps/api/src/workspace/service/workspace.service.ts +++ b/apps/api/src/workspace/service/workspace.service.ts @@ -20,6 +20,7 @@ import { Environment, EventSource, EventType, + Prisma, Project, ProjectAccessLevel, Secret, @@ -434,12 +435,10 @@ export class WorkspaceService { searchTerm: string ): Promise[]> { // Fetch projects where user has READ_PROJECT authority and match search term - return this.prisma.project.findMany( - this.createSearchQuery(projectIds, searchTerm, [ - 'name', - 'description' - ]) - ) + return this.createSearchQuery('Project', projectIds, searchTerm, [ + 'name', + 'description' + ]) } /** @@ -453,12 +452,10 @@ export class WorkspaceService { projectIds: string[], searchTerm: string ): Promise[]> { - return this.prisma.environment.findMany( - this.createSearchQuery(projectIds, searchTerm, [ - 'name', - 'description' - ]) - ) + return this.createSearchQuery('Environment', projectIds, searchTerm, [ + 'name', + 'description' + ]) } /** @@ -473,9 +470,10 @@ export class WorkspaceService { searchTerm: string ): Promise[]> { // Fetch secrets associated with projects user has READ_SECRET authority on - return await this.prisma.secret.findMany( - this.createSearchQuery(projectIds, searchTerm, ['name', 'note']) - ) + return this.createSearchQuery('Secret', projectIds, searchTerm, [ + 'name', + 'note' + ]) } /** @@ -489,9 +487,10 @@ export class WorkspaceService { projectIds: string[], searchTerm: string ): Promise[]> { - return this.prisma.variable.findMany( - this.createSearchQuery(projectIds, searchTerm, ['name', 'note']) - ) + return this.createSearchQuery('Variable', projectIds, searchTerm, [ + 'name', + 'note' + ]) } /** @@ -517,6 +516,7 @@ export class WorkspaceService { /** * Creates a search query object based on provided project IDs and search term. + * @param model The Prisma model name (e.g., 'Project', 'Environment') * @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 @@ -524,21 +524,31 @@ export class WorkspaceService { * @private */ private createSearchQuery( + model: Prisma.ModelName, projectIds: string[], searchTerm: string, fields: Array> - ) { - return { + ): Promise[]> { + const idQueryFilter = + model === 'Project' + ? { id: { in: projectIds } } + : { projectId: { in: projectIds } } + + const searchConditions = fields.map((field) => ({ + [field]: { contains: searchTerm, mode: 'insensitive' } + })) + + const selectFields = Object.fromEntries([ + ['slug', true], + ...fields.map((field) => [field, true]) + ]) + + return this.prisma[model].findMany({ where: { - id: { in: projectIds }, - OR: fields.map((field) => ({ - [field]: { contains: searchTerm, mode: 'insensitive' } - })) + ...idQueryFilter, + OR: searchConditions }, - select: Object.fromEntries([ - ['slug', true], - ...fields.map((field) => [field, true]) - ]) - } + select: selectFields + }) } } From d288831d2aa6a5c234a3c8a380bf5fcb67db9262 Mon Sep 17 00:00:00 2001 From: Priyobroto Kar Date: Sun, 22 Sep 2024 15:05:28 +0530 Subject: [PATCH 5/5] Revert refactor search queries in Workspace Service --- .../workspace/service/workspace.service.ts | 101 ++++++++---------- 1 file changed, 46 insertions(+), 55 deletions(-) diff --git a/apps/api/src/workspace/service/workspace.service.ts b/apps/api/src/workspace/service/workspace.service.ts index 8137c299..2a7ec59d 100644 --- a/apps/api/src/workspace/service/workspace.service.ts +++ b/apps/api/src/workspace/service/workspace.service.ts @@ -20,7 +20,6 @@ import { Environment, EventSource, EventType, - Prisma, Project, ProjectAccessLevel, Secret, @@ -435,10 +434,16 @@ export class WorkspaceService { searchTerm: string ): Promise[]> { // Fetch projects where user has READ_PROJECT authority and match search term - return this.createSearchQuery('Project', projectIds, searchTerm, [ - 'name', - 'description' - ]) + 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 } + }) } /** @@ -452,10 +457,18 @@ export class WorkspaceService { projectIds: string[], searchTerm: string ): Promise[]> { - return this.createSearchQuery('Environment', projectIds, searchTerm, [ - 'name', - 'description' - ]) + 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 } + }) } /** @@ -470,10 +483,18 @@ export class WorkspaceService { searchTerm: string ): Promise[]> { // Fetch secrets associated with projects user has READ_SECRET authority on - return this.createSearchQuery('Secret', projectIds, searchTerm, [ - 'name', - 'note' - ]) + 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 } + }) } /** @@ -487,10 +508,18 @@ export class WorkspaceService { projectIds: string[], searchTerm: string ): Promise[]> { - return this.createSearchQuery('Variable', projectIds, searchTerm, [ - 'name', - 'note' - ]) + 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 } + }) } /** @@ -513,42 +542,4 @@ export class WorkspaceService { })) > 0 ) } - - /** - * Creates a search query object based on provided project IDs and search term. - * @param model The Prisma model name (e.g., 'Project', 'Environment') - * @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( - model: Prisma.ModelName, - projectIds: string[], - searchTerm: string, - fields: Array> - ): Promise[]> { - const idQueryFilter = - model === 'Project' - ? { id: { in: projectIds } } - : { projectId: { in: projectIds } } - - const searchConditions = fields.map((field) => ({ - [field]: { contains: searchTerm, mode: 'insensitive' } - })) - - const selectFields = Object.fromEntries([ - ['slug', true], - ...fields.map((field) => [field, true]) - ]) - - return this.prisma[model].findMany({ - where: { - ...idQueryFilter, - OR: searchConditions - }, - select: selectFields - }) - } }