2026-06-04 00:06:17 +00:00
|
|
|
"""Activity model: calls, meetings, emails, tasks, notes tied to any entity."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from enum import Enum
|
2026-06-10 21:24:24 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2026-06-04 00:06:17 +00:00
|
|
|
|
|
|
|
|
from sqlalchemy import DateTime, ForeignKey, String, Text
|
|
|
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
|
|
|
|
|
|
from app.models.base import Base, OrgScopedMixin, SoftDeleteMixin, TimestampMixin
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from app.models.account import Account
|
|
|
|
|
from app.models.contact import Contact
|
|
|
|
|
from app.models.deal import Deal
|
2026-06-10 21:24:24 +00:00
|
|
|
from app.models.user import User
|
2026-06-04 00:06:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ActivityType(str, Enum):
|
|
|
|
|
"""Type of activity."""
|
|
|
|
|
|
|
|
|
|
call = "call"
|
|
|
|
|
meeting = "meeting"
|
|
|
|
|
email = "email"
|
|
|
|
|
task = "task"
|
|
|
|
|
note = "note"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Activity(Base, TimestampMixin, SoftDeleteMixin, OrgScopedMixin):
|
|
|
|
|
__tablename__ = "activities"
|
|
|
|
|
|
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
2026-06-10 21:24:24 +00:00
|
|
|
type: Mapped[ActivityType] = mapped_column(String(32), nullable=False, index=True)
|
2026-06-04 00:06:17 +00:00
|
|
|
subject: Mapped[str] = mapped_column(String(255), nullable=False)
|
2026-06-10 21:24:24 +00:00
|
|
|
body: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
|
|
|
due_date: Mapped[datetime | None] = mapped_column(
|
2026-06-04 00:06:17 +00:00
|
|
|
DateTime(timezone=True), nullable=True, index=True
|
|
|
|
|
)
|
2026-06-10 21:24:24 +00:00
|
|
|
completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
|
|
|
account_id: Mapped[int | None] = mapped_column(
|
2026-06-04 00:06:17 +00:00
|
|
|
ForeignKey("accounts.id"), nullable=True, index=True
|
|
|
|
|
)
|
2026-06-10 21:24:24 +00:00
|
|
|
contact_id: Mapped[int | None] = mapped_column(
|
2026-06-04 00:06:17 +00:00
|
|
|
ForeignKey("contacts.id"), nullable=True, index=True
|
|
|
|
|
)
|
2026-06-10 21:24:24 +00:00
|
|
|
deal_id: Mapped[int | None] = mapped_column(ForeignKey("deals.id"), nullable=True, index=True)
|
|
|
|
|
owner_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False, index=True)
|
2026-06-04 00:06:17 +00:00
|
|
|
|
|
|
|
|
# Relationships
|
2026-06-10 21:24:24 +00:00
|
|
|
account: Mapped[Account | None] = relationship(
|
2026-06-04 00:06:17 +00:00
|
|
|
"Account", back_populates="activities", lazy="selectin"
|
|
|
|
|
)
|
2026-06-10 21:24:24 +00:00
|
|
|
contact: Mapped[Contact | None] = relationship(
|
2026-06-04 00:06:17 +00:00
|
|
|
"Contact", back_populates="activities", lazy="selectin"
|
|
|
|
|
)
|
2026-06-10 21:24:24 +00:00
|
|
|
deal: Mapped[Deal | None] = relationship("Deal", back_populates="activities", lazy="selectin")
|
|
|
|
|
owner: Mapped[User] = relationship("User", foreign_keys=[owner_id], lazy="joined")
|
2026-06-04 00:06:17 +00:00
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"<Activity id={self.id} type={self.type} subject={self.subject!r}>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ["Activity", "ActivityType"]
|