From 373aeac00fa6011aaecc7f6441d514b07e714fb2 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 17 Jul 2024 13:10:19 +0200 Subject: [PATCH] Move automation creation to sdk --- .../server/src/api/controllers/automation.ts | 29 +--------- .../server/src/sdk/app/automations/crud.ts | 57 +++++++++++++++++++ .../server/src/sdk/app/automations/index.ts | 2 + 3 files changed, 62 insertions(+), 26 deletions(-) create mode 100644 packages/server/src/sdk/app/automations/crud.ts diff --git a/packages/server/src/api/controllers/automation.ts b/packages/server/src/api/controllers/automation.ts index a49fe834d3..4f29b27544 100644 --- a/packages/server/src/api/controllers/automation.ts +++ b/packages/server/src/api/controllers/automation.ts @@ -1,9 +1,5 @@ import * as triggers from "../../automations/triggers" -import { - getAutomationParams, - generateAutomationID, - DocumentType, -} from "../../db/utils" +import { getAutomationParams, DocumentType } from "../../db/utils" import { checkForWebhooks, updateTestHistory, @@ -76,7 +72,6 @@ function cleanAutomationInputs(automation: Automation) { export async function create( ctx: UserCtx ) { - const db = context.getAppDB() let automation = ctx.request.body automation.appId = ctx.appId @@ -86,30 +81,12 @@ export async function create( return } - // Respect existing IDs if recreating a deleted automation - if (!automation._id) { - automation._id = generateAutomationID() - } - - automation.type = "automation" - automation = cleanAutomationInputs(automation) - automation = await checkForWebhooks({ - newAuto: automation, - }) - const response = await db.put(automation) - await events.automation.created(automation) - for (let step of automation.definition.steps) { - await events.automation.stepCreated(automation, step) - } - automation._rev = response.rev + const response = await sdk.automations.create(automation) ctx.status = 200 ctx.body = { message: "Automation created successfully", - automation: { - ...automation, - ...response, - }, + automation: response, } builderSocket?.emitAutomationUpdate(ctx, automation) } diff --git a/packages/server/src/sdk/app/automations/crud.ts b/packages/server/src/sdk/app/automations/crud.ts new file mode 100644 index 0000000000..5726e8ad28 --- /dev/null +++ b/packages/server/src/sdk/app/automations/crud.ts @@ -0,0 +1,57 @@ +import { context, events } from "@budibase/backend-core" +import { Automation } from "@budibase/types" +import { checkForWebhooks } from "src/automations/utils" +import { generateAutomationID } from "src/db/utils" + +function getDb() { + return context.getAppDB() +} + +function cleanAutomationInputs(automation: Automation) { + if (automation == null) { + return automation + } + let steps = automation.definition.steps + let trigger = automation.definition.trigger + let allSteps = [...steps, trigger] + // live is not a property used anymore + if (automation.live != null) { + delete automation.live + } + for (let step of allSteps) { + if (step == null) { + continue + } + for (let inputName of Object.keys(step.inputs)) { + if (!step.inputs[inputName] || step.inputs[inputName] === "") { + delete step.inputs[inputName] + } + } + } + return automation +} + +export async function create(automation: Automation) { + automation = { ...automation } + const db = getDb() + + // Respect existing IDs if recreating a deleted automation + if (!automation._id) { + automation._id = generateAutomationID() + } + + automation.type = "automation" + automation = cleanAutomationInputs(automation) + automation = await checkForWebhooks({ + newAuto: automation, + }) + const response = await db.put(automation) + await events.automation.created(automation) + for (let step of automation.definition.steps) { + await events.automation.stepCreated(automation, step) + } + automation._rev = response.rev + automation._id = response.id + + return automation +} diff --git a/packages/server/src/sdk/app/automations/index.ts b/packages/server/src/sdk/app/automations/index.ts index 16530cf085..215fb2197e 100644 --- a/packages/server/src/sdk/app/automations/index.ts +++ b/packages/server/src/sdk/app/automations/index.ts @@ -1,7 +1,9 @@ +import * as crud from "./crud" import * as webhook from "./webhook" import * as utils from "./utils" export default { + ...crud, webhook, utils, }