Skip to content

Commit 433c764

Browse files
authored
Merge pull request #21 from topcoder-platform/PM-1504_create-edit-scorecard
Update scorecard data validation & throw error on not found
2 parents ce08f33 + ade1235 commit 433c764

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/api/scorecard/scorecard.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ export class ScoreCardService {
6666
body: ScorecardRequestDto,
6767
user: JwtUser,
6868
): Promise<ScorecardWithGroupResponseDto> {
69+
const original = await this.prisma.scorecard.findUnique({
70+
where: { id },
71+
});
72+
73+
if (!original) {
74+
throw new NotFoundException({ message: `Scorecard not found.` });
75+
}
76+
6977
const data = await this.prisma.scorecard
7078
.update({
7179
where: { id },

src/dto/scorecard.dto.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import {
55
IsArray,
66
IsBoolean,
77
IsEnum,
8+
IsNotEmpty,
89
IsNumber,
910
IsOptional,
1011
IsString,
12+
Max,
1113
Min,
1214
ValidateNested,
1315
} from 'class-validator';
@@ -61,6 +63,7 @@ export class ScorecardQuestionBaseDto {
6163
example: 'What is the challenge?',
6264
})
6365
@IsString()
66+
@IsNotEmpty()
6467
description: string;
6568

6669
@ApiProperty({
@@ -72,6 +75,8 @@ export class ScorecardQuestionBaseDto {
7275

7376
@ApiProperty({ description: 'The weight of the question', example: 10 })
7477
@IsNumber()
78+
@Min(0)
79+
@Max(100)
7580
weight: number;
7681

7782
@ApiProperty({
@@ -119,10 +124,13 @@ export class ScorecardSectionBaseDto {
119124
example: 'Technical Skills',
120125
})
121126
@IsString()
127+
@IsNotEmpty()
122128
name: string;
123129

124130
@ApiProperty({ description: 'The weight of the section', example: 20 })
125131
@IsNumber()
132+
@Min(0)
133+
@Max(100)
126134
weight: number;
127135

128136
@ApiProperty({ description: 'Sort order of the section', example: 1 })
@@ -162,10 +170,13 @@ export class ScorecardGroupBaseDto {
162170

163171
@ApiProperty({ description: 'The name of the group', example: 'Group A' })
164172
@IsString()
173+
@IsNotEmpty()
165174
name: string;
166175

167176
@ApiProperty({ description: 'The weight of the group', example: 30 })
168177
@IsNumber()
178+
@Min(0)
179+
@Max(100)
169180
weight: number;
170181

171182
@ApiProperty({ description: 'Sort order of the group', example: 1 })
@@ -241,6 +252,7 @@ export class ScorecardBaseDto {
241252

242253
@ApiProperty({ description: 'The maximum score', example: 100 })
243254
@IsNumber()
255+
@Max(100)
244256
@IsGreaterThan('minScore')
245257
maxScore: number;
246258

@@ -330,18 +342,22 @@ export function mapScorecardRequestForCreate(request: ScorecardRequestDto) {
330342

331343
return {
332344
...request,
345+
id: undefined,
333346
...userFields,
334347
scorecardGroups: {
335348
create: request.scorecardGroups.map((group) => ({
336349
...group,
350+
id: undefined,
337351
...userFields,
338352
sections: {
339353
create: group.sections.map((section) => ({
340354
...section,
355+
id: undefined,
341356
...userFields,
342357
questions: {
343358
create: section.questions.map((question) => ({
344359
...question,
360+
id: undefined,
345361
sortOrder: 1,
346362
...userFields,
347363
})),

0 commit comments

Comments
 (0)