mirror of
https://github.com/outline/outline.git
synced 2026-01-06 02:59:54 -06:00
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import fractionalIndex from "fractional-index";
|
|
import { Sequelize, type FindOptions } from "sequelize";
|
|
import Collection from "@server/models/Collection";
|
|
|
|
/**
|
|
*
|
|
* @param teamId The team id whose collections has to be fetched
|
|
* @param index the index for which collision has to be checked
|
|
* @param options Additional options to be passed to the query
|
|
* @returns An index, if there is collision returns a new index otherwise the same index
|
|
*/
|
|
export default async function removeIndexCollision(
|
|
teamId: string,
|
|
index: string,
|
|
options: FindOptions = {}
|
|
) {
|
|
const collection = await Collection.findOne({
|
|
where: {
|
|
teamId,
|
|
deletedAt: null,
|
|
index,
|
|
},
|
|
...options,
|
|
});
|
|
|
|
if (!collection) {
|
|
return index;
|
|
}
|
|
|
|
const nextCollection = await Collection.findAll({
|
|
where: {
|
|
teamId,
|
|
deletedAt: null,
|
|
index: Sequelize.literal(`"collection"."index" collate "C" > :index`),
|
|
},
|
|
attributes: ["id", "index"],
|
|
limit: 1,
|
|
order: [
|
|
Sequelize.literal('"collection"."index" collate "C"'),
|
|
["updatedAt", "DESC"],
|
|
],
|
|
replacements: { index },
|
|
...options,
|
|
});
|
|
const nextCollectionIndex = nextCollection.length
|
|
? nextCollection[0].index
|
|
: null;
|
|
return fractionalIndex(index, nextCollectionIndex);
|
|
}
|