From 2035713b9c9a84a2907036f9de838471815aef1d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 11 Jul 2024 17:33:40 +0200 Subject: [PATCH] Implement delete --- .../src/api/controllers/rowAction/crud.ts | 8 +++- .../src/api/routes/tests/rowAction.spec.ts | 45 +++++++++++++++++++ packages/server/src/sdk/app/rowActions.ts | 17 +++++++ .../src/tests/utilities/api/rowAction.ts | 15 +++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/controllers/rowAction/crud.ts b/packages/server/src/api/controllers/rowAction/crud.ts index b4e5612a7f..c946515150 100644 --- a/packages/server/src/api/controllers/rowAction/crud.ts +++ b/packages/server/src/api/controllers/rowAction/crud.ts @@ -69,6 +69,10 @@ export async function update( } } -export function remove() { - throw new Error("Function not implemented.") +export async function remove(ctx: Ctx) { + const table = await getTable(ctx) + const { actionId } = ctx.params + + await sdk.rowActions.remove(table._id!, actionId) + ctx.status = 204 } diff --git a/packages/server/src/api/routes/tests/rowAction.spec.ts b/packages/server/src/api/routes/tests/rowAction.spec.ts index a1c2973b66..2af3be00b5 100644 --- a/packages/server/src/api/routes/tests/rowAction.spec.ts +++ b/packages/server/src/api/routes/tests/rowAction.spec.ts @@ -251,4 +251,49 @@ describe("/rowsActions", () => { ) }) }) + + describe("delete", () => { + unauthorisedTests() + + it("can delete existing actions", async () => { + const actions: RowActionResponse[] = [] + for (const rowAction of createRowActionRequests(3)) { + actions.push(await createRowAction(tableId, rowAction)) + } + + const actionToDelete = _.sample(actions)! + + await config.api.rowAction.delete(tableId, actionToDelete.id, { + status: 204, + }) + + expect(await config.api.rowAction.find(tableId)).toEqual( + expect.objectContaining({ + actions: actions + .filter(a => a.id !== actionToDelete.id) + .reduce((acc, c) => ({ ...acc, [c.id]: expect.any(Object) }), {}), + }) + ) + }) + + it("throws Bad Request when trying to delete by a non-existing id", async () => { + await createRowAction(tableId, createRowActionRequest()) + + await config.api.rowAction.delete(tableId, generator.guid(), { + status: 400, + }) + }) + + it("throws Bad Request when trying to delete by a via another table id", async () => { + const otherTable = await config.api.table.save( + setup.structures.basicTable() + ) + await createRowAction(otherTable._id!, createRowActionRequest()) + + const action = await createRowAction(tableId, createRowActionRequest()) + await config.api.rowAction.delete(otherTable._id!, action.id, { + status: 400, + }) + }) + }) }) diff --git a/packages/server/src/sdk/app/rowActions.ts b/packages/server/src/sdk/app/rowActions.ts index b6fce219c2..4e96339b06 100644 --- a/packages/server/src/sdk/app/rowActions.ts +++ b/packages/server/src/sdk/app/rowActions.ts @@ -43,6 +43,7 @@ export async function docExists(tableId: string) { const result = await db.exists(rowActionsId) return result } + export async function update( tableId: string, rowActionId: string, @@ -66,3 +67,19 @@ export async function update( ...rowAction, } } + +export async function remove(tableId: string, rowActionId: string) { + const actionsDoc = await get(tableId) + + if (!actionsDoc.actions[rowActionId]) { + throw new HTTPError( + `Row action '${rowActionId}' not found in '${tableId}'`, + 400 + ) + } + + delete actionsDoc.actions[rowActionId] + + const db = context.getAppDB() + await db.put(actionsDoc) +} diff --git a/packages/server/src/tests/utilities/api/rowAction.ts b/packages/server/src/tests/utilities/api/rowAction.ts index 1ff352faee..b3a07e0a65 100644 --- a/packages/server/src/tests/utilities/api/rowAction.ts +++ b/packages/server/src/tests/utilities/api/rowAction.ts @@ -52,4 +52,19 @@ export class RowActionAPI extends TestAPI { } ) } + + delete = async ( + tableId: string, + rowActionId: string, + expectations?: Expectations, + config?: { publicUser?: boolean } + ) => { + return await this._delete( + `/api/tables/${tableId}/actions/${rowActionId}`, + { + expectations, + ...config, + } + ) + } }