From 2839bd5ece6e7d7c3f4a3f9cf657a5209aff3ffc Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 22 Jul 2024 17:38:19 +0200 Subject: [PATCH 1/5] Trim data --- .../server/src/sdk/app/automations/crud.ts | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/server/src/sdk/app/automations/crud.ts b/packages/server/src/sdk/app/automations/crud.ts index c0f3df6f28..7c28a8b710 100644 --- a/packages/server/src/sdk/app/automations/crud.ts +++ b/packages/server/src/sdk/app/automations/crud.ts @@ -1,4 +1,9 @@ -import { Automation, Webhook, WebhookActionType } from "@budibase/types" +import { + Automation, + RequiredKeys, + Webhook, + WebhookActionType, +} from "@budibase/types" import { generateAutomationID, getAutomationParams } from "../../../db/utils" import { deleteEntityMetadata } from "../../../utilities" import { MetadataTypes } from "../../../constants" @@ -76,17 +81,20 @@ export async function fetch() { include_docs: true, }) ) - return response.rows.map(row => row.doc) + return response.rows + .map(row => row.doc) + .filter(doc => !!doc) + .map(trimUnexpectedObjectFields) } export async function get(automationId: string) { const db = getDb() const result = await db.get(automationId) - return result + return trimUnexpectedObjectFields(result) } export async function create(automation: Automation) { - automation = { ...automation } + automation = trimUnexpectedObjectFields({ ...automation }) const db = getDb() // Respect existing IDs if recreating a deleted automation @@ -111,8 +119,7 @@ export async function create(automation: Automation) { } export async function update(automation: Automation) { - automation = { ...automation } - + automation = trimUnexpectedObjectFields({ ...automation }) if (!automation._id || !automation._rev) { throw new HTTPError("_id or _rev fields missing", 400) } @@ -246,3 +253,23 @@ async function checkForWebhooks({ oldAuto, newAuto }: any) { } return newAuto } + +function trimUnexpectedObjectFields(automation: Automation): Automation { + const result: RequiredKeys = { + _id: automation._id, + _rev: automation._rev, + definition: automation.definition, + screenId: automation.screenId, + uiTree: automation.uiTree, + appId: automation.appId, + live: automation.live, + name: automation.name, + internal: automation.internal, + type: automation.type, + disabled: automation.disabled, + testData: automation.testData, + createdAt: automation.createdAt, + updatedAt: automation.updatedAt, + } + return result +} From 5c41b372f3c96b09b3a3daa858084bb7a8b1af8f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 22 Jul 2024 18:05:11 +0200 Subject: [PATCH 2/5] Fix types --- packages/server/src/sdk/app/automations/crud.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/server/src/sdk/app/automations/crud.ts b/packages/server/src/sdk/app/automations/crud.ts index 7c28a8b710..02cbbe9695 100644 --- a/packages/server/src/sdk/app/automations/crud.ts +++ b/packages/server/src/sdk/app/automations/crud.ts @@ -254,7 +254,7 @@ async function checkForWebhooks({ oldAuto, newAuto }: any) { return newAuto } -function trimUnexpectedObjectFields(automation: Automation): Automation { +function trimUnexpectedObjectFields(automation: T): T { const result: RequiredKeys = { _id: automation._id, _rev: automation._rev, @@ -271,5 +271,5 @@ function trimUnexpectedObjectFields(automation: Automation): Automation { createdAt: automation.createdAt, updatedAt: automation.updatedAt, } - return result + return result as T } From 3d57a64bab7b371433710f9a210a41e839360b2d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 22 Jul 2024 18:06:07 +0200 Subject: [PATCH 3/5] Remove unnecessary spreads --- packages/server/src/sdk/app/automations/crud.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/server/src/sdk/app/automations/crud.ts b/packages/server/src/sdk/app/automations/crud.ts index 02cbbe9695..c376df8e61 100644 --- a/packages/server/src/sdk/app/automations/crud.ts +++ b/packages/server/src/sdk/app/automations/crud.ts @@ -94,7 +94,7 @@ export async function get(automationId: string) { } export async function create(automation: Automation) { - automation = trimUnexpectedObjectFields({ ...automation }) + automation = trimUnexpectedObjectFields(automation) const db = getDb() // Respect existing IDs if recreating a deleted automation @@ -119,7 +119,7 @@ export async function create(automation: Automation) { } export async function update(automation: Automation) { - automation = trimUnexpectedObjectFields({ ...automation }) + automation = trimUnexpectedObjectFields(automation) if (!automation._id || !automation._rev) { throw new HTTPError("_id or _rev fields missing", 400) } From cc77cea269eaccb2744bb89d7565c8a109816f4a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 Jul 2024 10:02:37 +0200 Subject: [PATCH 4/5] Fix tests --- packages/server/src/sdk/app/automations/crud.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/server/src/sdk/app/automations/crud.ts b/packages/server/src/sdk/app/automations/crud.ts index c376df8e61..bb53a75cf8 100644 --- a/packages/server/src/sdk/app/automations/crud.ts +++ b/packages/server/src/sdk/app/automations/crud.ts @@ -255,7 +255,7 @@ async function checkForWebhooks({ oldAuto, newAuto }: any) { } function trimUnexpectedObjectFields(automation: T): T { - const result: RequiredKeys = { + const allRequired: RequiredKeys = { _id: automation._id, _rev: automation._rev, definition: automation.definition, @@ -271,5 +271,11 @@ function trimUnexpectedObjectFields(automation: T): T { createdAt: automation.createdAt, updatedAt: automation.updatedAt, } + const result = { ...allRequired } as T + for (const key in result) { + if (!Object.prototype.hasOwnProperty.call(automation, key)) { + delete result[key] + } + } return result as T } From e29611f2eb218d8db1b06ef0a158fd7ff1409de8 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 Jul 2024 10:10:00 +0200 Subject: [PATCH 5/5] Add comment --- packages/server/src/sdk/app/automations/crud.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/server/src/sdk/app/automations/crud.ts b/packages/server/src/sdk/app/automations/crud.ts index bb53a75cf8..418da02c1c 100644 --- a/packages/server/src/sdk/app/automations/crud.ts +++ b/packages/server/src/sdk/app/automations/crud.ts @@ -255,6 +255,7 @@ async function checkForWebhooks({ oldAuto, newAuto }: any) { } function trimUnexpectedObjectFields(automation: T): T { + // This will ensure all the automation fields (and nothing else) is mapped to the result const allRequired: RequiredKeys = { _id: automation._id, _rev: automation._rev,