מסד נתונים

תיעוד מלא של מודלי מסד הנתונים, טבלאות, שדות ויחסים של מערכת Communication Hub.

Database: PostgreSQL 15.14 | ORM: SQLAlchemy 2.x | Tables: 15+ טבלאות

דיאגרמת יחסים (ERD)

CommunicationMessage
הודעות
CommunicationTemplate
תבניות
CommunicationChannel
ערוצים
CommunicationCampaign
קמפיינים
MessageQueue
תור
CommunicationAnalytics
אנליטיקס
יחסים: Message ↔ Template, Message ↔ Channel, Campaign ↔ Template

CommunicationMessage

טבלת ההודעות הראשית - מאחסנת את כל ההודעות שנשלחו/בתור

communication_messages

שדה סוג אילוצים תיאור
id Integer PK מזהה ייחודי
channel_id Integer FKIDX מזהה ערוץ תקשורת
template_id Integer FK מזהה תבנית (אופציונלי)
campaign_id Integer FK מזהה קמפיין (אופציונלי)
customer_id Integer FKIDX מזהה לקוח
recipient String(255) IDX כתובת נמען (email/phone)
channel_type Enum IDX email, whatsapp, sms, push
subject String(500) נושא (email)
content Text תוכן ההודעה (plain text)
html_content Text תוכן HTML (email)
status Enum IDX pending, queued, sent, delivered, failed
priority Enum low, normal, high, urgent
scheduled_at DateTime זמן שליחה מתוזמן
sent_at DateTime זמן שליחה בפועל
delivered_at DateTime זמן מסירה
opened_at DateTime זמן פתיחה
clicked_at DateTime זמן קליק ראשון
provider_message_id String(255) מזהה הודעה מהספק
error_message Text הודעת שגיאה (אם נכשל)
retry_count Integer מספר ניסיונות חוזרים
extra_data JSON נתונים נוספים
created_at DateTime IDX תאריך יצירה
updated_at DateTime תאריך עדכון

SQLAlchemy Model

class CommunicationMessage(db.Model): __tablename__ = 'communication_messages' id = db.Column(db.Integer, primary_key=True) channel_id = db.Column(db.Integer, db.ForeignKey('communication_channels.id')) template_id = db.Column(db.Integer, db.ForeignKey('communication_templates.id')) campaign_id = db.Column(db.Integer, db.ForeignKey('communication_campaigns.id')) customer_id = db.Column(db.Integer, db.ForeignKey('customers.id'), index=True) recipient = db.Column(db.String(255), nullable=False, index=True) channel_type = db.Column(db.Enum('email', 'whatsapp', 'sms', 'push'), index=True) subject = db.Column(db.String(500)) content = db.Column(db.Text, nullable=False) html_content = db.Column(db.Text) status = db.Column( db.Enum('pending', 'queued', 'sent', 'delivered', 'failed', 'bounced'), default='pending', index=True ) priority = db.Column(db.Enum('low', 'normal', 'high', 'urgent'), default='normal') scheduled_at = db.Column(db.DateTime) sent_at = db.Column(db.DateTime) delivered_at = db.Column(db.DateTime) opened_at = db.Column(db.DateTime) clicked_at = db.Column(db.DateTime) provider_message_id = db.Column(db.String(255)) error_message = db.Column(db.Text) retry_count = db.Column(db.Integer, default=0) extra_data = db.Column(db.JSON) created_at = db.Column(db.DateTime, default=datetime.utcnow, index=True) updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow) # Relationships channel = db.relationship('CommunicationChannel', backref='messages') template = db.relationship('CommunicationTemplate', backref='messages') campaign = db.relationship('CommunicationCampaign', backref='messages') customer = db.relationship('Customer', backref='communication_messages')

CommunicationTemplate

טבלת תבניות ההודעות

communication_templates

