File size: 2,308 Bytes
fe40b87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae2c68f
fe40b87
 
 
 
 
 
 
 
 
 
 
 
ae2c68f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""N20-N22: drill assignments and set completion."""

from typing import Annotated, Literal
from uuid import UUID

from fastapi import APIRouter, Depends, Header, Query

from app.common.academy_access import CurrentStaff
from app.core.dependencies import CurrentAccount, CurrentPlayer, Db, Paging, Storage
from app.integrations.push import PushClient, get_push_client
from app.modules.assignments.schemas import AssignIn, AssignmentEnvelope, AssignmentPage, Mode, SetIn, SetResult
from app.modules.assignments.service import AssignmentService
from app.modules.idempotency.service import run_idempotent

router = APIRouter(prefix="/v1", tags=["assignments"])


@router.get("/drill-assignments", response_model=AssignmentPage)
async def list_assignments(
    mode: Annotated[Mode, Query()], account: CurrentAccount, db: Db, storage: Storage, paging: Paging,
    player_id: UUID | None = None, status: Literal["open", "completed", "all"] = "all",
) -> AssignmentPage:
    return await AssignmentService(db, storage).list(account, mode, player_id, status, paging)


@router.post("/coach/players/{player_id}/drill-assignments", status_code=201, response_model=AssignmentEnvelope)
async def assign_drill(
    player_id: UUID, body: AssignIn, staff: CurrentStaff, db: Db, storage: Storage,
    push: Annotated[PushClient, Depends(get_push_client)],
    idempotency_key: Annotated[str | None, Header(max_length=128)] = None,
):
    service = AssignmentService(db, storage, push)

    async def handler() -> AssignmentEnvelope:
        return AssignmentEnvelope(assignment=await service.assign(staff, player_id, body))

    return await run_idempotent(db, staff.id, idempotency_key, body, handler, status_code=201, scope=player_id)


@router.post("/drill-assignments/{assignment_id}/sets", status_code=201, response_model=SetResult)
async def log_set(
    assignment_id: UUID, body: SetIn, player: CurrentPlayer, db: Db, storage: Storage,
    idempotency_key: Annotated[str | None, Header(max_length=128)] = None,
):
    service = AssignmentService(db, storage)

    async def handler() -> SetResult:
        return await service.log_set(player, assignment_id, body)

    return await run_idempotent(db, player.id, idempotency_key, body, handler, status_code=201,
                                scope=assignment_id)