From 371a3ad8ecc2abfb349e8acd02e50324b23cbc8a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 17 Jul 2024 12:26:36 +0200 Subject: [PATCH] Ensure unique on updates --- .../src/api/routes/tests/rowAction.spec.ts | 27 +++++++++++++++++++ packages/server/src/sdk/app/rowActions.ts | 17 +++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/server/src/api/routes/tests/rowAction.spec.ts b/packages/server/src/api/routes/tests/rowAction.spec.ts index d4169562d3..98eb7f7699 100644 --- a/packages/server/src/api/routes/tests/rowAction.spec.ts +++ b/packages/server/src/api/routes/tests/rowAction.spec.ts @@ -203,6 +203,16 @@ describe("/rowsActions", () => { } ) }) + + it("can reuse row action names between different tables", async () => { + const otherTable = await config.api.table.save( + setup.structures.basicTable() + ) + + const action = await createRowAction(tableId, createRowActionRequest()) + + await createRowAction(otherTable._id!, { name: action.name }) + }) }) describe("find", () => { @@ -328,6 +338,23 @@ describe("/rowsActions", () => { { status: 400 } ) }) + + it("can not use existing row action names (for the same table)", async () => { + const action1 = await createRowAction(tableId, createRowActionRequest()) + const action2 = await createRowAction(tableId, createRowActionRequest()) + + await config.api.rowAction.update( + tableId, + action1.id, + { name: action2.name }, + { + status: 409, + body: { + message: "A row action with the same name already exists.", + }, + } + ) + }) }) describe("delete", () => { diff --git a/packages/server/src/sdk/app/rowActions.ts b/packages/server/src/sdk/app/rowActions.ts index a247e62ec6..8bff216ab9 100644 --- a/packages/server/src/sdk/app/rowActions.ts +++ b/packages/server/src/sdk/app/rowActions.ts @@ -7,10 +7,16 @@ import { VirtualDocumentType, } from "@budibase/types" -function ensureUnique(doc: TableRowActions, newName: string) { +function ensureUniqueAndThrow( + doc: TableRowActions, + name: string, + existingRowActionId?: string +) { if ( - Object.values(doc.actions).find( - a => a.name.toLowerCase() === newName.toLowerCase() + Object.entries(doc.actions).find( + ([id, a]) => + a.name.toLowerCase() === name.toLowerCase() && + id !== existingRowActionId ) ) { throw new HTTPError("A row action with the same name already exists.", 409) @@ -33,7 +39,7 @@ export async function create(tableId: string, rowAction: { name: string }) { doc = { _id: rowActionsId, actions: {} } } - ensureUnique(doc, action.name) + ensureUniqueAndThrow(doc, action.name) const newId = `${VirtualDocumentType.ROW_ACTION}${SEPARATOR}${utils.newid()}` doc.actions[newId] = action @@ -72,6 +78,9 @@ export async function update( 400 ) } + + ensureUniqueAndThrow(actionsDoc, action.name, rowActionId) + actionsDoc.actions[rowActionId] = action const db = context.getAppDB()