שדה סוג אילוצים תיאור
id Integer PK מזהה ייחודי
name String(255) UQ שם התבנית
channel_type Enum IDX סוג ערוץ
category Enum IDX transactional, marketing, notification, reminder
subject String(500) נושא עם משתנים
content Text תוכן עם משתנים
html_content Text תוכן HTML
variables JSON הגדרות משתנים
is_active Boolean האם התבנית פעילה
usage_count Integer מספר שימושים
created_by Integer FK מזהה היוצר
created_at DateTime תאריך יצירה
updated_at DateTime תאריך עדכון

CommunicationChannel

טבלת הגדרות ערוצי התקשורת

communication_channels

שדה סוג אילוצים תיאור
id Integer PK מזהה ייחודי
name String(255) שם הערוץ
channel_type Enum IDX email, whatsapp, sms, push
provider String(100) smtp, sendgrid, green-api, twilio...
config JSON הגדרות החיבור (מוצפן)
is_active Boolean האם הערוץ פעיל
is_default Boolean ערוץ ברירת מחדל לסוג
daily_limit Integer מגבלה יומית
sent_today Integer נשלחו היום
last_used_at DateTime שימוש אחרון
created_at DateTime תאריך יצירה

CommunicationCampaign

טבלת קמפיינים לשליחה המונית

communication_campaigns

שדה סוג תיאור
id Integer מזהה ייחודי
name String(255) שם הקמפיין
description Text תיאור
channel_type Enum סוג ערוץ
template_id Integer מזהה תבנית
segment_id Integer מזהה סגמנט (אופציונלי)
recipients_data JSON נתוני נמענים
status Enum draft, scheduled, running, paused, completed, failed
scheduled_at DateTime תזמון שליחה
started_at DateTime זמן התחלה
completed_at DateTime זמן סיום
settings JSON הגדרות (batch_size, delay...)
total_recipients Integer סה"כ נמענים
sent_count Integer נשלחו
delivered_count Integer נמסרו
opened_count Integer נפתחו
clicked_count Integer הקליקו
failed_count Integer נכשלו
created_by Integer מזהה היוצר

טבלאות נוספות

MessageQueue

תור הודעות לעיבוד

  • message_id - מזהה הודעה
  • priority - עדיפות בתור
  • attempts - ניסיונות
  • next_retry_at - ניסיון הבא
  • locked_until - נעילה
MessageTracking

מעקב פעילות הודעות

  • message_id - מזהה הודעה
  • event_type - open, click, bounce
  • event_data - נתוני האירוע
  • ip_address - כתובת IP
  • user_agent - דפדפן
Unsubscribes

רשימת הסרות מתפוצה

  • email - כתובת מייל
  • phone - מספר טלפון
  • channel_type - סוג ערוץ
  • reason - סיבה
  • unsubscribed_at - תאריך
ChannelLogs

לוגים של ערוצים

  • channel_id - מזהה ערוץ
  • action - פעולה
  • status - סטטוס
  • response - תגובה
  • created_at - תאריך

אינדקסים

אינדקסים מוגדרים לשיפור ביצועי שאילתות נפוצות:

-- Messages indexes CREATE INDEX idx_messages_recipient ON communication_messages(recipient); CREATE INDEX idx_messages_status ON communication_messages(status); CREATE INDEX idx_messages_channel_type ON communication_messages(channel_type); CREATE INDEX idx_messages_customer_id ON communication_messages(customer_id); CREATE INDEX idx_messages_created_at ON communication_messages(created_at); CREATE INDEX idx_messages_campaign_id ON communication_messages(campaign_id); -- Composite index for common queries CREATE INDEX idx_messages_status_channel ON communication_messages(status, channel_type); CREATE INDEX idx_messages_customer_status ON communication_messages(customer_id, status); -- Templates indexes CREATE UNIQUE INDEX idx_templates_name ON communication_templates(name); CREATE INDEX idx_templates_channel ON communication_templates(channel_type); CREATE INDEX idx_templates_category ON communication_templates(category); CREATE INDEX idx_templates_active ON communication_templates(is_active); -- Campaigns indexes CREATE INDEX idx_campaigns_status ON communication_campaigns(status); CREATE INDEX idx_campaigns_scheduled ON communication_campaigns(scheduled_at); CREATE INDEX idx_campaigns_channel ON communication_campaigns(channel_type);