@@ -15,6 +15,7 @@ import {
15
15
ScorecardSectionBaseDto ,
16
16
ScorecardWithGroupResponseDto ,
17
17
} from 'src/dto/scorecard.dto' ;
18
+ import { JwtUser } from 'src/shared/modules/global/jwt.service' ;
18
19
import { PrismaService } from 'src/shared/modules/global/prisma.service' ;
19
20
20
21
@Injectable ( )
@@ -28,9 +29,16 @@ export class ScoreCardService {
28
29
*/
29
30
async addScorecard (
30
31
body : ScorecardRequestDto ,
32
+ user : JwtUser ,
31
33
) : Promise < ScorecardWithGroupResponseDto > {
32
34
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
+ } ,
34
42
include : {
35
43
scorecardGroups : {
36
44
include : {
@@ -44,7 +52,7 @@ export class ScoreCardService {
44
52
} ,
45
53
} ) ;
46
54
47
- return data as ScorecardWithGroupResponseDto ;
55
+ return data as unknown as ScorecardWithGroupResponseDto ;
48
56
}
49
57
50
58
/**
@@ -54,12 +62,17 @@ export class ScoreCardService {
54
62
*/
55
63
async editScorecard (
56
64
id : string ,
57
- body : ScorecardWithGroupResponseDto ,
65
+ body : ScorecardRequestDto ,
66
+ user : JwtUser ,
58
67
) : Promise < ScorecardWithGroupResponseDto > {
59
68
const data = await this . prisma . scorecard
60
69
. update ( {
61
70
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 ,
63
76
include : {
64
77
scorecardGroups : {
65
78
include : {
@@ -81,7 +94,7 @@ export class ScoreCardService {
81
94
} ) ;
82
95
} ) ;
83
96
84
- return data as ScorecardWithGroupResponseDto ;
97
+ return data as unknown as ScorecardWithGroupResponseDto ;
85
98
}
86
99
87
100
/**
@@ -203,59 +216,70 @@ export class ScoreCardService {
203
216
}
204
217
205
218
async cloneScorecard (
206
- id : string
219
+ id : string ,
220
+ user : { userId ?: string ; isMachine : boolean } ,
207
221
) : 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 ,
218
230
} ,
219
231
} ,
220
232
} ,
221
233
} ,
222
- } ) ;
234
+ } ,
235
+ } ) ;
223
236
224
237
if ( ! original ) {
225
238
throw new NotFoundException ( { message : `Scorecard not found.` } ) ;
226
239
}
227
240
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 ) ,
232
244
createdAt : undefined ,
233
245
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 ,
237
252
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 ;
251
276
252
277
const clonedScorecard = await this . prisma . scorecard . create ( {
253
278
data : {
254
279
...original ,
255
280
id : undefined ,
256
281
name : `${ original . name } (Clone)` ,
257
- createdAt : undefined ,
258
- updatedAt : undefined ,
282
+ ...auditFields ,
259
283
scorecardGroups : {
260
284
create : cloneGroups ,
261
285
} ,
0 commit comments