|
1 | 1 | from core.security import create_access_token |
2 | 2 | from exceptions.user_exceptions import UserNotFoundException |
3 | 3 | from models.users import User |
| 4 | +from models.teams import Team, TeamMember, TeamStatus |
| 5 | +from models.challenges import UserChallengeStatus |
4 | 6 | from sqlalchemy.orm import Session |
| 7 | +from core.enums import ProgressStatus |
5 | 8 |
|
6 | 9 |
|
7 | 10 | def auth_user(db: Session, email: str): |
@@ -31,3 +34,48 @@ def parse_user_id(tag: str): |
31 | 34 | return {"id": user_id} |
32 | 35 | except ValueError: |
33 | 36 | return {"error": "์ฌ๋ฐ๋ฅธ ํ์์ ์ ์ ํ๊ทธ๊ฐ ์๋๋๋ค. ์: ๊น๋ฏผ์ค#0001"} |
| 37 | + |
| 38 | + |
| 39 | +def progress_status( |
| 40 | + db: Session, |
| 41 | + user_id: int, |
| 42 | +): |
| 43 | + # 1. ์ ์ ํ ์กฐํ |
| 44 | + team_member = ( |
| 45 | + db.query(TeamMember) |
| 46 | + .filter(TeamMember.user_id == user_id) |
| 47 | + .first() |
| 48 | + ) |
| 49 | + |
| 50 | + if not team_member: |
| 51 | + return None, None, ProgressStatus.NONE_TEAM |
| 52 | + |
| 53 | + team = db.query(Team).get(team_member.team_id) |
| 54 | + |
| 55 | + # 2. ํ ์ํ |
| 56 | + if team.status == TeamStatus.PENDING: |
| 57 | + return team.id, None, ProgressStatus.TEAM_WAITING |
| 58 | + |
| 59 | + if team.status == TeamStatus.FAILED: |
| 60 | + return team.id, None, ProgressStatus.FAILED |
| 61 | + |
| 62 | + # 3. ๋ฌธ์ ์ํ |
| 63 | + ucs = ( |
| 64 | + db.query(UserChallengeStatus) |
| 65 | + .filter(UserChallengeStatus.user_id == user_id) |
| 66 | + .first() |
| 67 | + ) |
| 68 | + |
| 69 | + if not ucs: |
| 70 | + return team.id, None, ProgressStatus.CHALLENGE_ASSIGNED |
| 71 | + |
| 72 | + if ucs.is_redeemed: |
| 73 | + return team.id, ucs.challenge_id, ProgressStatus.REDEEMED |
| 74 | + |
| 75 | + if ucs.is_correct: |
| 76 | + return team.id, ucs.challenge_id, ProgressStatus.CHALLENGE_SUCCESS |
| 77 | + |
| 78 | + if ucs.retry_count >= 2: |
| 79 | + return team.id, ucs.challenge_id, ProgressStatus.CHALLENGE_FAILED |
| 80 | + |
| 81 | + return team.id, ucs.challenge_id, ProgressStatus.CHALLENGE_ASSIGNED |
0 commit comments