2026-03-19 11:15:35 +01:00
|
|
|
"""
|
|
|
|
|
Mitai Jinkendo API - Main Application
|
|
|
|
|
|
|
|
|
|
FastAPI backend with modular router structure.
|
|
|
|
|
"""
|
|
|
|
|
import os
|
2026-03-16 13:35:11 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2026-03-19 11:15:35 +01:00
|
|
|
from fastapi import FastAPI
|
2026-03-16 13:35:11 +01:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
from slowapi import Limiter, _rate_limit_exceeded_handler
|
|
|
|
|
from slowapi.util import get_remote_address
|
|
|
|
|
from slowapi.errors import RateLimitExceeded
|
|
|
|
|
|
2026-03-19 11:15:35 +01:00
|
|
|
from db import init_db
|
2026-03-18 08:27:33 +01:00
|
|
|
|
2026-03-19 11:15:35 +01:00
|
|
|
# Import routers
|
|
|
|
|
from routers import auth, profiles, weight, circumference, caliper
|
|
|
|
|
from routers import activity, nutrition, photos, insights, prompts
|
2026-03-19 13:09:33 +01:00
|
|
|
from routers import admin, stats, exportdata, importdata
|
|
|
|
|
from routers import subscription, coupons, features, tiers_mgmt, tier_limits
|
feat: admin CRUD for training types + distribution chart in ActivityPage
Backend (v9d Phase 1b):
- Migration 006: Add abilities JSONB column + descriptions
- admin_training_types.py: Full CRUD endpoints for training types
- List, Get, Create, Update, Delete
- Abilities taxonomy endpoint (5 dimensions: koordinativ, konditionell, kognitiv, psychisch, taktisch)
- Validation: Cannot delete types in use
- Register admin_training_types router in main.py
Frontend:
- AdminTrainingTypesPage: Full CRUD UI
- Create/edit form with all fields (category, subcategory, names, icon, descriptions, sort_order)
- List grouped by category with color coding
- Delete with usage check
- Note about abilities mapping coming in v9f
- Add TrainingTypeDistribution to ActivityPage stats tab
- Add admin link in AdminPanel (v9d section)
- Update api.js with admin training types methods
Notes:
- Abilities mapping UI deferred to v9f (flexible prompt system)
- Placeholders (abilities column) in place for future AI analysis
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-21 15:32:32 +01:00
|
|
|
from routers import user_restrictions, access_grants, training_types, admin_training_types
|
2026-03-21 19:31:58 +01:00
|
|
|
from routers import admin_activity_mappings
|
2026-03-19 11:15:35 +01:00
|
|
|
|
|
|
|
|
# ── App Configuration ─────────────────────────────────────────────────────────
|
2026-03-16 13:35:11 +01:00
|
|
|
DATA_DIR = Path(os.getenv("DATA_DIR", "./data"))
|
|
|
|
|
PHOTOS_DIR = Path(os.getenv("PHOTOS_DIR", "./photos"))
|
|
|
|
|
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
PHOTOS_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
app = FastAPI(title="Mitai Jinkendo API", version="3.0.0")
|
2026-03-19 11:15:35 +01:00
|
|
|
|
|
|
|
|
# Rate limiting
|
2026-03-16 13:35:11 +01:00
|
|
|
limiter = Limiter(key_func=get_remote_address)
|
|
|
|
|
app.state.limiter = limiter
|
|
|
|
|
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
2026-03-19 11:15:35 +01:00
|
|
|
|
|
|
|
|
# CORS
|
2026-03-16 13:35:11 +01:00
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
allow_origins=os.getenv("ALLOWED_ORIGINS", "*").split(","),
|
|
|
|
|
allow_credentials=True,
|
|
|
|
|
allow_methods=["GET","POST","PUT","DELETE","OPTIONS"],
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-19 11:15:35 +01:00
|
|
|
# ── Startup Event ─────────────────────────────────────────────────────────────
|
2026-03-19 07:56:36 +01:00
|
|
|
@app.on_event("startup")
|
|
|
|
|
async def startup_event():
|
2026-03-19 11:15:35 +01:00
|
|
|
"""Run database initialization on startup."""
|
2026-03-19 08:26:09 +01:00
|
|
|
try:
|
|
|
|
|
init_db()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"⚠️ init_db() failed (non-fatal): {e}")
|
2026-03-19 11:15:35 +01:00
|
|
|
# Don't crash on startup - can be created manually
|
|
|
|
|
|
feat: add v9c subscription system database schema
Phase 1: Database Migration Complete
Created migration infrastructure:
- backend/migrations/v9c_subscription_system.sql (11 new tables)
- backend/apply_v9c_migration.py (auto-migration runner)
- Updated main.py startup event to apply migration
New tables (Feature-Registry Pattern):
1. app_settings - Global configuration
2. tiers - Subscription tiers (free/basic/premium/selfhosted)
3. features - Feature registry (11 limitable features)
4. tier_limits - Tier x Feature matrix (44 initial limits)
5. user_feature_restrictions - Individual user overrides
6. user_feature_usage - Usage tracking with reset periods
7. coupons - Coupon management (single-use, period, Wellpass)
8. coupon_redemptions - Redemption history
9. access_grants - Time-limited access with pause/resume logic
10. user_activity_log - Activity tracking (JSONB details)
11. user_stats - Aggregated statistics
Extended profiles table:
- tier, trial_ends_at, email_verified, email_verify_token
- invited_by, invitation_token
Initial data inserted:
- 4 tiers (free/basic/premium/selfhosted)
- 11 features (weight, circumference, caliper, nutrition, activity, photos, ai_calls, ai_pipeline, export_*)
- 44 tier_limits (complete Tier x Feature matrix)
- App settings (trial duration, self-registration config)
Migration auto-runs on container startup (similar to SQLite→PostgreSQL).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-19 12:42:43 +01:00
|
|
|
# Apply v9c migration if needed
|
|
|
|
|
try:
|
|
|
|
|
from apply_v9c_migration import apply_migration
|
|
|
|
|
apply_migration()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"⚠️ v9c migration failed (non-fatal): {e}")
|
|
|
|
|
|
2026-03-19 11:15:35 +01:00
|
|
|
# ── Register Routers ──────────────────────────────────────────────────────────
|
|
|
|
|
app.include_router(auth.router) # /api/auth/*
|
|
|
|
|
app.include_router(profiles.router) # /api/profiles/*, /api/profile
|
|
|
|
|
app.include_router(weight.router) # /api/weight/*
|
|
|
|
|
app.include_router(circumference.router) # /api/circumferences/*
|
|
|
|
|
app.include_router(caliper.router) # /api/caliper/*
|
|
|
|
|
app.include_router(activity.router) # /api/activity/*
|
|
|
|
|
app.include_router(nutrition.router) # /api/nutrition/*
|
|
|
|
|
app.include_router(photos.router) # /api/photos/*
|
|
|
|
|
app.include_router(insights.router) # /api/insights/*, /api/ai/*
|
|
|
|
|
app.include_router(prompts.router) # /api/prompts/*
|
|
|
|
|
app.include_router(admin.router) # /api/admin/*
|
|
|
|
|
app.include_router(stats.router) # /api/stats
|
|
|
|
|
app.include_router(exportdata.router) # /api/export/*
|
|
|
|
|
app.include_router(importdata.router) # /api/import/*
|
2026-03-19 13:09:33 +01:00
|
|
|
|
|
|
|
|
# v9c Subscription System
|
2026-03-19 13:05:55 +01:00
|
|
|
app.include_router(subscription.router) # /api/subscription/*
|
2026-03-19 13:07:09 +01:00
|
|
|
app.include_router(coupons.router) # /api/coupons/*
|
2026-03-19 13:09:33 +01:00
|
|
|
app.include_router(features.router) # /api/features (admin)
|
|
|
|
|
app.include_router(tiers_mgmt.router) # /api/tiers (admin)
|
|
|
|
|
app.include_router(tier_limits.router) # /api/tier-limits (admin)
|
|
|
|
|
app.include_router(user_restrictions.router) # /api/user-restrictions (admin)
|
|
|
|
|
app.include_router(access_grants.router) # /api/access-grants (admin)
|
2026-03-19 11:15:35 +01:00
|
|
|
|
2026-03-21 13:05:33 +01:00
|
|
|
# v9d Training Types
|
feat: admin CRUD for training types + distribution chart in ActivityPage
Backend (v9d Phase 1b):
- Migration 006: Add abilities JSONB column + descriptions
- admin_training_types.py: Full CRUD endpoints for training types
- List, Get, Create, Update, Delete
- Abilities taxonomy endpoint (5 dimensions: koordinativ, konditionell, kognitiv, psychisch, taktisch)
- Validation: Cannot delete types in use
- Register admin_training_types router in main.py
Frontend:
- AdminTrainingTypesPage: Full CRUD UI
- Create/edit form with all fields (category, subcategory, names, icon, descriptions, sort_order)
- List grouped by category with color coding
- Delete with usage check
- Note about abilities mapping coming in v9f
- Add TrainingTypeDistribution to ActivityPage stats tab
- Add admin link in AdminPanel (v9d section)
- Update api.js with admin training types methods
Notes:
- Abilities mapping UI deferred to v9f (flexible prompt system)
- Placeholders (abilities column) in place for future AI analysis
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-21 15:32:32 +01:00
|
|
|
app.include_router(training_types.router) # /api/training-types/*
|
|
|
|
|
app.include_router(admin_training_types.router) # /api/admin/training-types/*
|
2026-03-21 19:31:58 +01:00
|
|
|
app.include_router(admin_activity_mappings.router) # /api/admin/activity-mappings/*
|
2026-03-21 13:05:33 +01:00
|
|
|
|
2026-03-19 11:15:35 +01:00
|
|
|
# ── Health Check ──────────────────────────────────────────────────────────────
|
|
|
|
|
@app.get("/")
|
|
|
|
|
def root():
|
|
|
|
|
"""API health check."""
|
feat: add v9c subscription system database schema
Phase 1: Database Migration Complete
Created migration infrastructure:
- backend/migrations/v9c_subscription_system.sql (11 new tables)
- backend/apply_v9c_migration.py (auto-migration runner)
- Updated main.py startup event to apply migration
New tables (Feature-Registry Pattern):
1. app_settings - Global configuration
2. tiers - Subscription tiers (free/basic/premium/selfhosted)
3. features - Feature registry (11 limitable features)
4. tier_limits - Tier x Feature matrix (44 initial limits)
5. user_feature_restrictions - Individual user overrides
6. user_feature_usage - Usage tracking with reset periods
7. coupons - Coupon management (single-use, period, Wellpass)
8. coupon_redemptions - Redemption history
9. access_grants - Time-limited access with pause/resume logic
10. user_activity_log - Activity tracking (JSONB details)
11. user_stats - Aggregated statistics
Extended profiles table:
- tier, trial_ends_at, email_verified, email_verify_token
- invited_by, invitation_token
Initial data inserted:
- 4 tiers (free/basic/premium/selfhosted)
- 11 features (weight, circumference, caliper, nutrition, activity, photos, ai_calls, ai_pipeline, export_*)
- 44 tier_limits (complete Tier x Feature matrix)
- App settings (trial duration, self-registration config)
Migration auto-runs on container startup (similar to SQLite→PostgreSQL).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-19 12:42:43 +01:00
|
|
|
return {"status": "ok", "service": "mitai-jinkendo", "version": "v9c-dev"}
|