1
0
Fork 0
mirror of synced 2024-09-17 09:49:11 +12:00

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",
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:
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
}

View file

@ -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)
}
}