fix: documents.list total and DESC paging for sort=index (#14771)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-11-26 19:25:05 -05:00
1 parent 373b098dfe
commit 0db2c71099
3 files changed
+56 -5

No files matched your search

+1 -1
View File
@@ -395,7 +395,7 @@ const RecentDocuments = observer(
collectionId: collection.id,
parentDocumentId: null,
sort: collection.sort.field,
direction: collection.sort.direction,
direction: collection.sort.direction.toUpperCase(),
}}
showParentDocuments
/>
@@ -1362,6 +1362,49 @@ describe("#documents.list", () => {
expect(body3.data[4].id).toEqual(expectedOrder[24].id);
});
it("should return total and page from the end with collection index sort", async () => {
const user = await buildUser();
const collection = await buildCollection({
userId: user.id,
teamId: user.teamId,
});
for (let i = 0; i < 25; i++) {
await buildDocument({
userId: user.id,
collectionId: collection.id,
teamId: user.teamId,
});
}
await collection.reload();
const structureIds = (collection.documentStructure ?? []).map(
(node) => node.id
);
const body = { collectionId: collection.id, sort: "index", limit: 10 };
const res = await server.post("/api/documents.list", user, {
body: { ...body, direction: "ASC" },
});
const json = await res.json();
expect(res.status).toEqual(200);
expect(json.pagination.total).toEqual(25);
const clientRes = await server.post("/api/documents.list", user, {
body: { ...body, direction: "ASC" },
headers: { "x-client-version": "1" },
});
const clientJson = await clientRes.json();
expect(clientJson.data).toHaveLength(10);
expect(clientJson.pagination.total).toEqual(11);
const descRes = await server.post("/api/documents.list", user, {
body: { ...body, direction: "DESC" },
});
const descJson = await descRes.json();
expect(descJson.data.map((doc: { id: string }) => doc.id)).toEqual(
structureIds.slice(15).reverse()
);
});
it("should allow filtering by collection", async () => {
const user = await buildUser();
const document = await buildDocument({
+12 -4
View File
@@ -280,6 +280,7 @@ router.post(
// pagination. Only meaningful when the filter targets a single collection.
let documentIds: string[] = [];
let collection: Collection | null | undefined;
let indexTotal: number | undefined;
const explicitCollectionId =
filter !== undefined
? extractTopLevelEqValue(filter, "collectionId")
@@ -290,8 +291,14 @@ router.post(
includeDocumentStructure: true,
});
authorize(user, "readDocument", collection);
documentIds = (collection.documentStructure || [])
.slice(offset, offset + limit)
const structure = collection.documentStructure || [];
indexTotal = structure.length;
// One extra id lets client requests detect a next page.
documentIds = (
direction === "DESC" ? [...structure].reverse() : structure
)
.slice(offset, offset + limit + 1)
.map((node) => node.id);
where[Op.and].push({ id: documentIds });
}
@@ -402,7 +409,7 @@ router.post(
Sequelize.literal(
`array_position(ARRAY[:documentIds]::uuid[], "document"."id")`
),
direction,
"ASC",
],
]
: undefined
@@ -437,7 +444,8 @@ router.post(
}),
// The membership includes are LEFT JOINs that never filter rows, so
// the total can be counted without them.
() =>
async () =>
indexTotal ??
(includeDrafts
? Document.unscoped()
: Document.scope("defaultScope")