From 53d6219b797a8a4fb7ae20195be08d18d9a4be49 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 17 Jul 2024 13:32:45 +0200 Subject: [PATCH] Extract find and fetch --- .../server/src/api/controllers/automation.ts | 13 +++--------- .../server/src/sdk/app/automations/crud.ts | 20 +++++++++++++++++-- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/server/src/api/controllers/automation.ts b/packages/server/src/api/controllers/automation.ts index e47d5565bc..7eca17e0e8 100644 --- a/packages/server/src/api/controllers/automation.ts +++ b/packages/server/src/api/controllers/automation.ts @@ -1,5 +1,5 @@ import * as triggers from "../../automations/triggers" -import { getAutomationParams, DocumentType } from "../../db/utils" +import { DocumentType } from "../../db/utils" import { updateTestHistory, removeDeprecated } from "../../automations/utils" import { setTestFlag, clearTestFlag } from "../../utilities/redis" import { context, cache, events, db as dbCore } from "@budibase/backend-core" @@ -74,18 +74,11 @@ export async function update(ctx: UserCtx) { } export async function fetch(ctx: UserCtx) { - const db = context.getAppDB() - const response = await db.allDocs( - getAutomationParams(null, { - include_docs: true, - }) - ) - ctx.body = response.rows.map(row => row.doc) + ctx.body = await sdk.automations.fetch() } export async function find(ctx: UserCtx) { - const db = context.getAppDB() - ctx.body = await db.get(ctx.params.id) + ctx.body = await sdk.automations.get(ctx.params.id) } export async function destroy(ctx: UserCtx) { diff --git a/packages/server/src/sdk/app/automations/crud.ts b/packages/server/src/sdk/app/automations/crud.ts index 3cbbff0a0a..74a9d3d5d1 100644 --- a/packages/server/src/sdk/app/automations/crud.ts +++ b/packages/server/src/sdk/app/automations/crud.ts @@ -2,7 +2,7 @@ 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 { generateAutomationID, getAutomationParams } from "src/db/utils" import { deleteEntityMetadata } from "src/utilities" function getDb() { @@ -63,6 +63,22 @@ async function handleStepEvents( } } +export async function fetch() { + const db = getDb() + const response = await db.allDocs( + getAutomationParams(null, { + include_docs: true, + }) + ) + return response.rows.map(row => row.doc) +} + +export async function get(automationId: string) { + const db = getDb() + const result = await db.get(automationId) + return result +} + export async function create(automation: Automation) { automation = { ...automation } const db = getDb() @@ -133,7 +149,7 @@ export async function update(automation: Automation) { } export async function remove(automationId: string, rev: string) { - const db = context.getAppDB() + const db = getDb() const existing = await db.get(automationId) await checkForWebhooks({ oldAuto: existing,