173 lines
5.6 KiB
Python
173 lines
5.6 KiB
Python
|
|
"""CustomFieldDefinition service — CRUD with tenant isolation and visibility filter."""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import uuid
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from sqlalchemy import select
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
|
||
|
|
from app.core.visibility import apply_visibility_filter, check_single_entity_access
|
||
|
|
from app.models.custom_field_definition import CustomFieldDefinition
|
||
|
|
|
||
|
|
|
||
|
|
def _definition_to_dict(d: CustomFieldDefinition) -> dict[str, Any]:
|
||
|
|
"""Serialize a CustomFieldDefinition ORM object to dict."""
|
||
|
|
return {
|
||
|
|
"id": str(d.id),
|
||
|
|
"entity": d.entity,
|
||
|
|
"name": d.name,
|
||
|
|
"label": d.label,
|
||
|
|
"field_type": d.field_type,
|
||
|
|
"options": d.options,
|
||
|
|
"default_value": d.default_value,
|
||
|
|
"required": d.required,
|
||
|
|
"is_active": d.is_active,
|
||
|
|
"sort_order": d.sort_order,
|
||
|
|
"owner_id": str(d.owner_id) if d.owner_id else None,
|
||
|
|
"created_by": str(d.created_by) if d.created_by else None,
|
||
|
|
"updated_by": str(d.updated_by) if d.updated_by else None,
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
async def list_custom_field_definitions(
|
||
|
|
db: AsyncSession,
|
||
|
|
tenant_id: uuid.UUID,
|
||
|
|
entity: str | None = None,
|
||
|
|
user_id: uuid.UUID | None = None,
|
||
|
|
is_system_admin: bool = False,
|
||
|
|
) -> list[dict[str, Any]]:
|
||
|
|
"""List custom field definitions for a tenant, optionally filtered by entity."""
|
||
|
|
q = select(CustomFieldDefinition).where(
|
||
|
|
CustomFieldDefinition.tenant_id == tenant_id,
|
||
|
|
CustomFieldDefinition.is_active == True, # noqa: E712
|
||
|
|
)
|
||
|
|
if entity:
|
||
|
|
q = q.where(CustomFieldDefinition.entity == entity)
|
||
|
|
if user_id and not is_system_admin:
|
||
|
|
q = await apply_visibility_filter(
|
||
|
|
db, q, "custom_field_definition", CustomFieldDefinition, user_id, tenant_id, is_system_admin
|
||
|
|
)
|
||
|
|
q = q.order_by(CustomFieldDefinition.sort_order, CustomFieldDefinition.name)
|
||
|
|
result = await db.execute(q)
|
||
|
|
return [_definition_to_dict(d) for d in result.scalars().all()]
|
||
|
|
|
||
|
|
|
||
|
|
async def get_custom_field_definition(
|
||
|
|
db: AsyncSession,
|
||
|
|
tenant_id: uuid.UUID,
|
||
|
|
definition_id: uuid.UUID,
|
||
|
|
user_id: uuid.UUID | None = None,
|
||
|
|
is_system_admin: bool = False,
|
||
|
|
) -> dict[str, Any] | None:
|
||
|
|
"""Get a single custom field definition by ID."""
|
||
|
|
q = select(CustomFieldDefinition).where(
|
||
|
|
CustomFieldDefinition.id == definition_id,
|
||
|
|
CustomFieldDefinition.tenant_id == tenant_id,
|
||
|
|
)
|
||
|
|
result = await db.execute(q)
|
||
|
|
definition = result.scalar_one_or_none()
|
||
|
|
if definition is None:
|
||
|
|
return None
|
||
|
|
if user_id and not is_system_admin:
|
||
|
|
has_access = await check_single_entity_access(
|
||
|
|
db, "custom_field_definition", definition.id, user_id, tenant_id, "read", is_system_admin
|
||
|
|
)
|
||
|
|
if not has_access:
|
||
|
|
raise PermissionError("No access")
|
||
|
|
return _definition_to_dict(definition)
|
||
|
|
|
||
|
|
|
||
|
|
async def create_custom_field_definition(
|
||
|
|
db: AsyncSession,
|
||
|
|
tenant_id: uuid.UUID,
|
||
|
|
user_id: uuid.UUID,
|
||
|
|
data: dict[str, Any],
|
||
|
|
) -> dict[str, Any]:
|
||
|
|
"""Create a new custom field definition."""
|
||
|
|
definition = CustomFieldDefinition(
|
||
|
|
tenant_id=tenant_id,
|
||
|
|
entity=data["entity"],
|
||
|
|
name=data["name"],
|
||
|
|
label=data["label"],
|
||
|
|
field_type=data["field_type"],
|
||
|
|
options=data.get("options"),
|
||
|
|
default_value=data.get("default_value"),
|
||
|
|
required=data.get("required", False),
|
||
|
|
is_active=data.get("is_active", True),
|
||
|
|
sort_order=data.get("sort_order", 0),
|
||
|
|
created_by=user_id,
|
||
|
|
updated_by=user_id,
|
||
|
|
owner_id=user_id,
|
||
|
|
)
|
||
|
|
db.add(definition)
|
||
|
|
await db.flush()
|
||
|
|
await db.refresh(definition)
|
||
|
|
return _definition_to_dict(definition)
|
||
|
|
|
||
|
|
|
||
|
|
async def update_custom_field_definition(
|
||
|
|
db: AsyncSession,
|
||
|
|
tenant_id: uuid.UUID,
|
||
|
|
user_id: uuid.UUID,
|
||
|
|
definition_id: uuid.UUID,
|
||
|
|
data: dict[str, Any],
|
||
|
|
is_system_admin: bool = False,
|
||
|
|
) -> dict[str, Any] | None:
|
||
|
|
"""Update an existing custom field definition."""
|
||
|
|
q = select(CustomFieldDefinition).where(
|
||
|
|
CustomFieldDefinition.id == definition_id,
|
||
|
|
CustomFieldDefinition.tenant_id == tenant_id,
|
||
|
|
)
|
||
|
|
result = await db.execute(q)
|
||
|
|
definition = result.scalar_one_or_none()
|
||
|
|
if definition is None:
|
||
|
|
return None
|
||
|
|
|
||
|
|
if not is_system_admin:
|
||
|
|
has_access = await check_single_entity_access(
|
||
|
|
db, "custom_field_definition", definition.id, user_id, tenant_id, "write", is_system_admin
|
||
|
|
)
|
||
|
|
if not has_access:
|
||
|
|
raise PermissionError("No access")
|
||
|
|
|
||
|
|
update_fields = ["label", "field_type", "options", "default_value", "required", "is_active", "sort_order"]
|
||
|
|
for field in update_fields:
|
||
|
|
if field in data:
|
||
|
|
setattr(definition, field, data[field])
|
||
|
|
|
||
|
|
definition.updated_by = user_id
|
||
|
|
await db.flush()
|
||
|
|
await db.refresh(definition)
|
||
|
|
return _definition_to_dict(definition)
|
||
|
|
|
||
|
|
|
||
|
|
async def delete_custom_field_definition(
|
||
|
|
db: AsyncSession,
|
||
|
|
tenant_id: uuid.UUID,
|
||
|
|
user_id: uuid.UUID,
|
||
|
|
definition_id: uuid.UUID,
|
||
|
|
is_system_admin: bool = False,
|
||
|
|
) -> bool:
|
||
|
|
"""Delete a custom field definition."""
|
||
|
|
q = select(CustomFieldDefinition).where(
|
||
|
|
CustomFieldDefinition.id == definition_id,
|
||
|
|
CustomFieldDefinition.tenant_id == tenant_id,
|
||
|
|
)
|
||
|
|
result = await db.execute(q)
|
||
|
|
definition = result.scalar_one_or_none()
|
||
|
|
if definition is None:
|
||
|
|
return False
|
||
|
|
|
||
|
|
if not is_system_admin:
|
||
|
|
has_access = await check_single_entity_access(
|
||
|
|
db, "custom_field_definition", definition.id, user_id, tenant_id, "admin", is_system_admin
|
||
|
|
)
|
||
|
|
if not has_access:
|
||
|
|
raise PermissionError("No access")
|
||
|
|
|
||
|
|
await db.delete(definition)
|
||
|
|
await db.flush()
|
||
|
|
return True
|