66 lines
2.8 KiB
Python
66 lines
2.8 KiB
Python
import uuid
|
|
from datetime import datetime
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import CheckConstraint, DateTime, ForeignKey, Index, String, text
|
|
from sqlalchemy.dialects.postgresql import JSONB, UUID
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.core.db import Base
|
|
from app.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.user import User
|
|
|
|
RULE_TYPES = (
|
|
"possible_duplicate_customer",
|
|
"missing_required_field",
|
|
"odometer_regression",
|
|
"booking_overlap",
|
|
"vehicle_status_conflict",
|
|
)
|
|
SEVERITIES = ("low", "medium", "high")
|
|
ISSUE_STATUSES = ("open", "deferred", "resolved", "rejected")
|
|
|
|
|
|
class DataQualityIssue(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
|
__tablename__ = "data_quality_issues"
|
|
__table_args__ = (
|
|
CheckConstraint(
|
|
"rule_type IN ('possible_duplicate_customer','missing_required_field',"
|
|
"'odometer_regression','booking_overlap','vehicle_status_conflict')",
|
|
name="ck_data_quality_rule_type",
|
|
),
|
|
CheckConstraint("severity IN ('low','medium','high')", name="ck_data_quality_severity"),
|
|
CheckConstraint(
|
|
"status IN ('open','deferred','resolved','rejected')",
|
|
name="ck_data_quality_status",
|
|
),
|
|
Index("ix_data_quality_work_queue", "status", "due_at", "severity"),
|
|
Index(
|
|
"uq_data_quality_one_open_condition",
|
|
"rule_type",
|
|
"entity_type",
|
|
"entity_id",
|
|
unique=True,
|
|
postgresql_where=text("status = 'open'"),
|
|
),
|
|
)
|
|
|
|
public_ref: Mapped[str] = mapped_column(String(20), unique=True, nullable=False)
|
|
rule_type: Mapped[str] = mapped_column(String(40), nullable=False)
|
|
entity_type: Mapped[str] = mapped_column(String(30), nullable=False)
|
|
entity_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False)
|
|
severity: Mapped[str] = mapped_column(String(10), nullable=False)
|
|
status: Mapped[str] = mapped_column(String(20), nullable=False, default="open")
|
|
evidence_json: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
|
|
proposed_action_json: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
|
|
detected_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
|
due_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), index=True)
|
|
assigned_to_user_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), index=True
|
|
)
|
|
assigned_to_user: Mapped["User | None"] = relationship(lazy="selectin")
|
|
resolved_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
|
resolved_by: Mapped[str | None] = mapped_column(String(120))
|