Skip to content

Commit 62d3f89

Browse files
committed
PM-1504 - create/edit scorecard
1 parent 704bb4c commit 62d3f89

File tree

5 files changed

+299
-78
lines changed

5 files changed

+299
-78
lines changed

src/api/scorecard/scorecard.controller.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import { ChallengeTrack } from 'src/shared/enums/challengeTrack.enum';
3232
import { ScoreCardService } from './scorecard.service';
3333
import { PaginationHeaderInterceptor } from 'src/interceptors/PaginationHeaderInterceptor';
3434
import { $Enums } from '@prisma/client';
35+
import { User } from 'src/shared/decorators/user.decorator';
36+
import { JwtUser } from 'src/shared/modules/global/jwt.service';
3537

3638
@ApiTags('Scorecard')
3739
@ApiBearerAuth()
@@ -55,8 +57,9 @@ export class ScorecardController {
5557
@ApiResponse({ status: 403, description: 'Forbidden.' })
5658
async addScorecard(
5759
@Body() body: ScorecardRequestDto,
60+
@User() user: JwtUser,
5861
): Promise<ScorecardWithGroupResponseDto> {
59-
return await this.scorecardService.addScorecard(body);
62+
return await this.scorecardService.addScorecard(body, user);
6063
}
6164

6265
@Put('/:id')
@@ -81,9 +84,10 @@ export class ScorecardController {
8184
@ApiResponse({ status: 404, description: 'Scorecard not found.' })
8285
async editScorecard(
8386
@Param('id') id: string,
84-
@Body() body: ScorecardWithGroupResponseDto,
87+
@Body() body: ScorecardRequestDto,
88+
@User() user: JwtUser,
8589
): Promise<ScorecardWithGroupResponseDto> {
86-
return await this.scorecardService.editScorecard(id, body);
90+
return await this.scorecardService.editScorecard(id, body, user);
8791
}
8892

8993
@Delete(':id')
@@ -249,7 +253,10 @@ export class ScorecardController {
249253
})
250254
@ApiResponse({ status: 403, description: 'Forbidden.' })
251255
@ApiResponse({ status: 404, description: 'Scorecard not found.' })
252-
async cloneScorecard(@Param('id') id: string): Promise<ScorecardResponseDto> {
253-
return this.scorecardService.cloneScorecard(id);
256+
async cloneScorecard(
257+
@Param('id') id: string,
258+
@User() user: JwtUser,
259+
): Promise<ScorecardResponseDto> {
260+
return this.scorecardService.cloneScorecard(id, user);
254261
}
255262
}

src/api/scorecard/scorecard.service.ts

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ScorecardSectionBaseDto,
1616
ScorecardWithGroupResponseDto,
1717
} from 'src/dto/scorecard.dto';
18+
import { JwtUser } from 'src/shared/modules/global/jwt.service';
1819
import { PrismaService } from 'src/shared/modules/global/prisma.service';
1920

