Implement unified work inbox module

This commit is contained in:
2026-08-06 16:06:17 +02:00
parent 2e89204a0c
commit 3cdab599ff
31 changed files with 3540 additions and 0 deletions
@@ -0,0 +1,3 @@
from govoplan_tasks.backend.db.models import TaskAssignment, TaskItem
__all__ = ["TaskAssignment", "TaskItem"]
+140
View File
@@ -0,0 +1,140 @@
from __future__ import annotations
import uuid
from datetime import datetime
from typing import Any
from sqlalchemy import (
DateTime,
ForeignKey,
Index,
Integer,
JSON,
String,
Text,
UniqueConstraint,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from govoplan_core.db.base import Base, TimestampMixin
def new_uuid() -> str:
return str(uuid.uuid4())
class TaskItem(Base, TimestampMixin):
__tablename__ = "task_items"
__table_args__ = (
UniqueConstraint(
"tenant_id",
"idempotency_key",
name="uq_task_item_idempotency",
),
Index("ix_task_items_tenant_status", "tenant_id", "status"),
Index("ix_task_items_tenant_due", "tenant_id", "due_at", "status"),
Index(
"ix_task_items_source",
"tenant_id",
"source_module",
"source_resource_type",
"source_resource_id",
),
)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid)
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
title: Mapped[str] = mapped_column(String(500), nullable=False)
summary: Mapped[str | None] = mapped_column(Text, nullable=True)
status: Mapped[str] = mapped_column(
String(30), nullable=False, default="open", index=True
)
priority: Mapped[str] = mapped_column(
String(20), nullable=False, default="normal", index=True
)
required_action: Mapped[str | None] = mapped_column(String(500), nullable=True)
action_url: Mapped[str | None] = mapped_column(String(1500), nullable=True)
due_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True, index=True
)
deferred_until: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True, index=True
)
source_module: Mapped[str | None] = mapped_column(
String(100), nullable=True, index=True
)
source_resource_type: Mapped[str | None] = mapped_column(
String(100), nullable=True, index=True
)
source_resource_id: Mapped[str | None] = mapped_column(
String(255), nullable=True, index=True
)
source_revision: Mapped[str | None] = mapped_column(String(255), nullable=True)
sources: Mapped[list[dict[str, Any]]] = mapped_column(
JSON, default=list, nullable=False
)
provenance: Mapped[dict[str, Any]] = mapped_column(
JSON, default=dict, nullable=False
)
metadata_: Mapped[dict[str, Any]] = mapped_column(
"metadata", JSON, default=dict, nullable=False
)
revision: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
idempotency_key: Mapped[str] = mapped_column(
String(255), nullable=False, index=True
)
request_sha256: Mapped[str] = mapped_column(String(64), nullable=False)
created_by: Mapped[str | None] = mapped_column(
String(255), nullable=True, index=True
)
updated_by: Mapped[str | None] = mapped_column(
String(255), nullable=True, index=True
)
completed_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True
)
completed_by: Mapped[str | None] = mapped_column(String(255), nullable=True)
cancelled_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True
)
assignments: Mapped[list["TaskAssignment"]] = relationship(
back_populates="task",
cascade="all, delete-orphan",
order_by="TaskAssignment.created_at",
)
class TaskAssignment(Base, TimestampMixin):
__tablename__ = "task_assignments"
__table_args__ = (
UniqueConstraint(
"task_id",
"assignment_kind",
"assignment_id",
name="uq_task_assignment_target",
),
Index(
"ix_task_assignment_lookup",
"tenant_id",
"assignment_kind",
"assignment_id",
"task_id",
),
)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid)
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
task_id: Mapped[str] = mapped_column(
ForeignKey("task_items.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
assignment_kind: Mapped[str] = mapped_column(String(40), nullable=False, index=True)
assignment_id: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
assignment_label: Mapped[str | None] = mapped_column(String(500), nullable=True)
task: Mapped[TaskItem] = relationship(back_populates="assignments")
__all__ = ["TaskAssignment", "TaskItem", "new_uuid"]