This commit is contained in:
Adria Navarro 2024-04-22 20:58:35 +02:00
parent b56c86e1b0
commit 53badf9263
1 changed files with 18 additions and 13 deletions

View File

@ -10,10 +10,11 @@ import { InvalidBBRefError } from "./errors"
const ROW_PREFIX = DocumentType.ROW + SEPARATOR const ROW_PREFIX = DocumentType.ROW + SEPARATOR
export async function processInputBBReferences< export function processInputBBReferences<T = FieldType.BB_REFERENCE_SINGLE>(
T = FieldType.BB_REFERENCE_SINGLE value: string,
>(value: string, type: T): Promise<string | null> type: T
export async function processInputBBReferences< ): Promise<string | null>
export function processInputBBReferences<
T = FieldType.BB_REFERENCE, T = FieldType.BB_REFERENCE,
TS = FieldSubtype.USER TS = FieldSubtype.USER
>( >(
@ -21,7 +22,7 @@ export async function processInputBBReferences<
type: T, type: T,
subtype: TS subtype: TS
): Promise<string | null> ): Promise<string | null>
export async function processInputBBReferences< export function processInputBBReferences<
T = FieldType.BB_REFERENCE, T = FieldType.BB_REFERENCE,
TS = FieldSubtype.USERS TS = FieldSubtype.USERS
>( >(
@ -74,7 +75,7 @@ export async function processInputBBReferences<
case undefined: case undefined:
throw "Subtype must be defined" throw "Subtype must be defined"
case FieldSubtype.USER: case FieldSubtype.USER:
case FieldSubtype.USERS: case FieldSubtype.USERS: {
const { notFoundIds } = await cache.user.getUsers(referenceIds) const { notFoundIds } = await cache.user.getUsers(referenceIds)
if (notFoundIds?.length) { if (notFoundIds?.length) {
@ -86,11 +87,12 @@ export async function processInputBBReferences<
} }
return referenceIds.join(",") || null return referenceIds.join(",") || null
}
default: default:
throw utils.unreachable(subtype) throw utils.unreachable(subtype)
} }
case FieldType.BB_REFERENCE_SINGLE: case FieldType.BB_REFERENCE_SINGLE: {
const user = await cache.user.getUser(referenceIds[0]) const user = await cache.user.getUser(referenceIds[0])
if (!user) { if (!user) {
@ -98,6 +100,7 @@ export async function processInputBBReferences<
} }
return referenceIds[0] || null return referenceIds[0] || null
}
default: default:
throw utils.unreachable(type) throw utils.unreachable(type)
@ -112,14 +115,15 @@ interface UserReferenceInfo {
lastName: string lastName: string
} }
export async function processOutputBBReferences< export function processOutputBBReferences<T = FieldType.BB_REFERENCE_SINGLE>(
T = FieldType.BB_REFERENCE_SINGLE value: string,
>(value: string, type: T): Promise<UserReferenceInfo> type: T
export async function processOutputBBReferences< ): Promise<UserReferenceInfo>
export function processOutputBBReferences<
T = FieldType.BB_REFERENCE, T = FieldType.BB_REFERENCE,
TS = FieldSubtype.USER TS = FieldSubtype.USER
>(value: string, type: T, subtype: TS): Promise<UserReferenceInfo[]> >(value: string, type: T, subtype: TS): Promise<UserReferenceInfo[]>
export async function processOutputBBReferences< export function processOutputBBReferences<
T = FieldType.BB_REFERENCE, T = FieldType.BB_REFERENCE,
TS = FieldSubtype.USERS TS = FieldSubtype.USERS
>(value: string[], type: T, subtype: TS): Promise<UserReferenceInfo[]> >(value: string[], type: T, subtype: TS): Promise<UserReferenceInfo[]>
@ -143,7 +147,7 @@ export async function processOutputBBReferences(
case undefined: case undefined:
throw "Subtype must be defined" throw "Subtype must be defined"
case FieldSubtype.USER: case FieldSubtype.USER:
case FieldSubtype.USERS: case FieldSubtype.USERS: {
const { users } = await cache.user.getUsers(ids) const { users } = await cache.user.getUsers(ids)
if (!users.length) { if (!users.length) {
return undefined return undefined
@ -156,6 +160,7 @@ export async function processOutputBBReferences(
firstName: u.firstName, firstName: u.firstName,
lastName: u.lastName, lastName: u.lastName,
})) }))
}
default: default:
throw utils.unreachable(subtype) throw utils.unreachable(subtype)
} }