diff --git a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/TestDataModal.svelte b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/TestDataModal.svelte index bd54123fa8..7d223299c7 100644 --- a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/TestDataModal.svelte +++ b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/TestDataModal.svelte @@ -15,8 +15,55 @@ let trigger = {} let schemaProperties = {} - const memoTestData = memo($selectedAutomation.testData) - $: memoTestData.set($selectedAutomation.testData) + const rowTriggers = [ + AutomationEventType.ROW_DELETE, + AutomationEventType.ROW_UPDATE, + AutomationEventType.ROW_SAVE, + ] + + /** + * Parses the automation test data and ensures it is valid + * @param {object} testData contains all config for the test + * @returns {object} valid testData + * @todo Parse *all* data for each trigger type and relay adequate feedback + */ + const parseTestData = testData => { + const autoTrigger = $selectedAutomation?.definition?.trigger + const { tableId } = autoTrigger?.inputs || {} + + // Ensure the tableId matches the trigger table for row trigger automations + if ( + rowTriggers.includes(autoTrigger?.event) && + testData?.row?.tableId !== tableId + ) { + return { + // Reset Core fields + row: { tableId }, + meta: {}, + id: "", + revision: "", + } + } else { + // Leave the core data as it is + return testData + } + } + + /** + * Before executing a test run, relay if an automation is in a valid state + * @param {object} trigger The automation trigger config + * @returns {boolean} validation status + * @todo Parse *all* trigger types relay adequate feedback + */ + const isTriggerValid = trigger => { + if (rowTriggers.includes(trigger?.event) && !trigger?.inputs?.tableId) { + return false + } + return true + } + + const memoTestData = memo(parseTestData($selectedAutomation.testData)) + $: memoTestData.set(parseTestData($selectedAutomation.testData)) $: { // clone the trigger so we're not mutating the reference @@ -32,18 +79,33 @@ } // Check the schema to see if required fields have been entered - $: isError = !trigger.schema.outputs.required.every( - required => $memoTestData?.[required] || required !== "row" - ) + $: isError = + !isTriggerValid(trigger) || + !trigger.schema.outputs.required.every( + required => $memoTestData?.[required] || required !== "row" + ) function parseTestJSON(e) { + let jsonUpdate + try { - const obj = JSON.parse(e.detail) + jsonUpdate = JSON.parse(e.detail) failedParse = null - automationStore.actions.addTestDataToAutomation(obj) } catch (e) { failedParse = "Invalid JSON" + return false } + + if (rowTriggers.includes(trigger?.event)) { + const tableId = trigger?.inputs?.tableId + + // Reset the tableId as it must match the trigger + if (jsonUpdate?.row?.tableId !== tableId) { + jsonUpdate.row.tableId = tableId + } + } + + automationStore.actions.addTestDataToAutomation(jsonUpdate) } const testAutomation = async () => { diff --git a/packages/builder/src/components/automation/SetupPanel/FieldSelector.svelte b/packages/builder/src/components/automation/SetupPanel/FieldSelector.svelte index 252705f6a0..7dd38ee44e 100644 --- a/packages/builder/src/components/automation/SetupPanel/FieldSelector.svelte +++ b/packages/builder/src/components/automation/SetupPanel/FieldSelector.svelte @@ -5,6 +5,7 @@ import ModalBindableInput from "../../common/bindings/ModalBindableInput.svelte" import AutomationBindingPanel from "../../common/bindings/ServerBindingPanel.svelte" import { DatePicker, Select } from "@budibase/bbui" + import { FieldType } from "@budibase/types" const dispatch = createEventDispatcher() @@ -13,7 +14,9 @@ export let block export let isTestModal - let schemaFields + const { STRING, NUMBER, ARRAY } = FieldType + + let schemaFields = [] let editableValue $: editableValue = { ...value } @@ -55,11 +58,11 @@ } -{#if schemaFields.length && isTestModal} +{#if schemaFields?.length && isTestModal}