53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
"""Contact ORM model and schemas."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from pydantic import BaseModel, ConfigDict, EmailStr
|
|
from sqlalchemy import DateTime, ForeignKey, Integer, String, func
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.models.base import Base
|
|
|
|
|
|
class Contact(Base):
|
|
"""Represents a CRM contact belonging to an organization."""
|
|
|
|
__tablename__ = "contacts"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
organization_id: Mapped[int] = mapped_column(ForeignKey("organizations.id", ondelete="CASCADE"))
|
|
owner_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="RESTRICT"))
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
email: Mapped[str | None] = mapped_column(String(320), nullable=True)
|
|
phone: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
server_default=func.now(),
|
|
nullable=False,
|
|
)
|
|
|
|
organization = relationship("Organization", back_populates="contacts")
|
|
owner = relationship("User", back_populates="owned_contacts")
|
|
deals = relationship("Deal", back_populates="contact")
|
|
|
|
|
|
class ContactBase(BaseModel):
|
|
organization_id: int
|
|
owner_id: int
|
|
name: str
|
|
email: EmailStr | None = None
|
|
phone: str | None = None
|
|
|
|
|
|
class ContactCreate(ContactBase):
|
|
pass
|
|
|
|
|
|
class ContactRead(ContactBase):
|
|
id: int
|
|
created_at: datetime
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|