From d1c6edc437cf577bf7562d2a5ee87ed3b59e1d8b Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 26 Aug 2024 12:15:55 +0200 Subject: [PATCH] Return types --- .../src/api/controllers/rowAction/crud.ts | 49 ++++++++++++++++--- packages/server/src/sdk/app/rowActions.ts | 2 +- .../src/tests/utilities/api/rowAction.ts | 10 +--- packages/types/src/documents/app/rowAction.ts | 1 - 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/packages/server/src/api/controllers/rowAction/crud.ts b/packages/server/src/api/controllers/rowAction/crud.ts index b94bc9d4b9..9b090a9549 100644 --- a/packages/server/src/api/controllers/rowAction/crud.ts +++ b/packages/server/src/api/controllers/rowAction/crud.ts @@ -36,6 +36,7 @@ export async function find(ctx: Ctx) { tableId: table._id!, name: action.name, automationId: action.automationId, + allowedViews: flattenAllowedViews(action.permissions.views), }, }), {} @@ -58,6 +59,7 @@ export async function create( id: createdAction.id, name: createdAction.name, automationId: createdAction.automationId, + allowedViews: undefined, } ctx.status = 201 } @@ -77,6 +79,7 @@ export async function update( id: action.id, name: action.name, automationId: action.automationId, + allowedViews: undefined, } } @@ -88,18 +91,52 @@ export async function remove(ctx: Ctx) { ctx.status = 204 } -export async function setViewPermission(ctx: Ctx) { +export async function setViewPermission(ctx: Ctx) { const table = await getTable(ctx) const { actionId, viewId } = ctx.params - await sdk.rowActions.setViewPermission(table._id!, actionId, viewId) - ctx.status = 204 + const action = await sdk.rowActions.setViewPermission( + table._id!, + actionId, + viewId + ) + ctx.body = { + tableId: table._id!, + id: action.id, + name: action.name, + automationId: action.automationId, + allowedViews: flattenAllowedViews(action.permissions.views), + } } -export async function unsetViewPermission(ctx: Ctx) { +export async function unsetViewPermission(ctx: Ctx) { const table = await getTable(ctx) const { actionId, viewId } = ctx.params - await sdk.rowActions.unsetViewPermission(table._id!, actionId, viewId) - ctx.status = 204 + const action = await sdk.rowActions.unsetViewPermission( + table._id!, + actionId, + viewId + ) + + ctx.body = { + tableId: table._id!, + id: action.id, + name: action.name, + automationId: action.automationId, + allowedViews: flattenAllowedViews(action.permissions.views), + } +} + +function flattenAllowedViews( + permissions: Record +) { + const allowedPermissions = Object.entries(permissions || {}) + .filter(([_, p]) => p.runAllowed) + .map(([viewId]) => viewId) + if (!allowedPermissions.length) { + return undefined + } + + return allowedPermissions } diff --git a/packages/server/src/sdk/app/rowActions.ts b/packages/server/src/sdk/app/rowActions.ts index 830df887a4..369327be86 100644 --- a/packages/server/src/sdk/app/rowActions.ts +++ b/packages/server/src/sdk/app/rowActions.ts @@ -40,7 +40,7 @@ export async function create(tableId: string, rowAction: { name: string }) { throw e } - doc = { _id: rowActionsId, tableId, actions: {} } + doc = { _id: rowActionsId, actions: {} } } ensureUniqueAndThrow(doc, action.name) diff --git a/packages/server/src/tests/utilities/api/rowAction.ts b/packages/server/src/tests/utilities/api/rowAction.ts index d59f9aac5a..b7420fb092 100644 --- a/packages/server/src/tests/utilities/api/rowAction.ts +++ b/packages/server/src/tests/utilities/api/rowAction.ts @@ -81,10 +81,7 @@ export class RowActionAPI extends TestAPI { return await this._post( `/api/tables/${tableId}/actions/${rowActionId}/permissions/${viewId}`, { - expectations: { - ...expectations, - status: expectations?.status || 204, - }, + expectations, ...config, } ) @@ -100,10 +97,7 @@ export class RowActionAPI extends TestAPI { return await this._delete( `/api/tables/${tableId}/actions/${rowActionId}/permissions/${viewId}`, { - expectations: { - ...expectations, - status: expectations?.status || 204, - }, + expectations, ...config, } ) diff --git a/packages/types/src/documents/app/rowAction.ts b/packages/types/src/documents/app/rowAction.ts index 8014b1f8e5..fc9a25c2e2 100644 --- a/packages/types/src/documents/app/rowAction.ts +++ b/packages/types/src/documents/app/rowAction.ts @@ -2,7 +2,6 @@ import { Document } from "../document" export interface TableRowActions extends Document { _id: string - tableId: string actions: Record }