File size: 14,545 Bytes
8bab08d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
-- CX Platform - Extended Database Schema
-- Adds tickets, knowledge base, chat, and customer interaction tracking
-- =============================================================================
-- CUSTOMERS (Enhanced from contacts)
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_customers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE NOT NULL,
first_name TEXT,
last_name TEXT,
company TEXT,
phone TEXT,
-- Segmentation
segment TEXT DEFAULT 'standard', -- vip, standard, at_risk, churned
lifecycle_stage TEXT DEFAULT 'active', -- new, active, at_risk, churned
-- Metrics
lifetime_value REAL DEFAULT 0.0,
satisfaction_score REAL DEFAULT 0.0, -- CSAT average
nps_score INTEGER, -- Net Promoter Score
sentiment TEXT DEFAULT 'neutral', -- positive, neutral, negative
-- Tracking
first_interaction_at TIMESTAMP,
last_interaction_at TIMESTAMP,
total_interactions INTEGER DEFAULT 0,
total_tickets INTEGER DEFAULT 0,
-- Metadata
tags TEXT, -- JSON array
custom_fields TEXT, -- JSON object
notes TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_cx_customers_email ON cx_customers(email);
CREATE INDEX idx_cx_customers_segment ON cx_customers(segment);
CREATE INDEX idx_cx_customers_sentiment ON cx_customers(sentiment);
-- =============================================================================
-- TICKETS
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_tickets (
id INTEGER PRIMARY KEY AUTOINCREMENT,
customer_id INTEGER NOT NULL,
-- Core fields
subject TEXT NOT NULL,
description TEXT,
status TEXT DEFAULT 'new', -- new, open, pending, resolved, closed
priority TEXT DEFAULT 'medium', -- low, medium, high, urgent
category TEXT, -- technical, billing, feature_request, etc.
-- Assignment
assigned_to TEXT, -- agent name/id
assigned_team TEXT,
-- SLA
sla_due_at TIMESTAMP,
first_response_at TIMESTAMP,
resolved_at TIMESTAMP,
closed_at TIMESTAMP,
-- Metrics
response_time_minutes INTEGER,
resolution_time_minutes INTEGER,
reopened_count INTEGER DEFAULT 0,
-- AI fields
sentiment TEXT, -- detected from description
ai_suggested_category TEXT,
ai_confidence REAL,
auto_resolved BOOLEAN DEFAULT 0,
-- Metadata
source TEXT DEFAULT 'manual', -- manual, email, chat, api, web_form
tags TEXT, -- JSON array
custom_fields TEXT, -- JSON
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (customer_id) REFERENCES cx_customers(id) ON DELETE CASCADE
);
CREATE INDEX idx_cx_tickets_customer ON cx_tickets(customer_id);
CREATE INDEX idx_cx_tickets_status ON cx_tickets(status);
CREATE INDEX idx_cx_tickets_priority ON cx_tickets(priority);
CREATE INDEX idx_cx_tickets_assigned_to ON cx_tickets(assigned_to);
CREATE INDEX idx_cx_tickets_sla_due ON cx_tickets(sla_due_at);
-- =============================================================================
-- TICKET MESSAGES
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_ticket_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ticket_id INTEGER NOT NULL,
-- Sender
sender_type TEXT NOT NULL, -- customer, agent, system, ai_bot
sender_id TEXT, -- customer_id, agent_id, or 'system'
sender_name TEXT,
-- Message
message TEXT NOT NULL,
message_html TEXT,
is_internal BOOLEAN DEFAULT 0, -- internal note vs customer-visible
-- AI fields
sentiment TEXT,
intent TEXT, -- question, complaint, praise, feedback
-- Metadata
meta_data TEXT, -- JSON
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (ticket_id) REFERENCES cx_tickets(id) ON DELETE CASCADE
);
CREATE INDEX idx_cx_ticket_messages_ticket ON cx_ticket_messages(ticket_id);
CREATE INDEX idx_cx_ticket_messages_created ON cx_ticket_messages(created_at);
-- =============================================================================
-- TICKET ATTACHMENTS
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_ticket_attachments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ticket_id INTEGER NOT NULL,
message_id INTEGER,
filename TEXT NOT NULL,
file_path TEXT NOT NULL,
file_size INTEGER,
mime_type TEXT,
uploaded_by TEXT,
uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (ticket_id) REFERENCES cx_tickets(id) ON DELETE CASCADE,
FOREIGN KEY (message_id) REFERENCES cx_ticket_messages(id) ON DELETE SET NULL
);
-- =============================================================================
-- KNOWLEDGE BASE
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_kb_categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT,
parent_id INTEGER,
display_order INTEGER DEFAULT 0,
icon TEXT,
is_active BOOLEAN DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (parent_id) REFERENCES cx_kb_categories(id) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS cx_kb_articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
category_id INTEGER,
-- Content
title TEXT NOT NULL,
summary TEXT,
content TEXT NOT NULL,
content_html TEXT,
-- Status
status TEXT DEFAULT 'draft', -- draft, published, archived
visibility TEXT DEFAULT 'public', -- public, internal, private
-- SEO
slug TEXT UNIQUE,
meta_description TEXT,
-- Metrics
view_count INTEGER DEFAULT 0,
helpful_count INTEGER DEFAULT 0,
not_helpful_count INTEGER DEFAULT 0,
average_rating REAL DEFAULT 0.0,
-- AI fields
ai_generated BOOLEAN DEFAULT 0,
ai_confidence REAL,
keywords TEXT, -- JSON array for semantic search
-- Versioning
version INTEGER DEFAULT 1,
-- Metadata
tags TEXT, -- JSON array
related_articles TEXT, -- JSON array of article IDs
-- Authoring
author TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
published_at TIMESTAMP,
FOREIGN KEY (category_id) REFERENCES cx_kb_categories(id) ON DELETE SET NULL
);
CREATE INDEX idx_cx_kb_articles_category ON cx_kb_articles(category_id);
CREATE INDEX idx_cx_kb_articles_status ON cx_kb_articles(status);
CREATE INDEX idx_cx_kb_articles_slug ON cx_kb_articles(slug);
-- =============================================================================
-- KB ARTICLE VERSIONS
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_kb_article_versions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
article_id INTEGER NOT NULL,
version INTEGER NOT NULL,
title TEXT NOT NULL,
content TEXT NOT NULL,
changed_by TEXT,
change_note TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (article_id) REFERENCES cx_kb_articles(id) ON DELETE CASCADE,
UNIQUE(article_id, version)
);
-- =============================================================================
-- LIVE CHAT SESSIONS
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_chat_sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
customer_id INTEGER,
-- Session info
session_id TEXT UNIQUE NOT NULL,
status TEXT DEFAULT 'active', -- active, waiting, assigned, closed
-- Routing
assigned_to TEXT, -- agent name/id
assigned_at TIMESTAMP,
-- AI bot
bot_active BOOLEAN DEFAULT 1,
bot_handed_off BOOLEAN DEFAULT 0,
bot_handoff_reason TEXT,
-- Metrics
wait_time_seconds INTEGER DEFAULT 0,
response_time_seconds INTEGER DEFAULT 0,
message_count INTEGER DEFAULT 0,
-- Metadata
page_url TEXT,
referrer TEXT,
user_agent TEXT,
ip_address TEXT,
-- Satisfaction
rated BOOLEAN DEFAULT 0,
rating INTEGER,
feedback TEXT,
started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
ended_at TIMESTAMP,
FOREIGN KEY (customer_id) REFERENCES cx_customers(id) ON DELETE SET NULL
);
CREATE INDEX idx_cx_chat_sessions_customer ON cx_chat_sessions(customer_id);
CREATE INDEX idx_cx_chat_sessions_status ON cx_chat_sessions(status);
CREATE INDEX idx_cx_chat_sessions_assigned_to ON cx_chat_sessions(assigned_to);
-- =============================================================================
-- CHAT MESSAGES
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_chat_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id INTEGER NOT NULL,
-- Sender
sender_type TEXT NOT NULL, -- customer, agent, bot, system
sender_id TEXT,
sender_name TEXT,
-- Message
message TEXT NOT NULL,
message_type TEXT DEFAULT 'text', -- text, image, file, system_message
-- AI fields
is_bot_response BOOLEAN DEFAULT 0,
bot_confidence REAL,
intent TEXT,
-- Status
is_read BOOLEAN DEFAULT 0,
read_at TIMESTAMP,
-- Metadata
meta_data TEXT, -- JSON
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (session_id) REFERENCES cx_chat_sessions(id) ON DELETE CASCADE
);
CREATE INDEX idx_cx_chat_messages_session ON cx_chat_messages(session_id);
CREATE INDEX idx_cx_chat_messages_created ON cx_chat_messages(created_at);
-- =============================================================================
-- AUTOMATION RULES
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_automation_rules (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT,
is_active BOOLEAN DEFAULT 1,
-- Trigger
trigger_type TEXT NOT NULL, -- ticket_created, ticket_updated, time_based, etc.
trigger_conditions TEXT NOT NULL, -- JSON
-- Actions
actions TEXT NOT NULL, -- JSON array of actions
-- Execution
execution_count INTEGER DEFAULT 0,
last_executed_at TIMESTAMP,
-- Priority
priority INTEGER DEFAULT 0,
created_by TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- =============================================================================
-- CUSTOMER INTERACTIONS
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_interactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
customer_id INTEGER NOT NULL,
type TEXT NOT NULL, -- ticket, chat, email, call, meeting
channel TEXT, -- web, email, phone, chat, api
summary TEXT,
sentiment TEXT,
intent TEXT,
-- References
reference_type TEXT, -- ticket, chat_session, email, etc.
reference_id INTEGER,
-- Metrics
duration_seconds INTEGER,
satisfaction_rating INTEGER,
-- Agent
handled_by TEXT,
occurred_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (customer_id) REFERENCES cx_customers(id) ON DELETE CASCADE
);
CREATE INDEX idx_cx_interactions_customer ON cx_interactions(customer_id);
CREATE INDEX idx_cx_interactions_type ON cx_interactions(type);
CREATE INDEX idx_cx_interactions_occurred ON cx_interactions(occurred_at);
-- =============================================================================
-- ANALYTICS SNAPSHOTS (Enhanced)
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_analytics_daily (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date DATE NOT NULL UNIQUE,
-- Ticket metrics
tickets_created INTEGER DEFAULT 0,
tickets_resolved INTEGER DEFAULT 0,
tickets_reopened INTEGER DEFAULT 0,
avg_resolution_time_minutes REAL DEFAULT 0.0,
avg_first_response_minutes REAL DEFAULT 0.0,
-- Chat metrics
chats_started INTEGER DEFAULT 0,
chats_completed INTEGER DEFAULT 0,
avg_wait_time_seconds REAL DEFAULT 0.0,
bot_resolution_rate REAL DEFAULT 0.0,
-- Satisfaction
avg_csat REAL DEFAULT 0.0,
avg_nps INTEGER DEFAULT 0,
-- KB metrics
kb_views INTEGER DEFAULT 0,
kb_helpful_votes INTEGER DEFAULT 0,
kb_searches INTEGER DEFAULT 0,
-- Sentiment
positive_interactions INTEGER DEFAULT 0,
neutral_interactions INTEGER DEFAULT 0,
negative_interactions INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_cx_analytics_daily_date ON cx_analytics_daily(date);
-- =============================================================================
-- CANNED RESPONSES (Templates)
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_canned_responses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
shortcut TEXT UNIQUE, -- e.g., "/greeting"
category TEXT,
subject TEXT,
content TEXT NOT NULL,
-- Usage
use_count INTEGER DEFAULT 0,
last_used_at TIMESTAMP,
is_active BOOLEAN DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- =============================================================================
-- AGENT PERFORMANCE
-- =============================================================================
CREATE TABLE IF NOT EXISTS cx_agent_stats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
agent_id TEXT NOT NULL,
agent_name TEXT NOT NULL,
date DATE NOT NULL,
-- Tickets
tickets_handled INTEGER DEFAULT 0,
tickets_resolved INTEGER DEFAULT 0,
avg_resolution_time_minutes REAL DEFAULT 0.0,
-- Chats
chats_handled INTEGER DEFAULT 0,
avg_chat_duration_minutes REAL DEFAULT 0.0,
-- Quality
avg_csat REAL DEFAULT 0.0,
positive_feedbacks INTEGER DEFAULT 0,
negative_feedbacks INTEGER DEFAULT 0,
-- Efficiency
avg_response_time_minutes REAL DEFAULT 0.0,
first_contact_resolutions INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(agent_id, date)
);
CREATE INDEX idx_cx_agent_stats_agent ON cx_agent_stats(agent_id);
CREATE INDEX idx_cx_agent_stats_date ON cx_agent_stats(date);
|