import type { WorkosConnectionSummary, WorkosDirectorySummary, WorkosDirectoryUserSummary, WorkosEventSummary, WorkosOrganizationSummary, WorkosUserSummary, } from './types.ts' import { booleanField, stringField } from './types.ts' function stringList(value: unknown, key: 'domain' | 'object' = 'domain'): Array { if (!Array.isArray(value)) return [] return value .map((item) => { if (typeof item === 'string') return item if (item && typeof item === 'object') { const record = item as Record return typeof record[key] === 'string' ? record[key] : null } return null }) .filter((item): item is string => Boolean(item)) } export function mapOrganization(value: unknown): WorkosOrganizationSummary { const record = value && typeof value === 'object' ? (value as Record) : {} return { id: stringField(record.id) ?? '', name: stringField(record.name), domains: stringList(record.domains), externalId: stringField(record.external_id), createdAt: stringField(record.created_at), updatedAt: stringField(record.updated_at), } } export function mapUser(value: unknown): WorkosUserSummary { const record = value && typeof value === 'object' ? (value as Record) : {} return { id: stringField(record.id) ?? '', email: stringField(record.email), firstName: stringField(record.first_name), lastName: stringField(record.last_name), emailVerified: booleanField(record.email_verified), externalId: stringField(record.external_id), createdAt: stringField(record.created_at), updatedAt: stringField(record.updated_at), } } export function mapConnection(value: unknown): WorkosConnectionSummary { const record = value && typeof value === 'object' ? (value as Record) : {} return { id: stringField(record.id) ?? '', name: stringField(record.name), connectionType: stringField(record.connection_type) ?? stringField(record.type), state: stringField(record.state), organizationId: stringField(record.organization_id), domains: stringList(record.domains), createdAt: stringField(record.created_at), updatedAt: stringField(record.updated_at), } } export function mapDirectory(value: unknown): WorkosDirectorySummary { const record = value && typeof value === 'object' ? (value as Record) : {} return { id: stringField(record.id) ?? '', name: stringField(record.name), type: stringField(record.type), state: stringField(record.state), organizationId: stringField(record.organization_id), domain: stringField(record.domain), createdAt: stringField(record.created_at), updatedAt: stringField(record.updated_at), } } export function mapDirectoryUser(value: unknown): WorkosDirectoryUserSummary { const record = value && typeof value === 'object' ? (value as Record) : {} const emails = Array.isArray(record.emails) ? record.emails : [] const primaryEmail = emails.find((item) => { if (!item || typeof item !== 'object') return false return (item as { primary?: unknown }).primary === true }) const firstEmail = emails[0] const emailFromList = primaryEmail && typeof primaryEmail === 'object' ? stringField((primaryEmail as { value?: unknown }).value) : firstEmail && typeof firstEmail === 'object' ? stringField((firstEmail as { value?: unknown }).value) : typeof firstEmail === 'string' ? firstEmail : null return { id: stringField(record.id) ?? '', idpId: stringField(record.idp_id), directoryId: stringField(record.directory_id), organizationId: stringField(record.organization_id), firstName: stringField(record.first_name), lastName: stringField(record.last_name), email: stringField(record.email) ?? emailFromList, state: stringField(record.state), createdAt: stringField(record.created_at), updatedAt: stringField(record.updated_at), } } export function mapEvent(value: unknown): WorkosEventSummary { const record = value && typeof value === 'object' ? (value as Record) : {} return { id: stringField(record.id) ?? '', event: stringField(record.event), createdAt: stringField(record.created_at), } }