157 行
4.7 KiB
Python
157 行
4.7 KiB
Python
"""Profile route handlers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from backend.routes.common import handle_request
|
|
from backend.routes.params import int_or_default, optional_str
|
|
from backend.services.event_stream_service import get_default_event_bus
|
|
from backend.services.profile_service import ProfileService
|
|
from backend.services.quick_profile_service import QuickProfileService
|
|
from backend.services.workflow_service import WorkflowService
|
|
from core.audit_service import AuditService
|
|
from core.identity import RequestContext
|
|
from core.security import require_authenticated, require_write
|
|
from database.db_helper import Database, load_database_settings
|
|
from database.repositories.profile_repository import ProfileRepository
|
|
from database.repositories.workflow_repository import WorkflowRepository
|
|
|
|
|
|
def list_profiles(
|
|
ctx: RequestContext,
|
|
query: dict[str, Any] | None = None,
|
|
service: ProfileService | None = None,
|
|
) -> dict:
|
|
profile_service = service or _create_profile_service()
|
|
params = query or {}
|
|
|
|
def action() -> dict:
|
|
require_authenticated(ctx)
|
|
result = profile_service.list_profiles(
|
|
ctx=ctx,
|
|
keyword=optional_str(params.get("keyword")),
|
|
status=optional_str(params.get("status")) or "active",
|
|
page=int_or_default(params.get("page"), 1),
|
|
page_size=int_or_default(params.get("page_size"), 30),
|
|
)
|
|
return result.to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def create_profile(
|
|
ctx: RequestContext,
|
|
payload: dict[str, Any],
|
|
service: ProfileService | None = None,
|
|
) -> dict:
|
|
profile_service = service or _create_profile_service()
|
|
|
|
def action() -> dict:
|
|
require_write(ctx)
|
|
return profile_service.create_profile(payload, ctx).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def get_profile(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
service: ProfileService | None = None,
|
|
) -> dict:
|
|
profile_service = service or _create_profile_service()
|
|
|
|
def action() -> dict:
|
|
require_authenticated(ctx)
|
|
return profile_service.get_profile(profile_id, ctx).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def update_profile(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
payload: dict[str, Any],
|
|
service: ProfileService | None = None,
|
|
) -> dict:
|
|
profile_service = service or _create_profile_service()
|
|
|
|
def action() -> dict:
|
|
require_write(ctx)
|
|
return profile_service.update_profile(profile_id, payload, ctx).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def delete_profile(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
service: ProfileService | None = None,
|
|
) -> dict:
|
|
profile_service = service or _create_profile_service()
|
|
|
|
def action() -> dict:
|
|
require_write(ctx)
|
|
return profile_service.delete_profile(profile_id, ctx).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def archive_profile(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
service: ProfileService | None = None,
|
|
) -> dict:
|
|
return delete_profile(ctx, profile_id, service)
|
|
|
|
|
|
def create_quick_profile_draft(
|
|
ctx: RequestContext,
|
|
payload: dict[str, Any],
|
|
service: QuickProfileService | None = None,
|
|
) -> dict:
|
|
quick_service = service or QuickProfileService()
|
|
|
|
def action() -> dict:
|
|
require_authenticated(ctx)
|
|
return quick_service.create_draft(payload)
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def save_quick_profile_draft(
|
|
ctx: RequestContext,
|
|
payload: dict[str, Any],
|
|
service: QuickProfileService | None = None,
|
|
profile_service: ProfileService | None = None,
|
|
workflow_service: WorkflowService | None = None,
|
|
) -> dict:
|
|
quick_service = service or QuickProfileService()
|
|
resolved_profile_service = profile_service or _create_profile_service()
|
|
resolved_workflow_service = workflow_service or _create_workflow_service(
|
|
resolved_profile_service.repository.database
|
|
)
|
|
|
|
def action() -> dict:
|
|
require_write(ctx)
|
|
return quick_service.save_draft(payload, resolved_profile_service, ctx, resolved_workflow_service)
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def _create_profile_service(database: Database | None = None) -> ProfileService:
|
|
database = database or Database(load_database_settings())
|
|
return ProfileService(
|
|
repository=ProfileRepository(database),
|
|
audit_service=AuditService(database),
|
|
)
|
|
|
|
|
|
def _create_workflow_service(database: Database | None = None) -> WorkflowService:
|
|
database = database or Database(load_database_settings())
|
|
return WorkflowService(
|
|
workflow_repository=WorkflowRepository(database),
|
|
profile_repository=ProfileRepository(database),
|
|
audit_service=AuditService(database),
|
|
event_bus=get_default_event_bus(),
|
|
)
|