feat(teams): helper resolve_channel_for_server (server > application > default)

This commit is contained in:
Pierre & Lumière 2026-05-05 19:13:15 +02:00
parent de9704facf
commit 6839b3e59d

View File

@ -76,6 +76,50 @@ def _adaptive_card(title: str, body_lines: list, color: str = "good") -> Dict[st
} }
def resolve_channel_for_server(db, server_id: int) -> Dict[str, Any]:
"""Résout le canal Teams à utiliser pour un serveur donné.
Priorité :
1. servers.teams_channel_id (override par serveur)
2. applications.teams_channel_id (canal de l'app rattachée)
3. teams_channels.is_default = true (canal global)
Retourne {ok, channel_id, name, webhook_url, source} ou {ok: False, msg}."""
from sqlalchemy import text as sqlt
if not server_id:
return {"ok": False, "msg": "server_id manquant"}
# 1) Override par serveur
row = db.execute(sqlt("""
SELECT tc.id, tc.name, tc.webhook_url, 'server' AS source
FROM servers s
JOIN teams_channels tc ON tc.id = s.teams_channel_id
WHERE s.id = :sid AND tc.is_active = true
"""), {"sid": server_id}).fetchone()
if row:
return {"ok": True, "channel_id": row.id, "name": row.name,
"webhook_url": row.webhook_url, "source": row.source}
# 2) Canal de l'application
row = db.execute(sqlt("""
SELECT tc.id, tc.name, tc.webhook_url, 'application' AS source
FROM servers s
JOIN applications a ON a.id = s.application_id
JOIN teams_channels tc ON tc.id = a.teams_channel_id
WHERE s.id = :sid AND tc.is_active = true
"""), {"sid": server_id}).fetchone()
if row:
return {"ok": True, "channel_id": row.id, "name": row.name,
"webhook_url": row.webhook_url, "source": row.source}
# 3) Canal défaut global
row = db.execute(sqlt("""
SELECT id, name, webhook_url, 'default' AS source
FROM teams_channels
WHERE is_default = true AND is_active = true
ORDER BY id LIMIT 1
""")).fetchone()
if row:
return {"ok": True, "channel_id": row.id, "name": row.name,
"webhook_url": row.webhook_url, "source": row.source}
return {"ok": False, "msg": "Aucun canal Teams configuré (ni serveur, ni app, ni défaut)"}
def send_test_message(webhook_url: str, channel_name: str, sender: str) -> Dict[str, Any]: def send_test_message(webhook_url: str, channel_name: str, sender: str) -> Dict[str, Any]:
"""Envoie un message de test pour valider la config du webhook.""" """Envoie un message de test pour valider la config du webhook."""
payload = _adaptive_card( payload = _adaptive_card(