- Backend: FastAPI app with all models, services, schemas, routes - Admin Panel: Next.js dashboard with all modules (CRM, POS, repairs, warranties, billing, notifications, etc.) - Ecommerce: storefront (submodule) + product catalog - AI Agents: .agents/ skill library for Antigravity IDE - Docs: architecture, billing, notification gateway markdown - Submodules: Backend/OpenWA, Ecommerce/File 2/storefront
86 lines
3.9 KiB
Python
86 lines
3.9 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)
|
|
|
|
notification_type = Column(String(50), nullable=True, index=True) # QUOTATION, INVOICE, REPAIR_TICKET, LEAD_QUALIFIED
|
|
entity_id = Column(String(100), nullable=True, index=True)
|
|
idempotency_key = Column(String(150), 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="QUEUED", index=True) # QUEUED, PROCESSING, ACCEPTED, SENT, DELIVERED, OPENED, FAILED, RETRYING, DLQ
|
|
|
|
subject = Column(String(255), nullable=True)
|
|
message_body = Column(Text, nullable=False)
|
|
|
|
attempt_count = Column(Integer, nullable=False, default=0)
|
|
last_attempt_at = Column(DateTime, nullable=True)
|
|
next_retry_at = Column(DateTime, nullable=True)
|
|
last_error = Column(Text, nullable=True)
|
|
provider_response = Column(Text, nullable=True)
|
|
|
|
completed_at = Column(DateTime, nullable=True)
|
|
|
|
attempts = relationship("NotificationAttempt", back_populates="notification", cascade="all, delete-orphan")
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint("tenant_id", "idempotency_key", name="uq_tenant_notification_idempotency"),
|
|
)
|
|
|
|
|
|
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"),
|
|
)
|