2021
@Injectable()
@@ -28,9 +29,16 @@ export class ScoreCardService {
2829
*/
2930
async addScorecard(
3031
body: ScorecardRequestDto,
32+
user: JwtUser,
3133
): Promise<ScorecardWithGroupResponseDto> {
3234
const data = await this.prisma.scorecard.create({
33-
data: mapScorecardRequestToDto(body),
35+
data: {
36+
...(mapScorecardRequestToDto({
37+
...body,
38+
createdBy: user.isMachine ? 'System' : (user.userId as string),
39+
updatedBy: user.isMachine ? 'System' : (user.userId as string),
40+
}) as any),
41+
},
3442
include: {
3543
scorecardGroups: {
3644
include: {
@@ -44,7 +52,7 @@ export class ScoreCardService {
4452
},
4553
});
4654

47-
return data as ScorecardWithGroupResponseDto;
55+
return data as unknown as ScorecardWithGroupResponseDto;
4856
}
4957

5058
/**
@@ -54,12 +62,17 @@ export class ScoreCardService {
5462
*/
5563
async editScorecard(
5664
id: string,
57-
body: ScorecardWithGroupResponseDto,
65+
body: ScorecardRequestDto,
66+
user: JwtUser,
5867
): Promise<ScorecardWithGroupResponseDto> {
5968
const data = await this.prisma.scorecard
6069
.update({
6170
where: { id },
62-
data: mapScorecardRequestToDto(body),
71+
data: mapScorecardRequestToDto({
72+
...body,
73+
createdBy: user.isMachine ? 'System' : (user.userId as string),
74+
updatedBy: user.isMachine ? 'System' : (user.userId as string),
75+
}) as any,
6376
include: {
6477
scorecardGroups: {
6578
include: {
@@ -81,7 +94,7 @@ export class ScoreCardService {
8194
});
8295
});
8396

84-
return data as ScorecardWithGroupResponseDto;
97+
return data as unknown as ScorecardWithGroupResponseDto;
8598
}
8699

87100
/**
@@ -203,59 +216,70 @@ export class ScoreCardService {
203216
}
204217

205218
async cloneScorecard(
206-
id: string
219+
id: string,
220+
user: { userId?: string; isMachine: boolean },
207221
): Promise<ScorecardResponseDto> {
208-
const original = await this.prisma.scorecard
209-
.findUnique({
210-
where: { id },
211-
include: {
212-
scorecardGroups: {
213-
include: {
214-
sections: {
215-
include: {
216-
questions: true,
217-
},
222+
const original = await this.prisma.scorecard.findUnique({
223+
where: { id },
224+
include: {
225+
scorecardGroups: {
226+
include: {
227+
sections: {
228+
include: {
229+
questions: true,
218230
},
219231
},
220232
},
221233
},
222-
});
234+
},
235+
});
223236

224237
if (!original) {
225238
throw new NotFoundException({ message: `Scorecard not found.` });
226239
}
227240

228-
// Remove id fields from nested objects for cloning
229-
const cloneGroups = original.scorecardGroups.map((group: ScorecardGroupBaseDto) => ({
230-
...group,
231-
id: undefined,
241+
const auditFields = {
242+
createdBy: user.isMachine ? 'System' : (user.userId as string),
243+
updatedBy: user.isMachine ? 'System' : (user.userId as string),
232244
createdAt: undefined,
233245
updatedAt: undefined,
234-
scorecardId: undefined,
235-
sections: group.sections.map((section: ScorecardSectionBaseDto) => ({
236-
...section,
246+
};
247+
248+
// Remove id fields from nested objects for cloning
249+
const cloneGroups = original.scorecardGroups.map(
250+
(group: ScorecardGroupBaseDto) => ({
251+
...group,
237252
id: undefined,
238-
createdAt: undefined,
239-
updatedAt: undefined,
240-
scorecardGroupId: undefined,
241-
questions: section.questions.map((question: ScorecardQuestionBaseDto) => ({
242-
...question,
243-
id: undefined,
244-
createdAt: undefined,
245-
updatedAt: undefined,
246-
sectionId: undefined,
247-
scorecardSectionId: undefined,
248-
})),
249-
})),
250-
}));
253+
...auditFields,
254+
scorecardId: undefined,
255+
sections: {
256+
create: group.sections.map((section: ScorecardSectionBaseDto) => ({
257+
...section,
258+
id: undefined,
259+
...auditFields,
260+
scorecardGroupId: undefined,
261+
questions: {
262+
create: section.questions.map(
263+
(question: ScorecardQuestionBaseDto) => ({
264+
...question,
265+
id: undefined,
266+
...auditFields,
267+
sectionId: undefined,
268+
scorecardSectionId: undefined,
269+
}),
270+
),
271+
},
272+
})),
273+
},
274+
}),
275+
) as any;
251276

252277
const clonedScorecard = await this.prisma.scorecard.create({
253278
data: {
254279
...original,
255280
id: undefined,
256281
name: `${original.name} (Clone)`,
257-
createdAt: undefined,
258-
updatedAt: undefined,
282+
...auditFields,
259283
scorecardGroups: {
260284
create: cloneGroups,
261285
},

0 commit comments

Comments
 (0)