File size: 2,270 Bytes
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
"""N37-N39 academy reporting and N49-N50 consent records."""

from typing import Annotated
from uuid import UUID

from fastapi import APIRouter, Depends, Header, Query
from fastapi.responses import JSONResponse

from app.common.academy_access import CurrentAdmin
from app.core.dependencies import Db, Paging, Storage
from app.modules.idempotency.service import run_idempotent
from app.modules.reporting.schemas import (
    ConsentExportRequest,
    ConsentPage,
    CorporateResponse,
    DashboardResponse,
    EarningsResponse,
    ExportResponse,
)
from app.modules.reporting.service import ReportingService

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


def get_reporting_service(db: Db, storage: Storage) -> ReportingService:
    return ReportingService(db, storage)


Service = Annotated[ReportingService, Depends(get_reporting_service)]
Month = Annotated[str | None, Query()]


@router.get("/dashboard", response_model=DashboardResponse)
async def dashboard(staff: CurrentAdmin, service: Service, month: Month = None) -> DashboardResponse:
    return await service.dashboard(staff, month)


@router.get("/earnings", response_model=EarningsResponse)
async def earnings(staff: CurrentAdmin, service: Service, month: Month = None) -> EarningsResponse:
    return await service.earnings(staff, month)


@router.get("/corp", response_model=CorporateResponse)
async def corporate(staff: CurrentAdmin, service: Service, month: Month = None) -> CorporateResponse:
    await service.corporate(staff)
    raise AssertionError("unreachable")


@router.get("/consent", response_model=ConsentPage)
async def consent(staff: CurrentAdmin, service: Service, paging: Paging,
                  group_id: UUID | None = None) -> ConsentPage:
    return await service.consent(staff, group_id, paging.limit, paging.cursor)


@router.post("/consent/export", response_model=ExportResponse, status_code=201)
async def export_consent(
    body: ConsentExportRequest, staff: CurrentAdmin, service: Service, db: Db,
    idempotency_key: Annotated[str | None, Header(max_length=128)] = None,
) -> JSONResponse:
    return await run_idempotent(db, staff.id, idempotency_key, body, lambda: service.export_consent(staff, body),
                                status_code=201)