From 3f751f48a31dccb4cf1ca54f8569c06ea01eccb2 Mon Sep 17 00:00:00 2001 From: Saumya Pandey Date: Thu, 4 Mar 2021 03:54:36 +0530 Subject: [PATCH] Fix bug in collections.list --- server/api/collections.js | 32 +++++++++++++++++++++++++++++- server/utils/collectionIndexing.js | 17 ++++++++-------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/server/api/collections.js b/server/api/collections.js index 12f771d10c..c1b88b6100 100644 --- a/server/api/collections.js +++ b/server/api/collections.js @@ -577,7 +577,10 @@ router.post("collections.list", auth(), pagination(), async (ctx) => { ); if (nullIndexCollection !== -1) { - await collectionIndexing(ctx.state.user.teamId); + const indexedCollections = await collectionIndexing(ctx.state.user.teamId); + collections.forEach((collection) => { + collection.index = indexedCollections[collection.id]; + }); } ctx.body = { @@ -617,4 +620,31 @@ router.post("collections.delete", auth(), async (ctx) => { }; }); +router.post("collections.move", auth(), async (ctx) => { + const { index, id } = ctx.body; + ctx.assertPresent(index, "index is required"); + ctx.assertUuid(id, "id must be a uuid"); + + const user = ctx.state.user; + const collection = await Collection.findByPk(id); + authorize(user, "move", collection); + + collection.index = index; + + await collection.save(); + + await Event.create({ + name: "collections.move", + collectionId: collection.id, + teamId: collection.teamId, + actorId: user.id, + data: { name: collection.name }, + ip: ctx.request.ip, + }); + + ctx.body = { + success: true, + policies: presentPolicies(user, [collection]), + }; +}); export default router; diff --git a/server/utils/collectionIndexing.js b/server/utils/collectionIndexing.js index 330ef82c5c..6d9ec12823 100644 --- a/server/utils/collectionIndexing.js +++ b/server/utils/collectionIndexing.js @@ -13,27 +13,19 @@ export default async function collectionIndexing(teamId: string) { // a set to store the index value of collections. const indexSet = new Set(); - // if there is index collision or collection with null index, we can return early - let nonull = true; // make the index null, if there is index collision sortableCollections = sortableCollections.map((collection) => { if (collection[1] === null) { - nonull = false; return collection; } else if (indexSet.has(collection[1])) { collection[1] = null; - nonull = false; return collection; } indexSet.add(collection[1]); return collection; }); - if (nonull) { - return; - } - //sort the collection in ASC and such that null values are the end sortableCollections.sort((a, b) => { if (a[1] === b[1]) { @@ -50,10 +42,17 @@ export default async function collectionIndexing(teamId: string) { let previousCollectionIndex = null; for (const collection of sortableCollections) { if (collection[1] === null) { - const index = fractionalIndex(previousCollectionIndex, collection[1]); + const index = fractionalIndex(collection[1], previousCollectionIndex); collection[0].index = index; await collection[0].save(); } previousCollectionIndex = collection[0].index; } + + const indexedCollections = {}; + sortableCollections.forEach((collection) => { + indexedCollections[collection[0].id] = collection[0].index; + }); + + return indexedCollections; }