feat: create message capture policies
This commit is contained in:
1 parent
62aac0bf32
commit
920a0235e2
15 files changed
+700
-5
No files matched your search
@@ -0,0 +1,87 @@
|
||||
from typing import Annotated
|
||||
|
||||
import asyncpg
|
||||
from dishka.integrations.fastapi import FromDishka, inject
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from pydantic import BaseModel
|
||||
|
||||
from utils.policy import repository
|
||||
from utils.policy.models import (
|
||||
CaptureToggles,
|
||||
ChatKind,
|
||||
ChatMeta,
|
||||
PolicyCreate,
|
||||
PolicyRecord,
|
||||
)
|
||||
from utils.policy.resolver import resolve
|
||||
|
||||
router = APIRouter(prefix="/api/policy", tags=["policy"])
|
||||
|
||||
|
||||
class EffectiveQuery(BaseModel):
|
||||
account_id: int
|
||||
chat_id: int
|
||||
kind: ChatKind
|
||||
is_bot: bool = False
|
||||
is_contact: bool | None = None
|
||||
|
||||
|
||||
@router.get("")
|
||||
@inject
|
||||
async def list_policies(
|
||||
pool: FromDishka[asyncpg.Pool], account_id: Annotated[int, Query()]
|
||||
) -> list[PolicyRecord]:
|
||||
return await repository.list_policies(pool, account_id)
|
||||
|
||||
|
||||
@router.get("/effective")
|
||||
@inject
|
||||
async def effective_policy(
|
||||
pool: FromDishka[asyncpg.Pool], query: Annotated[EffectiveQuery, Query()]
|
||||
) -> CaptureToggles:
|
||||
folders = await repository.list_folders(pool, query.account_id)
|
||||
policies = await repository.load_policy_set(pool, query.account_id)
|
||||
chat = ChatMeta(
|
||||
chat_id=query.chat_id,
|
||||
kind=query.kind,
|
||||
is_bot=query.is_bot,
|
||||
is_contact=query.is_contact,
|
||||
)
|
||||
return resolve(chat, folders, policies)
|
||||
|
||||
|
||||
@router.post("", status_code=201)
|
||||
@inject
|
||||
async def create_policy(
|
||||
pool: FromDishka[asyncpg.Pool], body: PolicyCreate
|
||||
) -> PolicyRecord:
|
||||
return await repository.create_policy(
|
||||
pool, body.account_id, body.scope_type, body.scope_id, body
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{policy_id}")
|
||||
@inject
|
||||
async def get_policy(pool: FromDishka[asyncpg.Pool], policy_id: int) -> PolicyRecord:
|
||||
record = await repository.get_policy(pool, policy_id)
|
||||
if record is None:
|
||||
raise HTTPException(status_code=404, detail="policy not found")
|
||||
return record
|
||||
|
||||
|
||||
@router.put("/{policy_id}")
|
||||
@inject
|
||||
async def update_policy(
|
||||
pool: FromDishka[asyncpg.Pool], policy_id: int, body: CaptureToggles
|
||||
) -> PolicyRecord:
|
||||
record = await repository.update_policy(pool, policy_id, body)
|
||||
if record is None:
|
||||
raise HTTPException(status_code=404, detail="policy not found")
|
||||
return record
|
||||
|
||||
|
||||
@router.delete("/{policy_id}", status_code=204)
|
||||
@inject
|
||||
async def delete_policy(pool: FromDishka[asyncpg.Pool], policy_id: int) -> None:
|
||||
if not await repository.delete_policy(pool, policy_id):
|
||||
raise HTTPException(status_code=404, detail="policy not found")
|
||||
Reference in new issue
Block a user