Merge remote-tracking branch 'origin/feature/automation-branching-ux' into automation-branching-ux-updates

This commit is contained in:
Dean 2024-10-31 16:39:43 +00:00
commit 50beda83ed
25 changed files with 384 additions and 112 deletions

View File

@ -1091,7 +1091,14 @@ class InternalBuilder {
) )
} }
} else { } else {
query = query.count(`* as ${aggregation.name}`) if (this.client === SqlClient.ORACLE) {
const field = this.convertClobs(`${tableName}.${aggregation.field}`)
query = query.select(
this.knex.raw(`COUNT(??) as ??`, [field, aggregation.name])
)
} else {
query = query.count(`${aggregation.field} as ${aggregation.name}`)
}
} }
} else { } else {
const fieldSchema = this.getFieldSchema(aggregation.field) const fieldSchema = this.getFieldSchema(aggregation.field)

View File

@ -286,6 +286,10 @@
// Optimization options // Optimization options
const onViewMouseMove = async e => { const onViewMouseMove = async e => {
if (!viewPort) {
return
}
const { x, y } = eleXY(e, viewPort) const { x, y } = eleXY(e, viewPort)
internalPos.update(() => ({ internalPos.update(() => ({
@ -427,6 +431,9 @@
} }
const onMouseMove = async e => { const onMouseMove = async e => {
if (!viewPort) {
return
}
// Update viewDims to get the latest viewport dimensions // Update viewDims to get the latest viewport dimensions
viewDims = viewPort.getBoundingClientRect() viewDims = viewPort.getBoundingClientRect()
@ -484,7 +491,7 @@
} }
const onMoveContent = e => { const onMoveContent = e => {
if (down) { if (down || !viewPort) {
return return
} }
const { x, y } = eleXY(e, viewPort) const { x, y } = eleXY(e, viewPort)

View File

@ -11,6 +11,8 @@
TooltipPosition, TooltipPosition,
TooltipType, TooltipType,
Button, Button,
Modal,
ModalContent,
} from "@budibase/bbui" } from "@budibase/bbui"
import PropField from "components/automation/SetupPanel/PropField.svelte" import PropField from "components/automation/SetupPanel/PropField.svelte"
import AutomationBindingPanel from "components/common/bindings/ServerBindingPanel.svelte" import AutomationBindingPanel from "components/common/bindings/ServerBindingPanel.svelte"
@ -36,6 +38,7 @@
let drawer let drawer
let condition let condition
let open = true let open = true
let confirmDeleteModal
$: branch = step.inputs?.branches?.[branchIdx] $: branch = step.inputs?.branches?.[branchIdx]
$: editableConditionUI = cloneDeep(branch.conditionUI || {}) $: editableConditionUI = cloneDeep(branch.conditionUI || {})
@ -55,6 +58,22 @@
} }
</script> </script>
<Modal bind:this={confirmDeleteModal}>
<ModalContent
size="M"
title={"Are you sure you want to delete?"}
confirmText="Delete"
onConfirm={async () => {
await automationStore.actions.deleteBranch(
branchBlockRef.pathTo,
$selectedAutomation.data
)
}}
>
<Body>By deleting this branch, you will delete all of its contents.</Body>
</ModalContent>
</Modal>
<Drawer bind:this={drawer} title="Branch condition" forceModal> <Drawer bind:this={drawer} title="Branch condition" forceModal>
<Button <Button
cta cta
@ -101,10 +120,15 @@
itemName={branch.name} itemName={branch.name}
block={step} block={step}
deleteStep={async () => { deleteStep={async () => {
const branchSteps = step.inputs?.children[branch.id]
if (branchSteps.length) {
confirmDeleteModal.show()
} else {
await automationStore.actions.deleteBranch( await automationStore.actions.deleteBranch(
branchBlockRef.pathTo, branchBlockRef.pathTo,
$selectedAutomation.data $selectedAutomation.data
) )
}
}} }}
on:update={async e => { on:update={async e => {
let stepUpdate = cloneDeep(step) let stepUpdate = cloneDeep(step)

View File

@ -13,7 +13,6 @@
Modal, Modal,
Label, Label,
AbsTooltip, AbsTooltip,
InlineAlert,
} from "@budibase/bbui" } from "@budibase/bbui"
import { sdk } from "@budibase/shared-core" import { sdk } from "@budibase/shared-core"
import AutomationBlockSetup from "../../SetupPanel/AutomationBlockSetup.svelte" import AutomationBlockSetup from "../../SetupPanel/AutomationBlockSetup.svelte"
@ -26,6 +25,7 @@
import DragHandle from "components/design/settings/controls/DraggableList/drag-handle.svelte" import DragHandle from "components/design/settings/controls/DraggableList/drag-handle.svelte"
import { getContext } from "svelte" import { getContext } from "svelte"
import DragZone from "./DragZone.svelte" import DragZone from "./DragZone.svelte"
import InfoDisplay from "pages/builder/app/[application]/design/[screenId]/[componentId]/_components/Component/InfoDisplay.svelte"
export let block export let block
export let blockRef export let blockRef
@ -335,9 +335,10 @@
{bindings} {bindings}
/> />
{#if isTrigger && triggerInfo} {#if isTrigger && triggerInfo}
<InlineAlert <InfoDisplay
header={triggerInfo.title} title={triggerInfo.title}
message={`This trigger is tied to your "${triggerInfo.tableName}" table`} body="This trigger is tied to your '{triggerInfo.tableName}' table"
icon="InfoOutline"
/> />
{/if} {/if}
</Layout> </Layout>

View File

@ -9,7 +9,7 @@
} from "@budibase/bbui" } from "@budibase/bbui"
import { onMount, createEventDispatcher } from "svelte" import { onMount, createEventDispatcher } from "svelte"
import { flags } from "stores/builder" import { flags } from "stores/builder"
import { licensing } from "stores/portal" import { featureFlags } from "stores/portal"
import { API } from "api" import { API } from "api"
import MagicWand from "../../../../assets/MagicWand.svelte" import MagicWand from "../../../../assets/MagicWand.svelte"
@ -26,8 +26,7 @@
let aiCronPrompt = "" let aiCronPrompt = ""
let loadingAICronExpression = false let loadingAICronExpression = false
$: aiEnabled = $: aiEnabled = $featureFlags.AI_CUSTOM_CONFIGS || $featureFlags.BUDIBASE_AI
$licensing.customAIConfigsEnabled || $licensing.budibaseAIEnabled
$: { $: {
if (cronExpression) { if (cronExpression) {
try { try {

View File

@ -8,6 +8,7 @@ const MAX_DEPTH = 1
const TYPES_TO_SKIP = [ const TYPES_TO_SKIP = [
FieldType.FORMULA, FieldType.FORMULA,
FieldType.AI,
FieldType.LONGFORM, FieldType.LONGFORM,
FieldType.SIGNATURE_SINGLE, FieldType.SIGNATURE_SINGLE,
FieldType.ATTACHMENTS, FieldType.ATTACHMENTS,

View File

@ -26,7 +26,7 @@
import { createEventDispatcher, getContext, onMount } from "svelte" import { createEventDispatcher, getContext, onMount } from "svelte"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { tables, datasources } from "stores/builder" import { tables, datasources } from "stores/builder"
import { licensing } from "stores/portal" import { featureFlags } from "stores/portal"
import { TableNames, UNEDITABLE_USER_FIELDS } from "constants" import { TableNames, UNEDITABLE_USER_FIELDS } from "constants"
import { import {
FIELDS, FIELDS,
@ -101,8 +101,7 @@
let optionsValid = true let optionsValid = true
$: rowGoldenSample = RowUtils.generateGoldenSample($rows) $: rowGoldenSample = RowUtils.generateGoldenSample($rows)
$: aiEnabled = $: aiEnabled = $featureFlags.BUDIBASE_AI || $featureFlags.AI_CUSTOM_CONFIGS
$licensing.customAIConfigsEnabled || $licensing.budibaseAIEnabled
$: if (primaryDisplay) { $: if (primaryDisplay) {
editableColumn.constraints.presence = { allowEmpty: false } editableColumn.constraints.presence = { allowEmpty: false }
} }

View File

@ -80,6 +80,9 @@
if (columnType === FieldType.FORMULA) { if (columnType === FieldType.FORMULA) {
return "https://docs.budibase.com/docs/formula" return "https://docs.budibase.com/docs/formula"
} }
if (columnType === FieldType.AI) {
return "https://docs.budibase.com/docs/ai"
}
if (columnType === FieldType.OPTIONS) { if (columnType === FieldType.OPTIONS) {
return "https://docs.budibase.com/docs/options" return "https://docs.budibase.com/docs/options"
} }

View File

@ -1,6 +1,6 @@
<script> <script>
import { viewsV2, rowActions } from "stores/builder" import { viewsV2, rowActions } from "stores/builder"
import { admin, themeStore, licensing } from "stores/portal" import { admin, themeStore, featureFlags } from "stores/portal"
import { Grid } from "@budibase/frontend-core" import { Grid } from "@budibase/frontend-core"
import { API } from "api" import { API } from "api"
import { notifications } from "@budibase/bbui" import { notifications } from "@budibase/bbui"
@ -53,7 +53,7 @@
{buttons} {buttons}
allowAddRows allowAddRows
allowDeleteRows allowDeleteRows
aiEnabled={$licensing.budibaseAIEnabled || $licensing.customAIConfigsEnabled} aiEnabled={$featureFlags.BUDIBASE_AI || $featureFlags.AI_CUSTOM_CONFIGS}
showAvatars={false} showAvatars={false}
on:updatedatasource={handleGridViewUpdate} on:updatedatasource={handleGridViewUpdate}
isCloud={$admin.cloud} isCloud={$admin.cloud}

View File

@ -8,7 +8,7 @@
rowActions, rowActions,
roles, roles,
} from "stores/builder" } from "stores/builder"
import { themeStore, admin, licensing } from "stores/portal" import { themeStore, admin, featureFlags } from "stores/portal"
import { TableNames } from "constants" import { TableNames } from "constants"
import { Grid } from "@budibase/frontend-core" import { Grid } from "@budibase/frontend-core"
import { API } from "api" import { API } from "api"
@ -130,8 +130,7 @@
schemaOverrides={isUsersTable ? userSchemaOverrides : null} schemaOverrides={isUsersTable ? userSchemaOverrides : null}
showAvatars={false} showAvatars={false}
isCloud={$admin.cloud} isCloud={$admin.cloud}
aiEnabled={$licensing.budibaseAIEnabled || aiEnabled={$featureFlags.BUDIBASE_AI || $featureFlags.AI_CUSTOM_CONFIGS}
$licensing.customAIConfigsEnabled}
{buttons} {buttons}
buttonsCollapsed buttonsCollapsed
on:updatedatasource={handleGridTableUpdate} on:updatedatasource={handleGridTableUpdate}

View File

@ -4,6 +4,8 @@ import { auth } from "stores/portal"
export const INITIAL_FEATUREFLAG_STATE = { export const INITIAL_FEATUREFLAG_STATE = {
SQS: false, SQS: false,
DEFAULT_VALUES: false, DEFAULT_VALUES: false,
BUDIBASE_AI: false,
AI_CUSTOM_CONFIGS: false,
} }
export const featureFlags = derived([auth], ([$auth]) => { export const featureFlags = derived([auth], ([$auth]) => {

View File

@ -1,4 +1,4 @@
import { AutoFieldSubType, FieldType } from "@budibase/types" import { AIOperationEnum, AutoFieldSubType, FieldType } from "@budibase/types"
import TestConfiguration from "../../../../tests/utilities/TestConfiguration" import TestConfiguration from "../../../../tests/utilities/TestConfiguration"
import { importToRows } from "../utils" import { importToRows } from "../utils"
@ -92,5 +92,65 @@ describe("utils", () => {
expect(result).toHaveLength(3) expect(result).toHaveLength(3)
}) })
}) })
it("Imports write as expected with AI columns", async () => {
await config.doInContext(config.appId, async () => {
const table = await config.createTable({
name: "table",
type: "table",
schema: {
autoId: {
name: "autoId",
type: FieldType.NUMBER,
subtype: AutoFieldSubType.AUTO_ID,
autocolumn: true,
constraints: {
type: FieldType.NUMBER,
presence: true,
},
},
name: {
name: "name",
type: FieldType.STRING,
constraints: {
type: FieldType.STRING,
presence: true,
},
},
aicol: {
name: "aicol",
type: FieldType.AI,
operation: AIOperationEnum.PROMPT,
prompt: "Test prompt",
},
},
})
const data = [
{ name: "Alice", aicol: "test" },
{ name: "Bob", aicol: "test" },
{ name: "Claire", aicol: "test" },
]
const result = await importToRows(data, table, config.user?._id)
expect(result).toEqual([
expect.objectContaining({
autoId: 1,
name: "Alice",
aicol: "test",
}),
expect.objectContaining({
autoId: 2,
name: "Bob",
aicol: "test",
}),
expect.objectContaining({
autoId: 3,
name: "Claire",
aicol: "test",
}),
])
})
})
}) })
}) })

View File

@ -15,7 +15,7 @@ import { getViews, saveView } from "../view/utils"
import viewTemplate from "../view/viewBuilder" import viewTemplate from "../view/viewBuilder"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { context, events, features } from "@budibase/backend-core" import { context, events, features, HTTPError } from "@budibase/backend-core"
import { import {
AutoFieldSubType, AutoFieldSubType,
Database, Database,
@ -145,14 +145,21 @@ export async function importToRows(
// the real schema of the table passed in, not the clone used for // the real schema of the table passed in, not the clone used for
// incrementing auto IDs // incrementing auto IDs
for (const [fieldName, schema] of Object.entries(originalTable.schema)) { for (const [fieldName, schema] of Object.entries(originalTable.schema)) {
const rowVal = Array.isArray(row[fieldName]) if (schema.type === FieldType.LINK) {
? row[fieldName] throw new HTTPError(
: [row[fieldName]] `Can't bulk import relationship fields for internal databases, found value in field "${fieldName}"`,
400
)
}
if ( if (
(schema.type === FieldType.OPTIONS || (schema.type === FieldType.OPTIONS ||
schema.type === FieldType.ARRAY) && schema.type === FieldType.ARRAY) &&
row[fieldName] row[fieldName]
) { ) {
const rowVal = Array.isArray(row[fieldName])
? row[fieldName]
: [row[fieldName]]
let merged = [...schema.constraints!.inclusion!, ...rowVal] let merged = [...schema.constraints!.inclusion!, ...rowVal]
let superSet = new Set(merged) let superSet = new Set(merged)
schema.constraints!.inclusion = Array.from(superSet) schema.constraints!.inclusion = Array.from(superSet)

View File

@ -12,6 +12,8 @@ import {
RelationSchemaField, RelationSchemaField,
ViewFieldMetadata, ViewFieldMetadata,
CalculationType, CalculationType,
CountDistinctCalculationFieldMetadata,
CountCalculationFieldMetadata,
} from "@budibase/types" } from "@budibase/types"
import { builderSocket, gridSocket } from "../../../websockets" import { builderSocket, gridSocket } from "../../../websockets"
import { helpers } from "@budibase/shared-core" import { helpers } from "@budibase/shared-core"
@ -22,7 +24,8 @@ function stripUnknownFields(
if (helpers.views.isCalculationField(field)) { if (helpers.views.isCalculationField(field)) {
if (field.calculationType === CalculationType.COUNT) { if (field.calculationType === CalculationType.COUNT) {
if ("distinct" in field && field.distinct) { if ("distinct" in field && field.distinct) {
return { const strippedField: RequiredKeys<CountDistinctCalculationFieldMetadata> =
{
order: field.order, order: field.order,
width: field.width, width: field.width,
visible: field.visible, visible: field.visible,
@ -33,16 +36,19 @@ function stripUnknownFields(
field: field.field, field: field.field,
columns: field.columns, columns: field.columns,
} }
return strippedField
} else { } else {
return { const strippedField: RequiredKeys<CountCalculationFieldMetadata> = {
order: field.order, order: field.order,
width: field.width, width: field.width,
visible: field.visible, visible: field.visible,
readonly: field.readonly, readonly: field.readonly,
icon: field.icon, icon: field.icon,
calculationType: field.calculationType, calculationType: field.calculationType,
field: field.field,
columns: field.columns, columns: field.columns,
} }
return strippedField
} }
} }
const strippedField: RequiredKeys<ViewCalculationFieldMetadata> = { const strippedField: RequiredKeys<ViewCalculationFieldMetadata> = {

View File

@ -1823,6 +1823,39 @@ describe.each([
expect(row.autoId).toEqual(3) expect(row.autoId).toEqual(3)
}) })
isInternal &&
it("should reject bulkImporting relationship fields", async () => {
const table1 = await config.api.table.save(saveTableRequest())
const table2 = await config.api.table.save(
saveTableRequest({
schema: {
relationship: {
name: "relationship",
type: FieldType.LINK,
tableId: table1._id!,
relationshipType: RelationshipType.ONE_TO_MANY,
fieldName: "relationship",
},
},
})
)
const table1Row1 = await config.api.row.save(table1._id!, {})
await config.api.row.bulkImport(
table2._id!,
{
rows: [{ relationship: [table1Row1._id!] }],
},
{
status: 400,
body: {
message:
'Can\'t bulk import relationship fields for internal databases, found value in field "relationship"',
},
}
)
})
it("should be able to bulkImport rows", async () => { it("should be able to bulkImport rows", async () => {
const table = await config.api.table.save( const table = await config.api.table.save(
saveTableRequest({ saveTableRequest({
@ -2462,7 +2495,7 @@ describe.each([
[FieldType.ATTACHMENT_SINGLE]: setup.structures.basicAttachment(), [FieldType.ATTACHMENT_SINGLE]: setup.structures.basicAttachment(),
[FieldType.FORMULA]: undefined, // generated field [FieldType.FORMULA]: undefined, // generated field
[FieldType.AUTO]: undefined, // generated field [FieldType.AUTO]: undefined, // generated field
[FieldType.AI]: undefined, // generated field [FieldType.AI]: "LLM Output",
[FieldType.JSON]: { name: generator.guid() }, [FieldType.JSON]: { name: generator.guid() },
[FieldType.INTERNAL]: generator.guid(), [FieldType.INTERNAL]: generator.guid(),
[FieldType.BARCODEQR]: generator.guid(), [FieldType.BARCODEQR]: generator.guid(),
@ -2494,6 +2527,7 @@ describe.each([
}), }),
[FieldType.FORMULA]: fullSchema[FieldType.FORMULA].formula, [FieldType.FORMULA]: fullSchema[FieldType.FORMULA].formula,
[FieldType.AUTO]: expect.any(Number), [FieldType.AUTO]: expect.any(Number),
[FieldType.AI]: expect.any(String),
[FieldType.JSON]: rowValues[FieldType.JSON], [FieldType.JSON]: rowValues[FieldType.JSON],
[FieldType.INTERNAL]: rowValues[FieldType.INTERNAL], [FieldType.INTERNAL]: rowValues[FieldType.INTERNAL],
[FieldType.BARCODEQR]: rowValues[FieldType.BARCODEQR], [FieldType.BARCODEQR]: rowValues[FieldType.BARCODEQR],
@ -2566,7 +2600,7 @@ describe.each([
expectedRowData["bb_reference_single"].sample, expectedRowData["bb_reference_single"].sample,
false false
), ),
ai: null, ai: "LLM Output",
}, },
]) ])
}) })

View File

@ -693,6 +693,12 @@ describe.each([
calculationType: CalculationType.COUNT, calculationType: CalculationType.COUNT,
field: "Price", field: "Price",
}, },
countDistinct: {
visible: true,
calculationType: CalculationType.COUNT,
distinct: true,
field: "Price",
},
min: { min: {
visible: true, visible: true,
calculationType: CalculationType.MIN, calculationType: CalculationType.MIN,
@ -708,11 +714,6 @@ describe.each([
calculationType: CalculationType.AVG, calculationType: CalculationType.AVG,
field: "Price", field: "Price",
}, },
sum2: {
visible: true,
calculationType: CalculationType.SUM,
field: "Price",
},
}, },
}, },
{ {
@ -763,10 +764,12 @@ describe.each([
count: { count: {
visible: true, visible: true,
calculationType: CalculationType.COUNT, calculationType: CalculationType.COUNT,
field: "Price",
}, },
count2: { count2: {
visible: true, visible: true,
calculationType: CalculationType.COUNT, calculationType: CalculationType.COUNT,
field: "Price",
}, },
}, },
}, },
@ -774,7 +777,7 @@ describe.each([
status: 400, status: 400,
body: { body: {
message: message:
'Duplicate calculation on field "*", calculation type "count"', 'Duplicate calculation on field "Price", calculation type "count"',
}, },
} }
) )
@ -805,7 +808,7 @@ describe.each([
status: 400, status: 400,
body: { body: {
message: message:
'Duplicate calculation on field "Price", calculation type "count"', 'Duplicate calculation on field "Price", calculation type "count distinct"',
}, },
} }
) )
@ -820,6 +823,7 @@ describe.each([
count: { count: {
visible: true, visible: true,
calculationType: CalculationType.COUNT, calculationType: CalculationType.COUNT,
field: "Price",
}, },
count2: { count2: {
visible: true, visible: true,
@ -831,6 +835,26 @@ describe.each([
}) })
}) })
it("does not confuse counts on different fields in the duplicate check", async () => {
await config.api.viewV2.create({
tableId: table._id!,
name: generator.guid(),
type: ViewV2Type.CALCULATION,
schema: {
count: {
visible: true,
calculationType: CalculationType.COUNT,
field: "Price",
},
count2: {
visible: true,
calculationType: CalculationType.COUNT,
field: "Category",
},
},
})
})
!isLucene && !isLucene &&
it("does not get confused when a calculation field shadows a basic one", async () => { it("does not get confused when a calculation field shadows a basic one", async () => {
const table = await config.api.table.save( const table = await config.api.table.save(
@ -1607,6 +1631,7 @@ describe.each([
view.schema!.count = { view.schema!.count = {
visible: true, visible: true,
calculationType: CalculationType.COUNT, calculationType: CalculationType.COUNT,
field: "age",
} }
await config.api.viewV2.update(view) await config.api.viewV2.update(view)
@ -3761,6 +3786,51 @@ describe.each([
expect(rows[0].sum).toEqual(55) expect(rows[0].sum).toEqual(55)
}) })
it("should be able to count non-numeric fields", async () => {
const table = await config.api.table.save(
saveTableRequest({
schema: {
firstName: {
type: FieldType.STRING,
name: "firstName",
},
lastName: {
type: FieldType.STRING,
name: "lastName",
},
},
})
)
const view = await config.api.viewV2.create({
tableId: table._id!,
name: generator.guid(),
type: ViewV2Type.CALCULATION,
schema: {
count: {
visible: true,
calculationType: CalculationType.COUNT,
field: "firstName",
},
},
})
await config.api.row.bulkImport(table._id!, {
rows: [
{ firstName: "Jane", lastName: "Smith" },
{ firstName: "Jane", lastName: "Doe" },
{ firstName: "Alice", lastName: "Smith" },
],
})
const { rows } = await config.api.viewV2.search(view.id, {
query: {},
})
expect(rows).toHaveLength(1)
expect(rows[0].count).toEqual(3)
})
it("should be able to filter rows on the view itself", async () => { it("should be able to filter rows on the view itself", async () => {
const table = await config.api.table.save( const table = await config.api.table.save(
saveTableRequest({ saveTableRequest({

View File

@ -351,6 +351,7 @@ export async function search(
aggregations.push({ aggregations.push({
name: key, name: key,
calculationType: field.calculationType, calculationType: field.calculationType,
field: mapToUserColumn(field.field),
}) })
} }
} else { } else {

View File

@ -61,12 +61,68 @@ export function isView(view: any): view is ViewV2 {
return view.id && docIds.isViewId(view.id) && view.version === 2 return view.id && docIds.isViewId(view.id) && view.version === 2
} }
function guardDuplicateCalculationFields(view: Omit<ViewV2, "id" | "version">) {
const seen: Record<string, Record<CalculationType, boolean>> = {}
const calculationFields = helpers.views.calculationFields(view)
for (const name of Object.keys(calculationFields)) {
const schema = calculationFields[name]
const isCount = schema.calculationType === CalculationType.COUNT
const isDistinct = "distinct" in schema
if (isCount && isDistinct) {
// We do these separately.
continue
}
if (seen[schema.field]?.[schema.calculationType]) {
throw new HTTPError(
`Duplicate calculation on field "${schema.field}", calculation type "${schema.calculationType}"`,
400
)
}
seen[schema.field] ??= {} as Record<CalculationType, boolean>
seen[schema.field][schema.calculationType] = true
}
}
function guardDuplicateCountDistinctFields(
view: Omit<ViewV2, "id" | "version">
) {
const seen: Record<string, Record<CalculationType, boolean>> = {}
const calculationFields = helpers.views.calculationFields(view)
for (const name of Object.keys(calculationFields)) {
const schema = calculationFields[name]
const isCount = schema.calculationType === CalculationType.COUNT
if (!isCount) {
continue
}
const isDistinct = "distinct" in schema
if (!isDistinct) {
// We do these separately.
continue
}
if (seen[schema.field]?.[schema.calculationType]) {
throw new HTTPError(
`Duplicate calculation on field "${schema.field}", calculation type "${schema.calculationType} distinct"`,
400
)
}
seen[schema.field] ??= {} as Record<CalculationType, boolean>
seen[schema.field][schema.calculationType] = true
}
}
async function guardCalculationViewSchema( async function guardCalculationViewSchema(
table: Table, table: Table,
view: Omit<ViewV2, "id" | "version"> view: Omit<ViewV2, "id" | "version">
) { ) {
const calculationFields = helpers.views.calculationFields(view) const calculationFields = helpers.views.calculationFields(view)
guardDuplicateCalculationFields(view)
guardDuplicateCountDistinctFields(view)
if (Object.keys(calculationFields).length > 5) { if (Object.keys(calculationFields).length > 5) {
throw new HTTPError( throw new HTTPError(
"Calculation views can only have a maximum of 5 fields", "Calculation views can only have a maximum of 5 fields",
@ -74,29 +130,8 @@ async function guardCalculationViewSchema(
) )
} }
const seen: Record<string, Record<CalculationType, boolean>> = {}
for (const name of Object.keys(calculationFields)) { for (const name of Object.keys(calculationFields)) {
const schema = calculationFields[name] const schema = calculationFields[name]
const isCount = schema.calculationType === CalculationType.COUNT
const isDistinct = isCount && "distinct" in schema && schema.distinct
const field = isCount && !isDistinct ? "*" : schema.field
if (seen[field]?.[schema.calculationType]) {
throw new HTTPError(
`Duplicate calculation on field "${field}", calculation type "${schema.calculationType}"`,
400
)
}
seen[field] ??= {} as Record<CalculationType, boolean>
seen[field][schema.calculationType] = true
// Count fields that aren't distinct don't need to reference another field,
// so we don't validate it.
if (isCount && !isDistinct) {
continue
}
if (!schema.field) { if (!schema.field) {
throw new HTTPError( throw new HTTPError(
`Calculation field "${name}" is missing a "field" property`, `Calculation field "${name}" is missing a "field" property`,
@ -112,6 +147,7 @@ async function guardCalculationViewSchema(
) )
} }
const isCount = schema.calculationType === CalculationType.COUNT
if (!isCount && !isNumeric(targetSchema.type)) { if (!isCount && !isNumeric(targetSchema.type)) {
throw new HTTPError( throw new HTTPError(
`Calculation field "${name}" references field "${schema.field}" which is not a numeric field`, `Calculation field "${name}" references field "${schema.field}" which is not a numeric field`,

View File

@ -8,7 +8,7 @@ import {
import * as actions from "../automations/actions" import * as actions from "../automations/actions"
import * as automationUtils from "../automations/automationUtils" import * as automationUtils from "../automations/automationUtils"
import { replaceFakeBindings } from "../automations/loopUtils" import { replaceFakeBindings } from "../automations/loopUtils"
import { dataFilters, helpers } from "@budibase/shared-core" import { dataFilters, helpers, utils } from "@budibase/shared-core"
import { default as AutomationEmitter } from "../events/AutomationEmitter" import { default as AutomationEmitter } from "../events/AutomationEmitter"
import { generateAutomationMetadataID, isProdAppID } from "../db/utils" import { generateAutomationMetadataID, isProdAppID } from "../db/utils"
import { definitions as triggerDefs } from "../automations/triggerInfo" import { definitions as triggerDefs } from "../automations/triggerInfo"
@ -23,10 +23,12 @@ import {
AutomationStatus, AutomationStatus,
AutomationStep, AutomationStep,
AutomationStepStatus, AutomationStepStatus,
BranchSearchFilters,
BranchStep, BranchStep,
isLogicalSearchOperator,
LoopStep, LoopStep,
SearchFilters,
UserBindings, UserBindings,
isBasicSearchOperator,
} from "@budibase/types" } from "@budibase/types"
import { AutomationContext, TriggerOutput } from "../definitions/automations" import { AutomationContext, TriggerOutput } from "../definitions/automations"
import { WorkerCallback } from "./definitions" import { WorkerCallback } from "./definitions"
@ -547,34 +549,51 @@ class Orchestrator {
} }
private async evaluateBranchCondition( private async evaluateBranchCondition(
conditions: SearchFilters conditions: BranchSearchFilters
): Promise<boolean> { ): Promise<boolean> {
const toFilter: Record<string, any> = {} const toFilter: Record<string, any> = {}
const processedConditions = dataFilters.recurseSearchFilters( const recurseSearchFilters = (
conditions, filters: BranchSearchFilters
filter => { ): BranchSearchFilters => {
Object.entries(filter).forEach(([_, value]) => { for (const filterKey of Object.keys(
Object.entries(value).forEach(([field, val]) => { filters
) as (keyof typeof filters)[]) {
if (!filters[filterKey]) {
continue
}
if (isLogicalSearchOperator(filterKey)) {
filters[filterKey].conditions = filters[filterKey].conditions.map(
condition => recurseSearchFilters(condition)
)
} else if (isBasicSearchOperator(filterKey)) {
for (const [field, value] of Object.entries(filters[filterKey])) {
const fromContext = processStringSync( const fromContext = processStringSync(
field, field,
this.processContext(this.context) this.processContext(this.context)
) )
toFilter[field] = fromContext toFilter[field] = fromContext
if (typeof val === "string" && findHBSBlocks(val).length > 0) { if (typeof value === "string" && findHBSBlocks(value).length > 0) {
const processedVal = processStringSync( const processedVal = processStringSync(
val, value,
this.processContext(this.context) this.processContext(this.context)
) )
value[field] = processedVal filters[filterKey][field] = processedVal
} }
})
})
return filter
} }
) } else {
// We want to types to complain if we extend BranchSearchFilters, but not to throw if the request comes with some extra data. It will just be ignored
utils.unreachable(filterKey, { doNotThrow: true })
}
}
return filters
}
const processedConditions = recurseSearchFilters(conditions)
const result = dataFilters.runQuery([toFilter], processedConditions) const result = dataFilters.runQuery([toFilter], processedConditions)
return result.length > 0 return result.length > 0

View File

@ -216,10 +216,6 @@ export async function inputProcessing(
if (field.type === FieldType.FORMULA) { if (field.type === FieldType.FORMULA) {
delete clonedRow[key] delete clonedRow[key]
} }
// remove any AI values, they are to be generated
if (field.type === FieldType.AI) {
delete clonedRow[key]
}
// otherwise coerce what is there to correct types // otherwise coerce what is there to correct types
else { else {
clonedRow[key] = coerce(value, field.type) clonedRow[key] = coerce(value, field.type)

View File

@ -142,25 +142,6 @@ export function recurseLogicalOperators(
return filters return filters
} }
export function recurseSearchFilters(
filters: SearchFilters,
processFn: (filter: SearchFilters) => SearchFilters
): SearchFilters {
// Process the current level
filters = processFn(filters)
// Recurse through logical operators
for (const logical of LOGICAL_OPERATORS) {
if (filters[logical]) {
filters[logical]!.conditions = filters[logical]!.conditions.map(
condition => recurseSearchFilters(condition, processFn)
)
}
}
return filters
}
/** /**
* Removes any fields that contain empty strings that would cause inconsistent * Removes any fields that contain empty strings that would cause inconsistent
* behaviour with how backend tables are filtered (no value means no filter). * behaviour with how backend tables are filtered (no value means no filter).

View File

@ -26,10 +26,17 @@ const FILTER_ALLOWED_KEYS: (keyof SearchFilter)[] = [
export function unreachable( export function unreachable(
value: never, value: never,
message = `No such case in exhaustive switch: ${value}` opts?: {
message?: string
doNotThrow?: boolean
}
) { ) {
const message = opts?.message || `No such case in exhaustive switch: ${value}`
const doNotThrow = !!opts?.doNotThrow
if (!doNotThrow) {
throw new Error(message) throw new Error(message)
} }
}
export async function parallelForeach<T>( export async function parallelForeach<T>(
items: T[], items: T[],

View File

@ -1,5 +1,10 @@
import { SortOrder } from "../../../api" import { SortOrder } from "../../../api"
import { SearchFilters, EmptyFilterOption } from "../../../sdk" import {
SearchFilters,
EmptyFilterOption,
BasicOperator,
LogicalOperator,
} from "../../../sdk"
import { HttpMethod } from "../query" import { HttpMethod } from "../query"
import { Row } from "../row" import { Row } from "../row"
import { LoopStepType, EmailAttachment, AutomationResults } from "./automation" import { LoopStepType, EmailAttachment, AutomationResults } from "./automation"
@ -118,9 +123,17 @@ export type BranchStepInputs = {
export type Branch = { export type Branch = {
id: any id: any
name: string name: string
condition: SearchFilters condition: BranchSearchFilters
} }
export type BranchSearchFilters = Pick<
SearchFilters,
| BasicOperator.EQUAL
| BasicOperator.NOT_EQUAL
| LogicalOperator.AND
| LogicalOperator.OR
>
export type MakeIntegrationInputs = { export type MakeIntegrationInputs = {
url: string url: string
body: any body: any

View File

@ -54,12 +54,12 @@ export interface NumericCalculationFieldMetadata
export interface CountCalculationFieldMetadata extends BasicViewFieldMetadata { export interface CountCalculationFieldMetadata extends BasicViewFieldMetadata {
calculationType: CalculationType.COUNT calculationType: CalculationType.COUNT
field: string
} }
export interface CountDistinctCalculationFieldMetadata export interface CountDistinctCalculationFieldMetadata
extends CountCalculationFieldMetadata { extends CountCalculationFieldMetadata {
distinct: true distinct: true
field: string
} }
export type ViewCalculationFieldMetadata = export type ViewCalculationFieldMetadata =

View File

@ -18,11 +18,11 @@ export interface NumericAggregation extends BaseAggregation {
export interface CountAggregation extends BaseAggregation { export interface CountAggregation extends BaseAggregation {
calculationType: CalculationType.COUNT calculationType: CalculationType.COUNT
field: string
} }
export interface CountDistinctAggregation extends CountAggregation { export interface CountDistinctAggregation extends CountAggregation {
distinct: true distinct: true
field: string
} }
export type Aggregation = export type Aggregation =