1
0
Fork 0
mirror of synced 2024-09-10 14:35:47 +12:00
This commit is contained in:
Adria Navarro 2023-10-05 16:22:01 +02:00
parent 40226c5c3a
commit 7ee63365f7

View file

@ -6,10 +6,17 @@ import isEqual from "lodash/isEqual"
import uniq from "lodash/uniq" import uniq from "lodash/uniq"
import { updateAllFormulasInTable } from "../row/staticFormula" import { updateAllFormulasInTable } from "../row/staticFormula"
import { context } from "@budibase/backend-core" import { context } from "@budibase/backend-core"
import { FieldSchema, Table } from "@budibase/types" import {
FieldSchema,
FormulaFieldMetadata,
RelationshipFieldMetadata,
Table,
} from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
function isStaticFormula(column: FieldSchema) { function isStaticFormula(
column: FieldSchema
): column is FormulaFieldMetadata & { formulaType: FormulaTypes.STATIC } {
return ( return (
column.type === FieldTypes.FORMULA && column.type === FieldTypes.FORMULA &&
column.formulaType === FormulaTypes.STATIC column.formulaType === FormulaTypes.STATIC
@ -57,7 +64,8 @@ async function checkIfFormulaNeedsCleared(
let tableToUse: Table | undefined = table let tableToUse: Table | undefined = table
// if relationship, get the related table // if relationship, get the related table
if (removed.type === FieldTypes.LINK) { 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) { if (!tableToUse) {
continue continue
@ -73,7 +81,7 @@ async function checkIfFormulaNeedsCleared(
} }
for (let relatedTableId of table.relatedFormula) { for (let relatedTableId of table.relatedFormula) {
const relatedColumns = Object.values(table.schema).filter( 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) const relatedTable = tables.find(table => table._id === relatedTableId)
// look to see if the column was used in a relationship formula, // look to see if the column was used in a relationship formula,
@ -83,7 +91,7 @@ async function checkIfFormulaNeedsCleared(
for (let column of relatedColumns) { for (let column of relatedColumns) {
relatedFormulaToRemove = relatedFormulaToRemove.concat( relatedFormulaToRemove = relatedFormulaToRemove.concat(
getFormulaThatUseColumn(relatedTable, [ getFormulaThatUseColumn(relatedTable, [
column.fieldName!, (column as any).fieldName!,
removed.name, 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 * This function adds a note to related tables that they are
* used in a static formula - so that the link controller * 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 // clone the tables, so we can compare at end
const initialTables = cloneDeep(tables) const initialTables = cloneDeep(tables)
// first find the related column names // first find the related column names
const relatedColumns = Object.values(table.schema).filter( const relatedColumns = Object.values(table.schema).filter(isLink)
col => col.type === FieldTypes.LINK
)
// we start by removing the formula field from all tables // we start by removing the formula field from all tables
for (let otherTable of tables) { for (let otherTable of tables) {
if (!otherTable.relatedFormula) { if (!otherTable.relatedFormula) {
@ -135,6 +145,7 @@ async function updateRelatedFormulaLinksOnTables(
if (!columns || columns.length === 0) { if (!columns || columns.length === 0) {
continue continue
} }
const relatedTable = tables.find( const relatedTable = tables.find(
related => related._id === relatedCol.tableId related => related._id === relatedCol.tableId
) )