From d03a0ebb6814523fbf7218c6cec52434770564fb Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 11 Jul 2024 17:08:57 +0200 Subject: [PATCH] Implement update --- .../src/api/controllers/rowAction/crud.ts | 23 +++++++++-- .../src/api/routes/tests/rowAction.spec.ts | 41 +++++++++++++++++++ packages/server/src/sdk/app/rowActions.ts | 27 +++++++++++- .../src/tests/utilities/api/rowAction.ts | 17 ++++++++ packages/types/src/api/web/app/rowAction.ts | 5 +++ 5 files changed, 107 insertions(+), 6 deletions(-) diff --git a/packages/server/src/api/controllers/rowAction/crud.ts b/packages/server/src/api/controllers/rowAction/crud.ts index 7f3123e120..1b93228f59 100644 --- a/packages/server/src/api/controllers/rowAction/crud.ts +++ b/packages/server/src/api/controllers/rowAction/crud.ts @@ -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 +) { + 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() { diff --git a/packages/server/src/api/routes/tests/rowAction.spec.ts b/packages/server/src/api/routes/tests/rowAction.spec.ts index bee6964956..e53d39b553 100644 --- a/packages/server/src/api/routes/tests/rowAction.spec.ts +++ b/packages/server/src/api/routes/tests/rowAction.spec.ts @@ -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, + }, + }), + }) + ) + }) + }) }) diff --git a/packages/server/src/sdk/app/rowActions.ts b/packages/server/src/sdk/app/rowActions.ts index fa60a075c9..1d9ed92441 100644 --- a/packages/server/src/sdk/app/rowActions.ts +++ b/packages/server/src/sdk/app/rowActions.ts @@ -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, + } +} diff --git a/packages/server/src/tests/utilities/api/rowAction.ts b/packages/server/src/tests/utilities/api/rowAction.ts index a7c3932bc4..1ff352faee 100644 --- a/packages/server/src/tests/utilities/api/rowAction.ts +++ b/packages/server/src/tests/utilities/api/rowAction.ts @@ -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( + `/api/tables/${tableId}/actions/${rowActionId}`, + { + body: rowAction, + expectations, + ...config, + } + ) + } } diff --git a/packages/types/src/api/web/app/rowAction.ts b/packages/types/src/api/web/app/rowAction.ts index 3219a5e4b7..760ab697f8 100644 --- a/packages/types/src/api/web/app/rowAction.ts +++ b/packages/types/src/api/web/app/rowAction.ts @@ -13,3 +13,8 @@ export interface RowActionsResponse { interface RowActionData { name: string } + +export interface UpdateRowActionRequest { + id: string + name: string +}