Move update and remove
This commit is contained in:
parent
373aeac00f
commit
dc47037dbb
|
@ -1,12 +1,6 @@
|
||||||
import * as triggers from "../../automations/triggers"
|
import * as triggers from "../../automations/triggers"
|
||||||
import { getAutomationParams, DocumentType } from "../../db/utils"
|
import { getAutomationParams, DocumentType } from "../../db/utils"
|
||||||
import {
|
import { updateTestHistory, removeDeprecated } from "../../automations/utils"
|
||||||
checkForWebhooks,
|
|
||||||
updateTestHistory,
|
|
||||||
removeDeprecated,
|
|
||||||
} from "../../automations/utils"
|
|
||||||
import { deleteEntityMetadata } from "../../utilities"
|
|
||||||
import { MetadataTypes } from "../../constants"
|
|
||||||
import { setTestFlag, clearTestFlag } from "../../utilities/redis"
|
import { setTestFlag, clearTestFlag } from "../../utilities/redis"
|
||||||
import { context, cache, events, db as dbCore } from "@budibase/backend-core"
|
import { context, cache, events, db as dbCore } from "@budibase/backend-core"
|
||||||
import { automations, features } from "@budibase/pro"
|
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(
|
export async function create(
|
||||||
ctx: UserCtx<Automation, { message: string; automation: Automation }>
|
ctx: UserCtx<Automation, { message: string; automation: Automation }>
|
||||||
) {
|
) {
|
||||||
|
@ -81,48 +43,17 @@ export async function create(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await sdk.automations.create(automation)
|
const createdAutomation = await sdk.automations.create(automation)
|
||||||
|
|
||||||
ctx.status = 200
|
ctx.status = 200
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
message: "Automation created successfully",
|
message: "Automation created successfully",
|
||||||
automation: response,
|
automation: createdAutomation,
|
||||||
}
|
}
|
||||||
builderSocket?.emitAutomationUpdate(ctx, automation)
|
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) {
|
export async function update(ctx: UserCtx) {
|
||||||
const db = context.getAppDB()
|
|
||||||
let automation = ctx.request.body
|
let automation = ctx.request.body
|
||||||
automation.appId = ctx.appId
|
automation.appId = ctx.appId
|
||||||
|
|
||||||
|
@ -132,42 +63,12 @@ export async function update(ctx: UserCtx) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const oldAutomation = await db.get<Automation>(automation._id)
|
const updatedAutomation = await sdk.automations.update(automation)
|
||||||
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)
|
|
||||||
|
|
||||||
ctx.status = 200
|
ctx.status = 200
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
message: `Automation ${automation._id} updated successfully.`,
|
message: `Automation ${automation._id} updated successfully.`,
|
||||||
automation: {
|
automation: updatedAutomation,
|
||||||
...automation,
|
|
||||||
_rev: response.rev,
|
|
||||||
_id: response.id,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
builderSocket?.emitAutomationUpdate(ctx, automation)
|
builderSocket?.emitAutomationUpdate(ctx, automation)
|
||||||
}
|
}
|
||||||
|
@ -188,16 +89,9 @@ export async function find(ctx: UserCtx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function destroy(ctx: UserCtx<void, DeleteAutomationResponse>) {
|
export async function destroy(ctx: UserCtx<void, DeleteAutomationResponse>) {
|
||||||
const db = context.getAppDB()
|
|
||||||
const automationId = ctx.params.id
|
const automationId = ctx.params.id
|
||||||
const oldAutomation = await db.get<Automation>(automationId)
|
|
||||||
await checkForWebhooks({
|
ctx.body = await sdk.automations.remove(automationId, ctx.params.rev)
|
||||||
oldAuto: oldAutomation,
|
|
||||||
})
|
|
||||||
// delete metadata first
|
|
||||||
await cleanupAutomationMetadata(automationId)
|
|
||||||
ctx.body = await db.remove(automationId, ctx.params.rev)
|
|
||||||
await events.automation.deleted(oldAutomation)
|
|
||||||
builderSocket?.emitAutomationDeletion(ctx, automationId)
|
builderSocket?.emitAutomationDeletion(ctx, automationId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 { Automation } from "@budibase/types"
|
||||||
import { checkForWebhooks } from "src/automations/utils"
|
import { checkForWebhooks } from "src/automations/utils"
|
||||||
|
import { MetadataTypes } from "src/constants"
|
||||||
import { generateAutomationID } from "src/db/utils"
|
import { generateAutomationID } from "src/db/utils"
|
||||||
|
import { deleteEntityMetadata } from "src/utilities"
|
||||||
|
|
||||||
function getDb() {
|
function getDb() {
|
||||||
return context.getAppDB()
|
return context.getAppDB()
|
||||||
|
@ -31,6 +33,36 @@ function cleanAutomationInputs(automation: Automation) {
|
||||||
return 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) {
|
export async function create(automation: Automation) {
|
||||||
automation = { ...automation }
|
automation = { ...automation }
|
||||||
const db = getDb()
|
const db = getDb()
|
||||||
|
@ -55,3 +87,68 @@ export async function create(automation: Automation) {
|
||||||
|
|
||||||
return 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>(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<Automation>(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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue