diff --git a/app/scenes/Collection/index.tsx b/app/scenes/Collection/index.tsx index 6d2751b9c8..928d4f76a8 100644 --- a/app/scenes/Collection/index.tsx +++ b/app/scenes/Collection/index.tsx @@ -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 /> diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index c7f2b8b053..4628ffa946 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -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({ diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index cb460794fa..ade14005a8 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -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")