72 lines
3.3 KiB
Python
72 lines
3.3 KiB
Python
from datetime import datetime, timezone
|
|
from sqlalchemy import Column, String, Integer, Float, Boolean, DateTime, Text, ForeignKey, UniqueConstraint
|
|
from sqlalchemy.orm import relationship
|
|
import ulid
|
|
|
|
from app.core.database.db_session import Base
|
|
from app.models.audit_base import AuditMixin
|
|
|
|
class Notification(Base, AuditMixin):
|
|
__tablename__ = "notifications"
|
|
|
|
notification_id = Column(String(36), primary_key=True, default=lambda: str(ulid.ULID()))
|
|
tenant_id = Column(String(36), nullable=False, default="tenant_default", index=True)
|
|
event_id = Column(String(36), nullable=True, index=True)
|
|
|
|
recipient = Column(String(255), nullable=False, index=True)
|
|
channel = Column(String(50), nullable=False) # WHATSAPP, EMAIL, WEB_PUSH
|
|
template_id = Column(String(100), nullable=True)
|
|
status = Column(String(50), nullable=False, default="PENDING", index=True) # PENDING, QUEUED, SENDING, SENT, DELIVERED, READ, FAILED, CANCELLED
|
|
|
|
subject = Column(String(255), nullable=True)
|
|
message_body = Column(Text, nullable=False)
|
|
|
|
completed_at = Column(DateTime, nullable=True)
|
|
|
|
attempts = relationship("NotificationAttempt", back_populates="notification", cascade="all, delete-orphan")
|
|
|
|
|
|
class NotificationAttempt(Base):
|
|
__tablename__ = "notification_attempts"
|
|
|
|
attempt_id = Column(String(36), primary_key=True, default=lambda: str(ulid.ULID()))
|
|
notification_id = Column(String(36), ForeignKey("notifications.notification_id", ondelete="CASCADE"), nullable=False, index=True)
|
|
|
|
attempt_number = Column(Integer, nullable=False, default=1)
|
|
provider = Column(String(50), nullable=False) # OpenWA, ZeptoMail, FCM
|
|
request_id = Column(String(100), nullable=True)
|
|
status = Column(String(50), nullable=False) # SUCCESS, FAILED
|
|
error_log = Column(Text, nullable=True)
|
|
created_at = Column(DateTime, nullable=False, default=lambda: datetime.now(timezone.utc))
|
|
|
|
notification = relationship("Notification", back_populates="attempts")
|
|
|
|
|
|
class DeadLetterNotification(Base):
|
|
__tablename__ = "dead_letter_notifications"
|
|
|
|
dlq_id = Column(String(36), primary_key=True, default=lambda: str(ulid.ULID()))
|
|
tenant_id = Column(String(36), nullable=False, default="tenant_default", index=True)
|
|
notification_id = Column(String(36), ForeignKey("notifications.notification_id", ondelete="CASCADE"), nullable=False, index=True)
|
|
|
|
reason = Column(Text, nullable=False)
|
|
failed_at = Column(DateTime, nullable=False, default=lambda: datetime.now(timezone.utc))
|
|
is_resolved = Column(Boolean, nullable=False, default=False)
|
|
resolved_at = Column(DateTime, nullable=True)
|
|
|
|
|
|
class WhatsAppSession(Base, AuditMixin):
|
|
__tablename__ = "whatsapp_sessions"
|
|
|
|
session_id = Column(String(36), primary_key=True, default=lambda: str(ulid.ULID()))
|
|
tenant_id = Column(String(36), nullable=False, default="tenant_default", index=True)
|
|
|
|
session_name = Column(String(100), nullable=False)
|
|
status = Column(String(50), nullable=False, default="DISCONNECTED") # CONNECTED, DISCONNECTED
|
|
battery_level = Column(Integer, nullable=True)
|
|
qr_code_base64 = Column(Text, nullable=True)
|
|
last_ping_at = Column(DateTime, nullable=True)
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint("tenant_id", "session_name", name="uq_tenant_whatsapp_session_name"),
|
|
)
|