from sqlalchemy import String, Integer, ForeignKey, UniqueConstraint, func from sqlalchemy.orm import Mapped, mapped_column from datetime import datetime from app.database import Base class NtfySent(Base): """ Deduplication table for ntfy notifications. Prevents the background job from re-sending the same notification within a given time window. Scoped per-user. Key format: "{type}:{entity_id}:{date_window}" Examples: "reminder:42:2026-02-25" "event:17:2026-02-25T09:00" "todo:8:2026-02-25" "project:3:2026-02-25" """ __tablename__ = "ntfy_sent" __table_args__ = ( UniqueConstraint("user_id", "notification_key", name="uq_ntfy_sent_user_key"), ) id: Mapped[int] = mapped_column(primary_key=True) user_id: Mapped[int] = mapped_column( Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True ) notification_key: Mapped[str] = mapped_column(String(255), index=True) sent_at: Mapped[datetime] = mapped_column(default=func.now(), server_default=func.now())