feat: Allow sorting collections in sidebar (#1870)

closes #1759

Co-authored-by: Tom Moor <tom.moor@gmail.com>
This commit is contained in:
Saumya Pandey
2021-03-19 05:57:33 +05:30
committed by GitHub
parent b93002ad93
commit 46bcc2e2ae
21 changed files with 677 additions and 120 deletions

View File

@ -0,0 +1,45 @@
// @flow
import fractionalIndex from "fractional-index";
import { Collection } from "../models";
import { sequelize, Op } from "../sequelize";
/**
*
* @param teamId The team id whose collections has to be fetched
* @param index the index for which collision has to be checked
* @returns An index, if there is collision returns a new index otherwise the same index
*/
export default async function removeIndexCollision(
teamId: string,
index: string
) {
const collection = await Collection.findOne({
where: { teamId, deletedAt: null, index },
});
if (!collection) {
return index;
}
const nextCollection = await Collection.findAll({
where: {
teamId,
deletedAt: null,
index: {
[Op.gt]: index,
},
},
attributes: ["id", "index"],
limit: 1,
order: [
sequelize.literal('"collection"."index" collate "C"'),
["updatedAt", "DESC"],
],
});
const nextCollectionIndex = nextCollection.length
? nextCollection[0].index
: null;
return fractionalIndex(index, nextCollectionIndex);
}