From bbe6df19ea04e2856959a8fa96c2b3ab2a5cfdc5 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 23 Jul 2024 22:13:56 -0400 Subject: [PATCH] Add locking to collection.move mutation Add removeIndexCollision tests --- server/routes/api/collections/collections.ts | 1 + server/utils/removeIndexCollision.test.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 server/utils/removeIndexCollision.test.ts diff --git a/server/routes/api/collections/collections.ts b/server/routes/api/collections/collections.ts index acb437a222..a38ef02313 100644 --- a/server/routes/api/collections/collections.ts +++ b/server/routes/api/collections/collections.ts @@ -875,6 +875,7 @@ router.post( const collection = await Collection.findByPk(id, { transaction, + lock: transaction.LOCK.UPDATE, }); authorize(user, "move", collection); diff --git a/server/utils/removeIndexCollision.test.ts b/server/utils/removeIndexCollision.test.ts new file mode 100644 index 0000000000..bba295e0a1 --- /dev/null +++ b/server/utils/removeIndexCollision.test.ts @@ -0,0 +1,16 @@ +import { buildCollection } from "@server/test/factories"; +import removeIndexCollision from "./removeIndexCollision"; + +describe("removeIndexCollision", () => { + it("should return the next available index", async () => { + const collection = await buildCollection({ index: "P" }); + expect( + await removeIndexCollision(collection.teamId, collection.index!) + ).toEqual("h"); + }); + + it("should return existing index if no collision", async () => { + const collection = await buildCollection({ index: "%P" }); + expect(await removeIndexCollision(collection.teamId, "n")).toEqual("n"); + }); +});