learning_ai_common_plat/services/platform-service/src/lib/cosmos-init.ts
saravanakumardb1 24d7896599 refactor(platform-service): migrate product-specific modules to product repo backends
Remove 23 product-specific module directories from platform-service:
- ChronoMind: timers, routines, households, shared-timers, webhooks
- JarvisJr: jarvis-agents, jarvis-sessions, jarvis-memory, jarvis-teams, marketplace
- NomGap: fasting-sessions, fasting-protocols, body-stages, social-fasting, meal-log, push-triggers
- PeakPulse: peak-sessions, peak-routes
- MindLyst: brains, memory, reflections, daily-briefs, streaks

Update server.ts: remove product module imports and registrations
Update cosmos-init.ts: remove product-specific container definitions
Clean up server.test.ts: remove 5 stale vi.mock() calls
Update AGENTS.md: add section 13 (product backends), update test counts

Platform-service tests: 759 passing (platform-common only)
Product backends: PeakPulse 32, ChronoMind 171, JarvisJr 198, NomGap 152, MindLyst 59
2026-03-01 20:38:32 -08:00

77 lines
3.3 KiB
TypeScript

import { initializeAllContainers, registerContainers } from '@bytelyst/cosmos';
import type { ContainerConfig } from '@bytelyst/cosmos';
import { config } from './config.js';
const CONTAINER_DEFS: Record<string, ContainerConfig> = {
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' },
// 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' },
// P2 — Product Intelligence
experiments: { partitionKeyPath: '/id' },
experiment_assignments: { partitionKeyPath: '/experimentId' },
analytics_rollups: { partitionKeyPath: '/productId' },
feedback: { partitionKeyPath: '/productId' },
impersonation_sessions: { partitionKeyPath: '/productId', defaultTtl: 90 * 86400 },
changelog: { partitionKeyPath: '/productId' },
};
export async function initCosmosIfNeeded(): Promise<void> {
registerContainers(CONTAINER_DEFS);
const shouldInit = config.NODE_ENV !== 'production' || process.env.COSMOS_AUTO_INIT === 'true';
if (!shouldInit) return;
try {
await initializeAllContainers();
process.stdout.write('[platform-service] Cosmos containers ensured\n');
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
process.stderr.write(`[platform-service] Cosmos init failed: ${msg}\n`);
}
}