
- Implemented comprehensive multi-tenant data isolation using database-level security - Built JWT authentication system with role-based access control (Super Admin, Org Admin, User, Viewer) - Created RESTful API endpoints for user and organization operations - Added complete audit logging for all data modifications with IP tracking - Implemented API rate limiting and input validation with security middleware - Built webhook processing engine with async event handling and retry logic - Created external API call handlers with circuit breaker pattern and error handling - Implemented data synchronization between external services and internal data - Added integration health monitoring and status tracking - Created three mock external services (User Management, Payment, Communication) - Implemented idempotency for webhook processing to handle duplicates gracefully - Added comprehensive security headers and XSS/CSRF protection - Set up Alembic database migrations with proper SQLite configuration - Included extensive documentation and API examples Architecture features: - Multi-tenant isolation at database level - Circuit breaker pattern for external API resilience - Async background task processing - Complete audit trail with user context - Role-based permission system - Webhook signature verification - Request validation and sanitization - Health monitoring endpoints Co-Authored-By: Claude <noreply@anthropic.com>
71 lines
2.7 KiB
Python
71 lines
2.7 KiB
Python
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey, Text, Enum
|
|
from sqlalchemy.sql import func
|
|
from sqlalchemy.orm import relationship
|
|
from app.db.base import Base
|
|
import enum
|
|
|
|
|
|
class IntegrationType(str, enum.Enum):
|
|
USER_MANAGEMENT = "user_management"
|
|
PAYMENT = "payment"
|
|
COMMUNICATION = "communication"
|
|
|
|
|
|
class WebhookStatus(str, enum.Enum):
|
|
PENDING = "pending"
|
|
PROCESSING = "processing"
|
|
SUCCESS = "success"
|
|
FAILED = "failed"
|
|
RETRY = "retry"
|
|
|
|
|
|
class ExternalIntegration(Base):
|
|
__tablename__ = "external_integrations"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
organization_id = Column(Integer, ForeignKey("organizations.id"), nullable=False)
|
|
name = Column(String(255), nullable=False)
|
|
type = Column(Enum(IntegrationType), nullable=False)
|
|
endpoint_url = Column(String(500), nullable=False)
|
|
api_key = Column(String(500))
|
|
is_active = Column(Boolean, default=True)
|
|
config = Column(Text)
|
|
last_sync = Column(DateTime(timezone=True))
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
webhooks = relationship("WebhookEvent", back_populates="integration")
|
|
health_checks = relationship("IntegrationHealth", back_populates="integration")
|
|
|
|
|
|
class WebhookEvent(Base):
|
|
__tablename__ = "webhook_events"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
organization_id = Column(Integer, ForeignKey("organizations.id"), nullable=False)
|
|
integration_id = Column(Integer, ForeignKey("external_integrations.id"), nullable=False)
|
|
external_id = Column(String(255), nullable=False, index=True)
|
|
event_type = Column(String(100), nullable=False)
|
|
payload = Column(Text, nullable=False)
|
|
status = Column(Enum(WebhookStatus), default=WebhookStatus.PENDING)
|
|
retry_count = Column(Integer, default=0)
|
|
max_retries = Column(Integer, default=3)
|
|
error_message = Column(Text)
|
|
processed_at = Column(DateTime(timezone=True))
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
integration = relationship("ExternalIntegration", back_populates="webhooks")
|
|
|
|
|
|
class IntegrationHealth(Base):
|
|
__tablename__ = "integration_health"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
integration_id = Column(Integer, ForeignKey("external_integrations.id"), nullable=False)
|
|
status = Column(String(50), nullable=False)
|
|
response_time = Column(Integer)
|
|
error_message = Column(Text)
|
|
checked_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
integration = relationship("ExternalIntegration", back_populates="health_checks") |