@@ -5,16 +5,18 @@ import {
5
5
} from '@nestjs/common' ;
6
6
import { Prisma } from '@prisma/client' ;
7
7
import {
8
+ mapScorecardRequestForCreate ,
8
9
mapScorecardRequestToDto ,
9
- // ScorecardGroupBaseDto,
10
+ ScorecardGroupBaseDto ,
10
11
ScorecardPaginatedResponseDto ,
11
12
ScorecardQueryDto ,
12
- // ScorecardQuestionBaseDto,
13
+ ScorecardQuestionBaseDto ,
13
14
ScorecardRequestDto ,
14
15
ScorecardResponseDto ,
15
- // ScorecardSectionBaseDto,
16
+ ScorecardSectionBaseDto ,
16
17
ScorecardWithGroupResponseDto ,
17
18
} from 'src/dto/scorecard.dto' ;
19
+ import { JwtUser } from 'src/shared/modules/global/jwt.service' ;
18
20
import { PrismaService } from 'src/shared/modules/global/prisma.service' ;
19
21
20
22
@Injectable ( )
@@ -28,9 +30,16 @@ export class ScoreCardService {
28
30
*/
29
31
async addScorecard (
30
32
body : ScorecardRequestDto ,
33
+ user : JwtUser ,
31
34
) : Promise < ScorecardWithGroupResponseDto > {
32
35
const data = await this . prisma . scorecard . create ( {
33
- data : mapScorecardRequestToDto ( body ) ,
36
+ data : {
37
+ ...( mapScorecardRequestForCreate ( {
38
+ ...body ,
39
+ createdBy : user . isMachine ? 'System' : ( user . userId as string ) ,
40
+ updatedBy : user . isMachine ? 'System' : ( user . userId as string ) ,
41
+ } ) as any ) ,
42
+ } ,
34
43
include : {
35
44
scorecardGroups : {
36
45
include : {
@@ -44,7 +53,7 @@ export class ScoreCardService {
44
53
} ,
45
54
} ) ;
46
55
47
- return data as ScorecardWithGroupResponseDto ;
56
+ return data as unknown as ScorecardWithGroupResponseDto ;
48
57
}
49
58
50
59
/**
@@ -54,12 +63,17 @@ export class ScoreCardService {
54
63
*/
55
64
async editScorecard (
56
65
id : string ,
57
- body : ScorecardWithGroupResponseDto ,
66
+ body : ScorecardRequestDto ,
67
+ user : JwtUser ,
58
68
) : Promise < ScorecardWithGroupResponseDto > {
59
69
const data = await this . prisma . scorecard
60
70
. update ( {
61
71
where : { id } ,
62
- data : mapScorecardRequestToDto ( body ) ,
72
+ data : mapScorecardRequestToDto ( {
73
+ ...body ,
74
+ createdBy : user . isMachine ? 'System' : ( user . userId as string ) ,
75
+ updatedBy : user . isMachine ? 'System' : ( user . userId as string ) ,
76
+ } ) as any ,
63
77
include : {
64
78
scorecardGroups : {
65
79
include : {
@@ -81,7 +95,7 @@ export class ScoreCardService {
81
95
} ) ;
82
96
} ) ;
83
97
84
- return data as ScorecardWithGroupResponseDto ;
98
+ return data as unknown as ScorecardWithGroupResponseDto ;
85
99
}
86
100
87
101
/**
@@ -202,79 +216,87 @@ export class ScoreCardService {
202
216
} ;
203
217
}
204
218
205
- // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
206
- async cloneScorecard ( id : string ) : Promise < ScorecardResponseDto > {
207
- // const original = await this.prisma.scorecard.findUnique({
208
- // where: { id },
209
- // include: {
210
- // scorecardGroups: {
211
- // include: {
212
- // sections: {
213
- // include: {
214
- // questions: true,
215
- // },
216
- // },
217
- // },
218
- // },
219
- // },
220
- // });
219
+ async cloneScorecard (
220
+ id : string ,
221
+ user : { userId ?: string ; isMachine : boolean } ,
222
+ ) : Promise < ScorecardResponseDto > {
223
+ const original = await this . prisma . scorecard . findUnique ( {
224
+ where : { id } ,
225
+ include : {
226
+ scorecardGroups : {
227
+ include : {
228
+ sections : {
229
+ include : {
230
+ questions : true ,
231
+ } ,
232
+ } ,
233
+ } ,
234
+ } ,
235
+ } ,
236
+ } ) ;
221
237
222
- // if (!original) {
223
- // throw new NotFoundException({ message: `Scorecard not found.` });
224
- // }
238
+ if ( ! original ) {
239
+ throw new NotFoundException ( { message : `Scorecard not found.` } ) ;
240
+ }
225
241
226
- // // Remove id fields from nested objects for cloning
227
- // const cloneGroups = original.scorecardGroups.map(
228
- // (group: ScorecardGroupBaseDto) => ({
229
- // ...group,
230
- // id: undefined,
231
- // createdAt: undefined,
232
- // updatedAt: undefined,
233
- // scorecardId: undefined,
234
- // sections: group.sections.map((section: ScorecardSectionBaseDto) => ({
235
- // ...section,
236
- // id: undefined,
237
- // createdAt: undefined,
238
- // updatedAt: undefined,
239
- // scorecardGroupId: undefined,
240
- // questions: section.questions.map(
241
- // (question: ScorecardQuestionBaseDto) => ({
242
- // ...question,
243
- // id: undefined,
244
- // createdAt: undefined,
245
- // updatedAt: undefined,
246
- // sectionId: undefined,
247
- // scorecardSectionId: undefined,
248
- // }),
249
- // ),
250
- // })),
251
- // }),
252
- // );
242
+ const auditFields = {
243
+ createdBy : user . isMachine ? 'System' : ( user . userId as string ) ,
244
+ updatedBy : user . isMachine ? 'System' : ( user . userId as string ) ,
245
+ createdAt : undefined ,
246
+ updatedAt : undefined ,
247
+ } ;
253
248
254
- // const clonedScorecard = await this.prisma.scorecard.create({
255
- // data: {
256
- // ...original,
257
- // id: undefined,
258
- // name: `${original.name} (Clone)`,
259
- // createdAt: undefined,
260
- // updatedAt: undefined,
261
- // scorecardGroups: {
262
- // create: cloneGroups,
263
- // },
264
- // },
265
- // include: {
266
- // scorecardGroups: {
267
- // include: {
268
- // sections: {
269
- // include: {
270
- // questions: true,
271
- // },
272
- // },
273
- // },
274
- // },
275
- // },
276
- // });
277
- const clonedScorecard = { } ;
249
+ // Remove id fields from nested objects for cloning
250
+ const cloneGroups = original . scorecardGroups . map (
251
+ ( group : ScorecardGroupBaseDto ) => ( {
252
+ ...group ,
253
+ id : undefined ,
254
+ ...auditFields ,
255
+ scorecardId : undefined ,
256
+ sections : {
257
+ create : group . sections . map ( ( section : ScorecardSectionBaseDto ) => ( {
258
+ ...section ,
259
+ id : undefined ,
260
+ ...auditFields ,
261
+ scorecardGroupId : undefined ,
262
+ questions : {
263
+ create : section . questions . map (
264
+ ( question : ScorecardQuestionBaseDto ) => ( {
265
+ ...question ,
266
+ id : undefined ,
267
+ ...auditFields ,
268
+ sectionId : undefined ,
269
+ scorecardSectionId : undefined ,
270
+ } ) ,
271
+ ) ,
272
+ } ,
273
+ } ) ) ,
274
+ } ,
275
+ } ) ,
276
+ ) as any ;
277
+
278
+ const clonedScorecard = await this . prisma . scorecard . create ( {
279
+ data : {
280
+ ...original ,
281
+ id : undefined ,
282
+ name : `${ original . name } (Clone)` ,
283
+ ...auditFields ,
284
+ scorecardGroups : {
285
+ create : cloneGroups ,
286
+ } ,
287
+ } ,
288
+ include : {
289
+ scorecardGroups : {
290
+ include : {
291
+ sections : {
292
+ include : {
293
+ questions : true ,
294
+ } ,
295
+ } ,
296
+ } ,
297
+ } ,
298
+ } ,
299
+ } ) ;
278
300
279
301
return clonedScorecard as ScorecardResponseDto ;
280
302
}
0 commit comments