export const DEFAULT_RELATION_FIELDS = ['id', 'code', 'name'] as const; export const USER_RELATION_FIELDS = ['id', 'username'] as const; export const CODE_RELATION_FIELDS = ['id', 'code'] as const; export type DefaultRelation = { readonly id: string; readonly code: string; readonly name: string; }; export type UserRelation = { readonly id: string; readonly username: string; }; export type CodeRelation = { readonly id: string; readonly code: string; }; export function pickRelation>( source: T | null | undefined, fields: readonly K[], ): Pick, K> | null { if (source == null) { return null; } const result = {} as Pick, K>; for (const field of fields) { result[field] = source[field]; } return result; } export function pickDefaultRelation( source: DefaultRelation | null | undefined, ): DefaultRelation | null { return pickRelation(source, DEFAULT_RELATION_FIELDS); } export function pickUserRelation(source: UserRelation): UserRelation { return ( pickRelation(source, USER_RELATION_FIELDS) ?? { id: source.id, username: source.username, } ); } export function pickCodeRelation( source: CodeRelation | null | undefined, ): CodeRelation | null { return pickRelation(source, CODE_RELATION_FIELDS); } export function fallbackUserRelation( source: UserRelation | null | undefined, fallbackId: string, ): UserRelation { return pickUserRelation(source ?? { id: fallbackId, username: '' }); }