From ac0ae34808af74294140ce72486bcbcc28394eba Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 19 Jul 2023 16:04:53 +0200 Subject: [PATCH] Fix search --- .../server/src/api/controllers/row/index.ts | 8 ++--- packages/server/src/api/routes/row.ts | 2 +- .../server/src/api/routes/tests/row.spec.ts | 34 +++++++++++++------ packages/server/src/sdk/app/views/index.ts | 14 +++++++- .../server/src/tests/utilities/api/viewV2.ts | 8 +++-- 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/packages/server/src/api/controllers/row/index.ts b/packages/server/src/api/controllers/row/index.ts index 3169e4f77c..8a9947e707 100644 --- a/packages/server/src/api/controllers/row/index.ts +++ b/packages/server/src/api/controllers/row/index.ts @@ -147,12 +147,12 @@ export async function search(ctx: any) { } export async function searchView(ctx: Ctx) { - const { viewId } = ctx.params - const view = await sdk.views.get(viewId) + const { tableId, viewId } = ctx.params + + const view = await sdk.views.get(tableId, viewId) if (!view) { - ctx.throw(404) + ctx.throw(404, `View ${viewId} not found in table ${tableId}`) } - const tableId = view.tableId ctx.status = 200 ctx.body = await quotas.addQuery( diff --git a/packages/server/src/api/routes/row.ts b/packages/server/src/api/routes/row.ts index 5fdc02b7a7..1f71d3c1ae 100644 --- a/packages/server/src/api/routes/row.ts +++ b/packages/server/src/api/routes/row.ts @@ -147,7 +147,7 @@ router rowController.search ) .get( - "/api/v2/views/:viewId/search", + "/api/v2/views/:tableId/:viewId/search", authorized(PermissionType.VIEW, PermissionLevel.READ), rowController.searchView ) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 95422a81e5..b082bacea0 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -716,8 +716,10 @@ describe("/rows", () => { } const createViewResponse = await config.api.viewV2.create() - - const response = await config.api.viewV2.search(createViewResponse._id!) + const response = await config.api.viewV2.search( + createViewResponse.tableId, + createViewResponse.id + ) expect(response.body.rows).toHaveLength(10) expect(response.body).toEqual({ @@ -744,11 +746,17 @@ describe("/rows", () => { }) ) - const createViewResponse = await config.api.viewV2.create({ - query: { equal: { age: 40 } }, - }) + const createViewResponse = await config.api.viewV2.create( + config.table?._id!, + { + query: { equal: { age: 40 } }, + } + ) - const response = await config.api.viewV2.search(createViewResponse._id!) + const response = await config.api.viewV2.search( + createViewResponse.tableId, + createViewResponse.id + ) expect(response.body.rows).toHaveLength(5) expect(response.body).toEqual({ @@ -831,11 +839,17 @@ describe("/rows", () => { }) } - const createViewResponse = await config.api.viewV2.create({ - sort: sortParams, - }) + const createViewResponse = await config.api.viewV2.create( + config.table?._id!, + { + sort: sortParams, + } + ) - const response = await config.api.viewV2.search(createViewResponse._id!) + const response = await config.api.viewV2.search( + createViewResponse.tableId, + createViewResponse.id + ) expect(response.body.rows).toHaveLength(4) expect(response.body).toEqual({ diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index f8798d4947..004fe7e8d9 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -4,6 +4,17 @@ import { ViewV2 } from "@budibase/types" import sdk from "../../../sdk" import { utils as coreUtils } from "@budibase/backend-core" +export async function get( + tableId: string, + viewId: string +): Promise { + const table = await sdk.tables.getTable(tableId) + const view = Object.values(table.views!).find(v => isV2(v) && v.id === viewId) + + // @ts-ignore TODO + return view +} + export async function create( tableId: string, viewRequest: Omit @@ -32,10 +43,11 @@ export async function remove(tableId: string, viewId: string): Promise { const db = context.getAppDB() const table = await sdk.tables.getTable(tableId) - const view = Object.values(table.views!).find(v => isV2(v) && v.id === viewId) + const view = await get(tableId, viewId) if (!view) { throw new HTTPError(`View ${viewId} not found in table ${tableId}`, 404) } + delete table.views![view?.name] await db.put(table) } diff --git a/packages/server/src/tests/utilities/api/viewV2.ts b/packages/server/src/tests/utilities/api/viewV2.ts index e40bfe7030..950b149cea 100644 --- a/packages/server/src/tests/utilities/api/viewV2.ts +++ b/packages/server/src/tests/utilities/api/viewV2.ts @@ -42,9 +42,13 @@ export class ViewV2API extends TestAPI { .expect(expectStatus) } - search = async (viewId: string, { expectStatus } = { expectStatus: 200 }) => { + search = async ( + tableId: string, + viewId: string, + { expectStatus } = { expectStatus: 200 } + ) => { return this.request - .get(`/api/v2/views/${viewId}/search`) + .get(`/api/v2/views/${tableId}/${viewId}/search`) .set(this.config.defaultHeaders()) .expect("Content-Type", /json/) .expect(expectStatus)