/** * Integration Key Response Mapper * * Transforms API response schemas to domain IntegrationKey objects. * * @layer Infrastructure - API Client */ import { integrationKeyResponseSchema, integrationKeyListResponseSchema, } from '@archer/api-interface'; import type { IntegrationKeyResponse, IntegrationKeyListResponse, } from '@archer/api-interface'; import { IntegrationKey, KeyEnvironment, IntegrationKeyStatus } from '@/domain/entities/IntegrationKey'; import type { PaginationMeta } from '@/infrastructure/http/api/shared/types'; export class IntegrationKeyResponseMapper { /** * Transforms IntegrationKeyResponse schema to IntegrationKey domain object */ static toIntegrationKey(response: IntegrationKeyResponse): IntegrationKey { const validated = integrationKeyResponseSchema.parse(response); const tenantId = validated.tenant.id; return IntegrationKey.fromPersistence({ id: validated.id, tenantId, key: validated.key, keyPrefix: validated.keyPrefix, environment: validated.environment as KeyEnvironment, status: validated.status as IntegrationKeyStatus, usageCount: validated.usageCount, expiresAt: validated.expiresAt ?? undefined, lastUsedAt: validated.lastUsedAt ?? undefined, createdAt: validated.createdAt, updatedAt: validated.updatedAt, }); } /** * Transforms IntegrationKeyListResponse to paginated domain objects */ static toKeyList(response: IntegrationKeyListResponse): { keys: IntegrationKey[]; pagination: PaginationMeta; } { const validated = integrationKeyListResponseSchema.parse(response); const keys = validated.rows.map((keyResponse) => { return this.toIntegrationKey(keyResponse); }); const pagination: PaginationMeta = { page: validated.page, limit: validated.limit, total: validated.totalRows, totalPages: validated.totalPages, hasNext: validated.page < validated.totalPages, hasPrevious: validated.page > 1, }; return { keys, pagination }; } }