@@ -69,15 +69,7 @@ export class ProjectService {
69
69
const workspaceId = workspace . id
70
70
71
71
// 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 )
81
73
82
74
// Create the public and private key pair
83
75
this . logger . log ( `Creating key pair for project ${ dto . name } ` )
@@ -115,6 +107,17 @@ export class ProjectService {
115
107
hasAdminAuthority : true
116
108
}
117
109
} )
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
+
118
121
this . logger . log (
119
122
`Admin role for workspace ${ workspace . slug } is ${ adminRole . slug } `
120
123
)
@@ -226,7 +229,7 @@ export class ProjectService {
226
229
this . prisma
227
230
)
228
231
229
- this . logger . debug ( `Created project ${ newProject } (${ newProject . slug } )` )
232
+ this . logger . debug ( `Created project ${ newProject . name } (${ newProject . slug } )` )
230
233
231
234
// It is important that we log before the private key is set
232
235
// in order to not log the private key
@@ -270,20 +273,7 @@ export class ProjectService {
270
273
} )
271
274
272
275
// 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 ) )
287
277
288
278
if ( dto . accessLevel ) {
289
279
this . logger . log ( `Access level specified while updating project.` )
@@ -330,10 +320,11 @@ export class ProjectService {
330
320
}
331
321
332
322
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 ,
337
328
description : dto . description ,
338
329
storePrivateKey : dto . storePrivateKey ,
339
330
privateKey : dto . storePrivateKey ? dto . privateKey : null ,
@@ -484,17 +475,7 @@ export class ProjectService {
484
475
this . logger . log ( `Forking project ${ projectSlug } as ${ newProjectName } ` )
485
476
486
477
// 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 )
498
479
499
480
this . logger . log ( `Creating key pair for project ${ newProjectName } ` )
500
481
const { privateKey, publicKey } = createKeyPair ( )
@@ -1033,11 +1014,12 @@ export class ProjectService {
1033
1014
private async projectExists (
1034
1015
projectName : string ,
1035
1016
workspaceId : Workspace [ 'id' ]
1036
- ) : Promise < boolean > {
1017
+ ) : Promise < void > {
1037
1018
this . logger . log (
1038
1019
`Checking if project ${ projectName } exists in workspace ${ workspaceId } `
1039
1020
)
1040
- return (
1021
+
1022
+ const projectExist : boolean =
1041
1023
( await this . prisma . workspaceMember . count ( {
1042
1024
where : {
1043
1025
workspaceId,
@@ -1050,7 +1032,22 @@ export class ProjectService {
1050
1032
}
1051
1033
}
1052
1034
} ) ) > 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
+ }
1054
1051
}
1055
1052
1056
1053
/**
0 commit comments