Skip to content

Commit eeec54d

Browse files
authored
chore(api): Add suggestions from keyshade-xyz#812 (keyshade-xyz#813)
1 parent ded126d commit eeec54d

File tree

4 files changed

+46
-48
lines changed

4 files changed

+46
-48
lines changed

apps/api/src/auth/service/authority-checker.service.ts

-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ export class AuthorityCheckerService {
494494
})
495495
}
496496
} catch (error) {
497-
this.logger.error(error)
498497
this.logger.error(error)
499498
throw new InternalServerErrorException(error)
500499
}

apps/api/src/project/project.e2e.spec.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,16 @@ describe('Project Controller Tests', () => {
334334
})
335335

336336
it('should not be able to update the name of a project to an existing name', async () => {
337+
await projectService.createProject(user1, workspace1.slug, {
338+
name: 'Existing Project',
339+
description: 'Existing Project description'
340+
})
341+
337342
const response = await app.inject({
338343
method: 'PUT',
339344
url: `/project/${project1.slug}`,
340345
payload: {
341-
name: 'Project 1'
346+
name: 'Existing Project'
342347
},
343348
headers: {
344349
'x-e2e-user-email': user1.email

apps/api/src/project/service/project.service.ts

+39-42
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,7 @@ export class ProjectService {
6969
const workspaceId = workspace.id
7070

7171
// Check if project with this name already exists for the user
72-
if (await this.projectExists(dto.name, workspaceId)) {
73-
const errorMessage = `Project with name ${dto.name} already exists in workspace ${workspace.slug}`
74-
this.logger.error(
75-
`User ${user.id} attempted to create a project that already exists: ${errorMessage}`
76-
)
77-
throw new ConflictException(
78-
constructErrorBody('Project already exists', errorMessage)
79-
)
80-
}
72+
await this.projectExists(dto.name, workspaceId)
8173

8274
// Create the public and private key pair
8375
this.logger.log(`Creating key pair for project ${dto.name}`)
@@ -115,6 +107,17 @@ export class ProjectService {
115107
hasAdminAuthority: true
116108
}
117109
})
110+
111+
if (!adminRole) {
112+
const errorMessage = `Admin role not found for workspace ${workspace.slug}`
113+
this.logger.error(
114+
`User ${user.id} attempted to create a project without an admin role: ${errorMessage}`
115+
)
116+
throw new BadRequestException(
117+
constructErrorBody('Admin role not found', errorMessage)
118+
)
119+
}
120+
118121
this.logger.log(
119122
`Admin role for workspace ${workspace.slug} is ${adminRole.slug}`
120123
)
@@ -226,7 +229,7 @@ export class ProjectService {
226229
this.prisma
227230
)
228231

229-
this.logger.debug(`Created project ${newProject} (${newProject.slug})`)
232+
this.logger.debug(`Created project ${newProject.name} (${newProject.slug})`)
230233

231234
// It is important that we log before the private key is set
232235
// in order to not log the private key
@@ -270,20 +273,7 @@ export class ProjectService {
270273
})
271274

272275
// Check if project with this name already exists for the user
273-
if (
274-
(dto.name && (await this.projectExists(dto.name, project.workspaceId))) ||
275-
project.name === dto.name
276-
) {
277-
this.logger.error(
278-
`Project with name ${dto.name} already exists in workspace ${project.workspaceId}`
279-
)
280-
throw new ConflictException(
281-
constructErrorBody(
282-
'Project already exists',
283-
`Project with this name ${dto.name} already exists in the workspace`
284-
)
285-
)
286-
}
276+
dto.name && (await this.projectExists(dto.name, project.workspaceId))
287277

288278
if (dto.accessLevel) {
289279
this.logger.log(`Access level specified while updating project.`)
@@ -330,10 +320,11 @@ export class ProjectService {
330320
}
331321

332322
const data: Partial<Project> = {
333-
name: dto.name,
334-
slug: dto.name
335-
? await generateEntitySlug(dto.name, 'PROJECT', this.prisma)
336-
: project.slug,
323+
name: dto.name === project.name ? undefined : dto.name,
324+
slug:
325+
dto.name === project.name
326+
? await generateEntitySlug(dto.name, 'PROJECT', this.prisma)
327+
: project.slug,
337328
description: dto.description,
338329
storePrivateKey: dto.storePrivateKey,
339330
privateKey: dto.storePrivateKey ? dto.privateKey : null,
@@ -484,17 +475,7 @@ export class ProjectService {
484475
this.logger.log(`Forking project ${projectSlug} as ${newProjectName}`)
485476

486477
// Check if project with this name already exists for the user
487-
if (await this.projectExists(newProjectName, workspaceId)) {
488-
this.logger.error(
489-
`Project with name ${newProjectName} already exists in workspace ${workspaceId}`
490-
)
491-
throw new ConflictException(
492-
constructErrorBody(
493-
'Project already exists',
494-
`Project with name ${newProjectName} already exists in the selected workspace`
495-
)
496-
)
497-
}
478+
await this.projectExists(newProjectName, workspaceId)
498479

499480
this.logger.log(`Creating key pair for project ${newProjectName}`)
500481
const { privateKey, publicKey } = createKeyPair()
@@ -1033,11 +1014,12 @@ export class ProjectService {
10331014
private async projectExists(
10341015
projectName: string,
10351016
workspaceId: Workspace['id']
1036-
): Promise<boolean> {
1017+
): Promise<void> {
10371018
this.logger.log(
10381019
`Checking if project ${projectName} exists in workspace ${workspaceId}`
10391020
)
1040-
return (
1021+
1022+
const projectExist: boolean =
10411023
(await this.prisma.workspaceMember.count({
10421024
where: {
10431025
workspaceId,
@@ -1050,7 +1032,22 @@ export class ProjectService {
10501032
}
10511033
}
10521034
})) > 0
1053-
)
1035+
1036+
if (projectExist) {
1037+
this.logger.error(
1038+
`Project ${projectName} already exists in workspace ${workspaceId}`
1039+
)
1040+
throw new ConflictException(
1041+
constructErrorBody(
1042+
'Project already exists',
1043+
`Project ${projectName} already exists in the workspace`
1044+
)
1045+
)
1046+
} else {
1047+
this.logger.log(
1048+
`Project ${projectName} does not exist in workspace ${workspaceId}`
1049+
)
1050+
}
10541051
}
10551052

10561053
/**

apps/api/src/secret/service/secret.service.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1129,10 +1129,7 @@ export class SecretService {
11291129
const errorMessage = `Project ${project.slug} does not store the private key`
11301130
this.logger.error(errorMessage)
11311131
throw new BadRequestException(
1132-
constructErrorBody(
1133-
'Can not decrypt secret values',
1134-
`Project ${project.slug} does not store the private key`
1135-
)
1132+
constructErrorBody('Can not decrypt secret values', errorMessage)
11361133
)
11371134
}
11381135

0 commit comments

Comments
 (0)