@@ -284,83 +284,69 @@ export async function deleteCollaborator(
284284 return true ;
285285}
286286
287- export async function addPod ( _ , { repoId, parent, index, input } , { userId } ) {
288- // make sure the repo is writable by this user
287+ export async function updatePod ( _ , { id, repoId, input } , { userId } ) {
289288 if ( ! userId ) throw new Error ( "Not authenticated." ) ;
290289 await ensureRepoEditAccess ( { repoId, userId } ) ;
291-
292- // update all other records
293- await prisma . pod . updateMany ( {
290+ // if repoId has id, just update
291+ let pod_found = await prisma . pod . findFirst ( {
294292 where : {
293+ id,
295294 repo : {
296295 id : repoId ,
297296 } ,
298- index : {
299- gte : index ,
300- } ,
301- parent :
302- parent === "ROOT"
303- ? null
304- : {
305- id : parent ,
306- } ,
307- } ,
308- data : {
309- index : {
310- increment : 1 ,
311- } ,
312297 } ,
313298 } ) ;
314-
315- let { id : podId } = input ;
316- const pod = await prisma . pod . create ( {
317- data : {
318- id : podId ,
319- ...input ,
320- // In case of [], create will throw an error. Thus I have to pass undefined.
321- children : input . children . length > 0 ? input . children : undefined ,
322- index,
323- repo : {
324- connect : {
325- id : repoId ,
299+ if ( pod_found ) {
300+ // if repoId doesn't have id, create it IF input.parent exists
301+ const pod = await prisma . pod . update ( {
302+ where : {
303+ id,
304+ } ,
305+ data : {
306+ ...input ,
307+ parent :
308+ input . parent && input . parent !== "ROOT"
309+ ? {
310+ connect : {
311+ id : input . parent ,
312+ } ,
313+ }
314+ : undefined ,
315+ children : {
316+ connect : input . children ?. map ( ( id ) => ( { id } ) ) ,
326317 } ,
327318 } ,
328- parent :
329- parent === "ROOT"
330- ? undefined
331- : {
332- connect : {
333- id : parent ,
334- } ,
335- } ,
336- } ,
337- } ) ;
338-
339- return true ;
340- }
341-
342- export async function updatePod ( _ , { id, input } , { userId } ) {
343- if ( ! userId ) throw new Error ( "Not authenticated." ) ;
344- await ensurePodEditAccess ( { id, userId } ) ;
345- const pod = await prisma . pod . update ( {
346- where : {
347- id,
348- } ,
349- data : {
350- ...input ,
351- parent :
352- input . parent && input . parent !== "ROOT"
353- ? {
354- connect : {
355- id : input . parent ,
356- } ,
357- }
358- : undefined ,
359- children : {
360- connect : input . children ?. map ( ( id ) => ( { id } ) ) ,
319+ } ) ;
320+ } else {
321+ // if repoId doesn't have id, create it IF input.parent exists, otherwise throw error.
322+ await prisma . pod . create ( {
323+ data : {
324+ id,
325+ ...input ,
326+ // Dummy index because it is a required field for historical reasons.
327+ index : 0 ,
328+ parent :
329+ input . parent && input . parent !== "ROOT"
330+ ? {
331+ connect : {
332+ id : input . parent ,
333+ } ,
334+ }
335+ : undefined ,
336+ // In case of [], create will throw an error. Thus I have to pass undefined.
337+ // children: input.children.length > 0 ? input.children : undefined,
338+ children : {
339+ connect : input . children ?. map ( ( id ) => ( { id } ) ) ,
340+ } ,
341+ repo : {
342+ connect : {
343+ id : repoId ,
344+ } ,
345+ } ,
361346 } ,
362- } ,
363- } ) ;
347+ } ) ;
348+ }
349+
364350 return true ;
365351}
366352
0 commit comments