File size: 1,979 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
"""N01 to N05: /me/settings, /me/consent, /me/exports."""

from typing import Annotated

from fastapi import APIRouter, Header, Response

from app.core.dependencies import CurrentAccount, Db
from app.modules.idempotency.service import run_idempotent
from app.modules.settings.schemas import (
    ConsentIn,
    ExportEnvelope,
    ExportIn,
    SettingsEnvelope,
    SettingsPatch,
)
from app.modules.settings.service import SettingsService

router = APIRouter(prefix="/v1", tags=["settings"])
IdempotencyKey = Annotated[str | None, Header(max_length=128)]


@router.get("/me/settings", response_model=SettingsEnvelope)
async def read_settings(account: CurrentAccount, db: Db) -> SettingsEnvelope:
    return SettingsEnvelope(settings=await SettingsService(db).get(account))


@router.patch("/me/settings", response_model=SettingsEnvelope)
async def update_settings(body: SettingsPatch, account: CurrentAccount, db: Db) -> SettingsEnvelope:
    return SettingsEnvelope(settings=await SettingsService(db).update(account, body))


@router.post("/me/consent", status_code=201)
async def accept_consent(body: ConsentIn, account: CurrentAccount, db: Db, idempotency_key: IdempotencyKey = None):
    service = SettingsService(db)
    return await run_idempotent(db, account.id, idempotency_key, body, lambda: service.accept(account, body),
                                status_code=201)


@router.delete("/me/consent", status_code=204)
async def withdraw_consent(account: CurrentAccount, db: Db) -> Response:
    await SettingsService(db).withdraw(account)
    return Response(status_code=204)


@router.post("/me/exports", status_code=202, response_model=ExportEnvelope)
async def request_export(body: ExportIn, account: CurrentAccount, db: Db, idempotency_key: IdempotencyKey = None):
    service = SettingsService(db)
    return await run_idempotent(db, account.id, idempotency_key, body, lambda: service.request_export(account),
                                status_code=202)