Merge pull request #14723 from Budibase/mark-calculation-views
Mark calculation views explicitly instead of figuring it out implicitly.
This commit is contained in:
commit
a82d3f310e
|
@ -127,13 +127,13 @@ export async function create(ctx: Ctx<CreateViewRequest, ViewResponse>) {
|
|||
|
||||
const parsedView: Omit<RequiredKeys<ViewV2>, "id" | "version"> = {
|
||||
name: view.name,
|
||||
type: view.type,
|
||||
tableId: view.tableId,
|
||||
query: view.query,
|
||||
queryUI: view.queryUI,
|
||||
sort: view.sort,
|
||||
schema,
|
||||
primaryDisplay: view.primaryDisplay,
|
||||
uiMetadata: view.uiMetadata,
|
||||
}
|
||||
const result = await sdk.views.create(tableId, parsedView)
|
||||
ctx.status = 201
|
||||
|
@ -163,6 +163,7 @@ export async function update(ctx: Ctx<UpdateViewRequest, ViewResponse>) {
|
|||
const parsedView: RequiredKeys<ViewV2> = {
|
||||
id: view.id,
|
||||
name: view.name,
|
||||
type: view.type,
|
||||
version: view.version,
|
||||
tableId: view.tableId,
|
||||
query: view.query,
|
||||
|
@ -170,7 +171,6 @@ export async function update(ctx: Ctx<UpdateViewRequest, ViewResponse>) {
|
|||
sort: view.sort,
|
||||
schema,
|
||||
primaryDisplay: view.primaryDisplay,
|
||||
uiMetadata: view.uiMetadata,
|
||||
}
|
||||
|
||||
const result = await sdk.views.update(tableId, parsedView)
|
||||
|
|
|
@ -26,6 +26,7 @@ import {
|
|||
BBReferenceFieldSubType,
|
||||
NumericCalculationFieldMetadata,
|
||||
ViewV2Schema,
|
||||
ViewV2Type,
|
||||
} from "@budibase/types"
|
||||
import { generator, mocks } from "@budibase/backend-core/tests"
|
||||
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
|
||||
|
@ -155,7 +156,7 @@ describe.each([
|
|||
})
|
||||
|
||||
it("can persist views with all fields", async () => {
|
||||
const newView: Required<Omit<CreateViewRequest, "queryUI">> = {
|
||||
const newView: Required<Omit<CreateViewRequest, "queryUI" | "type">> = {
|
||||
name: generator.name(),
|
||||
tableId: table._id!,
|
||||
primaryDisplay: "id",
|
||||
|
@ -177,9 +178,6 @@ describe.each([
|
|||
visible: true,
|
||||
},
|
||||
},
|
||||
uiMetadata: {
|
||||
foo: "bar",
|
||||
},
|
||||
}
|
||||
const res = await config.api.viewV2.create(newView)
|
||||
|
||||
|
@ -549,6 +547,7 @@ describe.each([
|
|||
let view = await config.api.viewV2.create({
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
type: ViewV2Type.CALCULATION,
|
||||
schema: {
|
||||
sum: {
|
||||
visible: true,
|
||||
|
@ -572,11 +571,35 @@ describe.each([
|
|||
expect(sum.field).toEqual("Price")
|
||||
})
|
||||
|
||||
it("cannot create a view with calculation fields unless it has the right type", async () => {
|
||||
await config.api.viewV2.create(
|
||||
{
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
schema: {
|
||||
sum: {
|
||||
visible: true,
|
||||
calculationType: CalculationType.SUM,
|
||||
field: "Price",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
status: 400,
|
||||
body: {
|
||||
message:
|
||||
"Calculation fields are not allowed in non-calculation views",
|
||||
},
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it("cannot create a calculation view with more than 5 aggregations", async () => {
|
||||
await config.api.viewV2.create(
|
||||
{
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
type: ViewV2Type.CALCULATION,
|
||||
schema: {
|
||||
sum: {
|
||||
visible: true,
|
||||
|
@ -624,6 +647,7 @@ describe.each([
|
|||
{
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
type: ViewV2Type.CALCULATION,
|
||||
schema: {
|
||||
sum: {
|
||||
visible: true,
|
||||
|
@ -652,6 +676,7 @@ describe.each([
|
|||
{
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
type: ViewV2Type.CALCULATION,
|
||||
schema: {
|
||||
count: {
|
||||
visible: true,
|
||||
|
@ -678,6 +703,7 @@ describe.each([
|
|||
{
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
type: ViewV2Type.CALCULATION,
|
||||
schema: {
|
||||
count: {
|
||||
visible: true,
|
||||
|
@ -707,6 +733,7 @@ describe.each([
|
|||
await config.api.viewV2.create({
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
type: ViewV2Type.CALCULATION,
|
||||
schema: {
|
||||
count: {
|
||||
visible: true,
|
||||
|
@ -765,7 +792,9 @@ describe.each([
|
|||
it("can update all fields", async () => {
|
||||
const tableId = table._id!
|
||||
|
||||
const updatedData: Required<Omit<UpdateViewRequest, "queryUI">> = {
|
||||
const updatedData: Required<
|
||||
Omit<UpdateViewRequest, "queryUI" | "type">
|
||||
> = {
|
||||
version: view.version,
|
||||
id: view.id,
|
||||
tableId,
|
||||
|
@ -793,9 +822,6 @@ describe.each([
|
|||
readonly: true,
|
||||
},
|
||||
},
|
||||
uiMetadata: {
|
||||
foo: "bar",
|
||||
},
|
||||
}
|
||||
await config.api.viewV2.update(updatedData)
|
||||
|
||||
|
@ -980,6 +1006,32 @@ describe.each([
|
|||
)
|
||||
})
|
||||
|
||||
it("cannot update view type after creation", async () => {
|
||||
const view = await config.api.viewV2.create({
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
schema: {
|
||||
id: { visible: true },
|
||||
Price: {
|
||||
visible: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await config.api.viewV2.update(
|
||||
{
|
||||
...view,
|
||||
type: ViewV2Type.CALCULATION,
|
||||
},
|
||||
{
|
||||
status: 400,
|
||||
body: {
|
||||
message: "Cannot update view type after creation",
|
||||
},
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
isInternal &&
|
||||
it("updating schema will only validate modified field", async () => {
|
||||
let view = await config.api.viewV2.create({
|
||||
|
@ -1052,6 +1104,7 @@ describe.each([
|
|||
view = await config.api.viewV2.create({
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
type: ViewV2Type.CALCULATION,
|
||||
schema: {
|
||||
country: {
|
||||
visible: true,
|
||||
|
@ -2809,6 +2862,7 @@ describe.each([
|
|||
it("should be able to search by calculations", async () => {
|
||||
const view = await config.api.viewV2.create({
|
||||
tableId: table._id!,
|
||||
type: ViewV2Type.CALCULATION,
|
||||
name: generator.guid(),
|
||||
schema: {
|
||||
"Quantity Sum": {
|
||||
|
@ -2843,6 +2897,7 @@ describe.each([
|
|||
const view = await config.api.viewV2.create({
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
type: ViewV2Type.CALCULATION,
|
||||
schema: {
|
||||
quantity: {
|
||||
visible: true,
|
||||
|
@ -2881,6 +2936,7 @@ describe.each([
|
|||
const view = await config.api.viewV2.create({
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
type: ViewV2Type.CALCULATION,
|
||||
schema: {
|
||||
aggregate: {
|
||||
visible: true,
|
||||
|
@ -2941,6 +2997,7 @@ describe.each([
|
|||
const view = await config.api.viewV2.create({
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
type: ViewV2Type.CALCULATION,
|
||||
schema: {
|
||||
count: {
|
||||
visible: true,
|
||||
|
@ -2975,6 +3032,7 @@ describe.each([
|
|||
{
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
type: ViewV2Type.CALCULATION,
|
||||
schema: {
|
||||
count: {
|
||||
visible: true,
|
||||
|
@ -3023,6 +3081,7 @@ describe.each([
|
|||
const view = await config.api.viewV2.create({
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
type: ViewV2Type.CALCULATION,
|
||||
schema: {
|
||||
sum: {
|
||||
visible: true,
|
||||
|
|
|
@ -70,6 +70,9 @@ export async function update(tableId: string, view: ViewV2): Promise<ViewV2> {
|
|||
if (!existingView || !existingView.name) {
|
||||
throw new HTTPError(`View ${view.id} not found in table ${tableId}`, 404)
|
||||
}
|
||||
if (isV2(existingView) && existingView.type !== view.type) {
|
||||
throw new HTTPError(`Cannot update view type after creation`, 400)
|
||||
}
|
||||
|
||||
delete views[existingView.name]
|
||||
views[view.name] = view
|
||||
|
|
|
@ -131,6 +131,13 @@ async function guardViewSchema(
|
|||
|
||||
if (helpers.views.isCalculationView(view)) {
|
||||
await guardCalculationViewSchema(table, view)
|
||||
} else {
|
||||
if (helpers.views.hasCalculationFields(view)) {
|
||||
throw new HTTPError(
|
||||
"Calculation fields are not allowed in non-calculation views",
|
||||
400
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
await checkReadonlyFields(table, view)
|
||||
|
|
|
@ -59,6 +59,10 @@ export async function update(tableId: string, view: ViewV2): Promise<ViewV2> {
|
|||
throw new HTTPError(`View ${view.id} not found in table ${tableId}`, 404)
|
||||
}
|
||||
|
||||
if (isV2(existingView) && existingView.type !== view.type) {
|
||||
throw new HTTPError(`Cannot update view type after creation`, 400)
|
||||
}
|
||||
|
||||
delete table.views[existingView.name]
|
||||
table.views[view.name] = view
|
||||
await db.put(table)
|
||||
|
|
|
@ -3,6 +3,7 @@ import {
|
|||
ViewCalculationFieldMetadata,
|
||||
ViewFieldMetadata,
|
||||
ViewV2,
|
||||
ViewV2Type,
|
||||
} from "@budibase/types"
|
||||
import { pickBy } from "lodash"
|
||||
|
||||
|
@ -21,6 +22,10 @@ export function isBasicViewField(
|
|||
type UnsavedViewV2 = Omit<ViewV2, "id" | "version">
|
||||
|
||||
export function isCalculationView(view: UnsavedViewV2) {
|
||||
return view.type === ViewV2Type.CALCULATION
|
||||
}
|
||||
|
||||
export function hasCalculationFields(view: UnsavedViewV2) {
|
||||
return Object.values(view.schema || {}).some(isCalculationField)
|
||||
}
|
||||
|
||||
|
|
|
@ -79,10 +79,15 @@ export enum CalculationType {
|
|||
MAX = "max",
|
||||
}
|
||||
|
||||
export enum ViewV2Type {
|
||||
CALCULATION = "calculation",
|
||||
}
|
||||
|
||||
export interface ViewV2 {
|
||||
version: 2
|
||||
id: string
|
||||
name: string
|
||||
type?: ViewV2Type
|
||||
primaryDisplay?: string
|
||||
tableId: string
|
||||
query?: LegacyFilter[] | SearchFilters
|
||||
|
@ -94,7 +99,6 @@ export interface ViewV2 {
|
|||
type?: SortType
|
||||
}
|
||||
schema?: ViewV2Schema
|
||||
uiMetadata?: Record<string, any>
|
||||
}
|
||||
|
||||
export type ViewV2Schema = Record<string, ViewFieldMetadata>
|
||||
|
|
Loading…
Reference in New Issue