diff --git a/packages/server/src/api/controllers/metadata.ts b/packages/server/src/api/controllers/metadata.ts index f579b14499..85c1ab2be8 100644 --- a/packages/server/src/api/controllers/metadata.ts +++ b/packages/server/src/api/controllers/metadata.ts @@ -1,24 +1,35 @@ -import { MetadataTypes } from "../../constants" import { generateMetadataID } from "../../db/utils" import { saveEntityMetadata, deleteEntityMetadata } from "../../utilities" import { context } from "@budibase/backend-core" -import { BBContext } from "@budibase/types" +import { + UserCtx, + MetadataType, + GetMetadataTypesResponse, + SaveMetadataRequest, + SaveMetadataResponse, + DeleteMetadataResponse, + FindMetadataResponse, +} from "@budibase/types" -export async function getTypes(ctx: BBContext) { +export async function getTypes(ctx: UserCtx) { ctx.body = { - types: MetadataTypes, + types: MetadataType, } } -export async function saveMetadata(ctx: BBContext) { +export async function saveMetadata( + ctx: UserCtx +) { const { type, entityId } = ctx.params - if (type === MetadataTypes.AUTOMATION_TEST_HISTORY) { + if (type === MetadataType.AUTOMATION_TEST_HISTORY) { ctx.throw(400, "Cannot save automation history type") } ctx.body = await saveEntityMetadata(type, entityId, ctx.request.body) } -export async function deleteMetadata(ctx: BBContext) { +export async function deleteMetadata( + ctx: UserCtx +) { const { type, entityId } = ctx.params await deleteEntityMetadata(type, entityId) ctx.body = { @@ -26,17 +37,9 @@ export async function deleteMetadata(ctx: BBContext) { } } -export async function getMetadata(ctx: BBContext) { +export async function getMetadata(ctx: UserCtx) { const { type, entityId } = ctx.params const db = context.getAppDB() const id = generateMetadataID(type, entityId) - try { - ctx.body = await db.get(id) - } catch (err: any) { - if (err.status === 404) { - ctx.body = {} - } else { - ctx.throw(err.status, err) - } - } + ctx.body = (await db.tryGet(id)) || {} } diff --git a/packages/server/src/api/routes/tests/metadata.spec.js b/packages/server/src/api/routes/tests/metadata.spec.ts similarity index 69% rename from packages/server/src/api/routes/tests/metadata.spec.js rename to packages/server/src/api/routes/tests/metadata.spec.ts index d5563f417e..6e991a272e 100644 --- a/packages/server/src/api/routes/tests/metadata.spec.js +++ b/packages/server/src/api/routes/tests/metadata.spec.ts @@ -1,11 +1,11 @@ -const { testAutomation } = require("./utilities/TestFunctions") -const setup = require("./utilities") -const { MetadataTypes } = require("../../../constants") +import { testAutomation } from "./utilities/TestFunctions" +import * as setup from "./utilities" +import { MetadataType, Automation } from "@budibase/types" describe("/metadata", () => { let request = setup.getRequest() let config = setup.getConfig() - let automation + let automation: Automation afterAll(setup.afterAll) @@ -15,8 +15,8 @@ describe("/metadata", () => { }) async function createMetadata( - data, - type = MetadataTypes.AUTOMATION_TEST_INPUT + data: Record, + type = MetadataType.AUTOMATION_TEST_INPUT ) { const res = await request .post(`/api/metadata/${type}/${automation._id}`) @@ -27,7 +27,7 @@ describe("/metadata", () => { expect(res.body._rev).toBeDefined() } - async function getMetadata(type) { + async function getMetadata(type: MetadataType) { const res = await request .get(`/api/metadata/${type}/${automation._id}`) .set(config.defaultHeaders()) @@ -39,14 +39,14 @@ describe("/metadata", () => { describe("save", () => { it("should be able to save some metadata", async () => { await createMetadata({ test: "a" }) - const testInput = await getMetadata(MetadataTypes.AUTOMATION_TEST_INPUT) + const testInput = await getMetadata(MetadataType.AUTOMATION_TEST_INPUT) expect(testInput.test).toBe("a") }) it("should save history metadata on automation run", async () => { // this should have created some history - await testAutomation(config, automation) - const metadata = await getMetadata(MetadataTypes.AUTOMATION_TEST_HISTORY) + await testAutomation(config, automation, {}) + const metadata = await getMetadata(MetadataType.AUTOMATION_TEST_HISTORY) expect(metadata).toBeDefined() expect(metadata.history.length).toBe(1) expect(typeof metadata.history[0].occurredAt).toBe("number") @@ -57,13 +57,13 @@ describe("/metadata", () => { it("should be able to delete some test inputs", async () => { const res = await request .delete( - `/api/metadata/${MetadataTypes.AUTOMATION_TEST_INPUT}/${automation._id}` + `/api/metadata/${MetadataType.AUTOMATION_TEST_INPUT}/${automation._id}` ) .set(config.defaultHeaders()) .expect("Content-Type", /json/) .expect(200) expect(res.body.message).toBeDefined() - const metadata = await getMetadata(MetadataTypes.AUTOMATION_TEST_INPUT) + const metadata = await getMetadata(MetadataType.AUTOMATION_TEST_INPUT) expect(metadata.test).toBeUndefined() }) }) diff --git a/packages/server/src/automations/utils.ts b/packages/server/src/automations/utils.ts index 3eeeae5734..292b288a70 100644 --- a/packages/server/src/automations/utils.ts +++ b/packages/server/src/automations/utils.ts @@ -2,7 +2,6 @@ import { Thread, ThreadType } from "../threads" import { definitions } from "./triggerInfo" import { automationQueue } from "./bullboard" import { updateEntityMetadata } from "../utilities" -import { MetadataTypes } from "../constants" import { context, db as dbCore, utils } from "@budibase/backend-core" import { getAutomationMetadataParams } from "../db/utils" import { cloneDeep } from "lodash/fp" @@ -14,6 +13,7 @@ import { AutomationStepDefinition, AutomationTriggerDefinition, AutomationTriggerStepId, + MetadataType, } from "@budibase/types" import { automationsEnabled } from "../features" import { helpers, REBOOT_CRON } from "@budibase/shared-core" @@ -107,7 +107,7 @@ export async function updateTestHistory( history: any ) { return updateEntityMetadata( - MetadataTypes.AUTOMATION_TEST_HISTORY, + MetadataType.AUTOMATION_TEST_HISTORY, automation._id, (metadata: any) => { if (metadata && Array.isArray(metadata.history)) { diff --git a/packages/server/src/constants/index.ts b/packages/server/src/constants/index.ts index 89e2f26516..fde1efd1b9 100644 --- a/packages/server/src/constants/index.ts +++ b/packages/server/src/constants/index.ts @@ -124,11 +124,6 @@ export enum BaseQueryVerbs { DELETE = "delete", } -export enum MetadataTypes { - AUTOMATION_TEST_INPUT = "automationTestInput", - AUTOMATION_TEST_HISTORY = "automationTestHistory", -} - export enum InvalidColumns { ID = "_id", REV = "_rev", diff --git a/packages/server/src/sdk/app/automations/crud.ts b/packages/server/src/sdk/app/automations/crud.ts index b982d5b45e..d16ae2d042 100644 --- a/packages/server/src/sdk/app/automations/crud.ts +++ b/packages/server/src/sdk/app/automations/crud.ts @@ -3,10 +3,10 @@ import { RequiredKeys, Webhook, WebhookActionType, + MetadataType, } from "@budibase/types" import { generateAutomationID, getAutomationParams } from "../../../db/utils" import { deleteEntityMetadata } from "../../../utilities" -import { MetadataTypes } from "../../../constants" import { context, events, @@ -161,7 +161,7 @@ export async function update(automation: Automation) { if (oldAutoTrigger && oldAutoTrigger.id !== newAutoTrigger?.id) { await events.automation.triggerUpdated(automation) await deleteEntityMetadata( - MetadataTypes.AUTOMATION_TEST_INPUT, + MetadataType.AUTOMATION_TEST_INPUT, automation._id! ) } @@ -183,11 +183,8 @@ export async function remove(automationId: string, rev: string) { }) // delete metadata first - await deleteEntityMetadata(MetadataTypes.AUTOMATION_TEST_INPUT, automationId) - await deleteEntityMetadata( - MetadataTypes.AUTOMATION_TEST_HISTORY, - automationId - ) + await deleteEntityMetadata(MetadataType.AUTOMATION_TEST_INPUT, automationId) + await deleteEntityMetadata(MetadataType.AUTOMATION_TEST_HISTORY, automationId) const result = await db.remove(automationId, rev) diff --git a/packages/server/src/utilities/index.ts b/packages/server/src/utilities/index.ts index 331a8e266f..db57b4ec12 100644 --- a/packages/server/src/utilities/index.ts +++ b/packages/server/src/utilities/index.ts @@ -88,7 +88,7 @@ export async function saveEntityMetadata( type: string, entityId: string, metadata: Document -) { +): Promise { return updateEntityMetadata(type, entityId, () => { return metadata }) diff --git a/packages/types/src/api/web/app/index.ts b/packages/types/src/api/web/app/index.ts index 614446a1b7..40c8ebf9ca 100644 --- a/packages/types/src/api/web/app/index.ts +++ b/packages/types/src/api/web/app/index.ts @@ -11,3 +11,4 @@ export * from "./rowAction" export * from "./automation" export * from "./component" export * from "./integration" +export * from "./metadata" diff --git a/packages/types/src/api/web/app/metadata.ts b/packages/types/src/api/web/app/metadata.ts new file mode 100644 index 0000000000..5940f8e3d0 --- /dev/null +++ b/packages/types/src/api/web/app/metadata.ts @@ -0,0 +1,14 @@ +import { MetadataType, Document } from "../../../documents" + +export interface GetMetadataTypesResponse { + types: typeof MetadataType +} + +export interface SaveMetadataRequest extends Document {} +export interface SaveMetadataResponse extends Document {} + +export interface DeleteMetadataResponse { + message: string +} + +export interface FindMetadataResponse extends Document {} diff --git a/packages/types/src/documents/app/index.ts b/packages/types/src/documents/app/index.ts index 51c6889f14..d6c4fd8637 100644 --- a/packages/types/src/documents/app/index.ts +++ b/packages/types/src/documents/app/index.ts @@ -19,3 +19,4 @@ export * from "./snippet" export * from "./rowAction" export * from "./theme" export * from "./deployment" +export * from "./metadata" diff --git a/packages/types/src/documents/app/metadata.ts b/packages/types/src/documents/app/metadata.ts new file mode 100644 index 0000000000..421e8a2f0d --- /dev/null +++ b/packages/types/src/documents/app/metadata.ts @@ -0,0 +1,4 @@ +export enum MetadataType { + AUTOMATION_TEST_INPUT = "automationTestInput", + AUTOMATION_TEST_HISTORY = "automationTestHistory", +}