From c87a67a37a4c5c2a95d319ed2ff1b0becb5cf4bd Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 9 Oct 2023 18:18:34 +0100 Subject: [PATCH] Converting user IDs where necessary to global user IDs in the frontend, correcting the null entry when using current user IDs, or other user IDs, as a default value for a user column form type. --- packages/backend-core/src/constants/db.ts | 11 ++++++----- .../app/forms/BBReferenceField.svelte | 8 ++++++++ .../app/forms/RelationshipField.svelte | 9 +++++++-- .../shared-core/src/sdk/documents/users.ts | 19 ++++++++++++++++++- packages/types/src/documents/document.ts | 4 ++++ 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/packages/backend-core/src/constants/db.ts b/packages/backend-core/src/constants/db.ts index f918dcc352..b33b4835a9 100644 --- a/packages/backend-core/src/constants/db.ts +++ b/packages/backend-core/src/constants/db.ts @@ -1,5 +1,10 @@ import { prefixed, DocumentType } from "@budibase/types" -export { SEPARATOR, UNICODE_MAX, DocumentType } from "@budibase/types" +export { + SEPARATOR, + UNICODE_MAX, + DocumentType, + InternalTable, +} from "@budibase/types" /** * Can be used to create a few different forms of querying a view. @@ -30,10 +35,6 @@ export const DeprecatedViews = { ], } -export enum InternalTable { - USER_METADATA = "ta_users", -} - export const StaticDatabases = { GLOBAL: { name: "global-db", diff --git a/packages/client/src/components/app/forms/BBReferenceField.svelte b/packages/client/src/components/app/forms/BBReferenceField.svelte index 7d11ff5525..4573452973 100644 --- a/packages/client/src/components/app/forms/BBReferenceField.svelte +++ b/packages/client/src/components/app/forms/BBReferenceField.svelte @@ -1,9 +1,17 @@ { + if (Array.isArray(value)) { + return value.map(val => sdk.users.getGlobalUserID(val)) + } else { + return sdk.users.getGlobalUserID(value) + } + }} /> diff --git a/packages/client/src/components/app/forms/RelationshipField.svelte b/packages/client/src/components/app/forms/RelationshipField.svelte index bb003730b3..c114a51f87 100644 --- a/packages/client/src/components/app/forms/RelationshipField.svelte +++ b/packages/client/src/components/app/forms/RelationshipField.svelte @@ -18,6 +18,7 @@ export let filter export let datasourceType = "table" export let primaryDisplay + export let valueConversion let fieldState let fieldApi @@ -160,7 +161,9 @@ const handleChange = value => { const changed = fieldApi.setValue(value) if (onChange && changed) { - onChange({ value }) + onChange({ + value: valueConversion ? valueConversion(value) : value, + }) } } @@ -176,7 +179,9 @@ {field} {disabled} {validation} - defaultValue={expandedDefaultValue} + defaultValue={valueConversion + ? valueConversion(expandedDefaultValue) + : expandedDefaultValue} {type} bind:fieldState bind:fieldApi diff --git a/packages/shared-core/src/sdk/documents/users.ts b/packages/shared-core/src/sdk/documents/users.ts index 1be8845656..cb171cfd5b 100644 --- a/packages/shared-core/src/sdk/documents/users.ts +++ b/packages/shared-core/src/sdk/documents/users.ts @@ -1,4 +1,10 @@ -import { ContextUser, User } from "@budibase/types" +import { + ContextUser, + DocumentType, + SEPARATOR, + User, + InternalTable, +} from "@budibase/types" import { getProdAppID } from "./applications" // checks if a user is specifically a builder, given an app ID @@ -67,3 +73,14 @@ export function hasAdminPermissions(user?: User | ContextUser): boolean { } return !!user.admin?.global } + +export function getGlobalUserID(userId?: string): string | undefined { + if (typeof userId !== "string") { + return userId + } + const prefix = `${DocumentType.ROW}${SEPARATOR}${InternalTable.USER_METADATA}${SEPARATOR}` + if (!userId.startsWith(prefix)) { + return userId + } + return userId.split(prefix)[1] +} diff --git a/packages/types/src/documents/document.ts b/packages/types/src/documents/document.ts index 763da62d61..fb9589b24d 100644 --- a/packages/types/src/documents/document.ts +++ b/packages/types/src/documents/document.ts @@ -58,6 +58,10 @@ export const DocumentTypesToImport: DocumentType[] = [ DocumentType.LAYOUT, ] +export enum InternalTable { + USER_METADATA = "ta_users", +} + // these documents don't really exist, they are part of other // documents or enriched into existence as part of get requests export enum VirtualDocumentType {