Ensure unique on updates
This commit is contained in:
parent
8297a58270
commit
371a3ad8ec
|
@ -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", () => {
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue