@@ -433,46 +433,58 @@ async function addPods(_, { repoId, pods }, { userId }) {
433433 return true ;
434434}
435435
436- async function deletePod ( _ , { id , toDelete } , { userId } ) {
436+ async function deletePods ( _ , { ids } : { ids : string [ ] } , { userId } ) {
437437 if ( ! userId ) throw new Error ( "Not authenticated." ) ;
438- await ensurePodEditAccess ( { id, userId } ) ;
439- // find all children of this ID
440- // FIXME how to ensure atomic
441- // 1. find the parent of this node
438+ if ( ids . length === 0 ) return false ;
439+ // find the repo
442440 const pod = await prisma . pod . findFirst ( {
443- where : {
444- id : id ,
445- } ,
446- include : {
447- parent : true ,
448- } ,
441+ where : { id : ids [ 0 ] } ,
442+ include : { repo : true } ,
449443 } ) ;
450444 if ( ! pod ) throw new Error ( "Pod not found" ) ;
445+ await ensureRepoEditAccess ( { repoId : pod . repo . id , userId } ) ;
446+ // If the pod is connected to a scope, the frontend will fire deleteEdge calls
447+ // as well simultaneously. Thus, if this call is fired before the edges are
448+ // deleted, an error will be thrown.
449+ //
450+ // Additional Notes:
451+ // 1. The deleteEdge graphQL call will still be fired. This would be redundant
452+ // but should be fine.
453+ // 2. We still need the deleteEdge graphQL calls when the edge is selected and
454+ // deleted.
451455
452- // 4. update all siblings index
453- await prisma . pod . updateMany ( {
456+ const deletedEdges = await prisma . edge . deleteMany ( {
454457 where : {
455- // CAUTION where to put null is tricky
456- parent : pod . parent
457- ? {
458- id : pod . parent . id ,
459- }
460- : null ,
461- index : {
462- gt : pod . index ,
463- } ,
464- } ,
465- data : {
466- index : {
467- decrement : 1 ,
458+ OR : [
459+ {
460+ source : {
461+ id : {
462+ in : ids ,
463+ } ,
464+ } ,
465+ } ,
466+ {
467+ target : {
468+ id : {
469+ in : ids ,
470+ } ,
471+ } ,
472+ } ,
473+ ] ,
474+ repo : {
475+ id : pod . repo . id ,
468476 } ,
469477 } ,
470478 } ) ;
471- // 5. delete it and all its children
472- await prisma . pod . deleteMany ( {
479+
480+ // delete all the nodes, but make sure they are in this exact repo.
481+ const deletedPods = await prisma . pod . deleteMany ( {
473482 where : {
474483 id : {
475- in : toDelete ,
484+ in : ids ,
485+ } ,
486+ repo : {
487+ id : pod . repo . id ,
476488 } ,
477489 } ,
478490 } ) ;
@@ -492,7 +504,7 @@ export default {
492504 updateRepo,
493505 deleteRepo,
494506 updatePod,
495- deletePod ,
507+ deletePods ,
496508 addEdge,
497509 deleteEdge,
498510 addCollaborator,
0 commit comments