From 7bad3ad39c7f68c1b618b35a665d083803132a8c Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 Jul 2024 13:34:30 +0200 Subject: [PATCH 01/16] Start implementing row action trigger --- .../server/src/api/controllers/rowAction/run.ts | 15 +++++++++++++-- packages/server/src/api/routes/rowAction.ts | 11 ++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/server/src/api/controllers/rowAction/run.ts b/packages/server/src/api/controllers/rowAction/run.ts index 06c4b36f86..a1fa0cfe69 100644 --- a/packages/server/src/api/controllers/rowAction/run.ts +++ b/packages/server/src/api/controllers/rowAction/run.ts @@ -1,3 +1,14 @@ -export function run() { - throw new Error("Function not implemented.") +import { RowActionTriggerRequest, Ctx } from "@budibase/types" +import sdk from "../../../sdk" + +export async function run(ctx: Ctx) { + const { tableId } = ctx.params + const table = await sdk.tables.getTable(tableId) + if (!table) { + ctx.throw(404) + } + + const { rowId } = ctx.request.body + console.warn({ rowId }) + ctx.status = 200 } diff --git a/packages/server/src/api/routes/rowAction.ts b/packages/server/src/api/routes/rowAction.ts index f4f20822d1..709a1c7c1c 100644 --- a/packages/server/src/api/routes/rowAction.ts +++ b/packages/server/src/api/routes/rowAction.ts @@ -1,9 +1,13 @@ import Router from "@koa/router" +import Joi from "joi" +import { middleware, permissions } from "@budibase/backend-core" import * as rowActionController from "../controllers/rowAction" import { authorizedResource } from "../../middleware/authorized" -import { middleware, permissions } from "@budibase/backend-core" -import Joi from "joi" +import { + middleware as appInfoMiddleware, + AppType, +} from "../../middleware/appInfo" const { PermissionLevel, PermissionType } = permissions @@ -45,7 +49,8 @@ router // Other endpoints .post( - "/api/tables/:tableId/actions/:actionId/run", + "/api/tables/:tableId/actions/:actionId/trigger", + appInfoMiddleware({ appType: AppType.PROD }), authorizedResource(PermissionType.TABLE, PermissionLevel.READ, "tableId"), rowActionController.run ) From 15a8907811b3419dd160085d4f97dd439bcef794 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 Jul 2024 13:34:40 +0200 Subject: [PATCH 02/16] Interface --- packages/types/src/api/web/app/rowAction.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/types/src/api/web/app/rowAction.ts b/packages/types/src/api/web/app/rowAction.ts index 305c42b473..2b2b6e1927 100644 --- a/packages/types/src/api/web/app/rowAction.ts +++ b/packages/types/src/api/web/app/rowAction.ts @@ -13,3 +13,7 @@ export interface RowActionResponse extends RowActionData { export interface RowActionsResponse { actions: Record } + +export interface RowActionTriggerRequest { + rowId: string +} From 37e237a3a4980e937b7166d89e74c176d6a9d0bf Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 Jul 2024 13:34:51 +0200 Subject: [PATCH 03/16] Add output properties --- .../src/automations/triggerInfo/rowAction.ts | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/server/src/automations/triggerInfo/rowAction.ts b/packages/server/src/automations/triggerInfo/rowAction.ts index a7454e8606..6dae632d16 100644 --- a/packages/server/src/automations/triggerInfo/rowAction.ts +++ b/packages/server/src/automations/triggerInfo/rowAction.ts @@ -28,8 +28,28 @@ export const definition: AutomationTriggerSchema = { }, required: ["tableId"], }, - outputs: { properties: {} }, + outputs: { + properties: { + id: { + type: AutomationIOType.STRING, + description: "Row ID - can be used for updating", + }, + revision: { + type: AutomationIOType.STRING, + description: "Revision of row", + }, + table: { + type: AutomationIOType.OBJECT, + customType: AutomationCustomIOType.TABLE, + title: "Table", + }, + row: { + type: AutomationIOType.OBJECT, + customType: AutomationCustomIOType.ROW, + description: "The new row that was created", + }, + }, + }, }, - event: AutomationEventType.ROW_SAVE, } From 890d573cac2ae3781c706f7ffd343e1603380e55 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 Jul 2024 16:07:37 +0200 Subject: [PATCH 04/16] Add validators --- packages/server/src/api/routes/rowAction.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/rowAction.ts b/packages/server/src/api/routes/rowAction.ts index 709a1c7c1c..3fa7709e96 100644 --- a/packages/server/src/api/routes/rowAction.ts +++ b/packages/server/src/api/routes/rowAction.ts @@ -11,7 +11,7 @@ import { const { PermissionLevel, PermissionType } = permissions -export function rowActionValidator() { +function rowActionValidator() { return middleware.joiValidator.body( Joi.object({ name: Joi.string().required(), @@ -20,6 +20,15 @@ export function rowActionValidator() { ) } +function rowTriggerValidator() { + return middleware.joiValidator.body( + Joi.object({ + rowId: Joi.string().required(), + }), + { allowUnknown: false } + ) +} + const router: Router = new Router() // CRUD endpoints @@ -51,6 +60,7 @@ router .post( "/api/tables/:tableId/actions/:actionId/trigger", appInfoMiddleware({ appType: AppType.PROD }), + rowTriggerValidator(), authorizedResource(PermissionType.TABLE, PermissionLevel.READ, "tableId"), rowActionController.run ) From f7a460a1eab4587bf9913a116d4e03023f01b631 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 Jul 2024 16:35:45 +0200 Subject: [PATCH 05/16] Implement run --- .../src/api/controllers/rowAction/run.ts | 10 +++---- packages/server/src/automations/triggers.ts | 2 +- packages/server/src/sdk/app/rowActions.ts | 26 ++++++++++++++++++- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/packages/server/src/api/controllers/rowAction/run.ts b/packages/server/src/api/controllers/rowAction/run.ts index a1fa0cfe69..c4b6a276bf 100644 --- a/packages/server/src/api/controllers/rowAction/run.ts +++ b/packages/server/src/api/controllers/rowAction/run.ts @@ -2,13 +2,9 @@ import { RowActionTriggerRequest, Ctx } from "@budibase/types" import sdk from "../../../sdk" export async function run(ctx: Ctx) { - const { tableId } = ctx.params - const table = await sdk.tables.getTable(tableId) - if (!table) { - ctx.throw(404) - } - + const { tableId, actionId } = ctx.params const { rowId } = ctx.request.body - console.warn({ rowId }) + + await sdk.rowActions.run(tableId, actionId, rowId) ctx.status = 200 } diff --git a/packages/server/src/automations/triggers.ts b/packages/server/src/automations/triggers.ts index 99fc7f02f2..4cbde14c46 100644 --- a/packages/server/src/automations/triggers.ts +++ b/packages/server/src/automations/triggers.ts @@ -121,7 +121,7 @@ function rowPassesFilters(row: Row, filters: SearchFilters) { export async function externalTrigger( automation: Automation, - params: { fields: Record; timeout?: number }, + params: { fields: Record; timeout?: number; appId?: string }, { getResponses }: { getResponses?: boolean } = {} ): Promise { if (automation.disabled) { diff --git a/packages/server/src/sdk/app/rowActions.ts b/packages/server/src/sdk/app/rowActions.ts index e59337a7c9..9540198931 100644 --- a/packages/server/src/sdk/app/rowActions.ts +++ b/packages/server/src/sdk/app/rowActions.ts @@ -1,13 +1,15 @@ import { context, HTTPError, utils } from "@budibase/backend-core" -import { generateRowActionsID } from "../../db/utils" import { SEPARATOR, TableRowActions, VirtualDocumentType, } from "@budibase/types" +import { generateRowActionsID } from "../../db/utils" import automations from "./automations" import { definitions as TRIGGER_DEFINITIONS } from "../../automations/triggerInfo" +import * as triggers from "../../automations/triggers" +import sdk from ".." function ensureUniqueAndThrow( doc: TableRowActions, @@ -143,3 +145,25 @@ export async function remove(tableId: string, rowActionId: string) { const db = context.getAppDB() await db.put(actionsDoc) } + +export async function run(tableId: any, rowActionId: any, rowId: string) { + const table = await sdk.tables.getTable(tableId) + if (!table) { + throw new HTTPError("Table not found", 404) + } + + const { actions } = await get(tableId) + + const rowAction = actions[rowActionId] + if (!rowAction) { + throw new HTTPError("Row action not found", 404) + } + + const automation = await sdk.automations.get(rowAction.automationId) + + const row = await sdk.rows.find(tableId, rowId) + await triggers.externalTrigger(automation, { + fields: { row, table }, + appId: context.getAppId(), + }) +} From 4743cc3ae522c3507f0d827493a08ab645059d51 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 Jul 2024 16:56:16 +0200 Subject: [PATCH 06/16] Free plan banner only for free users --- .../app/[application]/settings/automations/index.svelte | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/pages/builder/app/[application]/settings/automations/index.svelte b/packages/builder/src/pages/builder/app/[application]/settings/automations/index.svelte index 7bba5f638c..6518b6cf58 100644 --- a/packages/builder/src/pages/builder/app/[application]/settings/automations/index.svelte +++ b/packages/builder/src/pages/builder/app/[application]/settings/automations/index.svelte @@ -187,7 +187,9 @@ History - Free plan stores up to 1 day of automation history + {#if licensePlan?.type === Constants.PlanType.FREE} + Free plan stores up to 1 day of automation history + {/if}