learning_ai_common_plat/services/platform-service/src/lib/cosmos-init.ts
saravanakumardb1 27f271d983 feat(platform): add P1 operational maturity modules — sessions, maintenance, exports, IP rules
- modules/sessions: device session tracking, revoke one/all, admin force-revoke (9 tests)
- modules/maintenance: 4 modes (off/read_only/maintenance/emergency), bypass roles/IPs, scheduled windows (14 tests)
- modules/exports: GDPR data export jobs for users/audit/telemetry/usage/subscriptions/licenses (10 tests)
- modules/ip-rules: IP allow/deny with CIDR matching, temporary blocks with expiry (10 tests)
- cosmos-init: 4 new containers (sessions, ip_rules, export_jobs, maintenance_windows)
- 1029 platform-service tests passing across 74 test files
2026-02-28 02:47:22 -08:00

90 lines
3.8 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' },
// 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<void> {
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}`);
}
}