Ensure unique names

This commit is contained in:
Adria Navarro 2024-07-17 12:18:09 +02:00
parent c51d2cd431
commit 8297a58270
2 changed files with 39 additions and 0 deletions

View File

@ -176,6 +176,33 @@ describe("/rowsActions", () => {
},
})
})
it("can not create multiple row actions with the same name (for the same table)", async () => {
const action = await createRowAction(tableId, {
name: "Row action name ",
})
await createRowAction(
tableId,
{ name: action.name },
{
status: 409,
body: {
message: "A row action with the same name already exists.",
},
}
)
await createRowAction(
tableId,
{ name: "row action name" },
{
status: 409,
body: {
message: "A row action with the same name already exists.",
},
}
)
})
})
describe("find", () => {

View File

@ -7,6 +7,16 @@ import {
VirtualDocumentType,
} from "@budibase/types"
function ensureUnique(doc: TableRowActions, newName: string) {
if (
Object.values(doc.actions).find(
a => a.name.toLowerCase() === newName.toLowerCase()
)
) {
throw new HTTPError("A row action with the same name already exists.", 409)
}
}
export async function create(tableId: string, rowAction: { name: string }) {
const action = { name: rowAction.name.trim() }
@ -23,6 +33,8 @@ export async function create(tableId: string, rowAction: { name: string }) {
doc = { _id: rowActionsId, actions: {} }
}
ensureUnique(doc, action.name)
const newId = `${VirtualDocumentType.ROW_ACTION}${SEPARATOR}${utils.newid()}`
doc.actions[newId] = action
await db.put(doc)