From d09c583c72df9adc4332df8b4aa55f0e5d95843e Mon Sep 17 00:00:00 2001 From: Apoorv Mishra Date: Mon, 28 Nov 2022 19:12:58 +0530 Subject: [PATCH] Allow document to be dragged to the top of a collection (#4489) --- server/routes/api/documents/documents.test.ts | 17 ++++++++++++++++- server/routes/api/documents/schema.ts | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index a9ed8ac7f..69892b963 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -1910,7 +1910,22 @@ describe("#documents.move", () => { }); const body = await res.json(); expect(res.status).toEqual(400); - expect(body.message).toEqual("index: Number must be greater than 0"); + expect(body.message).toEqual( + "index: Number must be greater than or equal to 0" + ); + }); + + it("should move doc to the top of collection as its first child", async () => { + const { user, document, collection } = await seed(); + const res = await server.post("/api/documents.move", { + body: { + token: user.getJwtToken(), + id: document.id, + collectionId: collection.id, + index: 0, + }, + }); + expect(res.status).toEqual(200); }); it("should move the document", async () => { diff --git a/server/routes/api/documents/schema.ts b/server/routes/api/documents/schema.ts index 67d1f5d03..fc124a8de 100644 --- a/server/routes/api/documents/schema.ts +++ b/server/routes/api/documents/schema.ts @@ -208,7 +208,7 @@ export const DocumentsMoveSchema = BaseIdSchema.extend({ parentDocumentId: z.string().uuid().optional(), /** Helps evaluate the new index in collection structure upon move */ - index: z.number().positive().optional(), + index: z.number().gte(0).optional(), }).refine((obj) => !(obj.parentDocumentId === obj.id), { message: "infinite loop detected, cannot nest a document inside itself", });