Files
outline/server/utils/removeIndexCollision.ts
T
Tom Moor 754672e9dc Centralize document access rules for list queries (#14783)
* wip

* Route document list queries through findAllWithCtx

Add findAllWithCtx and countWithCtx on Document and Template, unsafe* methods
for system reads, and a lint rule that bans direct list queries. Remove the
cached membership list, accessibleSql and findByIds in favour of one gate.

* Remove test

* Include drafts when loading starred documents

* Scope the access gate to the user's team, move whereToSql to QueryHelper

* Move collection access to Collection.findAllWithCtx

Collection.accessibleIds keeps the cached id list inside the collection gate, and user.collectionIds() is a shortcut to it. List queries use findAllWithCtx and countWithCtx, system reads use the shared unsafe methods, and the admin listing in collections.list is explicit.

* Check permanentDelete for each document in empty_trash
2026-11-27 15:30:43 -05:00

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.unsafeFindAll({
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);
}