Fixing some automation typing issues.

This commit is contained in:
mike12345567 2024-11-29 17:13:39 +00:00
parent b2b74aa048
commit d83bd47bd7
4 changed files with 30 additions and 8 deletions

View File

@ -177,7 +177,7 @@ export async function trigger(
let hasCollectStep = sdk.automations.utils.checkForCollectStep(automation) let hasCollectStep = sdk.automations.utils.checkForCollectStep(automation)
if (hasCollectStep && (await features.isSyncAutomationsEnabled())) { if (hasCollectStep && (await features.isSyncAutomationsEnabled())) {
try { try {
const response: AutomationResults = await triggers.externalTrigger( const response = await triggers.externalTrigger(
automation, automation,
{ {
fields: ctx.request.body.fields, fields: ctx.request.body.fields,
@ -188,6 +188,10 @@ export async function trigger(
{ getResponses: true } { getResponses: true }
) )
if (!("steps" in response)) {
ctx.throw(400, "Unable to collect response")
}
let collectedValue = response.steps.find( let collectedValue = response.steps.find(
step => step.stepId === AutomationActionStepId.COLLECT step => step.stepId === AutomationActionStepId.COLLECT
) )

View File

@ -20,6 +20,7 @@ import {
AutomationStatus, AutomationStatus,
AutomationRowEvent, AutomationRowEvent,
UserBindings, UserBindings,
AutomationResults,
} from "@budibase/types" } from "@budibase/types"
import { executeInThread } from "../threads/automation" import { executeInThread } from "../threads/automation"
import { dataFilters, sdk } from "@budibase/shared-core" import { dataFilters, sdk } from "@budibase/shared-core"
@ -32,6 +33,14 @@ const JOB_OPTS = {
import * as automationUtils from "../automations/automationUtils" import * as automationUtils from "../automations/automationUtils"
import { doesTableExist } from "../sdk/app/tables/getters" import { doesTableExist } from "../sdk/app/tables/getters"
type DidNotTriggerResponse = {
outputs: {
success: false
status: AutomationStatus.STOPPED
}
message: AutomationStoppedReason.TRIGGER_FILTER_NOT_MET
}
async function getAllAutomations() { async function getAllAutomations() {
const db = context.getAppDB() const db = context.getAppDB()
let automations = await db.allDocs<Automation>( let automations = await db.allDocs<Automation>(
@ -148,7 +157,7 @@ export async function externalTrigger(
user?: UserBindings user?: UserBindings
}, },
{ getResponses }: { getResponses?: boolean } = {} { getResponses }: { getResponses?: boolean } = {}
): Promise<Record<string, any>> { ): Promise<AutomationResults | DidNotTriggerResponse | AutomationJob> {
if (automation.disabled) { if (automation.disabled) {
throw new Error("Automation is disabled") throw new Error("Automation is disabled")
} }

View File

@ -26,3 +26,6 @@ export interface AutomationContext extends AutomationResults {
company?: string company?: string
} }
} }
export interface AutomationResponse
extends Omit<AutomationContext, "stepsByName" | "stepsById"> {}

View File

@ -30,7 +30,11 @@ import {
UserBindings, UserBindings,
isBasicSearchOperator, isBasicSearchOperator,
} from "@budibase/types" } from "@budibase/types"
import { AutomationContext, TriggerOutput } from "../definitions/automations" import {
AutomationContext,
AutomationResponse,
TriggerOutput,
} from "../definitions/automations"
import { WorkerCallback } from "./definitions" import { WorkerCallback } from "./definitions"
import { context, logging, configs } from "@budibase/backend-core" import { context, logging, configs } from "@budibase/backend-core"
import { import {
@ -81,7 +85,7 @@ class Orchestrator {
private job: Job private job: Job
private loopStepOutputs: LoopStep[] private loopStepOutputs: LoopStep[]
private stopped: boolean private stopped: boolean
private executionOutput: Omit<AutomationContext, "stepsByName" | "stepsById"> private executionOutput: AutomationResponse
private currentUser: UserBindings | undefined private currentUser: UserBindings | undefined
constructor(job: AutomationJob) { constructor(job: AutomationJob) {
@ -257,7 +261,7 @@ class Orchestrator {
}) })
} }
async execute(): Promise<any> { async execute(): Promise<AutomationResponse | undefined> {
return tracer.trace( return tracer.trace(
"Orchestrator.execute", "Orchestrator.execute",
{ resource: "automation" }, { resource: "automation" },
@ -723,7 +727,9 @@ export function execute(job: Job<AutomationData>, callback: WorkerCallback) {
}) })
} }
export async function executeInThread(job: Job<AutomationData>) { export async function executeInThread(
job: Job<AutomationData>
): Promise<AutomationResponse> {
const appId = job.data.event.appId const appId = job.data.event.appId
if (!appId) { if (!appId) {
throw new Error("Unable to execute, event doesn't contain app ID.") throw new Error("Unable to execute, event doesn't contain app ID.")
@ -735,7 +741,7 @@ export async function executeInThread(job: Job<AutomationData>) {
}, job.data.event.timeout || env.AUTOMATION_THREAD_TIMEOUT) }, job.data.event.timeout || env.AUTOMATION_THREAD_TIMEOUT)
}) })
return await context.doInAppContext(appId, async () => { return (await context.doInAppContext(appId, async () => {
await context.ensureSnippetContext() await context.ensureSnippetContext()
const envVars = await sdkUtils.getEnvironmentVariables() const envVars = await sdkUtils.getEnvironmentVariables()
// put into automation thread for whole context // put into automation thread for whole context
@ -746,7 +752,7 @@ export async function executeInThread(job: Job<AutomationData>) {
timeoutPromise, timeoutPromise,
]) ])
}) })
}) })) as AutomationResponse
} }
export const removeStalled = async (job: Job) => { export const removeStalled = async (job: Job) => {