github-actions[bot]
Automated Deploy: 4a6a58c601a16c2489b51a7644dbeed2347a2eb2
be69d1e
Raw History Blame Contribute Delete
4.56 kB
"""GET, PATCH and DELETE /me."""
from decimal import Decimal
from sqlalchemy.ext.asyncio import AsyncSession
from app.common.academy_access import staff_context
from app.core.errors import Conflict
from app.integrations.storage import StorageClient
from app.modules.academy_staff.schemas import CoachAccessOut
from app.modules.accounts.models import Account
from app.modules.accounts.repository import AccountRepository
from app.modules.accounts.schemas import AcademyOut, CapabilitiesOut, MeOut, MeUpdate, UserOut
from app.modules.billing.entitlement import EntitlementService
from app.modules.teams.schemas import TeamOut
def _decimal(value: float | None) -> Decimal | None:
return None if value is None else Decimal(str(round(value, 1)))
class AccountService:
def __init__(self, db: AsyncSession, storage: StorageClient | None = None):
self.db = db
self.accounts = AccountRepository(db)
self.storage = storage
async def user(self, account: Account) -> UserOut:
team_code = await self.accounts.team_code(account.team_id) if account.role == "player" else None
return UserOut.build(account, team_code)
async def me(self, account: Account) -> MeOut:
found = await self.accounts.team_with_coach_name(account)
team = None
if found:
t, coach_name = found
team = TeamOut(id=t.id, code=t.code, name=t.name, coach_id=t.coach_id, coach_name=coach_name,
coaching=t.coaching, created_at=t.created_at)
staff = await staff_context(self.db, account)
academy = None
if staff is not None and staff.team_id is not None:
academy = AcademyOut(
id=staff.team_id, name=await self.accounts.team_name(staff.team_id) or "", role=staff.role,
status="active", sees_all=staff.sees_all,
group_ids=await self.accounts.coaching_group_ids(staff.team_id, account.id),
permissions=staff.permissions)
latest = await self.accounts.latest_coach_request(account.id)
if academy is not None:
approved = latest is not None and latest.status == "approved"
access = CoachAccessOut(status="approved", code=latest.code if approved else None,
sent_at=latest.created_at if approved else None)
elif latest is None:
access = CoachAccessOut(status="none", code=None, sent_at=None)
else:
access = CoachAccessOut(status=latest.status, code=latest.code, sent_at=latest.created_at)
capabilities = CapabilitiesOut(player=account.role == "player", coach=staff is not None,
academy_admin=staff is not None and staff.is_admin, corporate_view=False)
# A player's team was just loaded, so its code needs no second query.
user = UserOut.build(account, team.code) if team and account.role == "player" else await self.user(account)
return MeOut(user=user, team=team,
entitlement=await EntitlementService(self.db).get(account.id),
academy=academy, coach_access=access, capabilities=capabilities)
async def update(self, account: Account, patch: MeUpdate) -> UserOut:
values = patch.model_dump(exclude_unset=True, exclude={"profile"})
if "role" in values and values["role"] != account.role and await self._role_locked(account):
raise Conflict("Your role can't change once you have a team or sessions.", code="ROLE_LOCKED")
if patch.profile is not None:
profile = patch.profile.model_dump(exclude_unset=True)
for key in ("height_cm", "weight_kg"):
if key in profile:
profile[key] = _decimal(profile[key])
values.update(profile)
if values:
account = await self.accounts.update(account, **values)
return await self.user(account)
async def _role_locked(self, account: Account) -> bool:
return (account.team_id is not None or await self.accounts.coaches_a_team(account.id)
or await self.accounts.has_sessions(account.id))
async def delete(self, account: Account) -> None:
"""Delete the row first (children cascade), then the media; a storage failure rolls the delete back."""
keys = list(await self.accounts.upload_keys(account.id))
await self.accounts.delete(account)
if keys and self.storage is not None:
await self.storage.delete_many(keys)