@@ -6,10 +6,13 @@ import {
6
6
import { Prisma } from '@prisma/client' ;
7
7
import {
8
8
mapScorecardRequestToDto ,
9
+ ScorecardGroupBaseDto ,
9
10
ScorecardPaginatedResponseDto ,
10
11
ScorecardQueryDto ,
12
+ ScorecardQuestionBaseDto ,
11
13
ScorecardRequestDto ,
12
14
ScorecardResponseDto ,
15
+ ScorecardSectionBaseDto ,
13
16
ScorecardWithGroupResponseDto ,
14
17
} from 'src/dto/scorecard.dto' ;
15
18
import { PrismaService } from 'src/shared/modules/global/prisma.service' ;
@@ -198,4 +201,78 @@ export class ScoreCardService {
198
201
scoreCards : data as ScorecardResponseDto [ ] ,
199
202
} ;
200
203
}
204
+
205
+ async cloneScorecard (
206
+ id : string
207
+ ) : 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
+ } ,
218
+ } ,
219
+ } ,
220
+ } ,
221
+ } ,
222
+ } ) ;
223
+
224
+ if ( ! original ) {
225
+ throw new NotFoundException ( { message : `Scorecard not found.` } ) ;
226
+ }
227
+
228
+ // Remove id fields from nested objects for cloning
229
+ const cloneGroups = original . scorecardGroups . map ( ( group : ScorecardGroupBaseDto ) => ( {
230
+ ...group ,
231
+ id : undefined ,
232
+ createdAt : undefined ,
233
+ updatedAt : undefined ,
234
+ scorecardId : undefined ,
235
+ sections : group . sections . map ( ( section : ScorecardSectionBaseDto ) => ( {
236
+ ...section ,
237
+ 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
+ } ) ) ;
251
+
252
+ const clonedScorecard = await this . prisma . scorecard . create ( {
253
+ data : {
254
+ ...original ,
255
+ id : undefined ,
256
+ name : `${ original . name } (Clone)` ,
257
+ createdAt : undefined ,
258
+ updatedAt : undefined ,
259
+ scorecardGroups : {
260
+ create : cloneGroups ,
261
+ } ,
262
+ } ,
263
+ include : {
264
+ scorecardGroups : {
265
+ include : {
266
+ sections : {
267
+ include : {
268
+ questions : true ,
269
+ } ,
270
+ } ,
271
+ } ,
272
+ } ,
273
+ } ,
274
+ } ) ;
275
+
276
+ return clonedScorecard as ScorecardResponseDto ;
277
+ }
201
278
}
0 commit comments