import { initializeAllContainers, registerContainers } from '@bytelyst/cosmos'; import type { ContainerConfig } from '@bytelyst/cosmos'; import { config } from './config.js'; const CONTAINER_DEFS: Record = { products: { partitionKeyPath: '/id' }, users: { partitionKeyPath: '/id' }, settings: { partitionKeyPath: '/userId' }, devices: { partitionKeyPath: '/userId' }, notification_prefs: { partitionKeyPath: '/userId' }, audit_log: { partitionKeyPath: '/category', defaultTtl: 90 * 86400 }, feature_flags: { partitionKeyPath: '/id' }, // Growth modules invitation_codes: { partitionKeyPath: '/id' }, referrals: { partitionKeyPath: '/id' }, // Billing modules subscriptions: { partitionKeyPath: '/userId' }, payments: { partitionKeyPath: '/userId' }, licenses: { partitionKeyPath: '/id' }, plans: { partitionKeyPath: '/id' }, usage_daily: { partitionKeyPath: '/userId' }, // API tokens api_tokens: { partitionKeyPath: '/id' }, // Tracker modules tracker_items: { partitionKeyPath: '/id' }, comments: { partitionKeyPath: '/itemId' }, votes: { partitionKeyPath: '/itemId' }, // Themes themes: { partitionKeyPath: '/id' }, // Waitlist (pre-launch signups) waitlist: { partitionKeyPath: '/email' }, // Mobile capture primitives (MindLyst-style). memory_items: { partitionKeyPath: '/userId' }, daily_briefs: { partitionKeyPath: '/userId' }, reflections: { partitionKeyPath: '/userId' }, brain_insights: { partitionKeyPath: '/userId' }, // NomGap fasting modules fasting_sessions: { partitionKeyPath: '/userId' }, fasting_protocols: { partitionKeyPath: '/userId' }, social_fasts: { partitionKeyPath: '/id' }, meal_logs: { partitionKeyPath: '/userId' }, // ChronoMind timers timers: { partitionKeyPath: '/userId' }, routines: { partitionKeyPath: '/userId' }, households: { partitionKeyPath: '/id' }, shared_timers: { partitionKeyPath: '/householdId' }, // ChronoMind webhooks webhook_subscriptions: { partitionKeyPath: '/userId' }, webhook_events: { partitionKeyPath: '/subscriptionId', defaultTtl: 30 * 86400 }, // Sessions (refresh token rotation + device tracking) sessions: { partitionKeyPath: '/userId', defaultTtl: 30 * 86400 }, // Email/push delivery log delivery_log: { partitionKeyPath: '/pk', defaultTtl: 90 * 86400 }, // Status page incidents incidents: { partitionKeyPath: '/productId' }, // Password reset + email verification password_reset_tokens: { partitionKeyPath: '/productId', defaultTtl: 86400 }, email_verifications: { partitionKeyPath: '/productId', defaultTtl: 7 * 86400 }, // IP allow/deny rules ip_rules: { partitionKeyPath: '/productId' }, // Data exports export_jobs: { partitionKeyPath: '/productId', defaultTtl: 30 * 86400 }, // Maintenance windows maintenance_windows: { partitionKeyPath: '/productId' }, // Scheduled jobs job_definitions: { partitionKeyPath: '/productId' }, job_runs: { partitionKeyPath: '/productId:jobName' }, // Telemetry (client diagnostics — see docs/WINDSURF/CLIENT_TELEMETRY_DESIGN.md) telemetry_events: { partitionKeyPath: '/pk', defaultTtl: 30 * 86400 }, telemetry_error_clusters: { partitionKeyPath: '/pk', defaultTtl: 90 * 86400 }, telemetry_collection_policies: { partitionKeyPath: '/productId' }, }; export async function initCosmosIfNeeded(): Promise { registerContainers(CONTAINER_DEFS); const shouldInit = config.NODE_ENV !== 'production' || process.env.COSMOS_AUTO_INIT === 'true'; if (!shouldInit) return; try { await initializeAllContainers(); // eslint-disable-next-line no-console console.info('[platform-service] Cosmos containers ensured'); } catch (err) { const msg = err instanceof Error ? err.message : String(err); // eslint-disable-next-line no-console console.warn(`[platform-service] Cosmos init failed: ${msg}`); } }