Fix bug in collections.list

This commit is contained in:
Saumya Pandey committed 2021-03-04 03:54:36 +05:30
1 parent af2086f99f
commit 3f751f48a3
2 files changed
+39 -10

No files matched your search

+31 -1
View File
@@ -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;
+8 -9
View File
@@ -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;
}