Implement update

This commit is contained in:
Adria Navarro 2024-07-11 17:08:57 +02:00
parent 17fc605e4f
commit d03a0ebb68
5 changed files with 107 additions and 6 deletions

View File

@ -3,6 +3,7 @@ import {
Ctx,
RowActionResponse,
RowActionsResponse,
UpdateRowActionRequest,
} from "@budibase/types"
import sdk from "../../../sdk"
@ -38,21 +39,35 @@ export async function create(
) {
const table = await getTable(ctx)
const { id, ...createdAction } = await sdk.rowActions.create(
const createdAction = await sdk.rowActions.create(
table._id!,
ctx.request.body
)
ctx.body = {
tableId: table._id!,
actionId: id,
...createdAction,
}
ctx.status = 201
}
export function update() {
throw new Error("Function not implemented.")
export async function update(
ctx: Ctx<UpdateRowActionRequest, RowActionResponse>
) {
const table = await getTable(ctx)
const { actionId } = ctx.params
const actions = await sdk.rowActions.update(
table._id!,
actionId,
ctx.request.body
)
ctx.body = {
tableId: table._id!,
...actions,
}
}
export function remove() {

View File

@ -184,4 +184,45 @@ describe("/rowsActions", () => {
)
})
})
describe("update", () => {
unauthorisedTests()
it("can update existing actions", async () => {
for (const rowAction of createRowActionRequests(3)) {
await createRowAction(tableId, rowAction)
}
const persisted = await config.api.rowAction.find(tableId)
const [actionId, actionData] = _.sample(
Object.entries(persisted.actions)
)!
const updatedName = generator.word()
const res = await config.api.rowAction.update(tableId, actionId, {
...actionData,
name: updatedName,
})
expect(res).toEqual({
tableId,
actionId,
...actionData,
name: updatedName,
})
expect(await config.api.rowAction.find(tableId)).toEqual(
expect.objectContaining({
actions: expect.objectContaining({
[actionId]: {
...actionData,
name: updatedName,
},
}),
})
)
})
})
})

View File

@ -1,4 +1,4 @@
import { context, utils } from "@budibase/backend-core"
import { context, HTTPError, utils } from "@budibase/backend-core"
import { generateRowActionsID } from "../../db/utils"
import {
@ -26,7 +26,7 @@ export async function create(tableId: string, rowAction: { name: string }) {
await db.put(doc)
return {
id: newId,
actionId: newId,
...rowAction,
}
}
@ -43,3 +43,26 @@ export async function docExists(tableId: string) {
const result = await db.exists(rowActionsId)
return result
}
export async function update(
tableId: string,
rowActionId: string,
rowAction: { name: string }
) {
const actionsDoc = await get(tableId)
if (!actionsDoc.actions[rowActionId]) {
throw new HTTPError(
`Row action '${rowActionId}' not found in '${tableId}'`,
400
)
}
actionsDoc.actions[rowActionId] = rowAction
const db = context.getAppDB()
await db.put(actionsDoc)
return {
actionId: rowActionId,
...rowAction,
}
}

View File

@ -35,4 +35,21 @@ export class RowActionAPI extends TestAPI {
}
)
}
update = async (
tableId: string,
rowActionId: string,
rowAction: CreateRowActionRequest,
expectations?: Expectations,
config?: { publicUser?: boolean }
) => {
return await this._put<RowActionResponse>(
`/api/tables/${tableId}/actions/${rowActionId}`,
{
body: rowAction,
expectations,
...config,
}
)
}
}

View File

@ -13,3 +13,8 @@ export interface RowActionsResponse {
interface RowActionData {
name: string
}
export interface UpdateRowActionRequest {
id: string
name: string
}