diff --git a/backend/auth.py b/backend/auth.py index b2736f5..55c63fc 100644 --- a/backend/auth.py +++ b/backend/auth.py @@ -87,7 +87,7 @@ def get_current_user( user_id = int(payload["sub"]) with db() as conn: row = conn.execute( - "SELECT id, email, name, rolle, is_premium, is_moderator, is_banned, ban_reason, is_social_media, notes_ki_enabled, breeder_status, is_founder, is_partner, founder_number, email_verified FROM users WHERE id=?", + "SELECT id, email, name, rolle, is_premium, is_moderator, is_banned, ban_reason, is_social_media, notes_ki_enabled, breeder_status, is_founder, is_partner, founder_number, email_verified, luna_trial_until FROM users WHERE id=?", (user_id,) ).fetchone() @@ -131,7 +131,10 @@ def require_admin(user=Depends(get_current_user)): def require_social_media(user=Depends(get_current_user)): - """Dependency: Social-Media-Manager oder Admin.""" - if not (user.get("is_social_media") or user["rolle"] == "admin"): + """Dependency: Social-Media-Manager, Luna-Probezugang oder Admin.""" + from datetime import datetime as _dt + trial = user.get("luna_trial_until") + trial_active = bool(trial and _dt.utcnow().isoformat() < trial) + if not (user.get("is_social_media") or user["rolle"] == "admin" or trial_active): raise HTTPException(status.HTTP_403_FORBIDDEN, "Kein Zugriff.") return user diff --git a/backend/database.py b/backend/database.py index 8238bca..8ef5362 100644 --- a/backend/database.py +++ b/backend/database.py @@ -1582,6 +1582,35 @@ def _migrate(conn_factory): if 'from_account' not in existing_ol: conn.execute("ALTER TABLE outreach_log ADD COLUMN from_account TEXT DEFAULT 'partner'") + # Job-Bewerbungen + Luna-Probezugang + conn.executescript(""" + CREATE TABLE IF NOT EXISTS job_applications ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER REFERENCES users(id) ON DELETE SET NULL, + name TEXT NOT NULL, + email TEXT NOT NULL, + dog_name TEXT, + dog_rasse TEXT, + social_handle TEXT, + motivation TEXT NOT NULL, + status TEXT NOT NULL DEFAULT 'pending', + admin_note TEXT, + created_at TEXT NOT NULL DEFAULT (datetime('now')), + reviewed_at TEXT + ); + CREATE INDEX IF NOT EXISTS idx_job_apps_status ON job_applications(status, created_at DESC); + CREATE TABLE IF NOT EXISTS job_application_docs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + application_id INTEGER NOT NULL REFERENCES job_applications(id) ON DELETE CASCADE, + filename TEXT NOT NULL, + file_path TEXT NOT NULL, + uploaded_at TEXT NOT NULL DEFAULT (datetime('now')) + ); + """) + existing_u = [row[1] for row in conn.execute("PRAGMA table_info(users)").fetchall()] + if 'luna_trial_until' not in existing_u: + conn.execute("ALTER TABLE users ADD COLUMN luna_trial_until TEXT") + # js_exercise_id zu training_exercises — verbindet training_exercises mit exercise_progress existing_te = [row[1] for row in conn.execute("PRAGMA table_info(training_exercises)").fetchall()] if 'js_exercise_id' not in existing_te: diff --git a/backend/main.py b/backend/main.py index e7db176..d85556a 100644 --- a/backend/main.py +++ b/backend/main.py @@ -188,6 +188,7 @@ from routes.breeder_export import router as breeder_export_router from routes.zucht_ki import router as zucht_ki_router from routes.partner import router as partner_router from routes.outreach import router as outreach_router +from routes.jobs import router as jobs_router app.include_router(auth_router, prefix="/api/auth", tags=["Auth"]) app.include_router(dogs_router, prefix="/api/dogs", tags=["Hunde"]) @@ -221,6 +222,7 @@ app.include_router(breeder_export_router, prefix="/api", tags=["Export"]) app.include_router(zucht_ki_router, prefix="/api", tags=["Züchter-KI"]) app.include_router(partner_router, prefix="/api", tags=["Partner"]) app.include_router(outreach_router, prefix="/api/outreach", tags=["Outreach"]) +app.include_router(jobs_router, prefix="/api/jobs", tags=["Jobs"]) app.include_router(webcal_router, prefix="/api/webcal", tags=["WebCal"]) app.include_router(profile_router, prefix="/api/profile", tags=["Profil"]) app.include_router(import_router, prefix="/api/import", tags=["Import"]) diff --git a/backend/routes/jobs.py b/backend/routes/jobs.py new file mode 100644 index 0000000..8714ae2 --- /dev/null +++ b/backend/routes/jobs.py @@ -0,0 +1,318 @@ +"""BAN YARO — Social-Media-Job Bewerbungs-System""" + +import os +import uuid +from datetime import datetime, timedelta +from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Form +from fastapi.responses import FileResponse +from typing import Optional +from database import db +from auth import get_current_user, get_current_user_optional, require_admin +from mailer import send_email, email_html + +router = APIRouter() + +MEDIA_DIR = os.getenv("MEDIA_DIR", "/data/media") +JOBS_DIR = os.path.join(MEDIA_DIR, "jobs") +TRIAL_DAYS = 14 +MAX_FILES = 3 +MAX_FILE_MB = 10 + +os.makedirs(JOBS_DIR, exist_ok=True) + +_ALLOWED_EXT = {".pdf", ".jpg", ".jpeg", ".png", ".webp", ".mp4", ".mov"} + + +# ------------------------------------------------------------------ +# POST /api/jobs/apply +# ------------------------------------------------------------------ +async def apply( + name: str = Form(...), + email: str = Form(...), + dog_name: str = Form(""), + dog_rasse: str = Form(""), + social_handle: str = Form(...), + motivation: str = Form(...), + files: list[UploadFile] = File(default=[]), + user = Depends(get_current_user_optional), +): + if len(motivation.strip()) < 80: + raise HTTPException(400, "Bitte schreibe etwas mehr über dich (mindestens 80 Zeichen).") + if len(files) > MAX_FILES: + raise HTTPException(400, f"Maximal {MAX_FILES} Dateien erlaubt.") + + user_id = user["id"] if user else None + + # Doppelbewerbung verhindern + if user_id: + with db() as conn: + existing = conn.execute( + "SELECT id FROM job_applications WHERE user_id=? AND status NOT IN ('rejected')", + (user_id,) + ).fetchone() + if existing: + raise HTTPException(400, "Du hast bereits eine aktive Bewerbung eingereicht.") + + with db() as conn: + cur = conn.execute(""" + INSERT INTO job_applications + (user_id, name, email, dog_name, dog_rasse, social_handle, motivation) + VALUES (?,?,?,?,?,?,?) + """, (user_id, name.strip(), email.strip(), dog_name.strip(), + dog_rasse.strip(), social_handle.strip(), motivation.strip())) + app_id = cur.lastrowid + + # Dokumente speichern + app_dir = os.path.join(JOBS_DIR, str(app_id)) + os.makedirs(app_dir, exist_ok=True) + + for f in files: + if not f.filename: + continue + ext = os.path.splitext(f.filename)[1].lower() + if ext not in _ALLOWED_EXT: + continue + size = 0 + safe_name = f"{uuid.uuid4().hex}{ext}" + dest = os.path.join(app_dir, safe_name) + with open(dest, "wb") as out: + while chunk := await f.read(65536): + size += len(chunk) + if size > MAX_FILE_MB * 1024 * 1024: + out.close() + os.remove(dest) + raise HTTPException(400, f"Datei zu groß (max. {MAX_FILE_MB} MB).") + out.write(chunk) + conn.execute(""" + INSERT INTO job_application_docs (application_id, filename, file_path) + VALUES (?,?,?) + """, (app_id, f.filename, dest)) + + # Luna-Probezugang: 14 Tage ab sofort + if user_id: + trial_until = (datetime.utcnow() + timedelta(days=TRIAL_DAYS)).isoformat() + conn.execute( + "UPDATE users SET luna_trial_until=? WHERE id=?", + (trial_until, user_id) + ) + + # Bestätigungs-Mail an Bewerber + try: + body = f""" +
Hallo {name},
++ deine Bewerbung als Social-Media-Manager/in bei Ban Yaro ist bei uns eingegangen. + Wir melden uns bald bei dir! +
+ {"🎉 Luna-Probezugang aktiviert!
Du hast für 14 Tage kostenlos Zugang zu Luna, unserem KI-Social-Media-Assistenten. Logge dich ein und probiere ihn aus.
Das Ban Yaro Team
""" + await send_email( + email, + "Deine Bewerbung bei Ban Yaro 🐾", + email_html(body, cta_url="https://banyaro.app", cta_label="Zur App"), + f"Hallo {name}, deine Bewerbung ist eingegangen!", + ) + except Exception: + pass + + # Admin benachrichtigen + try: + admin_email = os.getenv("ADMIN_EMAIL", "") + if admin_email: + admin_body = f""" +Neue Job-Bewerbung eingegangen:
+| Name | {name} |
| {email} | |
| Hund | {dog_name} ({dog_rasse}) |
| Social | {social_handle} |
| Anhänge | {len([f for f in files if f.filename])} Datei(en) |
{motivation[:300]}{"…" if len(motivation)>300 else ""}
""" + await send_email( + admin_email, + f"[Banyaro Jobs] Neue Bewerbung — {name}", + email_html(admin_body, cta_url="https://banyaro.app/#admin", cta_label="Im Admin-Bereich prüfen"), + f"Neue Bewerbung von {name} ({email})", + ) + except Exception: + pass + + return { + "ok": True, + "application_id": app_id, + "luna_trial": user_id is not None, + "trial_days": TRIAL_DAYS, + } + + +# FastAPI braucht expliziten Router-Decorator +router.add_api_route("/apply", apply, methods=["POST"], status_code=201) + + +# ------------------------------------------------------------------ +# GET /api/jobs/my-application +# ------------------------------------------------------------------ +@router.get("/my-application") +async def my_application(user=Depends(get_current_user)): + with db() as conn: + row = conn.execute( + """SELECT id, status, admin_note, created_at + FROM job_applications WHERE user_id=? + ORDER BY created_at DESC LIMIT 1""", + (user["id"],) + ).fetchone() + if not row: + return {"application": None} + return {"application": dict(row)} + + +# ------------------------------------------------------------------ +# GET /api/jobs/luna-trial-status +# ------------------------------------------------------------------ +@router.get("/luna-trial-status") +async def luna_trial_status(user=Depends(get_current_user)): + from datetime import datetime as _dt + trial = user.get("luna_trial_until") + if not trial: + return {"active": False} + remaining = (_dt.fromisoformat(trial) - _dt.utcnow()).days + return {"active": remaining > 0, "until": trial, "remaining_days": max(0, remaining)} + + +# ------------------------------------------------------------------ +# Admin: Bewerbungen verwalten +# ------------------------------------------------------------------ +@router.get("/admin/applications") +async def list_applications( + status: str = "pending", + admin = Depends(require_admin), +): + where = "" if status == "alle" else "WHERE a.status=?" + params = [] if status == "alle" else [status] + with db() as conn: + rows = conn.execute(f""" + SELECT a.*, u.name AS username, + COUNT(d.id) AS doc_count + FROM job_applications a + LEFT JOIN users u ON u.id = a.user_id + LEFT JOIN job_application_docs d ON d.application_id = a.id + {where} + GROUP BY a.id + ORDER BY a.created_at DESC + """, params).fetchall() + return [dict(r) for r in rows] + + +@router.get("/admin/applications/{app_id}") +async def get_application(app_id: int, admin=Depends(require_admin)): + with db() as conn: + row = conn.execute( + """SELECT a.*, u.name AS username, u.email AS user_email + FROM job_applications a + LEFT JOIN users u ON u.id = a.user_id + WHERE a.id=?""", + (app_id,) + ).fetchone() + if not row: + raise HTTPException(404) + docs = conn.execute( + "SELECT id, filename, uploaded_at FROM job_application_docs WHERE application_id=?", + (app_id,) + ).fetchall() + return {**dict(row), "docs": [dict(d) for d in docs]} + + +@router.patch("/admin/applications/{app_id}") +async def update_application( + app_id: int, + status: Optional[str] = None, + admin_note: Optional[str] = None, + admin = Depends(require_admin), +): + valid = {"pending", "reviewing", "accepted", "rejected"} + if status and status not in valid: + raise HTTPException(400, f"Ungültiger Status. Erlaubt: {valid}") + + with db() as conn: + row = conn.execute( + "SELECT user_id, email, name, status FROM job_applications WHERE id=?", + (app_id,) + ).fetchone() + if not row: + raise HTTPException(404) + + updates: dict = {"reviewed_at": datetime.utcnow().isoformat()} + if status: + updates["status"] = status + if admin_note is not None: + updates["admin_note"] = admin_note + + set_clause = ", ".join(f"{k}=?" for k in updates) + conn.execute( + f"UPDATE job_applications SET {set_clause} WHERE id=?", + (*updates.values(), app_id) + ) + + # Bei Annahme: is_social_media aktivieren + Gründer-Status + if status == "accepted" and row["user_id"]: + conn.execute( + "UPDATE users SET is_social_media=1 WHERE id=?", + (row["user_id"],) + ) + founder_count = conn.execute( + "SELECT COUNT(*) FROM users WHERE is_founder=1" + ).fetchone()[0] + if founder_count < 100: + conn.execute( + "UPDATE users SET is_founder=1 WHERE id=? AND is_founder=0", + (row["user_id"],) + ) + + # Status-Mail an Bewerber + try: + if status in ("accepted", "rejected", "reviewing"): + _send_status_mail(row["email"], row["name"], status, admin_note or "") + except Exception: + pass + + return {"ok": True} + + +@router.get("/admin/applications/{app_id}/docs/{doc_id}") +async def download_doc(app_id: int, doc_id: int, admin=Depends(require_admin)): + with db() as conn: + doc = conn.execute( + "SELECT file_path, filename FROM job_application_docs WHERE id=? AND application_id=?", + (doc_id, app_id) + ).fetchone() + if not doc or not os.path.exists(doc["file_path"]): + raise HTTPException(404) + return FileResponse(doc["file_path"], filename=doc["filename"]) + + +def _send_status_mail(email: str, name: str, status: str, note: str): + import asyncio + texts = { + "reviewing": ("Wir schauen uns deine Bewerbung genauer an 🐾", + f"Hallo {name},
wir schauen uns deine Bewerbung gerade genauer an. Wir melden uns bald!
"), + "accepted": ("Herzlichen Glückwunsch — du bist dabei! 🎉", + f"Hallo {name},
wir freuen uns, dir mitzuteilen: du bist unser neuer Social-Media-Manager/in für Ban Yaro!
Du erhältst außerdem den Gründer-Status in unserer Community. Willkommen im Team!
Hallo {name},
vielen Dank für deine Bewerbung. Leider hat es diesmal nicht geklappt — aber wir wünschen dir alles Gute!
"), + } + subj, body_start = texts.get(status, ("Update zu deiner Bewerbung", f"Hallo {name},
")) + note_html = f'+ Werde das Gesicht von Ban Yaro auf Instagram & TikTok +
++ Mit deiner Bewerbung schalten wir dir sofort den vollen Zugang zu Luna frei — + unserem KI-Assistenten für Social-Media-Posts. Probiere ihn einfach aus, + bevor du dich entscheidest. +
+ ${trialStatus?.active ? `