refactor(services): un seul endroit pour annoncer un match planifie
Seconde moitie d ARCH-003.
Les memes vingt lignes vivaient dans trois routes -- matches.create_match,
matches.edit_match et team_matches.create_match : formater la date et
l heure, puis parcourir deux listes paralleles pour apparier un joueur avec
la ligne de participation qu une reaction Discord doit pouvoir retrouver.
Trois copies, donc trois occasions de diverger. Elles avaient deja diverge :
create_match lisait les heures des variables qu il venait d analyser, et
affichait 'TBD' des qu une des deux manquait ;
edit_match les relisait depuis la ligne enregistree et substituait
l heure de debut a une heure de fin absente.
Un match avec une heure de debut et pas de fin annoncait donc une heure
dans une route et 'TBD' dans l autre, pour la meme donnee.
app/services/scheduling.py retient la regle la plus soigneuse des deux.
zip_participants() isole l appariement par index, qui n est correct que
tant que les deux listes sont construites en phase -- desormais un seul
endroit a relire au lieu de trois, et une liste plus courte donne None,
converti en reference vers le match lui-meme.
matches.py 711 -> 687 lignes, team_matches.py 347 -> 337.
9 tests sur le service seul, sans base ni requete.
Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
+16
-40
@@ -25,7 +25,7 @@ from app.models import (
|
||||
PersonalNote,
|
||||
)
|
||||
from datetime import datetime, timedelta
|
||||
from app.discord_bot import send_schedule_notification
|
||||
from app.services.scheduling import notify_participants, zip_participants
|
||||
|
||||
matches_bp = Blueprint('matches', __name__, url_prefix='/matches')
|
||||
|
||||
@@ -375,25 +375,14 @@ def create_match(tryout_id):
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Discord notifications
|
||||
event_date_str = date_obj.strftime('%Y-%m-%d')
|
||||
event_time_str = (
|
||||
f"{start_time.strftime('%I:%M %p')} - {end_time.strftime('%I:%M %p')}"
|
||||
if start_time and end_time
|
||||
else 'TBD'
|
||||
notify_participants(
|
||||
title=match.title,
|
||||
date=date_obj,
|
||||
start_time=start_time,
|
||||
end_time=end_time,
|
||||
participants=zip_participants(notified_player_ids, notified_participant_ids),
|
||||
fallback_id=match.id,
|
||||
)
|
||||
for i, player_id in enumerate(notified_player_ids):
|
||||
reference_id = (
|
||||
notified_participant_ids[i] if i < len(notified_participant_ids) else match.id
|
||||
)
|
||||
send_schedule_notification(
|
||||
user_id=player_id,
|
||||
event_type='match',
|
||||
event_title=match.title,
|
||||
event_date=event_date_str,
|
||||
event_time=event_time_str,
|
||||
reference_id=reference_id,
|
||||
)
|
||||
|
||||
flash(_('Match scheduled successfully!'), 'success')
|
||||
return redirect(url_for('tryouts.view_tryout', tryout_id=tryout_id))
|
||||
@@ -553,27 +542,14 @@ def edit_match(match_id):
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Discord notifications
|
||||
end_time_val = match.end_time or (match.start_time if match.start_time else None)
|
||||
if match.start_time and end_time_val:
|
||||
event_time_str = (
|
||||
f"{match.start_time.strftime('%I:%M %p')} - {end_time_val.strftime('%I:%M %p')}"
|
||||
)
|
||||
else:
|
||||
event_time_str = 'TBD'
|
||||
event_date_str = match.date.strftime('%Y-%m-%d')
|
||||
for i, player_id in enumerate(notified_player_ids):
|
||||
reference_id = (
|
||||
notified_participant_ids[i] if i < len(notified_participant_ids) else match.id
|
||||
)
|
||||
send_schedule_notification(
|
||||
user_id=player_id,
|
||||
event_type='match',
|
||||
event_title=match.title,
|
||||
event_date=event_date_str,
|
||||
event_time=event_time_str,
|
||||
reference_id=reference_id,
|
||||
)
|
||||
notify_participants(
|
||||
title=match.title,
|
||||
date=match.date,
|
||||
start_time=match.start_time,
|
||||
end_time=match.end_time,
|
||||
participants=zip_participants(notified_player_ids, notified_participant_ids),
|
||||
fallback_id=match.id,
|
||||
)
|
||||
|
||||
flash(_('Match updated successfully!'), 'success')
|
||||
return redirect(url_for('tryouts.view_tryout', tryout_id=tryout.id))
|
||||
|
||||
+10
-20
@@ -19,7 +19,7 @@ from app.models import (
|
||||
)
|
||||
from app.permissions import can_manage_org_team, coach_org_teams, visible_org_teams
|
||||
from datetime import datetime, timedelta
|
||||
from app.discord_bot import send_schedule_notification
|
||||
from app.services.scheduling import notify_participants, zip_participants
|
||||
|
||||
team_matches_bp = Blueprint('team_matches', __name__, url_prefix='/team-matches')
|
||||
|
||||
@@ -204,27 +204,17 @@ def create_match(team_id):
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Discord notifications
|
||||
event_date_str = date_obj.strftime('%Y-%m-%d')
|
||||
event_time_str = (
|
||||
f"{start_time.strftime('%I:%M %p')} - {end_time.strftime('%I:%M %p')}"
|
||||
if start_time and end_time
|
||||
else 'TBD'
|
||||
notify_participants(
|
||||
title=team_match.title,
|
||||
date=date_obj,
|
||||
start_time=start_time,
|
||||
end_time=end_time,
|
||||
participants=zip_participants(
|
||||
[tp.player_id for tp in team_players], notified_participant_ids
|
||||
),
|
||||
fallback_id=team_match.id,
|
||||
)
|
||||
|
||||
for i, tp in enumerate(team_players):
|
||||
reference_id = (
|
||||
notified_participant_ids[i] if i < len(notified_participant_ids) else team_match.id
|
||||
)
|
||||
send_schedule_notification(
|
||||
user_id=tp.player_id,
|
||||
event_type='match',
|
||||
event_title=team_match.title,
|
||||
event_date=event_date_str,
|
||||
event_time=event_time_str,
|
||||
reference_id=reference_id,
|
||||
)
|
||||
|
||||
flash(_('Team match "%(title)s" scheduled successfully!', title=title), 'success')
|
||||
return redirect(url_for('team_matches.list_matches'))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user