diff --git a/packages/server/src/api/controllers/automation.ts b/packages/server/src/api/controllers/automation.ts index d8bc9d6b21..89f664853a 100644 --- a/packages/server/src/api/controllers/automation.ts +++ b/packages/server/src/api/controllers/automation.ts @@ -13,6 +13,22 @@ import { UserCtx, DeleteAutomationResponse, FetchAutomationResponse, + GetAutomationTriggerDefinitionsResponse, + GetAutomationStepDefinitionsResponse, + GetAutomationActionDefinitionsResponse, + FindAutomationResponse, + UpdateAutomationRequest, + UpdateAutomationResponse, + CreateAutomationRequest, + CreateAutomationResponse, + SearchAutomationLogsRequest, + SearchAutomationLogsResponse, + ClearAutomationLogRequest, + ClearAutomationLogResponse, + TriggerAutomationRequest, + TriggerAutomationResponse, + TestAutomationRequest, + TestAutomationResponse, } from "@budibase/types" import { getActionDefinitions as actionDefs } from "../../automations/actions" import sdk from "../../sdk" @@ -34,7 +50,7 @@ function getTriggerDefinitions() { *************************/ export async function create( - ctx: UserCtx + ctx: UserCtx ) { let automation = ctx.request.body automation.appId = ctx.appId @@ -55,7 +71,9 @@ export async function create( builderSocket?.emitAutomationUpdate(ctx, automation) } -export async function update(ctx: UserCtx) { +export async function update( + ctx: UserCtx +) { let automation = ctx.request.body automation.appId = ctx.appId @@ -80,7 +98,7 @@ export async function fetch(ctx: UserCtx) { ctx.body = { automations } } -export async function find(ctx: UserCtx) { +export async function find(ctx: UserCtx) { ctx.body = await sdk.automations.get(ctx.params.id) } @@ -96,11 +114,15 @@ export async function destroy(ctx: UserCtx) { builderSocket?.emitAutomationDeletion(ctx, automationId) } -export async function logSearch(ctx: UserCtx) { +export async function logSearch( + ctx: UserCtx +) { ctx.body = await automations.logs.logSearch(ctx.request.body) } -export async function clearLogError(ctx: UserCtx) { +export async function clearLogError( + ctx: UserCtx +) { const { automationId, appId } = ctx.request.body await context.doInAppContext(appId, async () => { const db = context.getProdAppDB() @@ -119,15 +141,21 @@ export async function clearLogError(ctx: UserCtx) { }) } -export async function getActionList(ctx: UserCtx) { +export async function getActionList( + ctx: UserCtx +) { ctx.body = await getActionDefinitions() } -export async function getTriggerList(ctx: UserCtx) { +export async function getTriggerList( + ctx: UserCtx +) { ctx.body = getTriggerDefinitions() } -export async function getDefinitionList(ctx: UserCtx) { +export async function getDefinitionList( + ctx: UserCtx +) { ctx.body = { trigger: getTriggerDefinitions(), action: await getActionDefinitions(), @@ -140,7 +168,9 @@ export async function getDefinitionList(ctx: UserCtx) { * * *********************/ -export async function trigger(ctx: UserCtx) { +export async function trigger( + ctx: UserCtx +) { const db = context.getAppDB() let automation = await db.get(ctx.params.id) @@ -185,7 +215,7 @@ export async function trigger(ctx: UserCtx) { } } -function prepareTestInput(input: any) { +function prepareTestInput(input: TestAutomationRequest) { // prepare the test parameters if (input.id && input.row) { input.row._id = input.id @@ -196,7 +226,9 @@ function prepareTestInput(input: any) { return input } -export async function test(ctx: UserCtx) { +export async function test( + ctx: UserCtx +) { const db = context.getAppDB() let automation = await db.get(ctx.params.id) await setTestFlag(automation._id!) diff --git a/packages/server/src/automations/actions.ts b/packages/server/src/automations/actions.ts index a8b69fa7d7..e91a708697 100644 --- a/packages/server/src/automations/actions.ts +++ b/packages/server/src/automations/actions.ts @@ -98,7 +98,9 @@ if (env.SELF_HOSTED) { BUILTIN_ACTION_DEFINITIONS["EXECUTE_BASH"] = bash.definition } -export async function getActionDefinitions() { +export async function getActionDefinitions(): Promise< + Record +> { if (await features.flags.isEnabled(FeatureFlag.AUTOMATION_BRANCHING)) { BUILTIN_ACTION_DEFINITIONS["BRANCH"] = branch.definition } diff --git a/packages/server/src/automations/triggers.ts b/packages/server/src/automations/triggers.ts index 70fda1f237..f422c14acb 100644 --- a/packages/server/src/automations/triggers.ts +++ b/packages/server/src/automations/triggers.ts @@ -148,7 +148,7 @@ export async function externalTrigger( user?: UserBindings }, { getResponses }: { getResponses?: boolean } = {} -): Promise { +): Promise> { if (automation.disabled) { throw new Error("Automation is disabled") } diff --git a/packages/server/src/automations/utils.ts b/packages/server/src/automations/utils.ts index 365dc36b68..3eeeae5734 100644 --- a/packages/server/src/automations/utils.ts +++ b/packages/server/src/automations/utils.ts @@ -9,9 +9,11 @@ import { cloneDeep } from "lodash/fp" import { quotas } from "@budibase/pro" import { Automation, + AutomationActionStepId, AutomationJob, AutomationStepDefinition, AutomationTriggerDefinition, + AutomationTriggerStepId, } from "@budibase/types" import { automationsEnabled } from "../features" import { helpers, REBOOT_CRON } from "@budibase/shared-core" @@ -120,19 +122,21 @@ export async function updateTestHistory( ) } -export function removeDeprecated( - definitions: Record< +export function removeDeprecated< + T extends + | Record + | Record +>(definitions: T): T { + const base: Record< string, - AutomationStepDefinition | AutomationTriggerDefinition - > -) { - const base = cloneDeep(definitions) + AutomationTriggerDefinition | AutomationStepDefinition + > = cloneDeep(definitions) for (let key of Object.keys(base)) { if (base[key].deprecated) { delete base[key] } } - return base + return base as T } // end the repetition and the job itself diff --git a/packages/types/src/api/web/app/automation.ts b/packages/types/src/api/web/app/automation.ts new file mode 100644 index 0000000000..c10f19c88d --- /dev/null +++ b/packages/types/src/api/web/app/automation.ts @@ -0,0 +1,21 @@ +import { + AutomationActionStepId, + AutomationStepDefinition, + AutomationTriggerDefinition, + AutomationTriggerStepId, +} from "../../../documents" + +export type GetAutomationTriggerDefinitionsResponse = Record< + keyof typeof AutomationTriggerStepId, + AutomationTriggerDefinition +> + +export type GetAutomationActionDefinitionsResponse = Record< + keyof typeof AutomationActionStepId, + AutomationStepDefinition +> + +export interface GetAutomationStepDefinitionsResponse { + trigger: GetAutomationTriggerDefinitionsResponse + action: GetAutomationActionDefinitionsResponse +} diff --git a/packages/types/src/api/web/app/index.ts b/packages/types/src/api/web/app/index.ts index 55e1428fb9..3b7b183cc3 100644 --- a/packages/types/src/api/web/app/index.ts +++ b/packages/types/src/api/web/app/index.ts @@ -8,3 +8,4 @@ export * from "./permission" export * from "./attachment" export * from "./user" export * from "./rowAction" +export * from "./automation" diff --git a/packages/types/src/api/web/automation.ts b/packages/types/src/api/web/automation.ts index 06080fc667..0f0699939e 100644 --- a/packages/types/src/api/web/automation.ts +++ b/packages/types/src/api/web/automation.ts @@ -1,8 +1,58 @@ import { DocumentDestroyResponse } from "@budibase/nano" -import { Automation } from "../../documents" +import { + Automation, + AutomationLogPage, + AutomationStatus, + Row, +} from "../../documents" export interface DeleteAutomationResponse extends DocumentDestroyResponse {} export interface FetchAutomationResponse { automations: Automation[] } + +export interface FindAutomationResponse extends Automation {} + +export interface UpdateAutomationRequest extends Automation {} +export interface UpdateAutomationResponse { + message: string + automation: Automation +} + +export interface CreateAutomationRequest extends Automation {} +export interface CreateAutomationResponse { + message: string + automation: Automation +} + +export interface SearchAutomationLogsRequest { + startDate?: string + status?: AutomationStatus + automationId?: string + page?: string +} +export interface SearchAutomationLogsResponse extends AutomationLogPage {} + +export interface ClearAutomationLogRequest { + automationId: string + appId: string +} +export interface ClearAutomationLogResponse { + message: string +} + +export interface TriggerAutomationRequest { + fields: Record + // time in seconds + timeout: number +} +export type TriggerAutomationResponse = Record | undefined + +export interface TestAutomationRequest { + id?: string + revision?: string + fields: Record + row?: Row +} +export interface TestAutomationResponse {} diff --git a/packages/types/src/documents/app/automation/schema.ts b/packages/types/src/documents/app/automation/schema.ts index b8a19b7b45..efdf60a4e2 100644 --- a/packages/types/src/documents/app/automation/schema.ts +++ b/packages/types/src/documents/app/automation/schema.ts @@ -311,6 +311,7 @@ export type AutomationStep = type EmptyInputs = {} export type AutomationStepDefinition = Omit & { inputs: EmptyInputs + deprecated?: boolean } export type AutomationTriggerDefinition = Omit< @@ -318,6 +319,7 @@ export type AutomationTriggerDefinition = Omit< "id" | "inputs" > & { inputs: EmptyInputs + deprecated?: boolean } export type AutomationTriggerInputs =