Move automation creation to sdk

This commit is contained in:
Adria Navarro 2024-07-17 13:10:19 +02:00
parent 68dea69cc1
commit 373aeac00f
3 changed files with 62 additions and 26 deletions

View File

@ -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<Automation, { message: string; automation: Automation }>
) {
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)
}

View File

@ -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
}

View File

@ -1,7 +1,9 @@
import * as crud from "./crud"
import * as webhook from "./webhook"
import * as utils from "./utils"
export default {
...crud,
webhook,
utils,
}