From ed0f60d5d75de9c568c9ab4df7223d243a47064a Mon Sep 17 00:00:00 2001 From: Dean Date: Wed, 26 Jun 2024 11:10:15 +0100 Subject: [PATCH] Added an app trigger update parser that will clean the testdata for the automation on save. Old values were not cleaned out. Added some padding to the PropField labels. General fixes --- .../SetupPanel/AutomationBlockSetup.svelte | 59 ++++++++++++-- .../SetupPanel/FieldSelector.svelte | 16 +--- .../automation/SetupPanel/PropField.svelte | 8 ++ .../automation/SetupPanel/RowSelector.svelte | 80 ++++++++----------- .../SetupPanel/RowSelectorTypes.svelte | 1 - .../server/src/automations/steps/updateRow.ts | 15 +--- 6 files changed, 100 insertions(+), 79 deletions(-) diff --git a/packages/builder/src/components/automation/SetupPanel/AutomationBlockSetup.svelte b/packages/builder/src/components/automation/SetupPanel/AutomationBlockSetup.svelte index 393af940af..c120adbf8e 100644 --- a/packages/builder/src/components/automation/SetupPanel/AutomationBlockSetup.svelte +++ b/packages/builder/src/components/automation/SetupPanel/AutomationBlockSetup.svelte @@ -120,8 +120,6 @@ ? [hbAutocomplete([...bindingsToCompletions(bindings, codeMode)])] : [] - let testDataRowVisibility = {} - const getInputData = (testData, blockInputs) => { // Test data is not cloned for reactivity let newInputData = testData || cloneDeep(blockInputs) @@ -275,7 +273,7 @@ const onRowTriggerUpdate = async update => { if ( update.hasOwnProperty("tableId") && - $selectedAutomation.testData.row.tableId !== update.tableId + $selectedAutomation.testData?.row?.tableId !== update.tableId ) { try { const reqSchema = getSchemaForDatasourcePlus(update.tableId, { @@ -309,6 +307,47 @@ } } + /** + * Handler for App trigger automation updates. + * Ensure updates to the field list are reflected in testData + @param {object} update - An app trigger update object + @example + onAppTriggerUpdate({ + "fields" : {"myField": "123", "myArray": "cat,dog,badger"} + }) + */ + const onAppTriggerUpdate = async update => { + try { + // Parse the block inputs as usual + const updatedAutomation = + await automationStore.actions.processBlockInputs(block, { + schema: {}, + ...update, + }) + + // Exclude default or invalid data from the test data + let updatedFields = {} + for (const key of Object.keys(block?.inputs?.fields || {})) { + if (update.fields.hasOwnProperty(key)) { + if (key !== "") { + updatedFields[key] = updatedAutomation.testData?.fields?.[key] + } + } + } + + // Save the entire automation and reset the testData + await automationStore.actions.save({ + ...updatedAutomation, + testData: { + fields: updatedFields, + }, + }) + } catch (e) { + console.error("Error saving automation", error) + notifications.error("Error saving automation") + } + } + /** * Handler for automation block input updates. @param {object} update - An automation inputs update object @@ -321,10 +360,18 @@ const onChange = Utils.sequential(async update => { const request = cloneDeep(update) - // Process row trigger updates + // Process app trigger updates if (isTrigger && !isTestModal) { - await onRowTriggerUpdate(request) - return + // Row trigger + if (rowEvents.includes(block.event)) { + await onRowTriggerUpdate(request) + return + } + // App trigger + if (block.event === AutomationEventType.APP_TRIGGER) { + await onAppTriggerUpdate(request) + return + } } // We need to cache the schema as part of the definition because it is diff --git a/packages/builder/src/components/automation/SetupPanel/FieldSelector.svelte b/packages/builder/src/components/automation/SetupPanel/FieldSelector.svelte index 3f2750e0ec..4469526e91 100644 --- a/packages/builder/src/components/automation/SetupPanel/FieldSelector.svelte +++ b/packages/builder/src/components/automation/SetupPanel/FieldSelector.svelte @@ -16,19 +16,7 @@ let schemaFields let editableValue - $: processValue(value) - - const processValue = value => { - editableValue = { ...value } - // DEAN - review this - // const fieldKeys = Object.keys(block?.inputs?.fields) - // // Purge orphaned keys - // Object.keys(editableValue || {}).forEach(key => { - // if (!fieldKeys.includes(key)) { - // delete editableValue[key] - // } - // }) - } + $: editableValue = { ...value } $: { let fields = {} @@ -60,7 +48,7 @@ ARRAY: "", } - const onChange = (e, field, type) => { + const onChange = (e, field) => { if (e.detail !== editableValue[field]) { editableValue[field] = e.detail dispatch("change", editableValue) diff --git a/packages/builder/src/components/automation/SetupPanel/PropField.svelte b/packages/builder/src/components/automation/SetupPanel/PropField.svelte index edfc46f97c..ba3c0ff081 100644 --- a/packages/builder/src/components/automation/SetupPanel/PropField.svelte +++ b/packages/builder/src/components/automation/SetupPanel/PropField.svelte @@ -49,4 +49,12 @@ white-space: nowrap; overflow: hidden; } + + .prop-control { + margin-left: var(--spacing-s); + } + + .prop-field.fullWidth .prop-control { + margin-left: 0px; + } diff --git a/packages/builder/src/components/automation/SetupPanel/RowSelector.svelte b/packages/builder/src/components/automation/SetupPanel/RowSelector.svelte index ddb3cffcbd..37c908c0f4 100644 --- a/packages/builder/src/components/automation/SetupPanel/RowSelector.svelte +++ b/packages/builder/src/components/automation/SetupPanel/RowSelector.svelte @@ -33,6 +33,7 @@ }) let table + // Row Schema Field let schemaFields let attachmentTypes = [ FieldType.ATTACHMENTS, @@ -51,8 +52,6 @@ meta, }) - $: fields = $memoStore?.meta?.fields - $: if ($memoStore?.meta?.fields) { editableFields = cloneDeep($memoStore?.meta?.fields) } @@ -91,26 +90,16 @@ } editableFields = editableFields } + // Go through the table schema and build out the editable content for (const entry of schemaFields) { const [key, fieldSchema] = entry - if ($memoStore?.row?.[key]) { - // DEAN - review this - editableRow = { - ...editableRow, - [key]: $memoStore?.row[key], - } - } - // Legacy const emptyField = - !$memoStore?.row[key] || $memoStore?.row[key]?.length === 0 + editableRow[key] == null || editableRow[key]?.length === 0 - // Legacy // Put non-empty elements into the update and add their key to the fields list. if (!emptyField && !editableFields.hasOwnProperty(key)) { - //DEAN - review this - IF THEY ADDED A NEW ONE IT WOULD BE MISSING FROM editableFields + editableFields - console.log("EMPTY STATE DETECTED") editableFields = { ...editableFields, [key]: key, @@ -119,39 +108,44 @@ // Legacy - clearRelationships // Init the field and add it to the update. - if (emptyField && editableFields[key]?.clearRelationships === true) { - const emptyField = coerce( - !$memoStore?.row.hasOwnProperty(key) ? "" : $memoStore?.row[key], - fieldSchema.type - ) + if (emptyField) { + if (editableFields[key]?.clearRelationships === true) { + const emptyField = coerce( + !$memoStore?.row.hasOwnProperty(key) ? "" : $memoStore?.row[key], + fieldSchema.type + ) - // remove this and place the field in the editable row. - delete editableFields[key]?.clearRelationships + // remove this and place the field in the editable row. + delete editableFields[key]?.clearRelationships - // Default the field - editableRow = { - ...editableRow, - [key]: emptyField, + // Default the field + editableRow = { + ...editableRow, + [key]: emptyField, + } + } else { + // Purge from the update as it's presence is not necessary. + delete editableRow[key] } } } - // Possible to go through the automation fields schema? - console.log("ACTUAL ROW", row) - console.log("EDITABLE FIELDS", editableFields) - console.log("EDITABLE ROW", editableRow) - } + // Parse all known row schema keys + const schemaKeys = [ + "tableId", + ...schemaFields.map(entry => { + const [key] = entry + return key + }), + ] - // Legacy - add explicitly cleared relationships to the request. - // DEAN - review this - $: if (schemaFields?.length && fields && false) { - // Meta fields processing. - Object.keys(fields).forEach(key => { - if (fields[key]?.clearRelationships) { - columns.add(key) + // Purge any row keys that are not present in the schema. + for (const rowKey of Object.keys(editableRow)) { + if (!schemaKeys.includes(rowKey)) { + delete editableRow[rowKey] + delete editableFields[rowKey] } - }) - columns = new Set(columns) + } } $: typeToField = Object.values(FIELDS).reduce((acc, field) => { @@ -209,7 +203,7 @@ } const onChange = update => { - const customizer = (objValue, srcValue, key) => { + const customizer = (objValue, srcValue) => { if (isPlainObject(objValue) && isPlainObject(srcValue)) { const result = mergeWith({}, objValue, srcValue, customizer) let outcome = Object.keys(result).reduce((acc, key) => { @@ -235,7 +229,6 @@ update, customizer ) - console.log("Row Selector - MERGED", result) dispatch("change", result) } @@ -283,10 +276,7 @@ meta={{ fields: editableFields, }} - onChange={change => { - console.log("RowSelectorTypes > RowSelector > ", change) - onChange(change) - }} + onChange={change => onChange(change)} /> {/if} diff --git a/packages/builder/src/components/automation/SetupPanel/RowSelectorTypes.svelte b/packages/builder/src/components/automation/SetupPanel/RowSelectorTypes.svelte index 02af3385ee..a8e9eb2ddb 100644 --- a/packages/builder/src/components/automation/SetupPanel/RowSelectorTypes.svelte +++ b/packages/builder/src/components/automation/SetupPanel/RowSelectorTypes.svelte @@ -122,7 +122,6 @@ { - console.log("JSON change", e.detail?.value, fieldData) if (e.detail?.value !== fieldData) { onChange({ row: { diff --git a/packages/server/src/automations/steps/updateRow.ts b/packages/server/src/automations/steps/updateRow.ts index 8916917956..8f8ff6879b 100644 --- a/packages/server/src/automations/steps/updateRow.ts +++ b/packages/server/src/automations/steps/updateRow.ts @@ -29,17 +29,6 @@ export const definition: AutomationStepSchema = { meta: { type: AutomationIOType.OBJECT, title: "Field settings", - // DEAN - REVIEW THIS - add in some record of these types - - // properties: { - // fields: { - // properties: { - // useAttachmentBinding: { - // type: AutomationIOType.BOOLEAN, - // }, - // }, - // }, - // }, }, row: { type: AutomationIOType.OBJECT, @@ -129,8 +118,8 @@ export async function run({ inputs, appId, emitter }: AutomationStepInput) { } acc[key] = inputs.row.hasOwnProperty(key) && - (inputs.row[key] == null || inputs.row[key]?.length === 0) - ? undefined + (!inputs.row[key] || inputs.row[key]?.length === 0) + ? null : inputs.row[key] return acc },