diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index f403d14180..804de14ea8 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -92,7 +92,19 @@ export async function update(ctx: Ctx) { const { tableId } = view - const result = await sdk.views.update(tableId, view) + const schemaUI = await parseSchemaUI(ctx, view) + const parsedView: ViewV2 = { + id: view.id, + name: view.name, + version: view.version, + tableId: view.tableId, + query: view.query, + sort: view.sort, + columns: view.schema && Object.keys(view.schema), + schemaUI, + } + + const result = await sdk.views.update(tableId, parsedView) ctx.body = { data: result, } diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index e60e9a5126..b665b261e6 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -298,6 +298,94 @@ describe("/v2/views", () => { status: 400, }) }) + + it("updates only UI schema overrides", async () => { + await config.api.viewV2.update({ + ...view, + schema: { + Price: { + name: "Price", + type: FieldType.NUMBER, + visible: true, + order: 1, + width: 100, + }, + Category: { + name: "Category", + type: FieldType.STRING, + visible: false, + icon: "ic", + }, + }, + }) + + expect(await config.api.viewV2.get(view.id)).toEqual({ + ...view, + schema: undefined, + columns: ["Price", "Category"], + schemaUI: { + Price: { + visible: true, + order: 1, + width: 100, + }, + Category: { + visible: false, + icon: "ic", + }, + }, + id: view.id, + version: 2, + }) + }) + + it("throw an exception if the schema overrides a non UI field", async () => { + await config.api.viewV2.update( + { + ...view, + schema: { + Price: { + name: "Price", + type: FieldType.NUMBER, + visible: true, + }, + Category: { + name: "Category", + type: FieldType.STRING, + constraints: { + type: "string", + presence: true, + }, + }, + }, + }, + { + expectStatus: 400, + } + ) + }) + + it("will not throw an exception if the schema is 'deleting' non UI fields", async () => { + await config.api.viewV2.update( + { + ...view, + schema: { + Price: { + name: "Price", + type: FieldType.NUMBER, + visible: true, + }, + Category: { + name: "Category", + type: FieldType.STRING, + }, + }, + }, + { + expectStatus: 200, + } + ) + }) }) describe("delete", () => { diff --git a/packages/server/src/tests/utilities/api/viewV2.ts b/packages/server/src/tests/utilities/api/viewV2.ts index 1f7fe6e2bb..b404904a28 100644 --- a/packages/server/src/tests/utilities/api/viewV2.ts +++ b/packages/server/src/tests/utilities/api/viewV2.ts @@ -1,4 +1,10 @@ -import { CreateViewRequest, SortOrder, SortType, ViewV2 } from "@budibase/types" +import { + CreateViewRequest, + SortOrder, + SortType, + UpdateViewRequest, + ViewV2, +} from "@budibase/types" import TestConfiguration from "../TestConfiguration" import { TestAPI } from "./base" import { generator } from "@budibase/backend-core/tests" @@ -34,7 +40,7 @@ export class ViewV2API extends TestAPI { } update = async ( - view: ViewV2, + view: UpdateViewRequest, { expectStatus, handleResponse, diff --git a/packages/types/src/api/web/app/view.ts b/packages/types/src/api/web/app/view.ts index b694521678..411bbef44e 100644 --- a/packages/types/src/api/web/app/view.ts +++ b/packages/types/src/api/web/app/view.ts @@ -9,4 +9,7 @@ export interface CreateViewRequest schema?: Record } -export type UpdateViewRequest = ViewV2 +export interface UpdateViewRequest + extends Omit { + schema?: Record +}