Spaces:
Running on T4
Running on T4
File size: 4,988 Bytes
fe40b87 ae2c68f fe40b87 | 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | """N26-N27 coaching access, N40-N43 academy coaches, N44-N45 access requests, N46-N48 staff codes."""
from datetime import date
from typing import Annotated, Literal
from uuid import UUID
from fastapi import APIRouter, Header, Query, Response
from fastapi.responses import JSONResponse
from app.common.academy_access import CurrentAdmin, CurrentStaff, staff_context
from app.common.schemas import Page
from app.core.dependencies import CurrentAccount, Db, Paging
from app.modules.academy_staff.schemas import (
CoachAccessBody,
CoachAccessCreate,
CoachBody,
CoachOut,
CoachUpdate,
DecisionIn,
DecisionOut,
RequestPage,
StaffCodeBody,
StaffCodeCreate,
StaffCodeOut,
)
from app.modules.academy_staff.service import StaffService
from app.modules.idempotency.service import run_idempotent
router = APIRouter(prefix="/v1", tags=["academy_staff"])
IdempotencyKey = Annotated[str | None, Header(max_length=128)]
@router.get("/me/coach-access", response_model=CoachAccessBody)
async def get_coach_access(db: Db, account: CurrentAccount) -> CoachAccessBody:
return CoachAccessBody(coach_access=await StaffService(db).coach_access(account, await staff_context(db, account)))
@router.post("/me/coach-access", response_model=CoachAccessBody, status_code=201)
async def apply_for_coach_access(body: CoachAccessCreate, db: Db, account: CurrentAccount,
idempotency_key: IdempotencyKey = None) -> JSONResponse:
created = {"value": True}
async def handler() -> CoachAccessBody:
access, created["value"] = await StaffService(db).apply(account, body.code)
return CoachAccessBody(coach_access=access)
response = await run_idempotent(db, account.id, idempotency_key, body, handler, status_code=201)
if not created["value"]:
response.status_code = 200
return response
@router.get("/academy/coaches", response_model=Page[CoachOut])
async def list_coaches(db: Db, staff: CurrentStaff,
status: Literal["active", "paused", "removed", "all"] | None = None) -> Page[CoachOut]:
return Page(items=await StaffService(db).list_coaches(staff, status))
@router.get("/academy/coaches/{coach_id}", response_model=CoachBody)
async def get_coach(coach_id: UUID, db: Db, staff: CurrentAdmin, activity_from: date | None = None,
activity_to: date | None = None) -> CoachBody:
return CoachBody(coach=await StaffService(db).get_coach(staff, coach_id, activity_from, activity_to))
@router.patch("/academy/coaches/{coach_id}", response_model=CoachBody)
async def update_coach(coach_id: UUID, body: CoachUpdate, db: Db, staff: CurrentAdmin) -> CoachBody:
return CoachBody(coach=await StaffService(db).update_coach(staff, coach_id, body))
@router.delete("/academy/coaches/{coach_id}", status_code=204)
async def remove_coach(coach_id: UUID, reassign_to: Annotated[UUID, Query()], db: Db,
staff: CurrentAdmin) -> Response:
await StaffService(db).remove_coach(staff, coach_id, reassign_to)
return Response(status_code=204)
@router.get("/academy/coach-requests", response_model=RequestPage)
async def list_requests(db: Db, staff: CurrentAdmin, paging: Paging,
status: Literal["pending", "approved", "declined", "all"] = "all") -> RequestPage:
return await StaffService(db).list_requests(staff, status, paging)
@router.post("/academy/coach-requests/{request_id}/decision", response_model=DecisionOut)
async def decide_request(request_id: UUID, body: DecisionIn, db: Db, staff: CurrentAdmin,
idempotency_key: IdempotencyKey = None) -> JSONResponse:
return await run_idempotent(db, staff.id, idempotency_key, body,
lambda: StaffService(db).decide(staff, request_id, body), status_code=200,
scope=request_id)
@router.get("/academy/staff-codes", response_model=Page[StaffCodeOut])
async def list_codes(db: Db, staff: CurrentAdmin, paging: Paging,
status: Literal["active", "expired", "revoked", "all"] = "all") -> Page[StaffCodeOut]:
items, next_cursor = await StaffService(db).list_codes(staff, status, paging)
return Page(items=items, next_cursor=next_cursor)
@router.post("/academy/staff-codes", response_model=StaffCodeBody, status_code=201)
async def create_code(body: StaffCodeCreate, db: Db, staff: CurrentAdmin,
idempotency_key: IdempotencyKey = None) -> JSONResponse:
async def handler() -> StaffCodeBody:
return StaffCodeBody(staff_code=await StaffService(db).create_code(staff, body))
return await run_idempotent(db, staff.id, idempotency_key, body, handler, status_code=201)
@router.delete("/academy/staff-codes/{code}", status_code=204)
async def revoke_code(code: str, db: Db, staff: CurrentAdmin) -> Response:
await StaffService(db).revoke_code(staff, code)
return Response(status_code=204)
|