Fix import validation

This commit is contained in:
Adria Navarro 2024-04-25 15:50:28 +02:00
parent 2edf3392a1
commit 6e4baf7215
3 changed files with 36 additions and 11 deletions

View File

@ -66,6 +66,10 @@
label: "Users", label: "Users",
value: `${FieldType.BB_REFERENCE}${FieldSubtype.USERS}`, value: `${FieldType.BB_REFERENCE}${FieldSubtype.USERS}`,
}, },
{
label: "User",
value: `${FieldType.BB_REFERENCE_SINGLE}${FieldSubtype.USER}`,
},
] ]
$: { $: {

View File

@ -176,7 +176,18 @@ export async function processOutputBBReferences(
} }
case FieldType.BB_REFERENCE_SINGLE: 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) { if (!user) {
return undefined return undefined
} }

View File

@ -94,7 +94,7 @@ export function validate(rows: Rows, schema: TableSchema): ValidationResults {
} else if ( } else if (
(columnType === FieldType.BB_REFERENCE || (columnType === FieldType.BB_REFERENCE ||
columnType === FieldType.BB_REFERENCE_SINGLE) && columnType === FieldType.BB_REFERENCE_SINGLE) &&
!isValidBBReference(columnData, columnSubtype) !isValidBBReference(columnData, columnType, columnSubtype)
) { ) {
results.schemaValidation[columnName] = false results.schemaValidation[columnName] = false
} else { } else {
@ -164,21 +164,31 @@ export function parse(rows: Rows, schema: TableSchema): Rows {
} }
function isValidBBReference( function isValidBBReference(
columnData: any, data: any,
columnSubtype: FieldSubtype.USER | FieldSubtype.USERS type: FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE,
subtype: FieldSubtype.USER | FieldSubtype.USERS
): boolean { ): 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.USER:
case FieldSubtype.USERS: { case FieldSubtype.USERS: {
if (typeof columnData !== "string") { const userArray = parseCsvExport<{ _id: string }[]>(data)
return false
}
const userArray = parseCsvExport<{ _id: string }[]>(columnData)
if (!Array.isArray(userArray)) { if (!Array.isArray(userArray)) {
return false return false
} }
if (columnSubtype === FieldSubtype.USER && userArray.length > 1) { if (subtype === FieldSubtype.USER && userArray.length > 1) {
return false return false
} }
@ -188,6 +198,6 @@ function isValidBBReference(
return !constainsWrongId return !constainsWrongId
} }
default: default:
throw utils.unreachable(columnSubtype) throw utils.unreachable(subtype)
} }
} }