7
7
Body ,
8
8
Param ,
9
9
Query ,
10
- NotFoundException ,
11
- InternalServerErrorException ,
10
+ UseInterceptors ,
12
11
} from '@nestjs/common' ;
13
12
import {
14
13
ApiTags ,
@@ -24,18 +23,20 @@ import { UserRole } from 'src/shared/enums/userRole.enum';
24
23
import { Scopes } from 'src/shared/decorators/scopes.decorator' ;
25
24
import { Scope } from 'src/shared/enums/scopes.enum' ;
26
25
import {
26
+ ScorecardPaginatedResponseDto ,
27
27
ScorecardRequestDto ,
28
28
ScorecardResponseDto ,
29
- mapScorecardRequestToDto ,
29
+ ScorecardWithGroupResponseDto ,
30
30
} from 'src/dto/scorecard.dto' ;
31
31
import { ChallengeTrack } from 'src/shared/enums/challengeTrack.enum' ;
32
- import { PrismaService } from '../../shared/modules/global/prisma.service' ;
32
+ import { ScoreCardService } from './scorecard.service' ;
33
+ import { PaginationHeaderInterceptor } from 'src/interceptors/PaginationHeaderInterceptor' ;
33
34
34
35
@ApiTags ( 'Scorecard' )
35
36
@ApiBearerAuth ( )
36
37
@Controller ( '/scorecards' )
37
38
export class ScorecardController {
38
- constructor ( private readonly prisma : PrismaService ) { }
39
+ constructor ( private readonly scorecardService : ScoreCardService ) { }
39
40
40
41
@Post ( )
41
42
@Roles ( UserRole . Admin )
@@ -48,27 +49,13 @@ export class ScorecardController {
48
49
@ApiResponse ( {
49
50
status : 201 ,
50
51
description : 'Scorecard added successfully.' ,
51
- type : ScorecardResponseDto ,
52
+ type : ScorecardWithGroupResponseDto ,
52
53
} )
53
54
@ApiResponse ( { status : 403 , description : 'Forbidden.' } )
54
55
async addScorecard (
55
56
@Body ( ) body : ScorecardRequestDto ,
56
- ) : Promise < ScorecardResponseDto > {
57
- const data = await this . prisma . scorecard . create ( {
58
- data : mapScorecardRequestToDto ( body ) ,
59
- include : {
60
- scorecardGroups : {
61
- include : {
62
- sections : {
63
- include : {
64
- questions : true ,
65
- } ,
66
- } ,
67
- } ,
68
- } ,
69
- } ,
70
- } ) ;
71
- return data as ScorecardResponseDto ;
57
+ ) : Promise < ScorecardWithGroupResponseDto > {
58
+ return await this . scorecardService . addScorecard ( body ) ;
72
59
}
73
60
74
61
@Put ( '/:id' )
@@ -87,41 +74,15 @@ export class ScorecardController {
87
74
@ApiResponse ( {
88
75
status : 200 ,
89
76
description : 'Scorecard updated successfully.' ,
90
- type : ScorecardResponseDto ,
77
+ type : ScorecardWithGroupResponseDto ,
91
78
} )
92
79
@ApiResponse ( { status : 403 , description : 'Forbidden.' } )
93
80
@ApiResponse ( { status : 404 , description : 'Scorecard not found.' } )
94
81
async editScorecard (
95
82
@Param ( 'id' ) id : string ,
96
- @Body ( ) body : ScorecardRequestDto ,
97
- ) : Promise < ScorecardResponseDto > {
98
- console . log ( JSON . stringify ( body ) ) ;
99
-
100
- const data = await this . prisma . scorecard
101
- . update ( {
102
- where : { id } ,
103
- data : mapScorecardRequestToDto ( body ) ,
104
- include : {
105
- scorecardGroups : {
106
- include : {
107
- sections : {
108
- include : {
109
- questions : true ,
110
- } ,
111
- } ,
112
- } ,
113
- } ,
114
- } ,
115
- } )
116
- . catch ( ( error ) => {
117
- if ( error . code !== 'P2025' ) {
118
- throw new NotFoundException ( { message : `Scorecard not found.` } ) ;
119
- }
120
- throw new InternalServerErrorException ( {
121
- message : `Error: ${ error . code } ` ,
122
- } ) ;
123
- } ) ;
124
- return data as ScorecardResponseDto ;
83
+ @Body ( ) body : ScorecardWithGroupResponseDto ,
84
+ ) : Promise < ScorecardWithGroupResponseDto > {
85
+ return await this . scorecardService . editScorecard ( id , body ) ;
125
86
}
126
87
127
88
@Delete ( ':id' )
@@ -139,24 +100,12 @@ export class ScorecardController {
139
100
@ApiResponse ( {
140
101
status : 200 ,
141
102
description : 'Scorecard deleted successfully.' ,
142
- type : ScorecardResponseDto ,
103
+ type : ScorecardWithGroupResponseDto ,
143
104
} )
144
105
@ApiResponse ( { status : 403 , description : 'Forbidden.' } )
145
106
@ApiResponse ( { status : 404 , description : 'Scorecard not found.' } )
146
107
async deleteScorecard ( @Param ( 'id' ) id : string ) {
147
- await this . prisma . scorecard
148
- . delete ( {
149
- where : { id } ,
150
- } )
151
- . catch ( ( error ) => {
152
- if ( error . code !== 'P2025' ) {
153
- throw new NotFoundException ( { message : `Scorecard not found.` } ) ;
154
- }
155
- throw new InternalServerErrorException ( {
156
- message : `Error: ${ error . code } ` ,
157
- } ) ;
158
- } ) ;
159
- return { message : `Scorecard ${ id } deleted successfully.` } ;
108
+ return await this . scorecardService . deleteScorecard ( id ) ;
160
109
}
161
110
162
111
@Get ( '/:id' )
@@ -173,38 +122,15 @@ export class ScorecardController {
173
122
@ApiResponse ( {
174
123
status : 200 ,
175
124
description : 'Scorecard retrieved successfully.' ,
176
- type : ScorecardResponseDto ,
125
+ type : ScorecardWithGroupResponseDto ,
177
126
} )
178
127
@ApiResponse ( { status : 404 , description : 'Scorecard not found.' } )
179
- async viewScorecard ( @Param ( 'id' ) id : string ) : Promise < ScorecardResponseDto > {
180
- const data = await this . prisma . scorecard
181
- . findUniqueOrThrow ( {
182
- where : { id } ,
183
- include : {
184
- scorecardGroups : {
185
- include : {
186
- sections : {
187
- include : {
188
- questions : true ,
189
- } ,
190
- } ,
191
- } ,
192
- } ,
193
- } ,
194
- } )
195
- . catch ( ( error ) => {
196
- if ( error . code !== 'P2025' ) {
197
- throw new NotFoundException ( { message : `Scorecard not found.` } ) ;
198
- }
199
- throw new InternalServerErrorException ( {
200
- message : `Error: ${ error . code } ` ,
201
- } ) ;
202
- } ) ;
203
- return data as ScorecardResponseDto ;
128
+ async viewScorecard ( @Param ( 'id' ) id : string ) : Promise < ScorecardWithGroupResponseDto > {
129
+ return await this . scorecardService . viewScorecard ( id ) ;
204
130
}
205
131
206
132
@Get ( )
207
- @Roles ( UserRole . Admin , UserRole . Copilot )
133
+ @Roles ( UserRole . Admin )
208
134
@Scopes ( Scope . ReadScorecard )
209
135
@ApiOperation ( {
210
136
summary : 'Search scorecards' ,
@@ -249,34 +175,31 @@ export class ScorecardController {
249
175
description : 'List of matching scorecards' ,
250
176
type : [ ScorecardResponseDto ] ,
251
177
} )
178
+ @UseInterceptors ( PaginationHeaderInterceptor )
252
179
async searchScorecards (
253
- @Query ( 'challengeTrack' ) challengeTrack ?: ChallengeTrack ,
254
- @Query ( 'challengeType' ) challengeType ?: string ,
180
+ @Query ( 'challengeTrack' ) challengeTrack ?: ChallengeTrack | ChallengeTrack [ ] ,
181
+ @Query ( 'challengeType' ) challengeType ?: string | string [ ] ,
255
182
@Query ( 'name' ) name ?: string ,
256
183
@Query ( 'page' ) page : number = 1 ,
257
184
@Query ( 'perPage' ) perPage : number = 10 ,
258
- ) {
259
- const skip = ( page - 1 ) * perPage ;
260
- const data = await this . prisma . scorecard . findMany ( {
261
- where : {
262
- ...( challengeTrack && { challengeTrack } ) ,
263
- ...( challengeType && { challengeType } ) ,
264
- ...( name && { name : { contains : name , mode : 'insensitive' } } ) ,
265
- } ,
266
- include : {
267
- scorecardGroups : {
268
- include : {
269
- sections : {
270
- include : {
271
- questions : true ,
272
- } ,
273
- } ,
274
- } ,
275
- } ,
276
- } ,
277
- skip,
278
- take : perPage ,
185
+ ) : Promise < ScorecardPaginatedResponseDto > {
186
+ const challengeTrackArray = Array . isArray ( challengeTrack )
187
+ ? challengeTrack
188
+ : challengeTrack
189
+ ? [ challengeTrack ]
190
+ : [ ] ;
191
+ const challengeTypeArray = Array . isArray ( challengeType )
192
+ ? challengeType
193
+ : challengeType
194
+ ? [ challengeType ]
195
+ : [ ] ;
196
+ const result = await this . scorecardService . getScoreCards ( {
197
+ challengeTrack : challengeTrackArray ,
198
+ challengeType : challengeTypeArray ,
199
+ name,
200
+ page,
201
+ perPage,
279
202
} ) ;
280
- return data as ScorecardResponseDto [ ] ;
203
+ return result ;
281
204
}
282
205
}
0 commit comments