From c7646afde008dc16569b6b0cc57721fb888b7aae Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 30 Oct 2024 14:23:58 +0000 Subject: [PATCH 1/9] Adding a change to nodemon configuration, this stops the services restarting twice when a build is triggered. --- packages/server/nodemon.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/nodemon.json b/packages/server/nodemon.json index 3b3d9aa46d..b0e792d945 100644 --- a/packages/server/nodemon.json +++ b/packages/server/nodemon.json @@ -8,6 +8,6 @@ "../string-templates" ], "ext": "js,ts,json,svelte", - "ignore": ["**/*.spec.ts", "**/*.spec.js", "../*/dist/**/*"], + "ignore": ["**/*.spec.ts", "**/*.spec.js", "../*/dist/**/*", "client/**/*", "builder/**/*"], "exec": "yarn build && node --no-node-snapshot ./dist/index.js" } From e235de54adfbe80ac136ef4af75d4417878dbe78 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 30 Oct 2024 17:32:39 +0000 Subject: [PATCH 2/9] Fixing an issue with serving the builder, increased error rate due to requests that hit the / endpoint, these were 500ing due to a lack of app ID. --- .../server/src/api/controllers/static/index.ts | 11 +++++++++++ .../server/src/api/routes/tests/static.spec.ts | 18 ++++++++++++++++++ .../src/sdk/app/applications/applications.ts | 8 +++++--- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/controllers/static/index.ts b/packages/server/src/api/controllers/static/index.ts index 1d04811019..b90d24c9b1 100644 --- a/packages/server/src/api/controllers/static/index.ts +++ b/packages/server/src/api/controllers/static/index.ts @@ -6,6 +6,7 @@ import { ObjectStoreBuckets } from "../../../constants" import { processString } from "@budibase/string-templates" import { loadHandlebarsFile, + streamFile, NODE_MODULES_PATH, shouldServeLocally, TOP_LEVEL_PATH, @@ -145,6 +146,16 @@ const requiresMigration = async (ctx: Ctx) => { } export const serveApp = async function (ctx: UserCtx) { + if (ctx.url.includes("apple-touch-icon")) { + ctx.redirect("/builder/bblogo.png") + return + } + // no app ID found, cannot serve - return message instead + if (!context.getAppId()) { + ctx.body = "No content found - requires app ID" + return + } + const needMigrations = await requiresMigration(ctx) const bbHeaderEmbed = diff --git a/packages/server/src/api/routes/tests/static.spec.ts b/packages/server/src/api/routes/tests/static.spec.ts index 62b72b2b8f..c2808603e9 100644 --- a/packages/server/src/api/routes/tests/static.spec.ts +++ b/packages/server/src/api/routes/tests/static.spec.ts @@ -152,4 +152,22 @@ describe("/static", () => { expect(res.body.builderPreview).toBe(true) }) }) + + describe("/", () => { + it("should move permanently from base call (public call)", async () => { + const res = await request.get(`/`) + expect(res.status).toEqual(301) + expect(res.text).toEqual( + `Redirecting to /builder.` + ) + }) + + it("should not error when trying to get 'apple-touch-icon.png' (public call)", async () => { + const res = await request.get(`/apple-touch-icon.png`) + expect(res.status).toEqual(302) + expect(res.text).toEqual( + `Redirecting to /builder/bblogo.png.` + ) + }) + }) }) diff --git a/packages/server/src/sdk/app/applications/applications.ts b/packages/server/src/sdk/app/applications/applications.ts index 07e303ccdd..1a705a211e 100644 --- a/packages/server/src/sdk/app/applications/applications.ts +++ b/packages/server/src/sdk/app/applications/applications.ts @@ -25,10 +25,12 @@ export async function fetch(status: AppStatus, user: ContextUser) { const all = status === AppStatus.ALL let apps = (await dbCore.getAllApps({ dev, all })) as App[] - const enrichedUser = await groups.enrichUserRolesFromGroups({ + // need to type this correctly - add roles back in to convert from ContextUser to User + const completeUser: User = { ...user, - roles: user.roles || {}, - }) + roles: user?.roles || {}, + } + const enrichedUser = await groups.enrichUserRolesFromGroups(completeUser) apps = filterAppList(enrichedUser, apps) const appIds = apps From b504f77d0f23eec97ab2ec54e4ea2e776a7af1d9 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 30 Oct 2024 17:32:49 +0000 Subject: [PATCH 3/9] Fix view calculations when relationships are present. --- packages/backend-core/src/sql/sql.ts | 10 +- .../api/controllers/row/ExternalRequest.ts | 23 ++-- .../src/api/routes/tests/viewV2.spec.ts | 103 ++++++++++++++++++ 3 files changed, 119 insertions(+), 17 deletions(-) diff --git a/packages/backend-core/src/sql/sql.ts b/packages/backend-core/src/sql/sql.ts index 91dceaf5a6..f62bb0f7e3 100644 --- a/packages/backend-core/src/sql/sql.ts +++ b/packages/backend-core/src/sql/sql.ts @@ -529,7 +529,7 @@ class InternalBuilder { if (!matchesTableName) { updatedKey = filterKey.replace( new RegExp(`^${relationship.column}.`), - `${aliases![relationship.tableName]}.` + `${aliases?.[relationship.tableName] || relationship.tableName}.` ) } else { updatedKey = filterKey @@ -1579,7 +1579,7 @@ class InternalBuilder { query = this.addFilters(query, filters, { relationship: true }) // handle relationships with a CTE for all others - if (relationships?.length) { + if (relationships?.length && aggregations.length === 0) { const mainTable = this.query.tableAliases?.[this.query.endpoint.entityId] || this.query.endpoint.entityId @@ -1594,10 +1594,8 @@ class InternalBuilder { // add JSON aggregations attached to the CTE return this.addJsonRelationships(cte, tableName, relationships) } - // no relationships found - return query - else { - return query - } + + return query } update(opts: QueryOptions): Knex.QueryBuilder { diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index 62aa4d8d2a..939ea399d9 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -682,7 +682,19 @@ export class ExternalRequest { filters = this.prepareFilters(id, filters || {}, table) const relationships = buildExternalRelationships(table, this.tables) + let aggregations: Aggregation[] = [] + if (sdk.views.isView(this.source)) { + const calculationFields = helpers.views.calculationFields(this.source) + for (const [key, field] of Object.entries(calculationFields)) { + aggregations.push({ + ...field, + name: key, + }) + } + } + const incRelationships = + aggregations.length === 0 && config.includeSqlRelationships === IncludeRelationship.INCLUDE // clean up row on ingress using schema @@ -709,17 +721,6 @@ export class ExternalRequest { throw "Deletion must be filtered" } - let aggregations: Aggregation[] = [] - if (sdk.views.isView(this.source)) { - const calculationFields = helpers.views.calculationFields(this.source) - for (const [key, field] of Object.entries(calculationFields)) { - aggregations.push({ - ...field, - name: key, - }) - } - } - let json: QueryJson = { endpoint: { datasourceId: this.datasource._id!, diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 15b9a29e11..7358cc1347 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -3659,6 +3659,109 @@ describe.each([ ) }) + it("should be able to filter on relationships", async () => { + const companies = await config.api.table.save( + saveTableRequest({ + schema: { + name: { + name: "name", + type: FieldType.STRING, + }, + }, + }) + ) + + const employees = await config.api.table.save( + saveTableRequest({ + schema: { + age: { + type: FieldType.NUMBER, + name: "age", + }, + name: { + type: FieldType.STRING, + name: "name", + }, + company: { + type: FieldType.LINK, + name: "company", + tableId: companies._id!, + relationshipType: RelationshipType.ONE_TO_MANY, + fieldName: "company", + }, + }, + }) + ) + + const view = await config.api.viewV2.create({ + tableId: employees._id!, + name: generator.guid(), + type: ViewV2Type.CALCULATION, + queryUI: { + groups: [ + { + filters: [ + { + operator: BasicOperator.EQUAL, + field: "company.name", + value: "Aperture Science Laboratories", + }, + ], + }, + ], + }, + schema: { + sum: { + visible: true, + calculationType: CalculationType.SUM, + field: "age", + }, + }, + }) + + const apertureScience = await config.api.row.save( + companies._id!, + { + name: "Aperture Science Laboratories", + } + ) + + const blackMesa = await config.api.row.save(companies._id!, { + name: "Black Mesa", + }) + + await config.api.row.save(employees._id!, { + name: "Alice", + age: 25, + company: apertureScience._id, + }) + + await config.api.row.save(employees._id!, { + name: "Bob", + age: 30, + company: apertureScience._id, + }) + + await config.api.row.save(employees._id!, { + name: "Charly", + age: 27, + company: blackMesa._id, + }) + + await config.api.row.save(employees._id!, { + name: "Danny", + age: 15, + company: blackMesa._id, + }) + + const { rows } = await config.api.viewV2.search(view.id, { + query: {}, + }) + + expect(rows).toHaveLength(1) + expect(rows[0].sum).toEqual(55) + }) + it("should be able to filter rows on the view itself", async () => { const table = await config.api.table.save( saveTableRequest({ From dc1389b8fbc1a7e281dc85db4a45b64d613cf8db Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 30 Oct 2024 17:39:48 +0000 Subject: [PATCH 4/9] PR comment and linting. --- packages/server/src/api/controllers/static/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/server/src/api/controllers/static/index.ts b/packages/server/src/api/controllers/static/index.ts index b90d24c9b1..daf7b9b25c 100644 --- a/packages/server/src/api/controllers/static/index.ts +++ b/packages/server/src/api/controllers/static/index.ts @@ -6,7 +6,6 @@ import { ObjectStoreBuckets } from "../../../constants" import { processString } from "@budibase/string-templates" import { loadHandlebarsFile, - streamFile, NODE_MODULES_PATH, shouldServeLocally, TOP_LEVEL_PATH, @@ -146,7 +145,7 @@ const requiresMigration = async (ctx: Ctx) => { } export const serveApp = async function (ctx: UserCtx) { - if (ctx.url.includes("apple-touch-icon")) { + if (ctx.url.includes("apple-touch-icon.png")) { ctx.redirect("/builder/bblogo.png") return } From e06c0629a0e8be425e51e09ac55451325633f34a Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 30 Oct 2024 17:40:15 +0000 Subject: [PATCH 5/9] Respond to PR comments. --- .../src/api/routes/tests/viewV2.spec.ts | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 7358cc1347..1f2470f097 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -3730,29 +3730,28 @@ describe.each([ name: "Black Mesa", }) - await config.api.row.save(employees._id!, { - name: "Alice", - age: 25, - company: apertureScience._id, - }) - - await config.api.row.save(employees._id!, { - name: "Bob", - age: 30, - company: apertureScience._id, - }) - - await config.api.row.save(employees._id!, { - name: "Charly", - age: 27, - company: blackMesa._id, - }) - - await config.api.row.save(employees._id!, { - name: "Danny", - age: 15, - company: blackMesa._id, - }) + await Promise.all([ + config.api.row.save(employees._id!, { + name: "Alice", + age: 25, + company: apertureScience._id, + }), + config.api.row.save(employees._id!, { + name: "Bob", + age: 30, + company: apertureScience._id, + }), + config.api.row.save(employees._id!, { + name: "Charly", + age: 27, + company: blackMesa._id, + }), + config.api.row.save(employees._id!, { + name: "Danny", + age: 15, + company: blackMesa._id, + }), + ]) const { rows } = await config.api.viewV2.search(view.id, { query: {}, From 41807e87b305019eb6380690160c08ee25d3b940 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Thu, 31 Oct 2024 08:44:36 +0000 Subject: [PATCH 6/9] Don't throw if no account-holder is found --- .../src/api/controllers/global/users.ts | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index 52f8821fab..fa19948bf5 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -312,16 +312,21 @@ export const tenantUserLookup = async (ctx: any) => { * So the account holder may not be found until further pagination has occurred */ export const accountHolderLookup = async (ctx: Ctx) => { - const users = await userSdk.core.getAllUsers() - const response = await userSdk.core.getExistingAccounts( - users.map(u => u.email) - ) - const holder = response[0] - if (!holder) { - return + try { + const users = await userSdk.core.getAllUsers() + const response = await userSdk.core.getExistingAccounts( + users.map(u => u.email) + ) + const holder = response[0] + if (!holder) { + ctx.body = null + return + } + holder._id = users.find(u => u.email === holder.email)?._id + ctx.body = holder + } catch (e) { + ctx.body = null } - holder._id = users.find(u => u.email === holder.email)?._id - ctx.body = holder } /* From eba7f015782ef8f02f26cdc082d37232f66320a0 Mon Sep 17 00:00:00 2001 From: melohagan <101575380+melohagan@users.noreply.github.com> Date: Thu, 31 Oct 2024 08:51:46 +0000 Subject: [PATCH 7/9] Improve users sort (#14894) --- .../builder/src/pages/builder/portal/users/users/index.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/builder/src/pages/builder/portal/users/users/index.svelte b/packages/builder/src/pages/builder/portal/users/users/index.svelte index 9ecb42b6d3..9c4e8f70c7 100644 --- a/packages/builder/src/pages/builder/portal/users/users/index.svelte +++ b/packages/builder/src/pages/builder/portal/users/users/index.svelte @@ -406,7 +406,7 @@ allowSelectRows={!readonly} {customRenderers} loading={!$fetch.loaded || !groupsLoaded} - defaultSortColumn={"__selectable"} + defaultSortColumn={"access"} />