This commit is contained in:
Martin McKeaveney 2024-12-08 23:50:22 +00:00
parent 917c235295
commit ec84cf1a56
4 changed files with 80 additions and 50 deletions

View File

@ -19,8 +19,6 @@ import { builderSocket } from "../../../websockets"
const cloneDeep = require("lodash/cloneDeep")
import isEqual from "lodash/isEqual"
export async function fetch(ctx: Ctx) {
ctx.body = await getViews()
}

View File

@ -16,14 +16,10 @@ import {
CountCalculationFieldMetadata,
CreateViewResponse,
UpdateViewResponse,
View,
Event,
} from "@budibase/types"
import { events } from "@budibase/backend-core"
import { builderSocket, gridSocket } from "../../../websockets"
import { helpers } from "@budibase/shared-core"
import isEqual from "lodash/isEqual"
import { publishEvent } from "@budibase/backend-core/src/events"
function stripUnknownFields(
field: ViewFieldMetadata
@ -168,48 +164,51 @@ export async function create(ctx: Ctx<CreateViewRequest, CreateViewResponse>) {
gridSocket?.emitViewUpdate(ctx, result)
}
async function handleViewFilterEvents(existingView: ViewV2, view: ViewV2) {
const filterGroups = view.queryUI?.groups?.length || 0
const properties = { filterGroups, tableId: view.tableId }
if (!existingView?.queryUI) {
await events.view.filterCreated(properties)
} else {
if (
filterGroups >
((existingView && existingView?.queryUI?.groups?.length) || 0)
) {
await events.view.filterUpdated(properties)
}
}
}
async function handleViewEvents(existingView: ViewV2, view: ViewV2) {
// Grouped filters
if (view.queryUI?.groups) {
const filterGroups = view.queryUI?.groups?.length || 0
const properties = { filterGroups, tableId: view.tableId }
if (!existingView?.queryUI) {
await publishEvent(Event.VIEW_FILTER_CREATED, properties)
await events.view.filterCreated(properties)
} else {
if (
filterGroups >
((existingView && existingView?.queryUI?.groups?.length) || 0)
) {
await events.view.filterUpdated(properties)
}
}
await handleViewFilterEvents(existingView, view)
}
// if new columns in the view
for (const key in view.schema) {
if (!existingView?.schema?.[key]) {
const newColumn = view.schema[key]
// view calculations
// @ts-expect-error non calculation types just won't have the calculationType field
const calculationType = newColumn.calculationType
const calculationType = view.schema[key].calculationType
if (calculationType) {
// Send the event
await events.view.calculationCreated({
calculationType,
tableId: view.tableId,
})
}
}
// view joins
if (newColumn.columns) {
for (const column in newColumn?.columns) {
// if the new column is visible and it wasn't before
if (!existingView?.schema?.[key].columns?.[column].visible) {
// new view join exposing a column
await events.view.viewJoinCreated({ tableId: view.tableId })
}
// view joins
if (view.schema[key].columns) {
for (const column in view.schema[key]?.columns) {
// if the new column is visible and it wasn't before
if (
!existingView?.schema?.[key].columns?.[column].visible &&
view.schema?.[key].columns?.[column].visible
) {
// new view join exposing a column
await events.view.viewJoinCreated({ tableId: view.tableId })
}
}
}

View File

@ -247,6 +247,7 @@ if (descriptions.length) {
},
},
},
primary: ["_id"],
views: {},
sql: true,
})

View File

@ -1,39 +1,39 @@
import {
ArrayOperator,
BasicOperator,
BBReferenceFieldSubType,
CalculationType,
CreateViewRequest,
Datasource,
EmptyFilterOption,
FieldSchema,
FieldType,
INTERNAL_TABLE_SOURCE_ID,
JsonFieldSubType,
JsonTypes,
LegacyFilter,
NumericCalculationFieldMetadata,
PermissionLevel,
QuotaUsageType,
RelationshipType,
RenameColumn,
Row,
SaveTableRequest,
SearchFilters,
SearchResponse,
SearchViewRowRequest,
SortOrder,
SortType,
StaticQuotaName,
Table,
TableSchema,
TableSourceType,
UILogicalOperator,
UISearchFilter,
UpdateViewRequest,
ViewV2,
SearchResponse,
BasicOperator,
CalculationType,
RelationshipType,
TableSchema,
RenameColumn,
BBReferenceFieldSubType,
NumericCalculationFieldMetadata,
ViewV2Schema,
ViewV2Type,
JsonTypes,
EmptyFilterOption,
JsonFieldSubType,
UISearchFilter,
LegacyFilter,
SearchViewRowRequest,
ArrayOperator,
UILogicalOperator,
SearchFilters,
} from "@budibase/types"
import { generator, mocks } from "@budibase/backend-core/tests"
import {
@ -42,7 +42,7 @@ import {
} from "../../../integrations/tests/utils"
import merge from "lodash/merge"
import { quotas } from "@budibase/pro"
import { db, roles, context, events } from "@budibase/backend-core"
import { context, db, events, roles } from "@budibase/backend-core"
const descriptions = datasourceDescribe({ exclude: [DatabaseName.MONGODB] })
@ -1360,6 +1360,8 @@ if (descriptions.length) {
},
})
expect(events.view.filterCreated).toHaveBeenCalledTimes(1)
updatedView = await config.api.viewV2.get(view.id)
expected = {
onEmptyFilter: EmptyFilterOption.RETURN_ALL,
@ -2160,7 +2162,7 @@ if (descriptions.length) {
}),
})
)
expect(events.view.viewJoinCreated).not.toBeCalled()
expect(events.view.viewJoinCreated).not.toHaveBeenCalled()
})
it("does not rename columns with the same name but from other tables", async () => {
@ -2233,6 +2235,36 @@ if (descriptions.length) {
)
})
it("handles events for changing column visibility from default false", async () => {
let auxTable = await createAuxTable()
let aux2Table = await createAuxTable()
const table = await createMainTable([
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux" },
{ name: "aux2", tableId: aux2Table._id!, fk: "fk_aux2" },
])
const view = await createView(table._id!, {
aux: {
visible: true,
columns: {
name: { visible: false, readonly: true },
},
},
aux2: {
visible: true,
columns: {
name: { visible: false, readonly: true },
},
},
})
// @ts-expect-error column exists above
view.schema.aux2.columns.name.visible = true
await config.api.viewV2.update(view)
expect(events.view.viewJoinCreated).toHaveBeenCalledTimes(1)
})
it("updates all views references", async () => {
let auxTable = await createAuxTable()