diff --git a/packages/builder/src/components/backend/TableNavigator/ExistingTableDataImport.svelte b/packages/builder/src/components/backend/TableNavigator/ExistingTableDataImport.svelte index 6901503071..3323a4122a 100644 --- a/packages/builder/src/components/backend/TableNavigator/ExistingTableDataImport.svelte +++ b/packages/builder/src/components/backend/TableNavigator/ExistingTableDataImport.svelte @@ -66,6 +66,10 @@ label: "Users", value: `${FieldType.BB_REFERENCE}${FieldSubtype.USERS}`, }, + { + label: "User", + value: `${FieldType.BB_REFERENCE_SINGLE}${FieldSubtype.USER}`, + }, ] $: { diff --git a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts index dcefd9c032..1a4cd831b1 100644 --- a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts +++ b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts @@ -176,7 +176,18 @@ export async function processOutputBBReferences( } case FieldType.BB_REFERENCE_SINGLE: - const user = await cache.user.getUser(value as string) + if (!value) { + return undefined + } + + let user + try { + user = await cache.user.getUser(value as string) + } catch (err: any) { + if (err.code !== 404) { + throw err + } + } if (!user) { return undefined } diff --git a/packages/server/src/utilities/schema.ts b/packages/server/src/utilities/schema.ts index 156ea5ea7b..ae77abd2e4 100644 --- a/packages/server/src/utilities/schema.ts +++ b/packages/server/src/utilities/schema.ts @@ -94,7 +94,7 @@ export function validate(rows: Rows, schema: TableSchema): ValidationResults { } else if ( (columnType === FieldType.BB_REFERENCE || columnType === FieldType.BB_REFERENCE_SINGLE) && - !isValidBBReference(columnData, columnSubtype) + !isValidBBReference(columnData, columnType, columnSubtype) ) { results.schemaValidation[columnName] = false } else { @@ -164,21 +164,31 @@ export function parse(rows: Rows, schema: TableSchema): Rows { } function isValidBBReference( - columnData: any, - columnSubtype: FieldSubtype.USER | FieldSubtype.USERS + data: any, + type: FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE, + subtype: FieldSubtype.USER | FieldSubtype.USERS ): boolean { - switch (columnSubtype) { + if (typeof data !== "string") { + return false + } + + if (type === FieldType.BB_REFERENCE_SINGLE) { + if (!data) { + return true + } + const user = parseCsvExport<{ _id: string }>(data) + return db.isGlobalUserID(user._id) + } + + switch (subtype) { case FieldSubtype.USER: case FieldSubtype.USERS: { - if (typeof columnData !== "string") { - return false - } - const userArray = parseCsvExport<{ _id: string }[]>(columnData) + const userArray = parseCsvExport<{ _id: string }[]>(data) if (!Array.isArray(userArray)) { return false } - if (columnSubtype === FieldSubtype.USER && userArray.length > 1) { + if (subtype === FieldSubtype.USER && userArray.length > 1) { return false } @@ -188,6 +198,6 @@ function isValidBBReference( return !constainsWrongId } default: - throw utils.unreachable(columnSubtype) + throw utils.unreachable(subtype) } }