From 7ee63365f78e6f9260d0d44c63dc42a7ee3d0444 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 5 Oct 2023 16:22:01 +0200 Subject: [PATCH] Fix --- .../src/api/controllers/table/bulkFormula.ts | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/server/src/api/controllers/table/bulkFormula.ts b/packages/server/src/api/controllers/table/bulkFormula.ts index e44cb95391..4cc3849c5b 100644 --- a/packages/server/src/api/controllers/table/bulkFormula.ts +++ b/packages/server/src/api/controllers/table/bulkFormula.ts @@ -6,10 +6,17 @@ import isEqual from "lodash/isEqual" import uniq from "lodash/uniq" import { updateAllFormulasInTable } from "../row/staticFormula" import { context } from "@budibase/backend-core" -import { FieldSchema, Table } from "@budibase/types" +import { + FieldSchema, + FormulaFieldMetadata, + RelationshipFieldMetadata, + Table, +} from "@budibase/types" import sdk from "../../../sdk" -function isStaticFormula(column: FieldSchema) { +function isStaticFormula( + column: FieldSchema +): column is FormulaFieldMetadata & { formulaType: FormulaTypes.STATIC } { return ( column.type === FieldTypes.FORMULA && column.formulaType === FormulaTypes.STATIC @@ -57,7 +64,8 @@ async function checkIfFormulaNeedsCleared( let tableToUse: Table | undefined = table // if relationship, get the related table if (removed.type === FieldTypes.LINK) { - tableToUse = tables.find(table => table._id === removed.tableId) + const removedTableId = removed.tableId + tableToUse = tables.find(table => table._id === removedTableId) } if (!tableToUse) { continue @@ -73,7 +81,7 @@ async function checkIfFormulaNeedsCleared( } for (let relatedTableId of table.relatedFormula) { const relatedColumns = Object.values(table.schema).filter( - column => column.tableId === relatedTableId + column => (column as any).tableId === relatedTableId ) const relatedTable = tables.find(table => table._id === relatedTableId) // look to see if the column was used in a relationship formula, @@ -83,7 +91,7 @@ async function checkIfFormulaNeedsCleared( for (let column of relatedColumns) { relatedFormulaToRemove = relatedFormulaToRemove.concat( getFormulaThatUseColumn(relatedTable, [ - column.fieldName!, + (column as any).fieldName!, removed.name, ]) ) @@ -96,6 +104,10 @@ async function checkIfFormulaNeedsCleared( } } +function isLink(column: FieldSchema): column is RelationshipFieldMetadata { + return column.type === FieldTypes.LINK +} + /** * This function adds a note to related tables that they are * used in a static formula - so that the link controller @@ -115,9 +127,7 @@ async function updateRelatedFormulaLinksOnTables( // clone the tables, so we can compare at end const initialTables = cloneDeep(tables) // first find the related column names - const relatedColumns = Object.values(table.schema).filter( - col => col.type === FieldTypes.LINK - ) + const relatedColumns = Object.values(table.schema).filter(isLink) // we start by removing the formula field from all tables for (let otherTable of tables) { if (!otherTable.relatedFormula) { @@ -135,6 +145,7 @@ async function updateRelatedFormulaLinksOnTables( if (!columns || columns.length === 0) { continue } + const relatedTable = tables.find( related => related._id === relatedCol.tableId )