Refactor automation store to properly handle errors
This commit is contained in:
parent
45547f1efc
commit
9d52841219
|
@ -22,109 +22,86 @@ export const getAutomationStore = () => {
|
|||
|
||||
const automationActions = store => ({
|
||||
fetch: async () => {
|
||||
try {
|
||||
const responses = await Promise.all([
|
||||
API.getAutomations(),
|
||||
API.getAutomationDefinitions(),
|
||||
])
|
||||
store.update(state => {
|
||||
let selected = state.selectedAutomation?.automation
|
||||
state.automations = responses[0]
|
||||
state.blockDefinitions = {
|
||||
TRIGGER: responses[1].trigger,
|
||||
ACTION: responses[1].action,
|
||||
}
|
||||
// If previously selected find the new obj and select it
|
||||
if (selected) {
|
||||
selected = responses[0].filter(
|
||||
automation => automation._id === selected._id
|
||||
)
|
||||
state.selectedAutomation = new Automation(selected[0])
|
||||
}
|
||||
return state
|
||||
})
|
||||
} catch (error) {
|
||||
notifications.error("Error fetching automations")
|
||||
store.set(initialAutomationState)
|
||||
}
|
||||
const responses = await Promise.all([
|
||||
API.getAutomations(),
|
||||
API.getAutomationDefinitions(),
|
||||
])
|
||||
store.update(state => {
|
||||
let selected = state.selectedAutomation?.automation
|
||||
state.automations = responses[0]
|
||||
state.blockDefinitions = {
|
||||
TRIGGER: responses[1].trigger,
|
||||
ACTION: responses[1].action,
|
||||
}
|
||||
// If previously selected find the new obj and select it
|
||||
if (selected) {
|
||||
selected = responses[0].filter(
|
||||
automation => automation._id === selected._id
|
||||
)
|
||||
state.selectedAutomation = new Automation(selected[0])
|
||||
}
|
||||
return state
|
||||
})
|
||||
},
|
||||
create: async ({ name }) => {
|
||||
try {
|
||||
const automation = {
|
||||
name,
|
||||
type: "automation",
|
||||
definition: {
|
||||
steps: [],
|
||||
},
|
||||
}
|
||||
const response = await API.createAutomation(automation)
|
||||
store.update(state => {
|
||||
state.automations = [...state.automations, response.automation]
|
||||
store.actions.select(response.automation)
|
||||
return state
|
||||
})
|
||||
} catch (error) {
|
||||
notifications.error("Error creating automation")
|
||||
const automation = {
|
||||
name,
|
||||
type: "automation",
|
||||
definition: {
|
||||
steps: [],
|
||||
},
|
||||
}
|
||||
const response = await API.createAutomation(automation)
|
||||
store.update(state => {
|
||||
state.automations = [...state.automations, response.automation]
|
||||
store.actions.select(response.automation)
|
||||
return state
|
||||
})
|
||||
},
|
||||
save: async automation => {
|
||||
try {
|
||||
const response = await API.updateAutomation(automation)
|
||||
store.update(state => {
|
||||
const updatedAutomation = response.automation
|
||||
const existingIdx = state.automations.findIndex(
|
||||
existing => existing._id === automation._id
|
||||
)
|
||||
if (existingIdx !== -1) {
|
||||
state.automations.splice(existingIdx, 1, updatedAutomation)
|
||||
state.automations = [...state.automations]
|
||||
store.actions.select(updatedAutomation)
|
||||
return state
|
||||
}
|
||||
})
|
||||
notifications.success("Automation saved successfully")
|
||||
} catch (error) {
|
||||
notifications.error("Error saving automation")
|
||||
}
|
||||
const response = await API.updateAutomation(automation)
|
||||
store.update(state => {
|
||||
const updatedAutomation = response.automation
|
||||
const existingIdx = state.automations.findIndex(
|
||||
existing => existing._id === automation._id
|
||||
)
|
||||
if (existingIdx !== -1) {
|
||||
state.automations.splice(existingIdx, 1, updatedAutomation)
|
||||
state.automations = [...state.automations]
|
||||
store.actions.select(updatedAutomation)
|
||||
return state
|
||||
}
|
||||
})
|
||||
},
|
||||
delete: async automation => {
|
||||
try {
|
||||
await API.deleteAutomation({
|
||||
automationId: automation?._id,
|
||||
automationRev: automation?._rev,
|
||||
})
|
||||
store.update(state => {
|
||||
const existingIdx = state.automations.findIndex(
|
||||
existing => existing._id === automation?._id
|
||||
)
|
||||
state.automations.splice(existingIdx, 1)
|
||||
state.automations = [...state.automations]
|
||||
state.selectedAutomation = null
|
||||
state.selectedBlock = null
|
||||
return state
|
||||
})
|
||||
notifications.success("Automation deleted successfully")
|
||||
} catch (error) {
|
||||
notifications.error("Error deleting automation")
|
||||
}
|
||||
await API.deleteAutomation({
|
||||
automationId: automation?._id,
|
||||
automationRev: automation?._rev,
|
||||
})
|
||||
store.update(state => {
|
||||
const existingIdx = state.automations.findIndex(
|
||||
existing => existing._id === automation?._id
|
||||
)
|
||||
state.automations.splice(existingIdx, 1)
|
||||
state.automations = [...state.automations]
|
||||
state.selectedAutomation = null
|
||||
state.selectedBlock = null
|
||||
return state
|
||||
})
|
||||
},
|
||||
test: async (automation, testData) => {
|
||||
try {
|
||||
const result = await API.testAutomation({
|
||||
automationId: automation?._id,
|
||||
testData,
|
||||
})
|
||||
store.update(state => {
|
||||
state.selectedAutomation.testResults = result
|
||||
return state
|
||||
})
|
||||
} catch (error) {
|
||||
notifications.error("Error testing automation")
|
||||
store.update(state => {
|
||||
state.selectedAutomation.testResults = null
|
||||
return state
|
||||
})
|
||||
}
|
||||
store.update(state => {
|
||||
state.selectedAutomation.testResults = null
|
||||
return state
|
||||
})
|
||||
const result = await API.testAutomation({
|
||||
automationId: automation?._id,
|
||||
testData,
|
||||
})
|
||||
store.update(state => {
|
||||
state.selectedAutomation.testResults = result
|
||||
return state
|
||||
})
|
||||
},
|
||||
select: automation => {
|
||||
store.update(state => {
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
Body,
|
||||
Icon,
|
||||
Tooltip,
|
||||
notifications,
|
||||
} from "@budibase/bbui"
|
||||
import { automationStore } from "builderStore"
|
||||
import { admin } from "stores/portal"
|
||||
|
@ -47,15 +48,19 @@
|
|||
}
|
||||
|
||||
async function addBlockToAutomation() {
|
||||
const newBlock = $automationStore.selectedAutomation.constructBlock(
|
||||
"ACTION",
|
||||
actionVal.stepId,
|
||||
actionVal
|
||||
)
|
||||
automationStore.actions.addBlockToAutomation(newBlock, blockIdx + 1)
|
||||
await automationStore.actions.save(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
try {
|
||||
const newBlock = $automationStore.selectedAutomation.constructBlock(
|
||||
"ACTION",
|
||||
actionVal.stepId,
|
||||
actionVal
|
||||
)
|
||||
automationStore.actions.addBlockToAutomation(newBlock, blockIdx + 1)
|
||||
await automationStore.actions.save(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
} catch (error) {
|
||||
notifications.error("Error saving automation")
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
|
|
@ -30,9 +30,13 @@
|
|||
}
|
||||
|
||||
async function deleteAutomation() {
|
||||
await automationStore.actions.delete(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
try {
|
||||
await automationStore.actions.delete(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
} catch (error) {
|
||||
notifications.error("Error deleting automation")
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
Button,
|
||||
StatusLight,
|
||||
ActionButton,
|
||||
notifications,
|
||||
} from "@budibase/bbui"
|
||||
import AutomationBlockSetup from "../../SetupPanel/AutomationBlockSetup.svelte"
|
||||
import CreateWebhookModal from "components/automation/Shared/CreateWebhookModal.svelte"
|
||||
|
@ -54,10 +55,14 @@
|
|||
).every(x => block?.inputs[x])
|
||||
|
||||
async function deleteStep() {
|
||||
automationStore.actions.deleteAutomationBlock(block)
|
||||
await automationStore.actions.save(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
try {
|
||||
automationStore.actions.deleteAutomationBlock(block)
|
||||
await automationStore.actions.save(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
} catch (error) {
|
||||
notifications.error("Error saving notification")
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
<script>
|
||||
import { ModalContent, Tabs, Tab, TextArea, Label } from "@budibase/bbui"
|
||||
import {
|
||||
ModalContent,
|
||||
Tabs,
|
||||
Tab,
|
||||
TextArea,
|
||||
Label,
|
||||
notifications,
|
||||
} from "@budibase/bbui"
|
||||
import { automationStore } from "builderStore"
|
||||
import AutomationBlockSetup from "../../SetupPanel/AutomationBlockSetup.svelte"
|
||||
import { cloneDeep } from "lodash/fp"
|
||||
|
@ -37,6 +44,17 @@
|
|||
failedParse = "Invalid JSON"
|
||||
}
|
||||
}
|
||||
|
||||
const testAutomation = async () => {
|
||||
try {
|
||||
await automationStore.actions.test(
|
||||
$automationStore.selectedAutomation?.automation,
|
||||
testData
|
||||
)
|
||||
} catch (error) {
|
||||
notifications.error("Error testing notification")
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<ModalContent
|
||||
|
@ -44,12 +62,7 @@
|
|||
confirmText="Test"
|
||||
showConfirmButton={true}
|
||||
disabled={isError}
|
||||
onConfirm={() => {
|
||||
automationStore.actions.test(
|
||||
$automationStore.selectedAutomation?.automation,
|
||||
testData
|
||||
)
|
||||
}}
|
||||
onConfirm={testAutomation}
|
||||
cancelText="Cancel"
|
||||
>
|
||||
<Tabs selected="Form" quiet
|
||||
|
|
|
@ -4,10 +4,16 @@
|
|||
import { automationStore } from "builderStore"
|
||||
import NavItem from "components/common/NavItem.svelte"
|
||||
import EditAutomationPopover from "./EditAutomationPopover.svelte"
|
||||
import { notifications } from "@budibase/bbui"
|
||||
|
||||
$: selectedAutomationId = $automationStore.selectedAutomation?.automation?._id
|
||||
onMount(() => {
|
||||
automationStore.actions.fetch()
|
||||
|
||||
onMount(async () => {
|
||||
try {
|
||||
await automationStore.actions.fetch()
|
||||
} catch (error) {
|
||||
notifications.error("Error getting automations list")
|
||||
}
|
||||
})
|
||||
|
||||
function selectAutomation(automation) {
|
||||
|
|
|
@ -24,29 +24,33 @@
|
|||
nameTouched && !name ? "Please specify a name for the automation." : null
|
||||
|
||||
async function createAutomation() {
|
||||
await automationStore.actions.create({
|
||||
name,
|
||||
instanceId,
|
||||
})
|
||||
const newBlock = $automationStore.selectedAutomation.constructBlock(
|
||||
"TRIGGER",
|
||||
triggerVal.stepId,
|
||||
triggerVal
|
||||
)
|
||||
try {
|
||||
await automationStore.actions.create({
|
||||
name,
|
||||
instanceId,
|
||||
})
|
||||
const newBlock = $automationStore.selectedAutomation.constructBlock(
|
||||
"TRIGGER",
|
||||
triggerVal.stepId,
|
||||
triggerVal
|
||||
)
|
||||
|
||||
automationStore.actions.addBlockToAutomation(newBlock)
|
||||
if (triggerVal.stepId === "WEBHOOK") {
|
||||
webhookModal.show
|
||||
automationStore.actions.addBlockToAutomation(newBlock)
|
||||
if (triggerVal.stepId === "WEBHOOK") {
|
||||
webhookModal.show
|
||||
}
|
||||
|
||||
await automationStore.actions.save(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
|
||||
notifications.success(`Automation ${name} created`)
|
||||
|
||||
$goto(`./${$automationStore.selectedAutomation.automation._id}`)
|
||||
analytics.captureEvent(Events.AUTOMATION.CREATED, { name })
|
||||
} catch (error) {
|
||||
notifications.error("Error creating automation")
|
||||
}
|
||||
|
||||
await automationStore.actions.save(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
|
||||
notifications.success(`Automation ${name} created.`)
|
||||
|
||||
$goto(`./${$automationStore.selectedAutomation.automation._id}`)
|
||||
analytics.captureEvent(Events.AUTOMATION.CREATED, { name })
|
||||
}
|
||||
$: triggers = Object.entries($automationStore.blockDefinitions.TRIGGER)
|
||||
|
||||
|
|
|
@ -11,9 +11,13 @@
|
|||
let updateAutomationDialog
|
||||
|
||||
async function deleteAutomation() {
|
||||
await automationStore.actions.delete(automation)
|
||||
notifications.success("Automation deleted.")
|
||||
$goto("../automate")
|
||||
try {
|
||||
await automationStore.actions.delete(automation)
|
||||
notifications.success("Automation deleted successfully")
|
||||
$goto("../automate")
|
||||
} catch (error) {
|
||||
notifications.error("Error deleting automation")
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
|
|
@ -20,14 +20,18 @@
|
|||
}
|
||||
|
||||
async function saveAutomation() {
|
||||
const updatedAutomation = {
|
||||
...automation,
|
||||
name,
|
||||
try {
|
||||
const updatedAutomation = {
|
||||
...automation,
|
||||
name,
|
||||
}
|
||||
await automationStore.actions.save(updatedAutomation)
|
||||
notifications.success(`Automation ${name} updated successfully`)
|
||||
analytics.captureEvent(Events.AUTOMATION.SAVED, { name })
|
||||
hide()
|
||||
} catch (error) {
|
||||
notifications.error("Error saving automation")
|
||||
}
|
||||
await automationStore.actions.save(updatedAutomation)
|
||||
notifications.success(`Automation ${name} updated successfully.`)
|
||||
analytics.captureEvent(Events.AUTOMATION.SAVED, { name })
|
||||
hide()
|
||||
}
|
||||
|
||||
function checkValid(evt) {
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
Drawer,
|
||||
Modal,
|
||||
Detail,
|
||||
notifications,
|
||||
} from "@budibase/bbui"
|
||||
import CreateWebhookModal from "components/automation/Shared/CreateWebhookModal.svelte"
|
||||
|
||||
|
@ -54,28 +55,32 @@
|
|||
$: schemaFields = table ? Object.values(table.schema) : []
|
||||
|
||||
const onChange = debounce(async function (e, key) {
|
||||
if (isTestModal) {
|
||||
// Special case for webhook, as it requires a body, but the schema already brings back the body's contents
|
||||
if (stepId === "WEBHOOK") {
|
||||
try {
|
||||
if (isTestModal) {
|
||||
// Special case for webhook, as it requires a body, but the schema already brings back the body's contents
|
||||
if (stepId === "WEBHOOK") {
|
||||
automationStore.actions.addTestDataToAutomation({
|
||||
body: {
|
||||
[key]: e.detail,
|
||||
...$automationStore.selectedAutomation.automation.testData.body,
|
||||
},
|
||||
})
|
||||
}
|
||||
automationStore.actions.addTestDataToAutomation({
|
||||
body: {
|
||||
[key]: e.detail,
|
||||
...$automationStore.selectedAutomation.automation.testData.body,
|
||||
},
|
||||
[key]: e.detail,
|
||||
})
|
||||
testData[key] = e.detail
|
||||
await automationStore.actions.save(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
} else {
|
||||
block.inputs[key] = e.detail
|
||||
await automationStore.actions.save(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
}
|
||||
automationStore.actions.addTestDataToAutomation({
|
||||
[key]: e.detail,
|
||||
})
|
||||
testData[key] = e.detail
|
||||
await automationStore.actions.save(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
} else {
|
||||
block.inputs[key] = e.detail
|
||||
await automationStore.actions.save(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
} catch (error) {
|
||||
notifications.error("Error saving automation")
|
||||
}
|
||||
}, 800)
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<script>
|
||||
import { Icon } from "@budibase/bbui"
|
||||
import { Icon, notifications } from "@budibase/bbui"
|
||||
import { automationStore } from "builderStore"
|
||||
import WebhookDisplay from "./WebhookDisplay.svelte"
|
||||
import { ModalContent } from "@budibase/bbui"
|
||||
|
@ -16,15 +16,24 @@
|
|||
onMount(async () => {
|
||||
if (!automation?.definition?.trigger?.inputs.schemaUrl) {
|
||||
// save the automation initially
|
||||
await automationStore.actions.save(automation)
|
||||
try {
|
||||
await automationStore.actions.save(automation)
|
||||
} catch (error) {
|
||||
notifications.error("Error saving automation")
|
||||
}
|
||||
}
|
||||
interval = setInterval(async () => {
|
||||
await automationStore.actions.fetch()
|
||||
const outputs = automation?.definition?.trigger.schema.outputs?.properties
|
||||
// always one prop for the "body"
|
||||
if (Object.keys(outputs).length > 1) {
|
||||
propCount = Object.keys(outputs).length - 1
|
||||
finished = true
|
||||
try {
|
||||
await automationStore.actions.fetch()
|
||||
const outputs =
|
||||
automation?.definition?.trigger.schema.outputs?.properties
|
||||
// always one prop for the "body"
|
||||
if (Object.keys(outputs).length > 1) {
|
||||
propCount = Object.keys(outputs).length - 1
|
||||
finished = true
|
||||
}
|
||||
} catch (error) {
|
||||
notifications.error("Error getting automations list")
|
||||
}
|
||||
}, POLL_RATE_MS)
|
||||
schemaURL = automation?.definition?.trigger?.inputs.schemaUrl
|
||||
|
|
|
@ -36,28 +36,37 @@
|
|||
|
||||
// called by the parent modal when actions are saved
|
||||
const createAutomation = async parameters => {
|
||||
if (parameters.automationId || !parameters.newAutomationName) return
|
||||
await automationStore.actions.create({ name: parameters.newAutomationName })
|
||||
const appActionDefinition = $automationStore.blockDefinitions.TRIGGER.APP
|
||||
const newBlock = $automationStore.selectedAutomation.constructBlock(
|
||||
"TRIGGER",
|
||||
"APP",
|
||||
appActionDefinition
|
||||
)
|
||||
|
||||
newBlock.inputs = {
|
||||
fields: Object.keys(parameters.fields).reduce((fields, key) => {
|
||||
fields[key] = "string"
|
||||
return fields
|
||||
}, {}),
|
||||
if (parameters.automationId || !parameters.newAutomationName) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
await automationStore.actions.create({
|
||||
name: parameters.newAutomationName,
|
||||
})
|
||||
const appActionDefinition = $automationStore.blockDefinitions.TRIGGER.APP
|
||||
const newBlock = $automationStore.selectedAutomation.constructBlock(
|
||||
"TRIGGER",
|
||||
"APP",
|
||||
appActionDefinition
|
||||
)
|
||||
|
||||
automationStore.actions.addBlockToAutomation(newBlock)
|
||||
await automationStore.actions.save(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
parameters.automationId = $automationStore.selectedAutomation.automation._id
|
||||
delete parameters.newAutomationName
|
||||
newBlock.inputs = {
|
||||
fields: Object.keys(parameters.fields).reduce((fields, key) => {
|
||||
fields[key] = "string"
|
||||
return fields
|
||||
}, {}),
|
||||
}
|
||||
|
||||
automationStore.actions.addBlockToAutomation(newBlock)
|
||||
await automationStore.actions.save(
|
||||
$automationStore.selectedAutomation?.automation
|
||||
)
|
||||
parameters.automationId =
|
||||
$automationStore.selectedAutomation.automation._id
|
||||
delete parameters.newAutomationName
|
||||
} catch (error) {
|
||||
notifications.error("Error creating automation")
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
|
Loading…
Reference in New Issue