Merge pull request #14209 from Budibase/BUDI-8430/rowaction-automation-display-in-frontend
Row action automation display in frontend
This commit is contained in:
commit
e5c05c3f0b
|
@ -3,6 +3,7 @@
|
||||||
automationStore,
|
automationStore,
|
||||||
selectedAutomation,
|
selectedAutomation,
|
||||||
permissions,
|
permissions,
|
||||||
|
selectedAutomationDisplayData,
|
||||||
} from "stores/builder"
|
} from "stores/builder"
|
||||||
import {
|
import {
|
||||||
Icon,
|
Icon,
|
||||||
|
@ -14,6 +15,7 @@
|
||||||
notifications,
|
notifications,
|
||||||
Label,
|
Label,
|
||||||
AbsTooltip,
|
AbsTooltip,
|
||||||
|
InlineAlert,
|
||||||
} from "@budibase/bbui"
|
} from "@budibase/bbui"
|
||||||
import AutomationBlockSetup from "../../SetupPanel/AutomationBlockSetup.svelte"
|
import AutomationBlockSetup from "../../SetupPanel/AutomationBlockSetup.svelte"
|
||||||
import CreateWebhookModal from "components/automation/Shared/CreateWebhookModal.svelte"
|
import CreateWebhookModal from "components/automation/Shared/CreateWebhookModal.svelte"
|
||||||
|
@ -49,6 +51,8 @@
|
||||||
$: isAppAction && setPermissions(role)
|
$: isAppAction && setPermissions(role)
|
||||||
$: isAppAction && getPermissions(automationId)
|
$: isAppAction && getPermissions(automationId)
|
||||||
|
|
||||||
|
$: triggerInfo = $selectedAutomationDisplayData?.triggerInfo
|
||||||
|
|
||||||
async function setPermissions(role) {
|
async function setPermissions(role) {
|
||||||
if (!role || !automationId) {
|
if (!role || !automationId) {
|
||||||
return
|
return
|
||||||
|
@ -183,6 +187,12 @@
|
||||||
{block}
|
{block}
|
||||||
{webhookModal}
|
{webhookModal}
|
||||||
/>
|
/>
|
||||||
|
{#if isTrigger && triggerInfo}
|
||||||
|
<InlineAlert
|
||||||
|
header={triggerInfo.type}
|
||||||
|
message={`This trigger is tied to the row action ${triggerInfo.rowAction.name} on your ${triggerInfo.table.name} table`}
|
||||||
|
/>
|
||||||
|
{/if}
|
||||||
{#if lastStep}
|
{#if lastStep}
|
||||||
<Button on:click={() => testDataModal.show()} cta>
|
<Button on:click={() => testDataModal.show()} cta>
|
||||||
Finish and test automation
|
Finish and test automation
|
||||||
|
|
|
@ -81,7 +81,7 @@
|
||||||
// Check the schema to see if required fields have been entered
|
// Check the schema to see if required fields have been entered
|
||||||
$: isError =
|
$: isError =
|
||||||
!isTriggerValid(trigger) ||
|
!isTriggerValid(trigger) ||
|
||||||
!trigger.schema.outputs.required?.every(
|
!(trigger.schema.outputs.required || []).every(
|
||||||
required => $memoTestData?.[required] || required !== "row"
|
required => $memoTestData?.[required] || required !== "row"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,12 @@
|
||||||
automation.name.toLowerCase().includes(searchString.toLowerCase())
|
automation.name.toLowerCase().includes(searchString.toLowerCase())
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
.map(automation => ({
|
||||||
|
...automation,
|
||||||
|
name:
|
||||||
|
$automationStore.automationDisplayData[automation._id].displayName ||
|
||||||
|
automation.name,
|
||||||
|
}))
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
const lowerA = a.name.toLowerCase()
|
const lowerA = a.name.toLowerCase()
|
||||||
const lowerB = b.name.toLowerCase()
|
const lowerB = b.name.toLowerCase()
|
||||||
|
|
|
@ -15,6 +15,7 @@ const initialAutomationState = {
|
||||||
ACTION: [],
|
ACTION: [],
|
||||||
},
|
},
|
||||||
selectedAutomationId: null,
|
selectedAutomationId: null,
|
||||||
|
automationDisplayData: {},
|
||||||
}
|
}
|
||||||
|
|
||||||
// If this functions, remove the actions elements
|
// If this functions, remove the actions elements
|
||||||
|
@ -58,18 +59,19 @@ const automationActions = store => ({
|
||||||
return response
|
return response
|
||||||
},
|
},
|
||||||
fetch: async () => {
|
fetch: async () => {
|
||||||
const responses = await Promise.all([
|
const [automationResponse, definitions] = await Promise.all([
|
||||||
API.getAutomations(),
|
API.getAutomations({ enrich: true }),
|
||||||
API.getAutomationDefinitions(),
|
API.getAutomationDefinitions(),
|
||||||
])
|
])
|
||||||
store.update(state => {
|
store.update(state => {
|
||||||
state.automations = responses[0]
|
state.automations = automationResponse.automations
|
||||||
state.automations.sort((a, b) => {
|
state.automations.sort((a, b) => {
|
||||||
return a.name < b.name ? -1 : 1
|
return a.name < b.name ? -1 : 1
|
||||||
})
|
})
|
||||||
|
state.automationDisplayData = automationResponse.builderData
|
||||||
state.blockDefinitions = {
|
state.blockDefinitions = {
|
||||||
TRIGGER: responses[1].trigger,
|
TRIGGER: definitions.trigger,
|
||||||
ACTION: responses[1].action,
|
ACTION: definitions.action,
|
||||||
}
|
}
|
||||||
return state
|
return state
|
||||||
})
|
})
|
||||||
|
@ -386,3 +388,13 @@ export const selectedAutomation = derived(automationStore, $automationStore => {
|
||||||
x => x._id === $automationStore.selectedAutomationId
|
x => x._id === $automationStore.selectedAutomationId
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const selectedAutomationDisplayData = derived(
|
||||||
|
[automationStore, selectedAutomation],
|
||||||
|
([$automationStore, $selectedAutomation]) => {
|
||||||
|
if (!$selectedAutomation._id) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return $automationStore.automationDisplayData[$selectedAutomation._id]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
|
@ -11,6 +11,7 @@ import {
|
||||||
automationStore,
|
automationStore,
|
||||||
selectedAutomation,
|
selectedAutomation,
|
||||||
automationHistoryStore,
|
automationHistoryStore,
|
||||||
|
selectedAutomationDisplayData,
|
||||||
} from "./automations.js"
|
} from "./automations.js"
|
||||||
import { userStore, userSelectedResourceMap, isOnlyUser } from "./users.js"
|
import { userStore, userSelectedResourceMap, isOnlyUser } from "./users.js"
|
||||||
import { deploymentStore } from "./deployments.js"
|
import { deploymentStore } from "./deployments.js"
|
||||||
|
@ -44,6 +45,7 @@ export {
|
||||||
previewStore,
|
previewStore,
|
||||||
automationStore,
|
automationStore,
|
||||||
selectedAutomation,
|
selectedAutomation,
|
||||||
|
selectedAutomationDisplayData,
|
||||||
automationHistoryStore,
|
automationHistoryStore,
|
||||||
sortedScreens,
|
sortedScreens,
|
||||||
userStore,
|
userStore,
|
||||||
|
|
|
@ -26,9 +26,14 @@ export const buildAutomationEndpoints = API => ({
|
||||||
/**
|
/**
|
||||||
* Gets a list of all automations.
|
* Gets a list of all automations.
|
||||||
*/
|
*/
|
||||||
getAutomations: async () => {
|
getAutomations: async ({ enrich }) => {
|
||||||
|
const params = new URLSearchParams()
|
||||||
|
if (enrich) {
|
||||||
|
params.set("enrich", true)
|
||||||
|
}
|
||||||
|
|
||||||
return await API.get({
|
return await API.get({
|
||||||
url: "/api/automations",
|
url: `/api/automations?${params.toString()}`,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import {
|
||||||
AutomationResults,
|
AutomationResults,
|
||||||
UserCtx,
|
UserCtx,
|
||||||
DeleteAutomationResponse,
|
DeleteAutomationResponse,
|
||||||
|
FetchAutomationResponse,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { getActionDefinitions as actionDefs } from "../../automations/actions"
|
import { getActionDefinitions as actionDefs } from "../../automations/actions"
|
||||||
import sdk from "../../sdk"
|
import sdk from "../../sdk"
|
||||||
|
@ -73,8 +74,17 @@ export async function update(ctx: UserCtx) {
|
||||||
builderSocket?.emitAutomationUpdate(ctx, automation)
|
builderSocket?.emitAutomationUpdate(ctx, automation)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetch(ctx: UserCtx) {
|
export async function fetch(ctx: UserCtx<void, FetchAutomationResponse>) {
|
||||||
ctx.body = await sdk.automations.fetch()
|
const query: { enrich?: string } = ctx.request.query || {}
|
||||||
|
const enrich = query.enrich === "true"
|
||||||
|
|
||||||
|
const automations = await sdk.automations.fetch()
|
||||||
|
ctx.body = { automations }
|
||||||
|
if (enrich) {
|
||||||
|
ctx.body.builderData = await sdk.automations.utils.getBuilderData(
|
||||||
|
automations
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function find(ctx: UserCtx) {
|
export async function find(ctx: UserCtx) {
|
||||||
|
|
|
@ -398,7 +398,9 @@ describe("/automations", () => {
|
||||||
.expect("Content-Type", /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
|
|
||||||
expect(res.body[0]).toEqual(expect.objectContaining(autoConfig))
|
expect(res.body.automations[0]).toEqual(
|
||||||
|
expect.objectContaining(autoConfig)
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should apply authorization to endpoint", async () => {
|
it("should apply authorization to endpoint", async () => {
|
||||||
|
|
|
@ -54,7 +54,7 @@ export const clearAllApps = async (
|
||||||
}
|
}
|
||||||
|
|
||||||
export const clearAllAutomations = async (config: TestConfiguration) => {
|
export const clearAllAutomations = async (config: TestConfiguration) => {
|
||||||
const automations = await config.getAllAutomations()
|
const { automations } = await config.getAllAutomations()
|
||||||
for (let auto of automations) {
|
for (let auto of automations) {
|
||||||
await context.doInAppContext(config.getAppId(), async () => {
|
await context.doInAppContext(config.getAppId(), async () => {
|
||||||
await config.deleteAutomation(auto)
|
await config.deleteAutomation(auto)
|
||||||
|
|
|
@ -11,7 +11,7 @@ import {
|
||||||
import { definitions } from "../../../automations/triggerInfo"
|
import { definitions } from "../../../automations/triggerInfo"
|
||||||
import automations from "."
|
import automations from "."
|
||||||
|
|
||||||
interface PersistedAutomation extends Automation {
|
export interface PersistedAutomation extends Automation {
|
||||||
_id: string
|
_id: string
|
||||||
_rev: string
|
_rev: string
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ export async function fetch() {
|
||||||
include_docs: true,
|
include_docs: true,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
return response.rows.map(row => row.doc)
|
return response.rows.map(row => row.doc).filter(doc => !!doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function get(automationId: string) {
|
export async function get(automationId: string) {
|
||||||
|
@ -254,6 +254,7 @@ async function checkForWebhooks({ oldAuto, newAuto }: any) {
|
||||||
}
|
}
|
||||||
return newAuto
|
return newAuto
|
||||||
}
|
}
|
||||||
|
|
||||||
function guardInvalidUpdatesAndThrow(
|
function guardInvalidUpdatesAndThrow(
|
||||||
automation: Automation,
|
automation: Automation,
|
||||||
oldAutomation: Automation
|
oldAutomation: Automation
|
||||||
|
|
|
@ -1,7 +1,68 @@
|
||||||
import { Automation, AutomationActionStepId } from "@budibase/types"
|
import {
|
||||||
|
Automation,
|
||||||
|
AutomationActionStepId,
|
||||||
|
AutomationBuilderData,
|
||||||
|
AutomationTriggerStepId,
|
||||||
|
TableRowActions,
|
||||||
|
} from "@budibase/types"
|
||||||
|
|
||||||
export function checkForCollectStep(automation: Automation) {
|
export function checkForCollectStep(automation: Automation) {
|
||||||
return automation.definition.steps.some(
|
return automation.definition.steps.some(
|
||||||
(step: any) => step.stepId === AutomationActionStepId.COLLECT
|
(step: any) => step.stepId === AutomationActionStepId.COLLECT
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getBuilderData(
|
||||||
|
automations: Automation[]
|
||||||
|
): Promise<Record<string, AutomationBuilderData>> {
|
||||||
|
const sdk = (await import("../../../sdk")).default
|
||||||
|
|
||||||
|
const tableNameCache: Record<string, string> = {}
|
||||||
|
async function getTableName(tableId: string) {
|
||||||
|
if (!tableNameCache[tableId]) {
|
||||||
|
const table = await sdk.tables.getTable(tableId)
|
||||||
|
tableNameCache[tableId] = table.name
|
||||||
|
}
|
||||||
|
|
||||||
|
return tableNameCache[tableId]
|
||||||
|
}
|
||||||
|
|
||||||
|
const rowActionNameCache: Record<string, TableRowActions> = {}
|
||||||
|
async function getRowActionName(tableId: string, rowActionId: string) {
|
||||||
|
if (!rowActionNameCache[tableId]) {
|
||||||
|
const rowActions = await sdk.rowActions.get(tableId)
|
||||||
|
rowActionNameCache[tableId] = rowActions
|
||||||
|
}
|
||||||
|
|
||||||
|
return rowActionNameCache[tableId].actions[rowActionId]?.name
|
||||||
|
}
|
||||||
|
|
||||||
|
const result: Record<string, AutomationBuilderData> = {}
|
||||||
|
for (const automation of automations) {
|
||||||
|
const { trigger } = automation.definition
|
||||||
|
const isRowAction = trigger.stepId === AutomationTriggerStepId.ROW_ACTION
|
||||||
|
if (!isRowAction) {
|
||||||
|
result[automation._id!] = { displayName: automation.name }
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tableId, rowActionId } = trigger.inputs
|
||||||
|
|
||||||
|
const tableName = await getTableName(tableId)
|
||||||
|
|
||||||
|
const rowActionName = await getRowActionName(tableId, rowActionId)
|
||||||
|
|
||||||
|
result[automation._id!] = {
|
||||||
|
displayName: `${tableName}: ${automation.name}`,
|
||||||
|
triggerInfo: {
|
||||||
|
type: "Automation trigger",
|
||||||
|
table: { id: tableId, name: tableName },
|
||||||
|
rowAction: {
|
||||||
|
id: rowActionId,
|
||||||
|
name: rowActionName,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@ import {
|
||||||
VirtualDocumentType,
|
VirtualDocumentType,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import automations from "./automations"
|
import automations from "./automations"
|
||||||
import tables from "./tables"
|
|
||||||
|
|
||||||
function ensureUniqueAndThrow(
|
function ensureUniqueAndThrow(
|
||||||
doc: TableRowActions,
|
doc: TableRowActions,
|
||||||
|
@ -45,8 +44,6 @@ export async function create(tableId: string, rowAction: { name: string }) {
|
||||||
doc = { _id: rowActionsId, actions: {} }
|
doc = { _id: rowActionsId, actions: {} }
|
||||||
}
|
}
|
||||||
|
|
||||||
const { name: tableName } = await tables.getTable(tableId)
|
|
||||||
|
|
||||||
ensureUniqueAndThrow(doc, action.name)
|
ensureUniqueAndThrow(doc, action.name)
|
||||||
|
|
||||||
const appId = context.getAppId()
|
const appId = context.getAppId()
|
||||||
|
@ -54,8 +51,12 @@ export async function create(tableId: string, rowAction: { name: string }) {
|
||||||
throw new Error("Could not get the current appId")
|
throw new Error("Could not get the current appId")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const newRowActionId = `${
|
||||||
|
VirtualDocumentType.ROW_ACTION
|
||||||
|
}${SEPARATOR}${utils.newid()}`
|
||||||
|
|
||||||
const automation = await automations.create({
|
const automation = await automations.create({
|
||||||
name: `${tableName}: ${action.name}`,
|
name: action.name,
|
||||||
appId,
|
appId,
|
||||||
definition: {
|
definition: {
|
||||||
trigger: {
|
trigger: {
|
||||||
|
@ -68,6 +69,7 @@ export async function create(tableId: string, rowAction: { name: string }) {
|
||||||
stepId: AutomationTriggerStepId.ROW_ACTION,
|
stepId: AutomationTriggerStepId.ROW_ACTION,
|
||||||
inputs: {
|
inputs: {
|
||||||
tableId,
|
tableId,
|
||||||
|
rowActionId: newRowActionId,
|
||||||
},
|
},
|
||||||
schema: {
|
schema: {
|
||||||
inputs: {
|
inputs: {
|
||||||
|
@ -88,16 +90,15 @@ export async function create(tableId: string, rowAction: { name: string }) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const newId = `${VirtualDocumentType.ROW_ACTION}${SEPARATOR}${utils.newid()}`
|
doc.actions[newRowActionId] = {
|
||||||
doc.actions[newId] = {
|
|
||||||
name: action.name,
|
name: action.name,
|
||||||
automationId: automation._id!,
|
automationId: automation._id!,
|
||||||
}
|
}
|
||||||
await db.put(doc)
|
await db.put(doc)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: newId,
|
id: newRowActionId,
|
||||||
...doc.actions[newId],
|
...doc.actions[newRowActionId],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,24 @@
|
||||||
import { DocumentDestroyResponse } from "@budibase/nano"
|
import { DocumentDestroyResponse } from "@budibase/nano"
|
||||||
|
import { Automation } from "../../documents"
|
||||||
|
|
||||||
export interface DeleteAutomationResponse extends DocumentDestroyResponse {}
|
export interface DeleteAutomationResponse extends DocumentDestroyResponse {}
|
||||||
|
|
||||||
|
export interface AutomationBuilderData {
|
||||||
|
displayName: string
|
||||||
|
triggerInfo?: {
|
||||||
|
type: string
|
||||||
|
table: {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
rowAction: {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FetchAutomationResponse {
|
||||||
|
automations: Automation[]
|
||||||
|
builderData?: Record<string, AutomationBuilderData> // The key will be the automationId
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue