1
0
Fork 0
mirror of synced 2024-09-20 19:33:10 +12:00

Ensure unique on updates

This commit is contained in:
Adria Navarro 2024-07-17 12:26:36 +02:00
parent 8297a58270
commit 371a3ad8ec
2 changed files with 40 additions and 4 deletions

View file

@ -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", () => {

View file

@ -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()