fix demo users

This commit is contained in:
YehorI 2025-09-28 12:50:29 +03:00
parent 5a7d7859fb
commit 88c84dfc71
2 changed files with 68 additions and 3 deletions

View File

@ -9,9 +9,6 @@ help: ## Show this help message
build: ## Build Docker containers build: ## Build Docker containers
docker compose build docker compose build
build-no-cache: ## Build Docker containers without cache
docker compose build --no-cache
# Database commands # Database commands
migrate: build ## Run database migrations in Docker migrate: build ## Run database migrations in Docker
docker compose run --rm backend python -m app.db.init docker compose run --rm backend python -m app.db.init

View File

@ -4,15 +4,76 @@ from __future__ import annotations
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session
from app.api.routes import admin, auth, journal, missions, onboarding, store, users from app.api.routes import admin, auth, journal, missions, onboarding, store, users
from app.core.config import settings from app.core.config import settings
from app.core.security import get_password_hash
from app.db.session import SessionLocal
from app.models.user import User, UserRole
from app.models.rank import Rank
# Import all models to ensure they're registered with Base.metadata # Import all models to ensure they're registered with Base.metadata
from app import models # This imports all models through the __init__.py from app import models # This imports all models through the __init__.py
app = FastAPI(title=settings.project_name) app = FastAPI(title=settings.project_name)
def create_demo_users() -> None:
"""Create demo users if they don't exist."""
session: Session = SessionLocal()
try:
# Check if demo users already exist
pilot_exists = session.query(User).filter(User.email == "candidate@alabuga.space").first()
hr_exists = session.query(User).filter(User.email == "hr@alabuga.space").first()
if pilot_exists and hr_exists:
print("✅ Demo users already exist")
return
# Get base rank (or None if no ranks exist)
base_rank = session.query(Rank).order_by(Rank.required_xp).first()
# Create pilot demo user
if not pilot_exists:
pilot = User(
email="candidate@alabuga.space",
full_name="Алексей Пилотов",
role=UserRole.PILOT,
hashed_password=get_password_hash("orbita123"),
current_rank_id=base_rank.id if base_rank else None,
is_email_confirmed=True,
preferred_branch="Получение оффера",
motivation="Хочу пройти все миссии и закрепиться в экипаже.",
)
session.add(pilot)
print("✅ Created demo pilot user: candidate@alabuga.space / orbita123")
# Create HR demo user
if not hr_exists:
hr_rank = session.query(Rank).order_by(Rank.required_xp.desc()).first()
hr = User(
email="hr@alabuga.space",
full_name="Мария HR",
role=UserRole.HR,
hashed_password=get_password_hash("orbita123"),
current_rank_id=hr_rank.id if hr_rank else None,
is_email_confirmed=True,
preferred_branch="Куратор миссий",
)
session.add(hr)
print("✅ Created demo HR user: hr@alabuga.space / orbita123")
session.commit()
except Exception as e:
print(f"❌ Failed to create demo users: {e}")
session.rollback()
finally:
session.close()
app = FastAPI(title=settings.project_name)
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
allow_origins=settings.backend_cors_origins, allow_origins=settings.backend_cors_origins,
@ -31,6 +92,13 @@ app.include_router(store.router)
app.include_router(admin.router) app.include_router(admin.router)
@app.on_event("startup")
async def startup_event():
"""Create demo users on startup if in debug mode."""
if settings.debug:
create_demo_users()
@app.get("/", summary="Проверка работоспособности") @app.get("/", summary="Проверка работоспособности")
def healthcheck() -> dict[str, str]: def healthcheck() -> dict[str, str]:
"""Простой ответ для Docker healthcheck.""" """Простой ответ для Docker healthcheck."""