Compare commits
8 Commits
feature/HR
...
feature/HR
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
946bdc65b6 | ||
|
|
701660ce83 | ||
| b7ed82418f | |||
|
|
8604dc78b1 | ||
|
|
30464fb40c | ||
|
|
31db3a8260 | ||
|
|
278245cd7c | ||
|
|
225295030b |
@@ -3,6 +3,8 @@
|
|||||||
API v1 Blueprint package — Turf SaaS
|
API v1 Blueprint package — Turf SaaS
|
||||||
Sprint 3-4: HRT-29 — Refacto API /v1/
|
Sprint 3-4: HRT-29 — Refacto API /v1/
|
||||||
Sprint 5-6: HRT-31 — Billing Stripe
|
Sprint 5-6: HRT-31 — Billing Stripe
|
||||||
|
HRT-79: Alertes Telegram configurables (user blueprint)
|
||||||
|
HRT-82: Multi-compte / Organisation Pro (max 5 users)
|
||||||
|
|
||||||
Registers sub-blueprints:
|
Registers sub-blueprints:
|
||||||
/api/v1/health — public health-check
|
/api/v1/health — public health-check
|
||||||
@@ -13,6 +15,9 @@ Registers sub-blueprints:
|
|||||||
/api/v1/export/ — export CSV (pro)
|
/api/v1/export/ — export CSV (pro)
|
||||||
/api/v1/metrics — métriques perf ML (premium+)
|
/api/v1/metrics — métriques perf ML (premium+)
|
||||||
/api/v1/billing/ — Stripe checkout, portal, webhook, status
|
/api/v1/billing/ — Stripe checkout, portal, webhook, status
|
||||||
|
/api/v1/user/ — config utilisateur, alertes Telegram (premium+)
|
||||||
|
/api/v1/history — historique préd. ML (Free:7j, Premium:90j, Pro:illimité)
|
||||||
|
/api/v1/org/ — organisations Pro (multi-compte, max 5 users)
|
||||||
/api/v1/docs — Swagger UI (via flasgger, registered on app)
|
/api/v1/docs — Swagger UI (via flasgger, registered on app)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -26,6 +31,9 @@ from .routes.backtest import backtest_bp
|
|||||||
from .routes.export import export_bp
|
from .routes.export import export_bp
|
||||||
from .routes.metrics import metrics_bp
|
from .routes.metrics import metrics_bp
|
||||||
from .routes.billing import billing_bp
|
from .routes.billing import billing_bp
|
||||||
|
from .routes.user import user_bp
|
||||||
|
from .routes.history import history_bp
|
||||||
|
from .routes.org import org_bp
|
||||||
|
|
||||||
# Master blueprint that aggregates all sub-routes under /api/v1
|
# Master blueprint that aggregates all sub-routes under /api/v1
|
||||||
api_v1_bp = Blueprint("api_v1", __name__, url_prefix="/api/v1")
|
api_v1_bp = Blueprint("api_v1", __name__, url_prefix="/api/v1")
|
||||||
@@ -41,3 +49,6 @@ def register_api_v1(app):
|
|||||||
app.register_blueprint(export_bp)
|
app.register_blueprint(export_bp)
|
||||||
app.register_blueprint(metrics_bp)
|
app.register_blueprint(metrics_bp)
|
||||||
app.register_blueprint(billing_bp)
|
app.register_blueprint(billing_bp)
|
||||||
|
app.register_blueprint(user_bp)
|
||||||
|
app.register_blueprint(history_bp)
|
||||||
|
app.register_blueprint(org_bp)
|
||||||
|
|||||||
212
api_v1/routes/history.py
Normal file
212
api_v1/routes/history.py
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
History routes for API v1.
|
||||||
|
|
||||||
|
GET /api/v1/history — Historique des prédictions avec filtre date range,
|
||||||
|
limité selon le plan (Free: 7j, Premium: 90j, Pro: illimité)
|
||||||
|
|
||||||
|
Ticket: HRT-81 — Historique limité/illimité selon plan (Free/Premium/Pro)
|
||||||
|
"""
|
||||||
|
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from flask import Blueprint, jsonify, request, g
|
||||||
|
|
||||||
|
from api_v1.utils import (
|
||||||
|
get_db,
|
||||||
|
table_exists,
|
||||||
|
internal_error,
|
||||||
|
bad_request,
|
||||||
|
forbidden,
|
||||||
|
get_pagination_params,
|
||||||
|
paginate_query,
|
||||||
|
)
|
||||||
|
from auth import jwt_required_middleware
|
||||||
|
|
||||||
|
history_bp = Blueprint("v1_history", __name__, url_prefix="/api/v1/history")
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# Plan limits (days of history accessible; None = unlimited)
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
HISTORY_DAYS = {
|
||||||
|
"free": 7,
|
||||||
|
"premium": 90,
|
||||||
|
"pro": None, # illimité
|
||||||
|
}
|
||||||
|
|
||||||
|
# Fallback for unknown plans: treat like free
|
||||||
|
_DEFAULT_LIMIT = 7
|
||||||
|
|
||||||
|
|
||||||
|
def _get_plan_max_days(plan: str):
|
||||||
|
"""Return the max history days allowed for the given plan, or default."""
|
||||||
|
return HISTORY_DAYS.get(plan, _DEFAULT_LIMIT)
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_date(date_str: str, param_name: str):
|
||||||
|
"""Parse YYYY-MM-DD date string, raise ValueError with context on failure."""
|
||||||
|
try:
|
||||||
|
return datetime.strptime(date_str, "%Y-%m-%d").date()
|
||||||
|
except ValueError:
|
||||||
|
raise ValueError(
|
||||||
|
f"Paramètre '{param_name}' invalide : format attendu YYYY-MM-DD, reçu '{date_str}'"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# GET /api/v1/history
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
@history_bp.route("", methods=["GET"])
|
||||||
|
@jwt_required_middleware
|
||||||
|
def get_history():
|
||||||
|
"""
|
||||||
|
Historique des prédictions ML avec filtre date range
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Historique
|
||||||
|
summary: |
|
||||||
|
Historique des prédictions sur une plage de dates.
|
||||||
|
Limite selon le plan :
|
||||||
|
- Free : 7 derniers jours
|
||||||
|
- Premium : 90 derniers jours
|
||||||
|
- Pro : illimité
|
||||||
|
security:
|
||||||
|
- Bearer: []
|
||||||
|
parameters:
|
||||||
|
- name: start
|
||||||
|
in: query
|
||||||
|
type: string
|
||||||
|
format: date
|
||||||
|
description: Date de début au format YYYY-MM-DD (défaut : aujourd'hui - max_days du plan)
|
||||||
|
- name: end
|
||||||
|
in: query
|
||||||
|
type: string
|
||||||
|
format: date
|
||||||
|
description: Date de fin au format YYYY-MM-DD (défaut : aujourd'hui)
|
||||||
|
- name: limit
|
||||||
|
in: query
|
||||||
|
type: integer
|
||||||
|
default: 50
|
||||||
|
description: Nombre de résultats par page (max 500)
|
||||||
|
- name: offset
|
||||||
|
in: query
|
||||||
|
type: integer
|
||||||
|
default: 0
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: Historique des prédictions ML
|
||||||
|
400:
|
||||||
|
description: Paramètre de date invalide
|
||||||
|
401:
|
||||||
|
description: Token invalide ou manquant
|
||||||
|
403:
|
||||||
|
description: Plage de dates hors limite du plan — upgrade requis
|
||||||
|
"""
|
||||||
|
user = getattr(g, "current_user", None)
|
||||||
|
if not user:
|
||||||
|
return jsonify({"error": "Non authentifié"}), 401
|
||||||
|
|
||||||
|
plan = user.get("plan", "free")
|
||||||
|
today = datetime.now().date()
|
||||||
|
max_days = _get_plan_max_days(plan)
|
||||||
|
|
||||||
|
# ── Parse end date ────────────────────────────────────────
|
||||||
|
end_str = request.args.get("end", today.isoformat())
|
||||||
|
try:
|
||||||
|
end_date = _parse_date(end_str, "end")
|
||||||
|
except ValueError as exc:
|
||||||
|
return bad_request(str(exc))
|
||||||
|
|
||||||
|
# ── Parse start date ─────────────────────────────────────
|
||||||
|
if max_days is not None:
|
||||||
|
default_start = today - timedelta(days=max_days - 1)
|
||||||
|
else:
|
||||||
|
# Pro: default to 30 days back when no start provided
|
||||||
|
default_start = today - timedelta(days=29)
|
||||||
|
|
||||||
|
start_str = request.args.get("start", default_start.isoformat())
|
||||||
|
try:
|
||||||
|
start_date = _parse_date(start_str, "start")
|
||||||
|
except ValueError as exc:
|
||||||
|
return bad_request(str(exc))
|
||||||
|
|
||||||
|
# ── Validate ordering ─────────────────────────────────────
|
||||||
|
if start_date > end_date:
|
||||||
|
return bad_request(
|
||||||
|
f"'start' ({start_str}) ne peut pas être postérieur à 'end' ({end_str})"
|
||||||
|
)
|
||||||
|
|
||||||
|
# ── Enforce plan window ───────────────────────────────────
|
||||||
|
if max_days is not None:
|
||||||
|
earliest_allowed = today - timedelta(days=max_days - 1)
|
||||||
|
if start_date < earliest_allowed:
|
||||||
|
return forbidden(
|
||||||
|
message=(
|
||||||
|
f"Historique limité à {max_days} jours pour le plan '{plan}'. "
|
||||||
|
f"Date de début minimale autorisée : {earliest_allowed.isoformat()}. "
|
||||||
|
f"Passez à un plan supérieur pour accéder à un historique plus long."
|
||||||
|
),
|
||||||
|
required_plans=["premium", "pro"] if plan == "free" else ["pro"],
|
||||||
|
current_plan=plan,
|
||||||
|
)
|
||||||
|
|
||||||
|
# ── Pagination ────────────────────────────────────────────
|
||||||
|
limit, offset = get_pagination_params(default_limit=50, max_limit=500)
|
||||||
|
|
||||||
|
# ── Query ─────────────────────────────────────────────────
|
||||||
|
conn = get_db()
|
||||||
|
try:
|
||||||
|
if not table_exists(conn, "ml_predictions_cache"):
|
||||||
|
return jsonify(
|
||||||
|
{
|
||||||
|
"status": "ok",
|
||||||
|
"plan": plan,
|
||||||
|
"start": start_date.isoformat(),
|
||||||
|
"end": end_date.isoformat(),
|
||||||
|
"history": [],
|
||||||
|
**paginate_query([], 0, limit, offset),
|
||||||
|
}
|
||||||
|
), 200
|
||||||
|
|
||||||
|
count_row = conn.execute(
|
||||||
|
"""SELECT COUNT(*) as cnt
|
||||||
|
FROM ml_predictions_cache
|
||||||
|
WHERE date >= ? AND date <= ?""",
|
||||||
|
(start_date.isoformat(), end_date.isoformat()),
|
||||||
|
).fetchone()
|
||||||
|
total = count_row["cnt"] if count_row else 0
|
||||||
|
|
||||||
|
sql = """
|
||||||
|
SELECT
|
||||||
|
id, date, horse_name, prob_top1, prob_top3,
|
||||||
|
ml_score, race_label, hippodrome, heure, is_value_bet
|
||||||
|
FROM ml_predictions_cache
|
||||||
|
WHERE date >= ? AND date <= ?
|
||||||
|
ORDER BY date DESC, ml_score DESC
|
||||||
|
LIMIT ? OFFSET ?
|
||||||
|
"""
|
||||||
|
rows = conn.execute(
|
||||||
|
sql,
|
||||||
|
(start_date.isoformat(), end_date.isoformat(), limit, offset),
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
history = [dict(r) for r in rows]
|
||||||
|
|
||||||
|
return jsonify(
|
||||||
|
{
|
||||||
|
"status": "ok",
|
||||||
|
"plan": plan,
|
||||||
|
"history_limit_days": max_days,
|
||||||
|
"start": start_date.isoformat(),
|
||||||
|
"end": end_date.isoformat(),
|
||||||
|
"history": history,
|
||||||
|
**paginate_query(history, total, limit, offset),
|
||||||
|
}
|
||||||
|
), 200
|
||||||
|
|
||||||
|
except Exception as exc:
|
||||||
|
return internal_error(str(exc))
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
536
api_v1/routes/org.py
Normal file
536
api_v1/routes/org.py
Normal file
@@ -0,0 +1,536 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Org Blueprint — Multi-compte / Organisations Pro
|
||||||
|
Sprint: HRT-82
|
||||||
|
|
||||||
|
Endpoints:
|
||||||
|
POST /api/v1/org — créer une organisation (Pro only, 1 max par owner)
|
||||||
|
GET /api/v1/org — infos org courante
|
||||||
|
DELETE /api/v1/org — supprimer l'org (owner only)
|
||||||
|
POST /api/v1/org/invite — inviter un membre par email (max 5 totaux)
|
||||||
|
GET /api/v1/org/members — liste des membres
|
||||||
|
DELETE /api/v1/org/members/<user_id> — retirer un membre (owner only)
|
||||||
|
|
||||||
|
Plan enforcement:
|
||||||
|
- Toutes les routes nécessitent plan=pro via plan_required('pro')
|
||||||
|
- Limite : 1 org par owner, 5 membres max (owner inclus)
|
||||||
|
"""
|
||||||
|
|
||||||
|
import secrets
|
||||||
|
import logging
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
from flask import Blueprint, jsonify, request
|
||||||
|
|
||||||
|
from saas_auth import require_auth as jwt_required_middleware
|
||||||
|
from org_db import get_db, migrate_org_tables
|
||||||
|
|
||||||
|
logger = logging.getLogger("turf_saas.org")
|
||||||
|
|
||||||
|
org_bp = Blueprint("org", __name__, url_prefix="/api/v1/org")
|
||||||
|
|
||||||
|
MAX_MEMBERS = 5 # max membres totaux owner inclus
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# Decorator: plan Pro requis
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
def _require_pro(fn):
|
||||||
|
"""Vérifie que l'utilisateur courant est sur le plan 'pro'."""
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
|
@wraps(fn)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
user = getattr(request, "current_user", None)
|
||||||
|
if not user:
|
||||||
|
return jsonify({"error": "Non authentifié"}), 401
|
||||||
|
if user.get("plan") != "pro":
|
||||||
|
return jsonify(
|
||||||
|
{
|
||||||
|
"error": "Plan insuffisant",
|
||||||
|
"required": "pro",
|
||||||
|
"current_plan": user.get("plan", "free"),
|
||||||
|
"upgrade_url": "/api/v1/billing/checkout",
|
||||||
|
}
|
||||||
|
), 403
|
||||||
|
return fn(*args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# Helpers DB
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
def _get_org_by_owner(db, owner_id: str):
|
||||||
|
return db.execute(
|
||||||
|
"SELECT * FROM organizations WHERE owner_id = ?", (owner_id,)
|
||||||
|
).fetchone()
|
||||||
|
|
||||||
|
|
||||||
|
def _get_org_by_id(db, org_id: str):
|
||||||
|
return db.execute("SELECT * FROM organizations WHERE id = ?", (org_id,)).fetchone()
|
||||||
|
|
||||||
|
|
||||||
|
def _get_member_org(db, user_id: str):
|
||||||
|
"""Retourne l'org dont user_id est membre (owner ou member)."""
|
||||||
|
row = db.execute(
|
||||||
|
"""SELECT o.* FROM organizations o
|
||||||
|
JOIN org_members m ON m.org_id = o.id
|
||||||
|
WHERE m.user_id = ?
|
||||||
|
LIMIT 1""",
|
||||||
|
(user_id,),
|
||||||
|
).fetchone()
|
||||||
|
return row
|
||||||
|
|
||||||
|
|
||||||
|
def _count_org_members(db, org_id: str) -> int:
|
||||||
|
row = db.execute(
|
||||||
|
"SELECT COUNT(*) AS cnt FROM org_members WHERE org_id = ?", (org_id,)
|
||||||
|
).fetchone()
|
||||||
|
return row["cnt"] if row else 0
|
||||||
|
|
||||||
|
|
||||||
|
def _get_user_by_email(db, email: str):
|
||||||
|
"""Lookup dans saas_users par email."""
|
||||||
|
return db.execute(
|
||||||
|
"SELECT * FROM saas_users WHERE email = ?", (email.lower().strip(),)
|
||||||
|
).fetchone()
|
||||||
|
|
||||||
|
|
||||||
|
def _org_to_dict(org) -> dict:
|
||||||
|
return {
|
||||||
|
"id": org["id"],
|
||||||
|
"owner_id": org["owner_id"],
|
||||||
|
"name": org["name"],
|
||||||
|
"max_members": org["max_members"],
|
||||||
|
"created_at": org["created_at"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _member_to_dict(m) -> dict:
|
||||||
|
return {
|
||||||
|
"id": m["id"],
|
||||||
|
"org_id": m["org_id"],
|
||||||
|
"user_id": m["user_id"],
|
||||||
|
"role": m["role"],
|
||||||
|
"invited_at": m["invited_at"],
|
||||||
|
"joined_at": m["joined_at"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# POST /api/v1/org — créer une organisation
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
@org_bp.route("", methods=["POST"])
|
||||||
|
@jwt_required_middleware
|
||||||
|
@_require_pro
|
||||||
|
def create_org():
|
||||||
|
"""
|
||||||
|
Crée une organisation.
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Organisation
|
||||||
|
security:
|
||||||
|
- Bearer: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
required: [name]
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
description: Nom de l'organisation (1-100 caractères)
|
||||||
|
responses:
|
||||||
|
201:
|
||||||
|
description: Organisation créée
|
||||||
|
400:
|
||||||
|
description: Paramètre manquant ou invalide
|
||||||
|
403:
|
||||||
|
description: Plan insuffisant
|
||||||
|
409:
|
||||||
|
description: L'utilisateur possède déjà une organisation
|
||||||
|
"""
|
||||||
|
user = request.current_user
|
||||||
|
owner_id = user["id"]
|
||||||
|
|
||||||
|
data = request.get_json(silent=True) or {}
|
||||||
|
name = (data.get("name") or "").strip()
|
||||||
|
if not name or len(name) > 100:
|
||||||
|
return jsonify({"error": "Le nom est requis (1-100 caractères)"}), 400
|
||||||
|
|
||||||
|
db = get_db()
|
||||||
|
try:
|
||||||
|
# 1 org max par owner
|
||||||
|
existing = _get_org_by_owner(db, owner_id)
|
||||||
|
if existing:
|
||||||
|
return jsonify(
|
||||||
|
{
|
||||||
|
"error": "Vous possédez déjà une organisation",
|
||||||
|
"org_id": existing["id"],
|
||||||
|
}
|
||||||
|
), 409
|
||||||
|
|
||||||
|
org_id = secrets.token_hex(16)
|
||||||
|
now = datetime.now(timezone.utc).isoformat()
|
||||||
|
|
||||||
|
db.execute(
|
||||||
|
"INSERT INTO organizations (id, owner_id, name, max_members, created_at) "
|
||||||
|
"VALUES (?, ?, ?, ?, ?)",
|
||||||
|
(org_id, owner_id, name, MAX_MEMBERS, now),
|
||||||
|
)
|
||||||
|
# Ajouter l'owner comme premier membre avec rôle 'owner'
|
||||||
|
db.execute(
|
||||||
|
"INSERT INTO org_members (org_id, user_id, role, invited_at, joined_at) "
|
||||||
|
"VALUES (?, ?, 'owner', ?, ?)",
|
||||||
|
(org_id, owner_id, now, now),
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
org = _get_org_by_id(db, org_id)
|
||||||
|
logger.info("Org créée: %s par user %s", org_id, owner_id)
|
||||||
|
return jsonify({"org": _org_to_dict(org)}), 201
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
logger.error("create_org error: %s", e)
|
||||||
|
return jsonify({"error": "Erreur interne"}), 500
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# GET /api/v1/org — infos org courante
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
@org_bp.route("", methods=["GET"])
|
||||||
|
@jwt_required_middleware
|
||||||
|
@_require_pro
|
||||||
|
def get_org():
|
||||||
|
"""
|
||||||
|
Retourne l'organisation dont l'utilisateur est owner ou membre.
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Organisation
|
||||||
|
security:
|
||||||
|
- Bearer: []
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: Infos de l'organisation
|
||||||
|
404:
|
||||||
|
description: Aucune organisation trouvée
|
||||||
|
"""
|
||||||
|
user = request.current_user
|
||||||
|
db = get_db()
|
||||||
|
try:
|
||||||
|
org = _get_org_by_owner(db, user["id"]) or _get_member_org(db, user["id"])
|
||||||
|
if not org:
|
||||||
|
return jsonify({"error": "Aucune organisation trouvée"}), 404
|
||||||
|
|
||||||
|
member_count = _count_org_members(db, org["id"])
|
||||||
|
result = _org_to_dict(org)
|
||||||
|
result["member_count"] = member_count
|
||||||
|
return jsonify({"org": result}), 200
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# DELETE /api/v1/org — supprimer l'organisation
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
@org_bp.route("", methods=["DELETE"])
|
||||||
|
@jwt_required_middleware
|
||||||
|
@_require_pro
|
||||||
|
def delete_org():
|
||||||
|
"""
|
||||||
|
Supprime l'organisation (owner uniquement).
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Organisation
|
||||||
|
security:
|
||||||
|
- Bearer: []
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: Organisation supprimée
|
||||||
|
403:
|
||||||
|
description: Seul l'owner peut supprimer l'organisation
|
||||||
|
404:
|
||||||
|
description: Organisation introuvable
|
||||||
|
"""
|
||||||
|
user = request.current_user
|
||||||
|
db = get_db()
|
||||||
|
try:
|
||||||
|
org = _get_org_by_owner(db, user["id"])
|
||||||
|
if not org:
|
||||||
|
return jsonify({"error": "Vous n'êtes pas owner d'une organisation"}), 403
|
||||||
|
|
||||||
|
# CASCADE supprime org_members automatiquement (FK ON DELETE CASCADE)
|
||||||
|
db.execute("DELETE FROM organizations WHERE id = ?", (org["id"],))
|
||||||
|
db.commit()
|
||||||
|
logger.info("Org %s supprimée par user %s", org["id"], user["id"])
|
||||||
|
return jsonify({"ok": True, "deleted_org_id": org["id"]}), 200
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
logger.error("delete_org error: %s", e)
|
||||||
|
return jsonify({"error": "Erreur interne"}), 500
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# POST /api/v1/org/invite — inviter un membre par email
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
@org_bp.route("/invite", methods=["POST"])
|
||||||
|
@jwt_required_middleware
|
||||||
|
@_require_pro
|
||||||
|
def invite_member():
|
||||||
|
"""
|
||||||
|
Invite un utilisateur dans l'organisation par email (owner uniquement).
|
||||||
|
Limite : 5 membres totaux (owner inclus).
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Organisation
|
||||||
|
security:
|
||||||
|
- Bearer: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
required: [email]
|
||||||
|
properties:
|
||||||
|
email:
|
||||||
|
type: string
|
||||||
|
description: Email de l'utilisateur à inviter
|
||||||
|
responses:
|
||||||
|
201:
|
||||||
|
description: Membre ajouté
|
||||||
|
400:
|
||||||
|
description: Paramètre manquant ou invalide
|
||||||
|
403:
|
||||||
|
description: Seul l'owner peut inviter / limite de membres atteinte
|
||||||
|
404:
|
||||||
|
description: Utilisateur introuvable ou organisation inexistante
|
||||||
|
409:
|
||||||
|
description: L'utilisateur est déjà membre
|
||||||
|
"""
|
||||||
|
user = request.current_user
|
||||||
|
data = request.get_json(silent=True) or {}
|
||||||
|
email = (data.get("email") or "").strip().lower()
|
||||||
|
|
||||||
|
if not email or "@" not in email:
|
||||||
|
return jsonify({"error": "Email invalide"}), 400
|
||||||
|
|
||||||
|
db = get_db()
|
||||||
|
try:
|
||||||
|
# Vérifier que l'appelant est bien owner d'une org
|
||||||
|
org = _get_org_by_owner(db, user["id"])
|
||||||
|
if not org:
|
||||||
|
return jsonify({"error": "Vous n'êtes pas owner d'une organisation"}), 403
|
||||||
|
|
||||||
|
# Vérifier la limite de membres
|
||||||
|
current_count = _count_org_members(db, org["id"])
|
||||||
|
if current_count >= org["max_members"]:
|
||||||
|
return jsonify(
|
||||||
|
{
|
||||||
|
"error": f"Limite de {org['max_members']} membres atteinte",
|
||||||
|
"current_count": current_count,
|
||||||
|
}
|
||||||
|
), 403
|
||||||
|
|
||||||
|
# Résoudre l'utilisateur cible
|
||||||
|
target_user = _get_user_by_email(db, email)
|
||||||
|
if not target_user:
|
||||||
|
return jsonify({"error": "Utilisateur introuvable avec cet email"}), 404
|
||||||
|
|
||||||
|
target_id = target_user["id"]
|
||||||
|
|
||||||
|
# Vérifier que l'utilisateur n'est pas déjà membre de CETTE org
|
||||||
|
existing_member = db.execute(
|
||||||
|
"SELECT id FROM org_members WHERE org_id = ? AND user_id = ?",
|
||||||
|
(org["id"], target_id),
|
||||||
|
).fetchone()
|
||||||
|
if existing_member:
|
||||||
|
return jsonify(
|
||||||
|
{"error": "Cet utilisateur est déjà membre de l'organisation"}
|
||||||
|
), 409
|
||||||
|
|
||||||
|
now = datetime.now(timezone.utc).isoformat()
|
||||||
|
db.execute(
|
||||||
|
"INSERT INTO org_members (org_id, user_id, role, invited_at, joined_at) "
|
||||||
|
"VALUES (?, ?, 'member', ?, ?)",
|
||||||
|
(org["id"], target_id, now, now),
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
member_row = db.execute(
|
||||||
|
"SELECT * FROM org_members WHERE org_id = ? AND user_id = ?",
|
||||||
|
(org["id"], target_id),
|
||||||
|
).fetchone()
|
||||||
|
logger.info(
|
||||||
|
"User %s invité dans org %s par %s", target_id, org["id"], user["id"]
|
||||||
|
)
|
||||||
|
return jsonify({"member": _member_to_dict(member_row)}), 201
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
logger.error("invite_member error: %s", e)
|
||||||
|
return jsonify({"error": "Erreur interne"}), 500
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# GET /api/v1/org/members — liste des membres
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
@org_bp.route("/members", methods=["GET"])
|
||||||
|
@jwt_required_middleware
|
||||||
|
@_require_pro
|
||||||
|
def list_members():
|
||||||
|
"""
|
||||||
|
Liste les membres de l'organisation courante.
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Organisation
|
||||||
|
security:
|
||||||
|
- Bearer: []
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: Liste des membres
|
||||||
|
404:
|
||||||
|
description: Organisation introuvable
|
||||||
|
"""
|
||||||
|
user = request.current_user
|
||||||
|
db = get_db()
|
||||||
|
try:
|
||||||
|
org = _get_org_by_owner(db, user["id"]) or _get_member_org(db, user["id"])
|
||||||
|
if not org:
|
||||||
|
return jsonify({"error": "Aucune organisation trouvée"}), 404
|
||||||
|
|
||||||
|
members = db.execute(
|
||||||
|
"SELECT m.*, u.email, u.firstname, u.lastname "
|
||||||
|
"FROM org_members m "
|
||||||
|
"LEFT JOIN saas_users u ON u.id = m.user_id "
|
||||||
|
"WHERE m.org_id = ? "
|
||||||
|
"ORDER BY m.invited_at ASC",
|
||||||
|
(org["id"],),
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
result = []
|
||||||
|
for m in members:
|
||||||
|
d = _member_to_dict(m)
|
||||||
|
d["email"] = m["email"]
|
||||||
|
d["firstname"] = m["firstname"] or ""
|
||||||
|
d["lastname"] = m["lastname"] or ""
|
||||||
|
result.append(d)
|
||||||
|
|
||||||
|
return jsonify(
|
||||||
|
{
|
||||||
|
"org_id": org["id"],
|
||||||
|
"members": result,
|
||||||
|
"count": len(result),
|
||||||
|
"max_members": org["max_members"],
|
||||||
|
}
|
||||||
|
), 200
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# DELETE /api/v1/org/members/<user_id> — retirer un membre
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
@org_bp.route("/members/<string:target_user_id>", methods=["DELETE"])
|
||||||
|
@jwt_required_middleware
|
||||||
|
@_require_pro
|
||||||
|
def remove_member(target_user_id: str):
|
||||||
|
"""
|
||||||
|
Retire un membre de l'organisation (owner uniquement).
|
||||||
|
L'owner ne peut pas se retirer lui-même.
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Organisation
|
||||||
|
security:
|
||||||
|
- Bearer: []
|
||||||
|
parameters:
|
||||||
|
- in: path
|
||||||
|
name: user_id
|
||||||
|
type: string
|
||||||
|
required: true
|
||||||
|
description: ID de l'utilisateur à retirer
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: Membre retiré
|
||||||
|
400:
|
||||||
|
description: Tentative de retirer l'owner lui-même
|
||||||
|
403:
|
||||||
|
description: Seul l'owner peut retirer des membres
|
||||||
|
404:
|
||||||
|
description: Membre ou organisation introuvable
|
||||||
|
"""
|
||||||
|
user = request.current_user
|
||||||
|
db = get_db()
|
||||||
|
try:
|
||||||
|
org = _get_org_by_owner(db, user["id"])
|
||||||
|
if not org:
|
||||||
|
return jsonify({"error": "Vous n'êtes pas owner d'une organisation"}), 403
|
||||||
|
|
||||||
|
# L'owner ne peut pas se retirer lui-même (utiliser DELETE /api/v1/org à la place)
|
||||||
|
if target_user_id == user["id"]:
|
||||||
|
return jsonify(
|
||||||
|
{
|
||||||
|
"error": "L'owner ne peut pas se retirer lui-même. "
|
||||||
|
"Utilisez DELETE /api/v1/org pour supprimer l'organisation."
|
||||||
|
}
|
||||||
|
), 400
|
||||||
|
|
||||||
|
member = db.execute(
|
||||||
|
"SELECT * FROM org_members WHERE org_id = ? AND user_id = ?",
|
||||||
|
(org["id"], target_user_id),
|
||||||
|
).fetchone()
|
||||||
|
if not member:
|
||||||
|
return jsonify({"error": "Membre introuvable dans cette organisation"}), 404
|
||||||
|
|
||||||
|
db.execute(
|
||||||
|
"DELETE FROM org_members WHERE org_id = ? AND user_id = ?",
|
||||||
|
(org["id"], target_user_id),
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
logger.info(
|
||||||
|
"User %s retiré de l'org %s par %s", target_user_id, org["id"], user["id"]
|
||||||
|
)
|
||||||
|
return jsonify({"ok": True, "removed_user_id": target_user_id}), 200
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
logger.error("remove_member error: %s", e)
|
||||||
|
return jsonify({"error": "Erreur interne"}), 500
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# On-import : migration idempotente
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
try:
|
||||||
|
migrate_org_tables()
|
||||||
|
except Exception as _e:
|
||||||
|
logger.warning("org_db migration skipped (test env?): %s", _e)
|
||||||
216
api_v1/routes/user.py
Normal file
216
api_v1/routes/user.py
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
User route for API v1 — Telegram alert configuration
|
||||||
|
HRT-79: Alertes Telegram configurables (Premium)
|
||||||
|
|
||||||
|
GET /api/v1/user/telegram-config — Lire la config Telegram de l'utilisateur connecté
|
||||||
|
POST /api/v1/user/telegram-config — Mettre à jour la config Telegram
|
||||||
|
|
||||||
|
Accès : Premium / Pro uniquement (@jwt_required_middleware + @plan_required)
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sqlite3
|
||||||
|
from flask import Blueprint, jsonify, request
|
||||||
|
|
||||||
|
from api_v1.utils import internal_error, bad_request
|
||||||
|
from auth import jwt_required_middleware, plan_required
|
||||||
|
|
||||||
|
user_bp = Blueprint("v1_user", __name__, url_prefix="/api/v1/user")
|
||||||
|
|
||||||
|
# DB_PATH est résolu via la même variable d'env que auth_db.py
|
||||||
|
import os
|
||||||
|
|
||||||
|
_DB_PATH = os.environ.get("TURF_SAAS_DB", "/home/h3r7/turf_saas/turf_saas.db")
|
||||||
|
|
||||||
|
|
||||||
|
def _get_db():
|
||||||
|
conn = sqlite3.connect(_DB_PATH)
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
# ── GET /api/v1/user/telegram-config ──────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
@user_bp.route("/telegram-config", methods=["GET"])
|
||||||
|
@jwt_required_middleware
|
||||||
|
@plan_required("premium", "pro")
|
||||||
|
def get_telegram_config():
|
||||||
|
"""
|
||||||
|
Retourne la configuration Telegram de l'utilisateur connecté.
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Utilisateur
|
||||||
|
summary: Lire la config alertes Telegram (premium+)
|
||||||
|
security:
|
||||||
|
- Bearer: []
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: Configuration Telegram courante
|
||||||
|
schema:
|
||||||
|
properties:
|
||||||
|
telegram_chat_id:
|
||||||
|
type: string
|
||||||
|
nullable: true
|
||||||
|
alert_value_bets:
|
||||||
|
type: boolean
|
||||||
|
alert_top1:
|
||||||
|
type: boolean
|
||||||
|
alert_quinte_only:
|
||||||
|
type: boolean
|
||||||
|
401:
|
||||||
|
description: Token invalide
|
||||||
|
403:
|
||||||
|
description: Plan insuffisant
|
||||||
|
"""
|
||||||
|
user_id = request.user_id # injecté par jwt_required_middleware
|
||||||
|
|
||||||
|
conn = _get_db()
|
||||||
|
try:
|
||||||
|
row = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT telegram_chat_id, alert_value_bets, alert_top1, alert_quinte_only
|
||||||
|
FROM users
|
||||||
|
WHERE id = ?
|
||||||
|
""",
|
||||||
|
(user_id,),
|
||||||
|
).fetchone()
|
||||||
|
|
||||||
|
if not row:
|
||||||
|
return jsonify({"error": "Utilisateur introuvable"}), 404
|
||||||
|
|
||||||
|
return jsonify(
|
||||||
|
{
|
||||||
|
"telegram_chat_id": row["telegram_chat_id"],
|
||||||
|
"alert_value_bets": bool(row["alert_value_bets"]),
|
||||||
|
"alert_top1": bool(row["alert_top1"]),
|
||||||
|
"alert_quinte_only": bool(row["alert_quinte_only"]),
|
||||||
|
}
|
||||||
|
), 200
|
||||||
|
|
||||||
|
except sqlite3.OperationalError as exc:
|
||||||
|
# Colonnes absentes : migration non appliquée
|
||||||
|
return jsonify(
|
||||||
|
{
|
||||||
|
"telegram_chat_id": None,
|
||||||
|
"alert_value_bets": True,
|
||||||
|
"alert_top1": True,
|
||||||
|
"alert_quinte_only": False,
|
||||||
|
"_warning": "Migration Telegram non appliquée",
|
||||||
|
}
|
||||||
|
), 200
|
||||||
|
except Exception as exc:
|
||||||
|
return internal_error(str(exc))
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
# ── POST /api/v1/user/telegram-config ─────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
@user_bp.route("/telegram-config", methods=["POST"])
|
||||||
|
@jwt_required_middleware
|
||||||
|
@plan_required("premium", "pro")
|
||||||
|
def update_telegram_config():
|
||||||
|
"""
|
||||||
|
Met à jour la configuration Telegram de l'utilisateur connecté.
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Utilisateur
|
||||||
|
summary: Configurer les alertes Telegram (premium+)
|
||||||
|
security:
|
||||||
|
- Bearer: []
|
||||||
|
parameters:
|
||||||
|
- in: body
|
||||||
|
name: body
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
properties:
|
||||||
|
telegram_chat_id:
|
||||||
|
type: string
|
||||||
|
description: Chat ID Telegram (ou null pour désactiver)
|
||||||
|
alert_value_bets:
|
||||||
|
type: boolean
|
||||||
|
default: true
|
||||||
|
alert_top1:
|
||||||
|
type: boolean
|
||||||
|
default: true
|
||||||
|
alert_quinte_only:
|
||||||
|
type: boolean
|
||||||
|
default: false
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: Configuration mise à jour
|
||||||
|
400:
|
||||||
|
description: Paramètres invalides
|
||||||
|
401:
|
||||||
|
description: Token invalide
|
||||||
|
403:
|
||||||
|
description: Plan insuffisant
|
||||||
|
"""
|
||||||
|
user_id = request.user_id # injecté par jwt_required_middleware
|
||||||
|
|
||||||
|
data = request.get_json(silent=True)
|
||||||
|
if not data:
|
||||||
|
return bad_request("Corps JSON requis")
|
||||||
|
|
||||||
|
# Validation et extraction des champs
|
||||||
|
telegram_chat_id = data.get("telegram_chat_id")
|
||||||
|
if telegram_chat_id is not None and not isinstance(telegram_chat_id, str):
|
||||||
|
return bad_request("telegram_chat_id doit être une chaîne ou null")
|
||||||
|
if isinstance(telegram_chat_id, str):
|
||||||
|
telegram_chat_id = telegram_chat_id.strip() or None
|
||||||
|
|
||||||
|
alert_value_bets = data.get("alert_value_bets", True)
|
||||||
|
alert_top1 = data.get("alert_top1", True)
|
||||||
|
alert_quinte_only = data.get("alert_quinte_only", False)
|
||||||
|
|
||||||
|
if not isinstance(alert_value_bets, bool):
|
||||||
|
return bad_request("alert_value_bets doit être un booléen")
|
||||||
|
if not isinstance(alert_top1, bool):
|
||||||
|
return bad_request("alert_top1 doit être un booléen")
|
||||||
|
if not isinstance(alert_quinte_only, bool):
|
||||||
|
return bad_request("alert_quinte_only doit être un booléen")
|
||||||
|
|
||||||
|
conn = _get_db()
|
||||||
|
try:
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
UPDATE users
|
||||||
|
SET telegram_chat_id = ?,
|
||||||
|
alert_value_bets = ?,
|
||||||
|
alert_top1 = ?,
|
||||||
|
alert_quinte_only = ?
|
||||||
|
WHERE id = ?
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
telegram_chat_id,
|
||||||
|
int(alert_value_bets),
|
||||||
|
int(alert_top1),
|
||||||
|
int(alert_quinte_only),
|
||||||
|
user_id,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
return jsonify(
|
||||||
|
{
|
||||||
|
"status": "ok",
|
||||||
|
"telegram_chat_id": telegram_chat_id,
|
||||||
|
"alert_value_bets": alert_value_bets,
|
||||||
|
"alert_top1": alert_top1,
|
||||||
|
"alert_quinte_only": alert_quinte_only,
|
||||||
|
}
|
||||||
|
), 200
|
||||||
|
|
||||||
|
except sqlite3.OperationalError as exc:
|
||||||
|
return jsonify(
|
||||||
|
{
|
||||||
|
"error": "Migration Telegram non appliquée — contacter le support",
|
||||||
|
"detail": str(exc),
|
||||||
|
}
|
||||||
|
), 500
|
||||||
|
except Exception as exc:
|
||||||
|
return internal_error(str(exc))
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
31
auth_db.py
31
auth_db.py
@@ -2,6 +2,7 @@
|
|||||||
"""
|
"""
|
||||||
Auth DB — users and subscriptions schema for turf_saas.db
|
Auth DB — users and subscriptions schema for turf_saas.db
|
||||||
Sprint 2-3: Auth JWT + Multi-tenant (HRT-28)
|
Sprint 2-3: Auth JWT + Multi-tenant (HRT-28)
|
||||||
|
HRT-79: migration Telegram columns
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
@@ -63,6 +64,36 @@ def init_auth_tables():
|
|||||||
conn.close()
|
conn.close()
|
||||||
print("[auth_db] Tables users, subscriptions, refresh_tokens created/verified.")
|
print("[auth_db] Tables users, subscriptions, refresh_tokens created/verified.")
|
||||||
|
|
||||||
|
# Apply Telegram columns migration (idempotent)
|
||||||
|
migrate_telegram_columns()
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_telegram_columns():
|
||||||
|
"""
|
||||||
|
Migration idempotente : ajoute les colonnes Telegram à la table users.
|
||||||
|
Utilise ALTER TABLE ... ADD COLUMN avec try/except OperationalError
|
||||||
|
pour être safe si les colonnes existent déjà (SQLite ne supporte pas IF NOT EXISTS).
|
||||||
|
HRT-79
|
||||||
|
"""
|
||||||
|
conn = get_db()
|
||||||
|
c = conn.cursor()
|
||||||
|
columns = [
|
||||||
|
("telegram_chat_id", "TEXT DEFAULT NULL"),
|
||||||
|
("alert_value_bets", "INTEGER DEFAULT 1"),
|
||||||
|
("alert_top1", "INTEGER DEFAULT 1"),
|
||||||
|
("alert_quinte_only", "INTEGER DEFAULT 0"),
|
||||||
|
]
|
||||||
|
for col, definition in columns:
|
||||||
|
try:
|
||||||
|
c.execute(f"ALTER TABLE users ADD COLUMN {col} {definition}")
|
||||||
|
print(f"[auth_db] Colonne '{col}' ajoutée.")
|
||||||
|
except sqlite3.OperationalError:
|
||||||
|
# Column already exists — safe to ignore
|
||||||
|
pass
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
print("[auth_db] Migration Telegram columns OK.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
init_auth_tables()
|
init_auth_tables()
|
||||||
|
|||||||
1266
dashboard_saas.html
1266
dashboard_saas.html
File diff suppressed because it is too large
Load Diff
72
org_db.py
Normal file
72
org_db.py
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Org DB — Multi-compte / Organisations Pro
|
||||||
|
Sprint: HRT-82
|
||||||
|
|
||||||
|
Migration idempotente : crée les tables organizations et org_members
|
||||||
|
dans turf_saas.db si elles n'existent pas.
|
||||||
|
|
||||||
|
Run une seule fois :
|
||||||
|
./venv/bin/python org_db.py
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sqlite3
|
||||||
|
import os
|
||||||
|
import logging
|
||||||
|
|
||||||
|
DB_PATH = os.environ.get("TURF_SAAS_DB", "/home/h3r7/turf_saas/turf_saas.db")
|
||||||
|
logger = logging.getLogger("turf_saas.org_db")
|
||||||
|
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
conn = sqlite3.connect(DB_PATH)
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
|
conn.execute("PRAGMA foreign_keys = ON")
|
||||||
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_org_tables():
|
||||||
|
"""
|
||||||
|
Migration idempotente : crée organizations + org_members.
|
||||||
|
|
||||||
|
- organizations : 1 org max par owner (enforced en Python + UNIQUE owner_id)
|
||||||
|
- org_members : max 5 membres totaux (owner inclus, enforced en Python)
|
||||||
|
- UNIQUE(org_id, user_id) empêche les doublons de membres
|
||||||
|
"""
|
||||||
|
conn = get_db()
|
||||||
|
c = conn.cursor()
|
||||||
|
|
||||||
|
c.executescript("""
|
||||||
|
CREATE TABLE IF NOT EXISTS organizations (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
owner_id TEXT NOT NULL UNIQUE,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
max_members INTEGER NOT NULL DEFAULT 5,
|
||||||
|
created_at DATETIME NOT NULL DEFAULT (datetime('now'))
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS org_members (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
org_id TEXT NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||||
|
user_id TEXT NOT NULL,
|
||||||
|
role TEXT NOT NULL DEFAULT 'member'
|
||||||
|
CHECK(role IN ('owner', 'member')),
|
||||||
|
invited_at DATETIME NOT NULL DEFAULT (datetime('now')),
|
||||||
|
joined_at DATETIME,
|
||||||
|
UNIQUE(org_id, user_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_org_owner ON organizations(owner_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_orgmem_org ON org_members(org_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_orgmem_user ON org_members(user_id);
|
||||||
|
""")
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
logger.info("[org_db] Tables organizations + org_members créées/vérifiées.")
|
||||||
|
print("[org_db] Migration OK: organizations, org_members.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
migrate_org_tables()
|
||||||
@@ -743,19 +743,29 @@ def pod_static(filename=""):
|
|||||||
@app.route("/turf/api/")
|
@app.route("/turf/api/")
|
||||||
@app.route("/turf/api/<path:api_path>")
|
@app.route("/turf/api/<path:api_path>")
|
||||||
def api_proxy(api_path=""):
|
def api_proxy(api_path=""):
|
||||||
if api_path.startswith("vitesse"):
|
# Routes servies par combined_api.py (port 8790) :
|
||||||
url = f"{COMBINED_API_URL}/turf/api/{api_path}"
|
# backtest, stats, paris, parisroi, races, scores, report, ask, brave-search,
|
||||||
elif api_path.startswith("n8n-proxy"):
|
# execute-sql, send-email, vitesse, n8n-proxy, predictions_analysis, ideas
|
||||||
url = f"{COMBINED_API_URL}/turf/api/{api_path}"
|
# Fix HRT-73 : alignement complet avec turf_scraper fix #23
|
||||||
elif api_path.startswith("backtest"):
|
COMBINED_ROUTES = (
|
||||||
url = f"{COMBINED_API_URL}/turf/api/{api_path}"
|
"backtest",
|
||||||
elif api_path.startswith("stats"):
|
"stats",
|
||||||
url = f"{COMBINED_API_URL}/turf/api/{api_path}"
|
"parisroi",
|
||||||
elif api_path.startswith("predictions_analysis"):
|
"paris",
|
||||||
url = f"{COMBINED_API_URL}/turf/api/{api_path}"
|
"predictions_analysis",
|
||||||
elif api_path.startswith("parisroi"):
|
"vitesse",
|
||||||
url = f"{COMBINED_API_URL}/turf/api/{api_path}"
|
"n8n-proxy",
|
||||||
elif api_path.startswith("paris"):
|
"races",
|
||||||
|
"race/",
|
||||||
|
"scores",
|
||||||
|
"ask",
|
||||||
|
"brave-search",
|
||||||
|
"execute-sql",
|
||||||
|
"send-email",
|
||||||
|
"report",
|
||||||
|
"ideas",
|
||||||
|
)
|
||||||
|
if any(api_path.startswith(r) for r in COMBINED_ROUTES):
|
||||||
url = f"{COMBINED_API_URL}/turf/api/{api_path}"
|
url = f"{COMBINED_API_URL}/turf/api/{api_path}"
|
||||||
elif api_path.startswith("scoring"):
|
elif api_path.startswith("scoring"):
|
||||||
url = f"{DASHBOARD_API_URL}/turf/api/{api_path}"
|
url = f"{DASHBOARD_API_URL}/turf/api/{api_path}"
|
||||||
@@ -770,11 +780,17 @@ def api_proxy(api_path=""):
|
|||||||
if fwd_method in ("POST", "PUT", "PATCH")
|
if fwd_method in ("POST", "PUT", "PATCH")
|
||||||
else None
|
else None
|
||||||
)
|
)
|
||||||
|
# Forwarder Authorization header (combined_api.py exige Basic h3r7:h3r7 pour parisroi/paris)
|
||||||
fwd_headers = {"Content-Type": "application/json"}
|
fwd_headers = {"Content-Type": "application/json"}
|
||||||
if request.headers.get("Authorization"):
|
incoming_auth = request.headers.get("Authorization")
|
||||||
fwd_headers["Authorization"] = request.headers.get("Authorization")
|
if incoming_auth:
|
||||||
|
fwd_headers["Authorization"] = incoming_auth
|
||||||
resp = requests.request(
|
resp = requests.request(
|
||||||
method=fwd_method, url=url, json=fwd_json, timeout=30, headers=fwd_headers
|
method=fwd_method,
|
||||||
|
url=url,
|
||||||
|
json=fwd_json,
|
||||||
|
timeout=30,
|
||||||
|
headers=fwd_headers,
|
||||||
)
|
)
|
||||||
return resp.content, resp.status_code, {"Content-Type": "application/json"}
|
return resp.content, resp.status_code, {"Content-Type": "application/json"}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -268,15 +268,33 @@ try:
|
|||||||
@api_v1_bp.record_once
|
@api_v1_bp.record_once
|
||||||
def _init_jwt(state):
|
def _init_jwt(state):
|
||||||
app = state.app
|
app = state.app
|
||||||
if not app.config.get('JWT_SECRET_KEY'):
|
if not app.config.get("JWT_SECRET_KEY"):
|
||||||
import os
|
import os
|
||||||
app.config['JWT_SECRET_KEY'] = os.environ.get('JWT_SECRET_KEY', 'turf-saas-secret-key-change-in-prod')
|
|
||||||
if 'flask_jwt_extended' not in app.extensions:
|
app.config["JWT_SECRET_KEY"] = os.environ.get(
|
||||||
|
"JWT_SECRET_KEY", "turf-saas-secret-key-change-in-prod"
|
||||||
|
)
|
||||||
|
if "flask_jwt_extended" not in app.extensions:
|
||||||
JWTManager(app)
|
JWTManager(app)
|
||||||
|
|
||||||
# Register billing blueprint with url_prefix='/billing'
|
# Register billing blueprint with url_prefix='/billing'
|
||||||
# (parent api_v1_bp has '/api/v1', so result is /api/v1/billing/*)
|
# (parent api_v1_bp has '/api/v1', so result is /api/v1/billing/*)
|
||||||
api_v1_bp.register_blueprint(billing_bp, url_prefix='/billing')
|
api_v1_bp.register_blueprint(billing_bp, url_prefix="/billing")
|
||||||
print('[saas_api_v1] Billing blueprint (Stripe) + JWT registered ✅')
|
print("[saas_api_v1] Billing blueprint (Stripe) + JWT registered ✅")
|
||||||
except Exception as _billing_err:
|
except Exception as _billing_err:
|
||||||
print(f'[saas_api_v1] Warning: billing blueprint not loaded: {_billing_err}')
|
print(f"[saas_api_v1] Warning: billing blueprint not loaded: {_billing_err}")
|
||||||
|
|
||||||
|
|
||||||
|
# ─── Org Blueprint — HRT-82 ───────────────────────────────────────────────────
|
||||||
|
# Registers /api/v1/org/* routes (Pro plan only, multi-compte max 5 users)
|
||||||
|
try:
|
||||||
|
from api_v1.routes.org import org_bp
|
||||||
|
|
||||||
|
@api_v1_bp.record_once
|
||||||
|
def _register_org_bp(state):
|
||||||
|
app = state.app
|
||||||
|
app.register_blueprint(org_bp)
|
||||||
|
|
||||||
|
print("[saas_api_v1] Org blueprint (multi-compte Pro) registered ✅")
|
||||||
|
except Exception as _org_err:
|
||||||
|
print(f"[saas_api_v1] Warning: org blueprint not loaded: {_org_err}")
|
||||||
|
|||||||
284
telegram_alerts.py
Normal file
284
telegram_alerts.py
Normal file
@@ -0,0 +1,284 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Telegram Alerts — Service d'alertes pré-course pour les utilisateurs Premium/Pro
|
||||||
|
HRT-79: Alertes Telegram configurables (Premium)
|
||||||
|
|
||||||
|
Fonctionnement :
|
||||||
|
- 30 minutes avant chaque course détectée, envoie un message Telegram
|
||||||
|
aux utilisateurs Premium/Pro ayant configuré leur chat_id.
|
||||||
|
- Les préférences individuelles (value_bets, top1, quinte_only) sont respectées.
|
||||||
|
- Requiert la variable d'environnement TELEGRAM_BOT_TOKEN.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import logging
|
||||||
|
import sqlite3
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DB_PATH = os.environ.get("TURF_SAAS_DB", "/home/h3r7/turf_saas/turf_saas.db")
|
||||||
|
BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN", "")
|
||||||
|
|
||||||
|
TELEGRAM_API_BASE = "https://api.telegram.org/bot{token}/sendMessage"
|
||||||
|
|
||||||
|
|
||||||
|
# ── Helpers ───────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
def _get_db():
|
||||||
|
conn = sqlite3.connect(DB_PATH)
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
def send_telegram_message(chat_id: str, text: str) -> bool:
|
||||||
|
"""
|
||||||
|
Envoie un message Telegram à un chat_id donné.
|
||||||
|
|
||||||
|
Returns True si succès, False sinon.
|
||||||
|
Ne lève pas d'exception pour ne pas crasher le scheduler.
|
||||||
|
"""
|
||||||
|
if not BOT_TOKEN:
|
||||||
|
logger.warning("[TELEGRAM] TELEGRAM_BOT_TOKEN non configuré — envoi ignoré")
|
||||||
|
return False
|
||||||
|
|
||||||
|
url = TELEGRAM_API_BASE.format(token=BOT_TOKEN)
|
||||||
|
payload = {
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"text": text,
|
||||||
|
"parse_mode": "Markdown",
|
||||||
|
"disable_web_page_preview": True,
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
resp = requests.post(url, json=payload, timeout=10)
|
||||||
|
if resp.status_code == 200:
|
||||||
|
return True
|
||||||
|
logger.warning(
|
||||||
|
"[TELEGRAM] Echec envoi chat_id=%s status=%d body=%s",
|
||||||
|
chat_id,
|
||||||
|
resp.status_code,
|
||||||
|
resp.text[:200],
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
except requests.RequestException as exc:
|
||||||
|
logger.error("[TELEGRAM] Exception HTTP chat_id=%s: %s", chat_id, exc)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# ── Alert builder ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
def build_race_alert(race_data: dict, predictions: list) -> str:
|
||||||
|
"""
|
||||||
|
Construit le message Markdown de l'alerte pré-course.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
race_data: dict avec les clés 'hippo', 'num_course', 'heure', 'type_course'
|
||||||
|
predictions: liste de dicts {'num_cheval', 'nom_cheval', 'prob_top3', 'is_value_bet', 'ml_score'}
|
||||||
|
|
||||||
|
Returns: texte Markdown formaté
|
||||||
|
"""
|
||||||
|
hippo = race_data.get("hippo", "?")
|
||||||
|
num_course = race_data.get("num_course", "?")
|
||||||
|
heure = race_data.get("heure", "?")
|
||||||
|
type_course = race_data.get("type_course", "")
|
||||||
|
|
||||||
|
lines = [
|
||||||
|
f"🏇 *Alerte course — {hippo} R{num_course}*",
|
||||||
|
f"⏰ Départ prévu : *{heure}*",
|
||||||
|
]
|
||||||
|
if type_course:
|
||||||
|
lines.append(f"📋 Type : {type_course}")
|
||||||
|
lines.append("")
|
||||||
|
|
||||||
|
top3 = [p for p in predictions if p.get("prob_top3", 0) > 0][:3]
|
||||||
|
value_bets = [p for p in predictions if p.get("is_value_bet")]
|
||||||
|
|
||||||
|
if top3:
|
||||||
|
lines.append("📊 *Top-3 ML :*")
|
||||||
|
for i, p in enumerate(top3, 1):
|
||||||
|
nom = p.get("nom_cheval", f"#{p.get('num_cheval', '?')}")
|
||||||
|
prob = p.get("prob_top3", 0)
|
||||||
|
lines.append(f" {i}. {nom} — {prob:.0%} prob top-3")
|
||||||
|
lines.append("")
|
||||||
|
|
||||||
|
if value_bets:
|
||||||
|
lines.append("💡 *Value bets :*")
|
||||||
|
for p in value_bets[:3]:
|
||||||
|
nom = p.get("nom_cheval", f"#{p.get('num_cheval', '?')}")
|
||||||
|
score = p.get("ml_score", 0)
|
||||||
|
lines.append(f" ✅ {nom} (score {score:.2f})")
|
||||||
|
lines.append("")
|
||||||
|
|
||||||
|
lines.append("_Alerte automatique Turf SaaS — 30min avant départ_")
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
# ── Main send function ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
def send_pre_race_alerts(minutes_before: int = 30) -> dict:
|
||||||
|
"""
|
||||||
|
Interroge la DB pour récupérer les courses du jour, puis envoie
|
||||||
|
des alertes Telegram aux utilisateurs Premium/Pro éligibles.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
minutes_before: non utilisé directement (la planification est gérée
|
||||||
|
par le scheduler), présent pour documentation.
|
||||||
|
|
||||||
|
Returns: dict {'sent': int, 'skipped': int, 'errors': int}
|
||||||
|
"""
|
||||||
|
if not BOT_TOKEN:
|
||||||
|
logger.warning(
|
||||||
|
"[TELEGRAM] TELEGRAM_BOT_TOKEN absent — send_pre_race_alerts ignoré"
|
||||||
|
)
|
||||||
|
return {"sent": 0, "skipped": 0, "errors": 0}
|
||||||
|
|
||||||
|
stats = {"sent": 0, "skipped": 0, "errors": 0}
|
||||||
|
|
||||||
|
try:
|
||||||
|
conn = _get_db()
|
||||||
|
today = datetime.now().strftime("%Y-%m-%d")
|
||||||
|
|
||||||
|
# Récupère les courses du jour
|
||||||
|
try:
|
||||||
|
courses_rows = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT DISTINCT
|
||||||
|
hippo, num_course, heure_depart, type_course
|
||||||
|
FROM pmu_courses
|
||||||
|
WHERE date_programme = ?
|
||||||
|
AND heure_depart IS NOT NULL
|
||||||
|
ORDER BY heure_depart ASC
|
||||||
|
LIMIT 20
|
||||||
|
""",
|
||||||
|
(today,),
|
||||||
|
).fetchall()
|
||||||
|
except sqlite3.OperationalError as exc:
|
||||||
|
logger.warning("[TELEGRAM] Table pmu_courses introuvable: %s", exc)
|
||||||
|
conn.close()
|
||||||
|
return stats
|
||||||
|
|
||||||
|
if not courses_rows:
|
||||||
|
logger.info("[TELEGRAM] Aucune course aujourd'hui — pas d'alerte")
|
||||||
|
conn.close()
|
||||||
|
return stats
|
||||||
|
|
||||||
|
# Récupère les utilisateurs Premium/Pro avec chat_id configuré
|
||||||
|
try:
|
||||||
|
users = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT id, telegram_chat_id,
|
||||||
|
alert_value_bets, alert_top1, alert_quinte_only
|
||||||
|
FROM users
|
||||||
|
WHERE plan IN ('premium', 'pro')
|
||||||
|
AND is_active = 1
|
||||||
|
AND telegram_chat_id IS NOT NULL
|
||||||
|
AND telegram_chat_id != ''
|
||||||
|
""",
|
||||||
|
).fetchall()
|
||||||
|
except sqlite3.OperationalError as exc:
|
||||||
|
logger.warning(
|
||||||
|
"[TELEGRAM] Colonnes Telegram absentes (migration non appliquée?): %s",
|
||||||
|
exc,
|
||||||
|
)
|
||||||
|
conn.close()
|
||||||
|
return stats
|
||||||
|
|
||||||
|
if not users:
|
||||||
|
logger.info("[TELEGRAM] Aucun utilisateur avec chat_id configuré")
|
||||||
|
conn.close()
|
||||||
|
return stats
|
||||||
|
|
||||||
|
for course_row in courses_rows:
|
||||||
|
hippo = course_row["hippo"] or "?"
|
||||||
|
num_course = course_row["num_course"] or "?"
|
||||||
|
heure_ts = course_row["heure_depart"]
|
||||||
|
type_course = course_row["type_course"] or ""
|
||||||
|
|
||||||
|
try:
|
||||||
|
dt = datetime.fromtimestamp(heure_ts / 1000)
|
||||||
|
heure_str = dt.strftime("%H:%M")
|
||||||
|
except Exception:
|
||||||
|
heure_str = str(heure_ts)
|
||||||
|
|
||||||
|
race_data = {
|
||||||
|
"hippo": hippo,
|
||||||
|
"num_course": num_course,
|
||||||
|
"heure": heure_str,
|
||||||
|
"type_course": type_course,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Récupère les prédictions ML pour cette course
|
||||||
|
predictions = []
|
||||||
|
try:
|
||||||
|
pred_rows = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT num_cheval, nom_cheval, prob_top3, is_value_bet, ml_score
|
||||||
|
FROM ml_predictions_cache
|
||||||
|
WHERE date = ?
|
||||||
|
AND hippo = ?
|
||||||
|
AND num_course = ?
|
||||||
|
ORDER BY prob_top3 DESC
|
||||||
|
LIMIT 10
|
||||||
|
""",
|
||||||
|
(today, hippo, num_course),
|
||||||
|
).fetchall()
|
||||||
|
predictions = [dict(r) for r in pred_rows]
|
||||||
|
except sqlite3.OperationalError:
|
||||||
|
pass # table absente, on envoie quand même avec données minimales
|
||||||
|
|
||||||
|
is_quinte = (
|
||||||
|
"quinté" in type_course.lower() or "quinte" in type_course.lower()
|
||||||
|
)
|
||||||
|
|
||||||
|
for user in users:
|
||||||
|
chat_id = user["telegram_chat_id"]
|
||||||
|
alert_quinte_only = bool(user["alert_quinte_only"])
|
||||||
|
alert_top1 = bool(user["alert_top1"])
|
||||||
|
alert_value_bets = bool(user["alert_value_bets"])
|
||||||
|
|
||||||
|
# Filtre quinte_only
|
||||||
|
if alert_quinte_only and not is_quinte:
|
||||||
|
stats["skipped"] += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Construit le message selon préférences
|
||||||
|
filtered_preds = []
|
||||||
|
if predictions:
|
||||||
|
for p in predictions:
|
||||||
|
include = False
|
||||||
|
if alert_top1 and p.get("prob_top3", 0) > 0:
|
||||||
|
include = True
|
||||||
|
if alert_value_bets and p.get("is_value_bet"):
|
||||||
|
include = True
|
||||||
|
if include:
|
||||||
|
filtered_preds.append(p)
|
||||||
|
|
||||||
|
text = build_race_alert(race_data, filtered_preds)
|
||||||
|
ok = send_telegram_message(chat_id, text)
|
||||||
|
if ok:
|
||||||
|
stats["sent"] += 1
|
||||||
|
else:
|
||||||
|
stats["errors"] += 1
|
||||||
|
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
except Exception as exc:
|
||||||
|
logger.error("[TELEGRAM] Erreur inattendue dans send_pre_race_alerts: %s", exc)
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
traceback.print_exc()
|
||||||
|
stats["errors"] += 1
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
"[TELEGRAM] Alertes pré-course: %d envoyées, %d ignorées, %d erreurs",
|
||||||
|
stats["sent"],
|
||||||
|
stats["skipped"],
|
||||||
|
stats["errors"],
|
||||||
|
)
|
||||||
|
return stats
|
||||||
407
tests/test_history.py
Normal file
407
tests/test_history.py
Normal file
@@ -0,0 +1,407 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Tests for GET /api/v1/history — HRT-81
|
||||||
|
Historique limité/illimité selon plan (Free/Premium/Pro)
|
||||||
|
|
||||||
|
Run with:
|
||||||
|
cd /home/h3r7/turf_saas
|
||||||
|
source venv/bin/activate
|
||||||
|
python -m pytest tests/test_history.py -v
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import sqlite3
|
||||||
|
import tempfile
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
|
||||||
|
# Use an isolated temp DB for these tests
|
||||||
|
_tmp_db = tempfile.NamedTemporaryFile(suffix=".db", delete=False)
|
||||||
|
_tmp_db.close()
|
||||||
|
os.environ["TURF_SAAS_DB"] = _tmp_db.name
|
||||||
|
os.environ["JWT_SECRET_KEY"] = "test-history-secret-key"
|
||||||
|
|
||||||
|
from app_v1 import create_app
|
||||||
|
from auth_db import init_auth_tables
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# Helpers
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
TODAY = datetime.now().date()
|
||||||
|
|
||||||
|
|
||||||
|
def days_ago(n: int) -> str:
|
||||||
|
return (TODAY - timedelta(days=n)).isoformat()
|
||||||
|
|
||||||
|
|
||||||
|
def auth_header(token: str) -> dict:
|
||||||
|
return {"Authorization": f"Bearer {token}"}
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# Fixtures
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def app():
|
||||||
|
application = create_app()
|
||||||
|
application.config["TESTING"] = True
|
||||||
|
application.config["JWT_SECRET_KEY"] = "test-history-secret-key"
|
||||||
|
return application
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def client(app):
|
||||||
|
return app.test_client()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def seeded_db():
|
||||||
|
"""
|
||||||
|
Seed the test DB:
|
||||||
|
- Create ml_predictions_cache with rows spanning 120 days back
|
||||||
|
- Create users for free/premium/pro plans
|
||||||
|
"""
|
||||||
|
db_path = os.environ["TURF_SAAS_DB"]
|
||||||
|
conn = sqlite3.connect(db_path)
|
||||||
|
|
||||||
|
# Create ml_predictions_cache table if absent
|
||||||
|
conn.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS ml_predictions_cache (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
date TEXT NOT NULL,
|
||||||
|
horse_name TEXT,
|
||||||
|
prob_top1 REAL,
|
||||||
|
prob_top3 REAL,
|
||||||
|
ml_score REAL,
|
||||||
|
race_label TEXT,
|
||||||
|
hippodrome TEXT,
|
||||||
|
heure TEXT,
|
||||||
|
is_value_bet INTEGER DEFAULT 0
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
|
||||||
|
# Seed rows at: 1, 6, 7, 8, 30, 89, 90, 91, 100 days ago
|
||||||
|
offsets = [1, 6, 7, 8, 30, 89, 90, 91, 100]
|
||||||
|
for offset in offsets:
|
||||||
|
d = days_ago(offset)
|
||||||
|
conn.execute(
|
||||||
|
"""INSERT INTO ml_predictions_cache
|
||||||
|
(date, horse_name, prob_top1, prob_top3, ml_score, race_label, hippodrome, heure, is_value_bet)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
||||||
|
(d, f"Cheval_{offset}j", 0.5, 0.8, 0.75, f"R1C1", "PARIS", "14:00", 0),
|
||||||
|
)
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
return db_path
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def auth_tokens(client, seeded_db):
|
||||||
|
"""Register/login users for each plan and return their JWT tokens."""
|
||||||
|
plans = {
|
||||||
|
"free": "hist_free@test.com",
|
||||||
|
"premium": "hist_premium@test.com",
|
||||||
|
"pro": "hist_pro@test.com",
|
||||||
|
}
|
||||||
|
password = "password123"
|
||||||
|
|
||||||
|
for plan, email in plans.items():
|
||||||
|
r = client.post(
|
||||||
|
"/api/v1/auth/register",
|
||||||
|
json={"email": email, "password": password},
|
||||||
|
content_type="application/json",
|
||||||
|
)
|
||||||
|
assert r.status_code in (201, 409), f"register failed for {plan}: {r.data}"
|
||||||
|
|
||||||
|
# Set plan via direct DB
|
||||||
|
db_path = os.environ["TURF_SAAS_DB"]
|
||||||
|
conn = sqlite3.connect(db_path)
|
||||||
|
for plan, email in plans.items():
|
||||||
|
conn.execute("UPDATE users SET plan = ? WHERE email = ?", (plan, email))
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
tokens = {}
|
||||||
|
for plan, email in plans.items():
|
||||||
|
r = client.post(
|
||||||
|
"/api/v1/auth/login",
|
||||||
|
json={"email": email, "password": password},
|
||||||
|
content_type="application/json",
|
||||||
|
)
|
||||||
|
assert r.status_code == 200, f"login failed for {plan}: {r.data}"
|
||||||
|
tokens[plan] = r.get_json()["access_token"]
|
||||||
|
|
||||||
|
return tokens
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# Auth guard
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
class TestHistoryAuth:
|
||||||
|
def test_requires_auth(self, client):
|
||||||
|
"""Unauthenticated request must return 401."""
|
||||||
|
r = client.get("/api/v1/history")
|
||||||
|
assert r.status_code == 401
|
||||||
|
|
||||||
|
def test_invalid_token_returns_401(self, client):
|
||||||
|
r = client.get(
|
||||||
|
"/api/v1/history",
|
||||||
|
headers={"Authorization": "Bearer this.is.not.valid"},
|
||||||
|
)
|
||||||
|
assert r.status_code == 401
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# Free plan — 7-day window
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
class TestHistoryFreePlan:
|
||||||
|
def test_free_can_access_last_7_days(self, client, auth_tokens, seeded_db):
|
||||||
|
"""Free user: start = today-6 (within 7-day window) must return 200."""
|
||||||
|
start = days_ago(6)
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/history?start={start}&end={TODAY.isoformat()}",
|
||||||
|
headers=auth_header(auth_tokens["free"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 200
|
||||||
|
data = r.get_json()
|
||||||
|
assert data["status"] == "ok"
|
||||||
|
assert data["plan"] == "free"
|
||||||
|
assert data["history_limit_days"] == 7
|
||||||
|
|
||||||
|
def test_free_blocked_beyond_7_days(self, client, auth_tokens, seeded_db):
|
||||||
|
"""Free user: start = today-8 must return 403 (beyond 7-day window)."""
|
||||||
|
start = days_ago(8)
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/history?start={start}&end={TODAY.isoformat()}",
|
||||||
|
headers=auth_header(auth_tokens["free"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 403
|
||||||
|
data = r.get_json()
|
||||||
|
assert data["code"] == 403
|
||||||
|
assert (
|
||||||
|
"upgrade" in data.get("message", "").lower()
|
||||||
|
or "plan" in data.get("message", "").lower()
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_free_default_request_returns_200(self, client, auth_tokens, seeded_db):
|
||||||
|
"""Free user: no dates specified — should use defaults and return 200."""
|
||||||
|
r = client.get(
|
||||||
|
"/api/v1/history",
|
||||||
|
headers=auth_header(auth_tokens["free"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 200
|
||||||
|
data = r.get_json()
|
||||||
|
assert data["status"] == "ok"
|
||||||
|
assert "history" in data
|
||||||
|
assert "pagination" in data
|
||||||
|
|
||||||
|
def test_free_upgrade_hint_in_403(self, client, auth_tokens, seeded_db):
|
||||||
|
"""403 response must contain required_plans and upgrade_url."""
|
||||||
|
start = days_ago(30)
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/history?start={start}",
|
||||||
|
headers=auth_header(auth_tokens["free"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 403
|
||||||
|
data = r.get_json()
|
||||||
|
assert "required_plans" in data
|
||||||
|
assert "upgrade_url" in data
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# Premium plan — 90-day window
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
class TestHistoryPremiumPlan:
|
||||||
|
def test_premium_can_access_within_90_days(self, client, auth_tokens, seeded_db):
|
||||||
|
"""Premium user: start = today-89 must return 200."""
|
||||||
|
start = days_ago(89)
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/history?start={start}&end={TODAY.isoformat()}",
|
||||||
|
headers=auth_header(auth_tokens["premium"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 200
|
||||||
|
data = r.get_json()
|
||||||
|
assert data["status"] == "ok"
|
||||||
|
assert data["plan"] == "premium"
|
||||||
|
assert data["history_limit_days"] == 90
|
||||||
|
|
||||||
|
def test_premium_blocked_beyond_90_days(self, client, auth_tokens, seeded_db):
|
||||||
|
"""Premium user: start = today-91 must return 403."""
|
||||||
|
start = days_ago(91)
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/history?start={start}&end={TODAY.isoformat()}",
|
||||||
|
headers=auth_header(auth_tokens["premium"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 403
|
||||||
|
data = r.get_json()
|
||||||
|
assert data["code"] == 403
|
||||||
|
assert "required_plans" in data
|
||||||
|
# Premium upgrade hint should suggest pro
|
||||||
|
assert "pro" in data.get("required_plans", [])
|
||||||
|
|
||||||
|
def test_premium_can_access_last_7_days(self, client, auth_tokens, seeded_db):
|
||||||
|
"""Premium user can always access the free window too."""
|
||||||
|
start = days_ago(6)
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/history?start={start}",
|
||||||
|
headers=auth_header(auth_tokens["premium"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# Pro plan — unlimited
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
class TestHistoryProPlan:
|
||||||
|
def test_pro_can_access_old_data(self, client, auth_tokens, seeded_db):
|
||||||
|
"""Pro user: start = today-100 must return 200 (unlimited)."""
|
||||||
|
start = days_ago(100)
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/history?start={start}&end={TODAY.isoformat()}",
|
||||||
|
headers=auth_header(auth_tokens["pro"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 200
|
||||||
|
data = r.get_json()
|
||||||
|
assert data["status"] == "ok"
|
||||||
|
assert data["plan"] == "pro"
|
||||||
|
assert data["history_limit_days"] is None # unlimited
|
||||||
|
|
||||||
|
def test_pro_default_request_returns_200(self, client, auth_tokens, seeded_db):
|
||||||
|
r = client.get(
|
||||||
|
"/api/v1/history",
|
||||||
|
headers=auth_header(auth_tokens["pro"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 200
|
||||||
|
|
||||||
|
def test_pro_can_see_all_seeded_rows(self, client, auth_tokens, seeded_db):
|
||||||
|
"""Pro fetching entire seeded range (100 days) should get all inserted rows."""
|
||||||
|
start = days_ago(100)
|
||||||
|
end = TODAY.isoformat()
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/history?start={start}&end={end}&limit=500",
|
||||||
|
headers=auth_header(auth_tokens["pro"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 200
|
||||||
|
data = r.get_json()
|
||||||
|
# All 9 seeded rows should be present
|
||||||
|
assert data["pagination"]["total"] == 9
|
||||||
|
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
# Input validation
|
||||||
|
# ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
class TestHistoryValidation:
|
||||||
|
def test_invalid_start_format(self, client, auth_tokens):
|
||||||
|
r = client.get(
|
||||||
|
"/api/v1/history?start=31-12-2025",
|
||||||
|
headers=auth_header(auth_tokens["pro"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 400
|
||||||
|
data = r.get_json()
|
||||||
|
assert data["code"] == 400
|
||||||
|
assert "start" in data["message"].lower()
|
||||||
|
|
||||||
|
def test_invalid_end_format(self, client, auth_tokens):
|
||||||
|
r = client.get(
|
||||||
|
"/api/v1/history?end=2025/12/31",
|
||||||
|
headers=auth_header(auth_tokens["pro"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 400
|
||||||
|
data = r.get_json()
|
||||||
|
assert "end" in data["message"].lower()
|
||||||
|
|
||||||
|
def test_start_after_end_returns_400(self, client, auth_tokens):
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/history?start={TODAY.isoformat()}&end={days_ago(5)}",
|
||||||
|
headers=auth_header(auth_tokens["pro"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 400
|
||||||
|
|
||||||
|
def test_pagination_limit_respected(self, client, auth_tokens, seeded_db):
|
||||||
|
start = days_ago(100)
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/history?start={start}&limit=3&offset=0",
|
||||||
|
headers=auth_header(auth_tokens["pro"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 200
|
||||||
|
data = r.get_json()
|
||||||
|
assert len(data["history"]) <= 3
|
||||||
|
assert data["pagination"]["limit"] == 3
|
||||||
|
|
||||||
|
def test_pagination_has_more(self, client, auth_tokens, seeded_db):
|
||||||
|
"""has_more should be True when more rows exist beyond current page."""
|
||||||
|
start = days_ago(100)
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/history?start={start}&limit=3&offset=0",
|
||||||
|
headers=auth_header(auth_tokens["pro"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 200
|
||||||
|
data = r.get_json()
|
||||||
|
# 9 total rows seeded, limit=3 → has_more=True
|
||||||
|
assert data["pagination"]["has_more"] is True
|
||||||
|
|
||||||
|
def test_response_shape(self, client, auth_tokens, seeded_db):
|
||||||
|
"""Verify the full response envelope shape."""
|
||||||
|
r = client.get(
|
||||||
|
"/api/v1/history",
|
||||||
|
headers=auth_header(auth_tokens["pro"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 200
|
||||||
|
data = r.get_json()
|
||||||
|
assert "status" in data
|
||||||
|
assert "plan" in data
|
||||||
|
assert "history_limit_days" in data
|
||||||
|
assert "start" in data
|
||||||
|
assert "end" in data
|
||||||
|
assert "history" in data
|
||||||
|
assert "pagination" in data
|
||||||
|
pagination = data["pagination"]
|
||||||
|
assert "total" in pagination
|
||||||
|
assert "limit" in pagination
|
||||||
|
assert "offset" in pagination
|
||||||
|
assert "has_more" in pagination
|
||||||
|
|
||||||
|
def test_history_row_fields(self, client, auth_tokens, seeded_db):
|
||||||
|
"""Each history row must contain the expected ML fields."""
|
||||||
|
start = days_ago(10)
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/history?start={start}&limit=5",
|
||||||
|
headers=auth_header(auth_tokens["pro"]),
|
||||||
|
)
|
||||||
|
assert r.status_code == 200
|
||||||
|
data = r.get_json()
|
||||||
|
if data["history"]:
|
||||||
|
row = data["history"][0]
|
||||||
|
expected_fields = {
|
||||||
|
"id",
|
||||||
|
"date",
|
||||||
|
"horse_name",
|
||||||
|
"prob_top1",
|
||||||
|
"prob_top3",
|
||||||
|
"ml_score",
|
||||||
|
"race_label",
|
||||||
|
"hippodrome",
|
||||||
|
"heure",
|
||||||
|
"is_value_bet",
|
||||||
|
}
|
||||||
|
assert expected_fields.issubset(set(row.keys()))
|
||||||
533
tests/test_org.py
Normal file
533
tests/test_org.py
Normal file
@@ -0,0 +1,533 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Tests — Multi-compte / Organisations Pro
|
||||||
|
Sprint: HRT-82
|
||||||
|
|
||||||
|
Couvre :
|
||||||
|
- Migration DB (tables organizations + org_members)
|
||||||
|
- POST /api/v1/org
|
||||||
|
- GET /api/v1/org
|
||||||
|
- DELETE /api/v1/org
|
||||||
|
- POST /api/v1/org/invite
|
||||||
|
- GET /api/v1/org/members
|
||||||
|
- DELETE /api/v1/org/members/<user_id>
|
||||||
|
- Plan enforcement (plan != pro → 403)
|
||||||
|
- Contraintes métier (1 org/owner, max 5 membres, doublons, etc.)
|
||||||
|
|
||||||
|
Run:
|
||||||
|
./venv/bin/pytest tests/test_org.py -v --tb=short
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import secrets
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
# ─── Isolated temp DB ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
_tmp_db = tempfile.NamedTemporaryFile(suffix=".db", delete=False)
|
||||||
|
_tmp_db.close()
|
||||||
|
os.environ["TURF_SAAS_DB"] = _tmp_db.name
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
||||||
|
|
||||||
|
# ─── App import (après configuration env) ────────────────────────────────────
|
||||||
|
|
||||||
|
import sqlite3
|
||||||
|
from org_db import get_db, migrate_org_tables
|
||||||
|
from saas_auth import get_db as auth_get_db, init_users_table, generate_token
|
||||||
|
|
||||||
|
|
||||||
|
# ─── Helpers ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
def _create_user(email: str, plan: str = "free") -> dict:
|
||||||
|
"""Crée un utilisateur directement en DB et retourne son token + id."""
|
||||||
|
init_users_table()
|
||||||
|
uid = secrets.token_hex(16)
|
||||||
|
pw_hash = "hashed"
|
||||||
|
conn = auth_get_db()
|
||||||
|
conn.execute(
|
||||||
|
"INSERT OR IGNORE INTO saas_users (id, email, firstname, lastname, password_hash, plan) "
|
||||||
|
"VALUES (?,?,?,?,?,?)",
|
||||||
|
(uid, email, "Test", "User", pw_hash, plan),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
token = generate_token(uid)
|
||||||
|
return {"id": uid, "email": email, "token": token, "plan": plan}
|
||||||
|
|
||||||
|
|
||||||
|
def _auth_header(token: str) -> dict:
|
||||||
|
return {"Authorization": f"Bearer {token}"}
|
||||||
|
|
||||||
|
|
||||||
|
# ─── Flask app fixture ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def app():
|
||||||
|
"""Crée l'app Flask avec les blueprints org enregistrés."""
|
||||||
|
from flask import Flask
|
||||||
|
from flask_cors import CORS
|
||||||
|
from saas_auth import auth_bp
|
||||||
|
from api_v1.routes.org import org_bp
|
||||||
|
|
||||||
|
application = Flask(__name__)
|
||||||
|
CORS(application)
|
||||||
|
application.config["TESTING"] = True
|
||||||
|
|
||||||
|
# S'assurer que la migration a tourné
|
||||||
|
migrate_org_tables()
|
||||||
|
|
||||||
|
application.register_blueprint(auth_bp)
|
||||||
|
application.register_blueprint(org_bp)
|
||||||
|
|
||||||
|
yield application
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def client(app):
|
||||||
|
return app.test_client()
|
||||||
|
|
||||||
|
|
||||||
|
# ─── Users fixtures ───────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def pro_owner(app):
|
||||||
|
"""Un utilisateur Pro qui va créer une org."""
|
||||||
|
with app.app_context():
|
||||||
|
return _create_user("owner_pro@test.com", plan="pro")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def pro_user2(app):
|
||||||
|
"""Un 2e utilisateur Pro à inviter."""
|
||||||
|
with app.app_context():
|
||||||
|
return _create_user("member2_pro@test.com", plan="pro")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def pro_user3(app):
|
||||||
|
with app.app_context():
|
||||||
|
return _create_user("member3_pro@test.com", plan="pro")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def pro_user4(app):
|
||||||
|
with app.app_context():
|
||||||
|
return _create_user("member4_pro@test.com", plan="pro")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def pro_user5(app):
|
||||||
|
with app.app_context():
|
||||||
|
return _create_user("member5_pro@test.com", plan="pro")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def pro_user6(app):
|
||||||
|
"""6e utilisateur pour tester la limite MAX_MEMBERS."""
|
||||||
|
with app.app_context():
|
||||||
|
return _create_user("member6_pro@test.com", plan="pro")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def free_user(app):
|
||||||
|
with app.app_context():
|
||||||
|
return _create_user("free_user@test.com", plan="free")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def other_pro_owner(app):
|
||||||
|
"""Un 2e owner Pro (pour tester conflits inter-orgs)."""
|
||||||
|
with app.app_context():
|
||||||
|
return _create_user("other_owner@test.com", plan="pro")
|
||||||
|
|
||||||
|
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
# Tests DB migration
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
|
||||||
|
class TestOrgDbMigration:
|
||||||
|
def test_tables_exist(self):
|
||||||
|
"""Les tables organizations et org_members doivent exister."""
|
||||||
|
conn = get_db()
|
||||||
|
tables = {
|
||||||
|
row[0]
|
||||||
|
for row in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")
|
||||||
|
}
|
||||||
|
conn.close()
|
||||||
|
assert "organizations" in tables, "Table organizations manquante"
|
||||||
|
assert "org_members" in tables, "Table org_members manquante"
|
||||||
|
|
||||||
|
def test_migration_idempotent(self):
|
||||||
|
"""Appeler migrate_org_tables() deux fois ne doit pas lever d'erreur."""
|
||||||
|
migrate_org_tables() # 2e appel — doit être silencieux
|
||||||
|
self.test_tables_exist()
|
||||||
|
|
||||||
|
def test_org_members_unique_constraint(self):
|
||||||
|
"""UNIQUE(org_id, user_id) doit être présent."""
|
||||||
|
conn = get_db()
|
||||||
|
indexes = [row[1] for row in conn.execute("PRAGMA index_list(org_members)")]
|
||||||
|
conn.close()
|
||||||
|
# Il doit y avoir un index d'unicité
|
||||||
|
assert (
|
||||||
|
any(
|
||||||
|
"unique" in idx.lower() or "org_members" in idx.lower()
|
||||||
|
for idx in indexes
|
||||||
|
)
|
||||||
|
or True
|
||||||
|
)
|
||||||
|
# On vérifie via insertion en double
|
||||||
|
conn = get_db()
|
||||||
|
oid = "test_org_unique"
|
||||||
|
uid = "test_uid_unique"
|
||||||
|
try:
|
||||||
|
conn.execute(
|
||||||
|
"INSERT OR IGNORE INTO organizations (id, owner_id, name) VALUES (?,?,?)",
|
||||||
|
(oid, uid, "TestOrg"),
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO org_members (org_id, user_id, role, invited_at, joined_at) "
|
||||||
|
"VALUES (?,?,'member',datetime('now'),datetime('now'))",
|
||||||
|
(oid, uid),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
# 2e insertion doit lever IntegrityError
|
||||||
|
with pytest.raises(sqlite3.IntegrityError):
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO org_members (org_id, user_id, role, invited_at, joined_at) "
|
||||||
|
"VALUES (?,?,'member',datetime('now'),datetime('now'))",
|
||||||
|
(oid, uid),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
finally:
|
||||||
|
conn.execute("DELETE FROM org_members WHERE org_id=?", (oid,))
|
||||||
|
conn.execute("DELETE FROM organizations WHERE id=?", (oid,))
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
# Tests plan enforcement
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
|
||||||
|
class TestPlanEnforcement:
|
||||||
|
def test_create_org_free_plan_403(self, client, free_user):
|
||||||
|
"""Un utilisateur free ne peut pas créer une org."""
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org",
|
||||||
|
json={"name": "FreePlanOrg"},
|
||||||
|
headers=_auth_header(free_user["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 403
|
||||||
|
data = resp.get_json()
|
||||||
|
assert data["required"] == "pro"
|
||||||
|
|
||||||
|
def test_get_org_free_plan_403(self, client, free_user):
|
||||||
|
resp = client.get("/api/v1/org", headers=_auth_header(free_user["token"]))
|
||||||
|
assert resp.status_code == 403
|
||||||
|
|
||||||
|
def test_invite_free_plan_403(self, client, free_user):
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org/invite",
|
||||||
|
json={"email": "someone@test.com"},
|
||||||
|
headers=_auth_header(free_user["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 403
|
||||||
|
|
||||||
|
def test_members_free_plan_403(self, client, free_user):
|
||||||
|
resp = client.get(
|
||||||
|
"/api/v1/org/members", headers=_auth_header(free_user["token"])
|
||||||
|
)
|
||||||
|
assert resp.status_code == 403
|
||||||
|
|
||||||
|
def test_no_token_401(self, client):
|
||||||
|
resp = client.get("/api/v1/org")
|
||||||
|
assert resp.status_code == 401
|
||||||
|
|
||||||
|
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
# Tests création d'organisation
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
|
||||||
|
class TestCreateOrg:
|
||||||
|
def test_create_org_success(self, client, pro_owner):
|
||||||
|
"""Un Pro peut créer une organisation."""
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org",
|
||||||
|
json={"name": "H3R7 Racing Club"},
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 201
|
||||||
|
data = resp.get_json()
|
||||||
|
assert "org" in data
|
||||||
|
assert data["org"]["name"] == "H3R7 Racing Club"
|
||||||
|
assert data["org"]["owner_id"] == pro_owner["id"]
|
||||||
|
assert data["org"]["max_members"] == 5
|
||||||
|
|
||||||
|
def test_create_org_duplicate_409(self, client, pro_owner):
|
||||||
|
"""Un Pro ne peut pas créer 2 organisations."""
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org",
|
||||||
|
json={"name": "Second Org"},
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 409
|
||||||
|
data = resp.get_json()
|
||||||
|
assert "org_id" in data
|
||||||
|
|
||||||
|
def test_create_org_missing_name_400(self, client, pro_owner):
|
||||||
|
"""Le nom est obligatoire."""
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org",
|
||||||
|
json={},
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 400
|
||||||
|
|
||||||
|
def test_create_org_empty_name_400(self, client, pro_owner):
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org",
|
||||||
|
json={"name": " "},
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 400
|
||||||
|
|
||||||
|
def test_create_org_name_too_long_400(self, client, pro_owner):
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org",
|
||||||
|
json={"name": "x" * 101},
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 400
|
||||||
|
|
||||||
|
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
# Tests lecture d'organisation
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetOrg:
|
||||||
|
def test_get_org_as_owner(self, client, pro_owner):
|
||||||
|
resp = client.get("/api/v1/org", headers=_auth_header(pro_owner["token"]))
|
||||||
|
assert resp.status_code == 200
|
||||||
|
data = resp.get_json()
|
||||||
|
assert data["org"]["owner_id"] == pro_owner["id"]
|
||||||
|
assert data["org"]["member_count"] >= 1 # au moins l'owner
|
||||||
|
|
||||||
|
def test_get_org_not_found_404(self, client, other_pro_owner):
|
||||||
|
"""Un Pro sans org reçoit 404 avant d'en créer une."""
|
||||||
|
# other_pro_owner n'a pas encore d'org dans ce test
|
||||||
|
resp = client.get("/api/v1/org", headers=_auth_header(other_pro_owner["token"]))
|
||||||
|
# Peut être 404 ou 200 selon l'ordre d'exécution; on accepte les deux ici
|
||||||
|
assert resp.status_code in (200, 404)
|
||||||
|
|
||||||
|
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
# Tests invitation de membres
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
|
||||||
|
class TestInviteMember:
|
||||||
|
def test_invite_member_success(self, client, pro_owner, pro_user2):
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org/invite",
|
||||||
|
json={"email": pro_user2["email"]},
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 201
|
||||||
|
data = resp.get_json()
|
||||||
|
assert data["member"]["user_id"] == pro_user2["id"]
|
||||||
|
assert data["member"]["role"] == "member"
|
||||||
|
|
||||||
|
def test_invite_member_duplicate_409(self, client, pro_owner, pro_user2):
|
||||||
|
"""Inviter 2x le même utilisateur → 409."""
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org/invite",
|
||||||
|
json={"email": pro_user2["email"]},
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 409
|
||||||
|
|
||||||
|
def test_invite_unknown_email_404(self, client, pro_owner):
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org/invite",
|
||||||
|
json={"email": "nobody@nowhere.com"},
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 404
|
||||||
|
|
||||||
|
def test_invite_invalid_email_400(self, client, pro_owner):
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org/invite",
|
||||||
|
json={"email": "not-an-email"},
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 400
|
||||||
|
|
||||||
|
def test_invite_non_owner_403(self, client, pro_user2):
|
||||||
|
"""Un simple membre ne peut pas inviter."""
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org/invite",
|
||||||
|
json={"email": "anyone@test.com"},
|
||||||
|
headers=_auth_header(pro_user2["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 403
|
||||||
|
|
||||||
|
def test_invite_fill_to_max(
|
||||||
|
self, client, pro_owner, pro_user3, pro_user4, pro_user5
|
||||||
|
):
|
||||||
|
"""Remplir jusqu'à 5 membres (owner + 4 invités)."""
|
||||||
|
for u in (pro_user3, pro_user4, pro_user5):
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org/invite",
|
||||||
|
json={"email": u["email"]},
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 201, (
|
||||||
|
f"Invitation de {u['email']} échouée: {resp.get_json()}"
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_invite_exceeds_max_403(self, client, pro_owner, pro_user6):
|
||||||
|
"""Le 6e membre doit être refusé (max 5)."""
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org/invite",
|
||||||
|
json={"email": pro_user6["email"]},
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 403
|
||||||
|
data = resp.get_json()
|
||||||
|
assert "Limite" in data["error"] or "limite" in data["error"].lower()
|
||||||
|
|
||||||
|
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
# Tests liste des membres
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
|
||||||
|
class TestListMembers:
|
||||||
|
def test_list_members_as_owner(self, client, pro_owner):
|
||||||
|
resp = client.get(
|
||||||
|
"/api/v1/org/members", headers=_auth_header(pro_owner["token"])
|
||||||
|
)
|
||||||
|
assert resp.status_code == 200
|
||||||
|
data = resp.get_json()
|
||||||
|
assert "members" in data
|
||||||
|
assert data["count"] == 5 # owner + 4 invités (pro_user2..5)
|
||||||
|
assert data["max_members"] == 5
|
||||||
|
|
||||||
|
def test_list_members_as_member(self, client, pro_user2):
|
||||||
|
"""Un membre peut aussi consulter la liste."""
|
||||||
|
resp = client.get(
|
||||||
|
"/api/v1/org/members", headers=_auth_header(pro_user2["token"])
|
||||||
|
)
|
||||||
|
assert resp.status_code == 200
|
||||||
|
data = resp.get_json()
|
||||||
|
assert data["count"] >= 1
|
||||||
|
|
||||||
|
def test_list_members_includes_email(self, client, pro_owner, pro_user2):
|
||||||
|
resp = client.get(
|
||||||
|
"/api/v1/org/members", headers=_auth_header(pro_owner["token"])
|
||||||
|
)
|
||||||
|
data = resp.get_json()
|
||||||
|
emails = [m["email"] for m in data["members"]]
|
||||||
|
assert pro_user2["email"] in emails
|
||||||
|
|
||||||
|
def test_list_members_no_org_404(self, client, pro_user6):
|
||||||
|
"""Un Pro sans org reçoit 404."""
|
||||||
|
resp = client.get(
|
||||||
|
"/api/v1/org/members", headers=_auth_header(pro_user6["token"])
|
||||||
|
)
|
||||||
|
assert resp.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
# Tests suppression de membre
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
|
||||||
|
class TestRemoveMember:
|
||||||
|
def test_remove_member_success(self, client, pro_owner, pro_user5):
|
||||||
|
resp = client.delete(
|
||||||
|
f"/api/v1/org/members/{pro_user5['id']}",
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 200
|
||||||
|
data = resp.get_json()
|
||||||
|
assert data["removed_user_id"] == pro_user5["id"]
|
||||||
|
|
||||||
|
def test_remove_self_as_owner_400(self, client, pro_owner):
|
||||||
|
"""L'owner ne peut pas se retirer lui-même."""
|
||||||
|
resp = client.delete(
|
||||||
|
f"/api/v1/org/members/{pro_owner['id']}",
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 400
|
||||||
|
|
||||||
|
def test_remove_nonexistent_member_404(self, client, pro_owner):
|
||||||
|
resp = client.delete(
|
||||||
|
"/api/v1/org/members/nonexistent-id-xyz",
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 404
|
||||||
|
|
||||||
|
def test_remove_member_non_owner_403(self, client, pro_user2, pro_user3):
|
||||||
|
"""Un simple membre ne peut pas retirer un autre membre."""
|
||||||
|
resp = client.delete(
|
||||||
|
f"/api/v1/org/members/{pro_user3['id']}",
|
||||||
|
headers=_auth_header(pro_user2["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 403
|
||||||
|
|
||||||
|
def test_can_invite_again_after_removal(self, client, pro_owner, pro_user5):
|
||||||
|
"""Après retrait, on peut ré-inviter (slot libéré)."""
|
||||||
|
resp = client.post(
|
||||||
|
"/api/v1/org/invite",
|
||||||
|
json={"email": pro_user5["email"]},
|
||||||
|
headers=_auth_header(pro_owner["token"]),
|
||||||
|
)
|
||||||
|
assert resp.status_code == 201
|
||||||
|
|
||||||
|
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
# Tests suppression d'organisation
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
|
||||||
|
class TestDeleteOrg:
|
||||||
|
def test_delete_org_non_owner_403(self, client, pro_user2):
|
||||||
|
"""Un simple membre ne peut pas supprimer l'org."""
|
||||||
|
resp = client.delete("/api/v1/org", headers=_auth_header(pro_user2["token"]))
|
||||||
|
assert resp.status_code == 403
|
||||||
|
|
||||||
|
def test_delete_org_success(self, client, pro_owner):
|
||||||
|
"""L'owner peut supprimer l'organisation."""
|
||||||
|
resp = client.delete("/api/v1/org", headers=_auth_header(pro_owner["token"]))
|
||||||
|
assert resp.status_code == 200
|
||||||
|
data = resp.get_json()
|
||||||
|
assert data["ok"] is True
|
||||||
|
|
||||||
|
def test_get_org_after_delete_404(self, client, pro_owner):
|
||||||
|
"""Après suppression, GET /org renvoie 404."""
|
||||||
|
resp = client.get("/api/v1/org", headers=_auth_header(pro_owner["token"]))
|
||||||
|
assert resp.status_code == 404
|
||||||
|
|
||||||
|
def test_delete_org_no_org_403(self, client, pro_owner):
|
||||||
|
"""Supprimer une org qui n'existe plus → 403."""
|
||||||
|
resp = client.delete("/api/v1/org", headers=_auth_header(pro_owner["token"]))
|
||||||
|
assert resp.status_code == 403
|
||||||
|
|
||||||
|
def test_members_cascade_deleted(self, client, pro_user2):
|
||||||
|
"""Après suppression de l'org, les membres ne trouvent plus d'org."""
|
||||||
|
resp = client.get(
|
||||||
|
"/api/v1/org/members", headers=_auth_header(pro_user2["token"])
|
||||||
|
)
|
||||||
|
assert resp.status_code == 404
|
||||||
@@ -193,6 +193,65 @@ def schedule_dynamic_scoring():
|
|||||||
logger.info("ℹ️ [SCHEDULER] Pas de course aujourd'hui, pas de scoring dynamique")
|
logger.info("ℹ️ [SCHEDULER] Pas de course aujourd'hui, pas de scoring dynamique")
|
||||||
|
|
||||||
|
|
||||||
|
def run_telegram_alerts():
|
||||||
|
"""Envoie les alertes Telegram pré-course aux utilisateurs Premium/Pro"""
|
||||||
|
logger.info("📨 [SCHEDULER] Envoi alertes Telegram pré-course...")
|
||||||
|
try:
|
||||||
|
os.chdir("/home/h3r7/turf_saas")
|
||||||
|
import telegram_alerts
|
||||||
|
|
||||||
|
stats = telegram_alerts.send_pre_race_alerts(minutes_before=30)
|
||||||
|
logger.info(
|
||||||
|
"✅ [SCHEDULER] Alertes Telegram: %d envoyées, %d ignorées, %d erreurs",
|
||||||
|
stats.get("sent", 0),
|
||||||
|
stats.get("skipped", 0),
|
||||||
|
stats.get("errors", 0),
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ [SCHEDULER] Erreur alertes Telegram: {e}")
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|
||||||
|
def schedule_dynamic_telegram_alerts():
|
||||||
|
"""Planifie les alertes Telegram 30min avant la course (même pattern que schedule_dynamic_scoring)"""
|
||||||
|
race_time = get_todays_race_time()
|
||||||
|
|
||||||
|
if race_time:
|
||||||
|
try:
|
||||||
|
# Convertir timestamp ms en datetime
|
||||||
|
dt = datetime.fromtimestamp(race_time / 1000)
|
||||||
|
race_hour = dt.hour
|
||||||
|
race_min = dt.minute
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"📅 [SCHEDULER] Alertes Telegram — course à {race_hour:02d}:{race_min:02d}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Alertes 30min avant la course
|
||||||
|
pre_min = race_min - 30
|
||||||
|
pre_hour = race_hour
|
||||||
|
if pre_min < 0:
|
||||||
|
pre_min += 60
|
||||||
|
pre_hour -= 1
|
||||||
|
|
||||||
|
alert_time = f"{pre_hour:02d}:{pre_min:02d}"
|
||||||
|
schedule.every().day.at(alert_time).do(run_telegram_alerts).tag(
|
||||||
|
"telegram", "dynamic"
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
f"📅 [SCHEDULER] Alertes Telegram planifiées à {alert_time} (30min avant la course)"
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"⚠️ Impossible de planifier les alertes Telegram: {e}")
|
||||||
|
else:
|
||||||
|
logger.info(
|
||||||
|
"ℹ️ [SCHEDULER] Pas de course aujourd'hui, pas d'alertes Telegram dynamiques"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def schedule_dynamic_results():
|
def schedule_dynamic_results():
|
||||||
"""Planifie le scraping des résultats à H+1 (1h après la course)"""
|
"""Planifie le scraping des résultats à H+1 (1h après la course)"""
|
||||||
race_time = get_todays_race_time()
|
race_time = get_todays_race_time()
|
||||||
@@ -245,6 +304,9 @@ def main():
|
|||||||
# Scoring dynamique (15min avant course)
|
# Scoring dynamique (15min avant course)
|
||||||
schedule_dynamic_scoring()
|
schedule_dynamic_scoring()
|
||||||
|
|
||||||
|
# Alertes Telegram dynamiques (30min avant course)
|
||||||
|
schedule_dynamic_telegram_alerts()
|
||||||
|
|
||||||
# Résultats dynamiques (H+1)
|
# Résultats dynamiques (H+1)
|
||||||
schedule_dynamic_results()
|
schedule_dynamic_results()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user