|
1 | 1 | from models.challenges import UserChallengeStatus |
2 | 2 | from sqlalchemy import func |
3 | 3 | from sqlalchemy.orm import Session |
4 | | -from models.teams import Team |
5 | | -from schemas.team_schema import TeamCreateRequest, TeamResponse |
| 4 | +from models.teams import Team, TeamMember, TeamStatus |
| 5 | +from schemas.team_schema import TeamCreateRequest |
6 | 6 | from fastapi import HTTPException |
| 7 | +from typing import List |
| 8 | +import os |
| 9 | +from datetime import datetime, timedelta |
7 | 10 |
|
8 | | -def create_team(db: Session, req: TeamCreateRequest) -> TeamResponse: |
9 | | - all_ids = [req.my_id] + req.member_ids |
| 11 | +TEAM_SIZE = int(os.getenv("TEAM_SIZE", "0")) |
| 12 | +PENDING_TIMEOUT_MINUTES = int(os.getenv("PENDING_TIMEOUT_MINUTES", "0")) |
10 | 13 |
|
11 | | - records = ( |
12 | | - db.query(Team.user_id, Team.is_active, UserChallengeStatus.is_correct) |
13 | | - .outerjoin(UserChallengeStatus, Team.user_id == UserChallengeStatus.user_id) |
14 | | - .filter(Team.user_id.in_(all_ids)) |
| 14 | +def _now(): |
| 15 | + return datetime.utcnow() |
| 16 | + |
| 17 | +def create_team(db: Session, my_id: int, member_ids: List[int]): |
| 18 | + all_ids = sorted([my_id] + member_ids) |
| 19 | + |
| 20 | + if len(all_ids) != TEAM_SIZE: |
| 21 | + raise HTTPException(status_code=400, detail="Team must have exactly 5 members (you + 4).") |
| 22 | + if len(set(all_ids)) != TEAM_SIZE: |
| 23 | + raise HTTPException(status_code=400, detail="Duplicate user IDs in request.") |
| 24 | + |
| 25 | + corrected = db.query(UserChallengeStatus.user_id).filter( |
| 26 | + UserChallengeStatus.user_id.in_(all_ids), |
| 27 | + UserChallengeStatus.is_correct == True |
| 28 | + ).all() |
| 29 | + |
| 30 | + if corrected: |
| 31 | + corrected_ids = [r[0] for r in corrected] |
| 32 | + raise HTTPException(status_code=400, detail=f"Users already corrected: {corrected_ids}") |
| 33 | + |
| 34 | + blocking_users = ( |
| 35 | + db.query(TeamMember.user_id) |
| 36 | + .join(Team) |
| 37 | + .filter( |
| 38 | + TeamMember.user_id.in_(all_ids), |
| 39 | + Team.status == TeamStatus.ACTIVE |
| 40 | + ) |
15 | 41 | .all() |
16 | 42 | ) |
| 43 | + if blocking_users: |
| 44 | + raise HTTPException(status_code=400, |
| 45 | + detail=f"Users already in another team: {[u[0] for u in blocking_users]}") |
| 46 | + |
| 47 | + group_hash = "-".join(map(str, all_ids)) |
17 | 48 |
|
18 | | - active_ids = [r.user_id for r in records if r.is_active] |
19 | | - corrected_ids = [r.user_id for r in records if r.is_correct] |
| 49 | + team = ( |
| 50 | + db.query(Team) |
| 51 | + .filter(Team.group_hash == group_hash, |
| 52 | + Team.status.in_([TeamStatus.PENDING, TeamStatus.ACTIVE])) |
| 53 | + .first() |
| 54 | + ) |
20 | 55 |
|
21 | | - if active_ids: |
22 | | - raise HTTPException( |
23 | | - status_code=400, |
24 | | - detail=f"Users already in active team: {active_ids}", |
25 | | - ) |
| 56 | + target_team = None |
| 57 | + |
| 58 | + if team and team.status == TeamStatus.PENDING: |
| 59 | + if _now() - team.created_at.replace(tzinfo=None) > timedelta(minutes=PENDING_TIMEOUT_MINUTES): |
| 60 | + team.status = TeamStatus.CANCELLED |
| 61 | + db.commit() |
| 62 | + else: |
| 63 | + me = ( |
| 64 | + db.query(TeamMember) |
| 65 | + .filter(TeamMember.team_id == team.id, TeamMember.user_id == my_id) |
| 66 | + .first() |
| 67 | + ) |
| 68 | + if me: |
| 69 | + me.confirmed = True |
| 70 | + |
| 71 | + db.flush() |
26 | 72 |
|
27 | | - if corrected_ids: |
28 | | - raise HTTPException( |
29 | | - status_code=400, |
30 | | - detail=f"Users already corrected: {corrected_ids}", |
31 | | - ) |
| 73 | + ready_count = ( |
| 74 | + db.query(TeamMember) |
| 75 | + .filter(TeamMember.team_id == team.id, TeamMember.confirmed == True) |
| 76 | + .count() |
| 77 | + ) |
| 78 | + |
| 79 | + if ready_count == TEAM_SIZE: |
| 80 | + team.status = TeamStatus.ACTIVE |
| 81 | + db.commit() |
| 82 | + return {"status": "ACTIVE", "team_id": team.id, "message": "Team matched"} |
32 | 83 |
|
33 | | - last_team_id = db.query(func.max(Team.team_id)).scalar() |
34 | | - new_team_id = (last_team_id or 0) + 1 |
| 84 | + db.commit() |
| 85 | + return {"status": "PENDING", "team_id": team.id, "message": "Joined pending team request"} |
| 86 | + |
| 87 | + if team and team.status == TeamStatus.ACTIVE: |
| 88 | + raise HTTPException(status_code=400, detail="This team is already active.") |
| 89 | + |
| 90 | + team = Team(group_hash=group_hash, status=TeamStatus.PENDING) |
| 91 | + db.add(team) |
| 92 | + db.flush() |
35 | 93 |
|
36 | | - teams_to_add = [ |
37 | | - {"team_id": new_team_id, "user_id": uid, "is_active": True} |
38 | | - for uid in all_ids |
| 94 | + members = [ |
| 95 | + TeamMember(team_id=team.id, user_id=uid, confirmed=(uid == my_id)) |
| 96 | + for uid in all_ids |
39 | 97 | ] |
| 98 | + db.bulk_save_objects(members) |
| 99 | + db.commit() |
| 100 | + |
| 101 | + return {"status": "PENDING", "team_id": team.id, "message": "New team request created"} |
| 102 | + |
| 103 | +def cancel_team(db: Session, team_id: int, user_id: int): |
| 104 | + team = ( |
| 105 | + db.query(Team) |
| 106 | + .filter( |
| 107 | + Team.id == team_id, |
| 108 | + Team.status == TeamStatus.PENDING |
| 109 | + ).first() |
| 110 | + ) |
| 111 | + |
| 112 | + if not team: |
| 113 | + raise HTTPException(status_code=400, detail="No pending team found") |
40 | 114 |
|
41 | | - db.bulk_insert_mappings(Team, teams_to_add) |
| 115 | + member = db.query(TeamMember).filter( |
| 116 | + TeamMember.team_id == team_id, |
| 117 | + TeamMember.user_id == user_id |
| 118 | + ).first() |
| 119 | + |
| 120 | + if not member: |
| 121 | + raise HTTPException(status_code=403, detail="You are not a member of this team") |
| 122 | + |
| 123 | + if member.confirmed is False: |
| 124 | + return {"message": "Already cancelled"} |
| 125 | + |
| 126 | + member.confirmed = False |
| 127 | + db.commit() |
| 128 | + db.refresh(team) |
| 129 | + |
| 130 | + remaining_confirmed = db.query(TeamMember).filter( |
| 131 | + TeamMember.team_id == team_id, |
| 132 | + TeamMember.confirmed == True |
| 133 | + ).count() |
| 134 | + |
| 135 | + if remaining_confirmed == 0: |
| 136 | + team.status = TeamStatus.CANCELLED |
42 | 137 | db.commit() |
| 138 | + return {"message": "Team cancelled (last member left)"} |
| 139 | + |
| 140 | + return {"message": "You left the team"} |
| 141 | + |
| 142 | +def get_team_status(db: Session, team_id: int, user_id: int): |
| 143 | + last_member_entry = ( |
| 144 | + db.query(TeamMember) |
| 145 | + .join(Team) |
| 146 | + .filter(TeamMember.user_id == user_id) |
| 147 | + .order_by(Team.created_at.desc()) |
| 148 | + .first() |
| 149 | + ) |
43 | 150 |
|
44 | | - return TeamResponse(team_id=new_team_id, members_ids=all_ids) |
| 151 | + if not last_member_entry: |
| 152 | + return {"status": "NONE"} |
45 | 153 |
|
46 | | -def dissolve_team_by_user(db: Session, user_id: int): |
47 | | - team_entry = db.query(Team).filter(Team.user_id == user_id, Team.is_active == True).first() |
48 | | - if not team_entry: |
49 | | - raise HTTPException(status_code=400, detail="User not in active team") |
| 154 | + team = last_member_entry.team |
| 155 | + |
| 156 | + if team.status == TeamStatus.PENDING: |
| 157 | + time_diff = datetime.now() - team.created_at |
| 158 | + if time_diff > timedelta(minutes=PENDING_TIMEOUT_MINUTES): |
| 159 | + team.status = TeamStatus.CANCELLED |
| 160 | + db.commit() |
| 161 | + return {"status": "EXPIRED"} |
| 162 | + |
| 163 | + return { |
| 164 | + "team_id": team.id, |
| 165 | + "status": team.status.value, |
| 166 | + "members_ready": [m.user_id for m in team.members if m.confirmed] |
| 167 | + } |
50 | 168 |
|
51 | | - team_id = team_entry.team_id |
52 | | - team_members = db.query(Team).filter(Team.team_id == team_id, Team.is_active == True).all() |
| 169 | +def dissolve_team_by_user(db: Session, user_id: int): |
| 170 | + team_entry = ( |
| 171 | + db.query(Team) |
| 172 | + .join(TeamMember) |
| 173 | + .filter( |
| 174 | + TeamMember.user_id == user_id, |
| 175 | + Team.status == TeamStatus.ACTIVE |
| 176 | + ).first() |
| 177 | + ) |
53 | 178 |
|
54 | | - for member in team_members: |
55 | | - member.is_active = False |
| 179 | + if not team_entry: |
| 180 | + raise HTTPException(status_code=400, detail="User is not in an active team") |
56 | 181 |
|
| 182 | + team_entry.status = TeamStatus.FAILED |
| 183 | + |
57 | 184 | db.commit() |
58 | | - return {"message": f"Team {team_id} dissolved", "team_id": team_id} |
| 185 | + |
| 186 | + return {"message": f"Team {team_entry.id} dissolved due to quiz failure.", "team_id": team_entry.id} |
0 commit comments