@@ -20,7 +20,7 @@ import {
20
20
queryCollections ,
21
21
queryInsightsProjectById ,
22
22
queryInsightsProjects ,
23
- softDeleteInsightsProjectRepositories ,
23
+ softDeleteMissingInsightsProjectRepositories ,
24
24
updateCollection ,
25
25
updateInsightsProject ,
26
26
upsertInsightsProjectRepositories ,
@@ -364,21 +364,20 @@ export class CollectionService extends LoggerBase {
364
364
offset,
365
365
}
366
366
}
367
-
368
- static normalizeRepositories (
369
- repositories ?: string [ ] | { platform : string ; url : string } [ ]
370
- ) : string [ ] {
371
- if ( ! repositories ) return [ ] ;
372
367
373
- if ( typeof repositories [ 0 ] === 'string' ) {
374
- return repositories as string [ ] ;
375
- }
368
+ static normalizeRepositories (
369
+ repositories ?: string [ ] | { platform : string ; url : string } [ ] ,
370
+ ) : string [ ] {
371
+ if ( ! repositories ) return [ ]
376
372
377
- return ( repositories as { platform : string ; url : string } [ ] ) . map ( r => r . url ) ;
378
- }
373
+ if ( typeof repositories [ 0 ] === 'string' ) {
374
+ return repositories as string [ ]
375
+ }
379
376
377
+ return ( repositories as { platform : string ; url : string } [ ] ) . map ( ( r ) => r . url )
378
+ }
380
379
381
- async updateInsightsProject ( id : string , project : Partial < ICreateInsightsProject > ) {
380
+ async updateInsightsProject ( insightsProjectId : string , project : Partial < ICreateInsightsProject > ) {
382
381
return SequelizeRepository . withTx ( this . options , async ( tx ) => {
383
382
const qx = SequelizeRepository . getQueryExecutor ( this . options , tx )
384
383
@@ -388,26 +387,23 @@ export class CollectionService extends LoggerBase {
388
387
project . isLF = segment ?. isLF ?? false
389
388
}
390
389
391
- await updateInsightsProject ( qx , id , project )
392
-
393
- console . log ( `repositories: ${ project . repositories } and id: ${ id } ` )
394
-
395
- if ( project . repositories ?. length > 0 ) {
396
- // TODO: sistemare tutto in modo che le repository possono essere anche zero
397
- const repositories = CollectionService . normalizeRepositories ( project . repositories )
398
-
399
- await upsertInsightsProjectRepositories ( qx , `${ id } ` , repositories )
400
-
401
- await softDeleteInsightsProjectRepositories ( qx , id , repositories )
402
- }
390
+ await updateInsightsProject ( qx , insightsProjectId , project )
391
+
392
+ const repositories = CollectionService . normalizeRepositories ( project . repositories )
403
393
394
+ await upsertInsightsProjectRepositories ( qx , { insightsProjectId, repositories } )
395
+
396
+ await softDeleteMissingInsightsProjectRepositories ( qx , {
397
+ insightsProjectId,
398
+ repositories,
399
+ } )
404
400
405
401
if ( project . collections ) {
406
- await disconnectProjectsAndCollections ( qx , { insightsProjectId : id } )
402
+ await disconnectProjectsAndCollections ( qx , { insightsProjectId } )
407
403
await connectProjectsAndCollections (
408
404
qx ,
409
405
project . collections . map ( ( c ) => ( {
410
- insightsProjectId : id ,
406
+ insightsProjectId,
411
407
collectionId : c ,
412
408
starred : project . starred ?? true ,
413
409
} ) ) ,
@@ -418,7 +414,7 @@ export class CollectionService extends LoggerBase {
418
414
...this . options ,
419
415
transaction : tx ,
420
416
} )
421
- return txSvc . findInsightsProjectById ( id )
417
+ return txSvc . findInsightsProjectById ( insightsProjectId )
422
418
} )
423
419
}
424
420
0 commit comments