From dc47037dbb8b435058957c5c9f066cfdc7a85be7 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 17 Jul 2024 13:26:40 +0200 Subject: [PATCH] Move update and remove --- .../server/src/api/controllers/automation.ts | 120 +----------------- .../server/src/sdk/app/automations/crud.ts | 99 ++++++++++++++- 2 files changed, 105 insertions(+), 114 deletions(-) diff --git a/packages/server/src/api/controllers/automation.ts b/packages/server/src/api/controllers/automation.ts index 4f29b27544..e47d5565bc 100644 --- a/packages/server/src/api/controllers/automation.ts +++ b/packages/server/src/api/controllers/automation.ts @@ -1,12 +1,6 @@ import * as triggers from "../../automations/triggers" import { getAutomationParams, DocumentType } from "../../db/utils" -import { - checkForWebhooks, - updateTestHistory, - removeDeprecated, -} from "../../automations/utils" -import { deleteEntityMetadata } from "../../utilities" -import { MetadataTypes } from "../../constants" +import { updateTestHistory, removeDeprecated } from "../../automations/utils" import { setTestFlag, clearTestFlag } from "../../utilities/redis" import { context, cache, events, db as dbCore } from "@budibase/backend-core" import { automations, features } from "@budibase/pro" @@ -37,38 +31,6 @@ function getTriggerDefinitions() { * * *************************/ -async function cleanupAutomationMetadata(automationId: string) { - await deleteEntityMetadata(MetadataTypes.AUTOMATION_TEST_INPUT, automationId) - await deleteEntityMetadata( - MetadataTypes.AUTOMATION_TEST_HISTORY, - automationId - ) -} - -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( ctx: UserCtx ) { @@ -81,48 +43,17 @@ export async function create( return } - const response = await sdk.automations.create(automation) + const createdAutomation = await sdk.automations.create(automation) ctx.status = 200 ctx.body = { message: "Automation created successfully", - automation: response, + automation: createdAutomation, } builderSocket?.emitAutomationUpdate(ctx, automation) } -export function getNewSteps(oldAutomation: Automation, automation: Automation) { - const oldStepIds = oldAutomation.definition.steps.map(s => s.id) - return automation.definition.steps.filter(s => !oldStepIds.includes(s.id)) -} - -export function getDeletedSteps( - oldAutomation: Automation, - automation: Automation -) { - const stepIds = automation.definition.steps.map(s => s.id) - return oldAutomation.definition.steps.filter(s => !stepIds.includes(s.id)) -} - -export async function handleStepEvents( - oldAutomation: Automation, - automation: Automation -) { - // new steps - const newSteps = getNewSteps(oldAutomation, automation) - for (let step of newSteps) { - await events.automation.stepCreated(automation, step) - } - - // old steps - const deletedSteps = getDeletedSteps(oldAutomation, automation) - for (let step of deletedSteps) { - await events.automation.stepDeleted(automation, step) - } -} - export async function update(ctx: UserCtx) { - const db = context.getAppDB() let automation = ctx.request.body automation.appId = ctx.appId @@ -132,42 +63,12 @@ export async function update(ctx: UserCtx) { return } - const oldAutomation = await db.get(automation._id) - automation = cleanAutomationInputs(automation) - automation = await checkForWebhooks({ - oldAuto: oldAutomation, - newAuto: automation, - }) - const response = await db.put(automation) - automation._rev = response.rev - - const oldAutoTrigger = - oldAutomation && oldAutomation.definition.trigger - ? oldAutomation.definition.trigger - : undefined - const newAutoTrigger = - automation && automation.definition.trigger - ? automation.definition.trigger - : {} - // trigger has been updated, remove the test inputs - if (oldAutoTrigger && oldAutoTrigger.id !== newAutoTrigger.id) { - await events.automation.triggerUpdated(automation) - await deleteEntityMetadata( - MetadataTypes.AUTOMATION_TEST_INPUT, - automation._id! - ) - } - - await handleStepEvents(oldAutomation, automation) + const updatedAutomation = await sdk.automations.update(automation) ctx.status = 200 ctx.body = { message: `Automation ${automation._id} updated successfully.`, - automation: { - ...automation, - _rev: response.rev, - _id: response.id, - }, + automation: updatedAutomation, } builderSocket?.emitAutomationUpdate(ctx, automation) } @@ -188,16 +89,9 @@ export async function find(ctx: UserCtx) { } export async function destroy(ctx: UserCtx) { - const db = context.getAppDB() const automationId = ctx.params.id - const oldAutomation = await db.get(automationId) - await checkForWebhooks({ - oldAuto: oldAutomation, - }) - // delete metadata first - await cleanupAutomationMetadata(automationId) - ctx.body = await db.remove(automationId, ctx.params.rev) - await events.automation.deleted(oldAutomation) + + ctx.body = await sdk.automations.remove(automationId, ctx.params.rev) builderSocket?.emitAutomationDeletion(ctx, automationId) } diff --git a/packages/server/src/sdk/app/automations/crud.ts b/packages/server/src/sdk/app/automations/crud.ts index 5726e8ad28..3cbbff0a0a 100644 --- a/packages/server/src/sdk/app/automations/crud.ts +++ b/packages/server/src/sdk/app/automations/crud.ts @@ -1,7 +1,9 @@ -import { context, events } from "@budibase/backend-core" +import { context, events, HTTPError } from "@budibase/backend-core" import { Automation } from "@budibase/types" import { checkForWebhooks } from "src/automations/utils" +import { MetadataTypes } from "src/constants" import { generateAutomationID } from "src/db/utils" +import { deleteEntityMetadata } from "src/utilities" function getDb() { return context.getAppDB() @@ -31,6 +33,36 @@ function cleanAutomationInputs(automation: Automation) { return automation } +async function handleStepEvents( + oldAutomation: Automation, + automation: Automation +) { + const getNewSteps = (oldAutomation: Automation, automation: Automation) => { + const oldStepIds = oldAutomation.definition.steps.map(s => s.id) + return automation.definition.steps.filter(s => !oldStepIds.includes(s.id)) + } + + const getDeletedSteps = ( + oldAutomation: Automation, + automation: Automation + ) => { + const stepIds = automation.definition.steps.map(s => s.id) + return oldAutomation.definition.steps.filter(s => !stepIds.includes(s.id)) + } + + // new steps + const newSteps = getNewSteps(oldAutomation, automation) + for (let step of newSteps) { + await events.automation.stepCreated(automation, step) + } + + // old steps + const deletedSteps = getDeletedSteps(oldAutomation, automation) + for (let step of deletedSteps) { + await events.automation.stepDeleted(automation, step) + } +} + export async function create(automation: Automation) { automation = { ...automation } const db = getDb() @@ -55,3 +87,68 @@ export async function create(automation: Automation) { return automation } + +export async function update(automation: Automation) { + automation = { ...automation } + + if (!automation._id || !automation._rev) { + throw new HTTPError("_id or _rev fields missing", 400) + } + + const db = getDb() + + const oldAutomation = await db.get(automation._id) + automation = cleanAutomationInputs(automation) + automation = await checkForWebhooks({ + oldAuto: oldAutomation, + newAuto: automation, + }) + const response = await db.put(automation) + automation._rev = response.rev + + const oldAutoTrigger = + oldAutomation && oldAutomation.definition.trigger + ? oldAutomation.definition.trigger + : undefined + const newAutoTrigger = + automation && automation.definition.trigger + ? automation.definition.trigger + : undefined + // trigger has been updated, remove the test inputs + if (oldAutoTrigger && oldAutoTrigger.id !== newAutoTrigger?.id) { + await events.automation.triggerUpdated(automation) + await deleteEntityMetadata( + MetadataTypes.AUTOMATION_TEST_INPUT, + automation._id! + ) + } + + await handleStepEvents(oldAutomation, automation) + + return { + ...automation, + _rev: response.rev, + _id: response.id, + } +} + +export async function remove(automationId: string, rev: string) { + const db = context.getAppDB() + const existing = await db.get(automationId) + await checkForWebhooks({ + oldAuto: existing, + }) + + // delete metadata first + await deleteEntityMetadata(MetadataTypes.AUTOMATION_TEST_INPUT, automationId) + await deleteEntityMetadata( + MetadataTypes.AUTOMATION_TEST_HISTORY, + automationId + ) + + const result = await db.remove(automationId, rev) + + await events.automation.deleted(existing) + + return result +}