ifixkart-backend/app/models/NotificationModel.py
adithiyan 10bfba7d58 feat: add warranties, refunds, notifications, CRM calendar, system config & delivery modules
- New models: WarrantyModel, WalletModel, SystemConfigModel
- New schemas: WarrantySchema, RefundSchema, SystemConfig, CrmTaskCalendar, NotificationTemplate, NotificationGateway, RepairErp, ReviewModeration, InventoryPos, MultiDashboard, BillingFinance
- New services: WarrantyService, RefundService, CrmService, CrmTaskCalendarService, DeliveryService, EmailTemplateService, InventoryPosService, MultiDashboardService, NotificationGatewayService, RepairErpService, ReviewModerationService, SystemConfigService, TemplateService, CreditService
- Updated routers: AdminOrder, BillingFinance, Cart, Catalog, Checkout, Crm, InventoryPos, NotificationGateway, RolePermission, ServiceJob, Settings, Storefront, UserCreation
- Updated models: Inventory, Notification, Order, Product, Service, Setting, SystemConfig, User
- Updated core: database init, invoice generator, main app
- Added WarrantyRouter with full CRUD and claim flow
2026-10-13 22:05:16 +05:30

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"),
)