File size: 7,893 Bytes
1cf0854
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sqlite3 from 'sqlite3';
import { promisify } from 'util';
import path from 'path';
import fs from 'fs';

// Enable verbose mode for debugging
const Database = sqlite3.verbose().Database;

class DatabaseConnection {
  private db: sqlite3.Database | null = null;

  async connect(): Promise<sqlite3.Database> {
    if (this.db) {
      return this.db;
    }

    const dbPath = process.env.DATABASE_URL || path.join(__dirname, '../../database.sqlite');
    
    // Ensure directory exists
    const dbDir = path.dirname(dbPath);
    if (!fs.existsSync(dbDir)) {
      fs.mkdirSync(dbDir, { recursive: true });
    }

    return new Promise((resolve, reject) => {
      this.db = new Database(dbPath, (err) => {
        if (err) {
          console.error('Error opening database:', err.message);
          reject(err);
        } else {
          console.log('Connected to SQLite database');
          resolve(this.db!);
        }
      });
    });
  }

  async query(sql: string, params: any[] = []): Promise<any[]> {
    const db = await this.connect();
    return new Promise((resolve, reject) => {
      db.all(sql, params, (err, rows) => {
        if (err) reject(err);
        else resolve(rows || []);
      });
    });
  }

  async run(sql: string, params: any[] = []): Promise<{ lastID: number; changes: number }> {
    const db = await this.connect();
    return new Promise((resolve, reject) => {
      db.run(sql, params, function(err) {
        if (err) {
          reject(err);
        } else {
          resolve({ 
            lastID: this.lastID || 0, 
            changes: this.changes || 0 
          });
        }
      });
    });
  }

  async get(sql: string, params: any[] = []): Promise<any> {
    const db = await this.connect();
    return new Promise((resolve, reject) => {
      db.get(sql, params, (err, row) => {
        if (err) reject(err);
        else resolve(row);
      });
    });
  }

  async close(): Promise<void> {
    if (this.db) {
      return new Promise((resolve, reject) => {
        this.db!.close((err) => {
          if (err) {
            reject(err);
          } else {
            this.db = null;
            resolve();
          }
        });
      });
    }
  }
}

// Singleton instance
export const database = new DatabaseConnection();

// Database initialization function
export async function initializeDatabase(): Promise<void> {
  const createTables = [
    // Users table
    `CREATE TABLE IF NOT EXISTS users (

      id INTEGER PRIMARY KEY AUTOINCREMENT,

      email TEXT UNIQUE NOT NULL,

      name TEXT NOT NULL,

      password_hash TEXT,

      avatar TEXT,

      google_id TEXT UNIQUE,

      email_verified BOOLEAN DEFAULT FALSE,

      verification_token TEXT,

      created_at DATETIME DEFAULT CURRENT_TIMESTAMP,

      updated_at DATETIME DEFAULT CURRENT_TIMESTAMP

    )`,

    // Tenants table
    `CREATE TABLE IF NOT EXISTS tenants (

      id INTEGER PRIMARY KEY AUTOINCREMENT,

      name TEXT NOT NULL,

      subdomain TEXT UNIQUE,

      plan TEXT DEFAULT 'starter',

      settings TEXT DEFAULT '{}',

      created_at DATETIME DEFAULT CURRENT_TIMESTAMP,

      updated_at DATETIME DEFAULT CURRENT_TIMESTAMP

    )`,

    // User-Tenant relationships
    `CREATE TABLE IF NOT EXISTS user_tenants (

      id INTEGER PRIMARY KEY AUTOINCREMENT,

      user_id INTEGER NOT NULL,

      tenant_id INTEGER NOT NULL,

      role TEXT DEFAULT 'owner',

      permissions TEXT DEFAULT '{}',

      created_at DATETIME DEFAULT CURRENT_TIMESTAMP,

      FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,

      FOREIGN KEY (tenant_id) REFERENCES tenants (id) ON DELETE CASCADE,

      UNIQUE(user_id, tenant_id)

    )`,

    // Domains table
    `CREATE TABLE IF NOT EXISTS domains (

      id INTEGER PRIMARY KEY AUTOINCREMENT,

      tenant_id INTEGER NOT NULL,

      domain TEXT NOT NULL,

      verified BOOLEAN DEFAULT FALSE,

      verification_token TEXT,

      created_at DATETIME DEFAULT CURRENT_TIMESTAMP,

      FOREIGN KEY (tenant_id) REFERENCES tenants (id) ON DELETE CASCADE

    )`,

    // Knowledge base table
    `CREATE TABLE IF NOT EXISTS knowledge_base (

      id INTEGER PRIMARY KEY AUTOINCREMENT,

      tenant_id INTEGER NOT NULL,

      name TEXT NOT NULL,

      type TEXT NOT NULL,

      source TEXT NOT NULL,

      status TEXT DEFAULT 'processing',

      size INTEGER,

      metadata TEXT DEFAULT '{}',

      created_at DATETIME DEFAULT CURRENT_TIMESTAMP,

      updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,

      FOREIGN KEY (tenant_id) REFERENCES tenants (id) ON DELETE CASCADE

    )`,

    // Chat sessions table
    `CREATE TABLE IF NOT EXISTS chat_sessions (

      id INTEGER PRIMARY KEY AUTOINCREMENT,

      tenant_id INTEGER NOT NULL,

      domain TEXT,

      user_ip TEXT,

      user_agent TEXT,

      session_token TEXT UNIQUE NOT NULL,

      started_at DATETIME DEFAULT CURRENT_TIMESTAMP,

      ended_at DATETIME,

      resolved BOOLEAN DEFAULT FALSE,

      rating INTEGER,

      feedback TEXT,

      FOREIGN KEY (tenant_id) REFERENCES tenants (id) ON DELETE CASCADE

    )`,

    // Chat messages table
    `CREATE TABLE IF NOT EXISTS chat_messages (

      id INTEGER PRIMARY KEY AUTOINCREMENT,

      session_id INTEGER NOT NULL,

      sender TEXT NOT NULL, -- 'user' or 'ai'

      message TEXT NOT NULL,

      metadata TEXT DEFAULT '{}',

      timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,

      FOREIGN KEY (session_id) REFERENCES chat_sessions (id) ON DELETE CASCADE

    )`,

    // Analytics events table
    `CREATE TABLE IF NOT EXISTS analytics_events (

      id INTEGER PRIMARY KEY AUTOINCREMENT,

      tenant_id INTEGER NOT NULL,

      event_type TEXT NOT NULL,

      event_data TEXT DEFAULT '{}',

      timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,

      FOREIGN KEY (tenant_id) REFERENCES tenants (id) ON DELETE CASCADE

    )`,

    // Widget configurations table
    `CREATE TABLE IF NOT EXISTS widget_configs (

      id INTEGER PRIMARY KEY AUTOINCREMENT,

      tenant_id INTEGER NOT NULL,

      config TEXT NOT NULL DEFAULT '{}',

      created_at DATETIME DEFAULT CURRENT_TIMESTAMP,

      updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,

      FOREIGN KEY (tenant_id) REFERENCES tenants (id) ON DELETE CASCADE

    )`
  ];

  const createIndexes = [
    `CREATE INDEX IF NOT EXISTS idx_users_email ON users(email)`,
    `CREATE INDEX IF NOT EXISTS idx_users_google_id ON users(google_id)`,
    `CREATE INDEX IF NOT EXISTS idx_tenants_subdomain ON tenants(subdomain)`,
    `CREATE INDEX IF NOT EXISTS idx_user_tenants_user_id ON user_tenants(user_id)`,
    `CREATE INDEX IF NOT EXISTS idx_user_tenants_tenant_id ON user_tenants(tenant_id)`,
    `CREATE INDEX IF NOT EXISTS idx_domains_tenant_id ON domains(tenant_id)`,
    `CREATE INDEX IF NOT EXISTS idx_knowledge_base_tenant_id ON knowledge_base(tenant_id)`,
    `CREATE INDEX IF NOT EXISTS idx_chat_sessions_tenant_id ON chat_sessions(tenant_id)`,
    `CREATE INDEX IF NOT EXISTS idx_chat_sessions_token ON chat_sessions(session_token)`,
    `CREATE INDEX IF NOT EXISTS idx_chat_messages_session_id ON chat_messages(session_id)`,
    `CREATE INDEX IF NOT EXISTS idx_analytics_events_tenant_id ON analytics_events(tenant_id)`,
    `CREATE INDEX IF NOT EXISTS idx_analytics_events_timestamp ON analytics_events(timestamp)`
  ];

  try {
    // Create tables
    for (const sql of createTables) {
      await database.run(sql);
    }

    // Create indexes
    for (const sql of createIndexes) {
      await database.run(sql);
    }

    console.log('Database tables and indexes created successfully');
  } catch (error) {
    console.error('Error initializing database:', error);
    throw error;
  }
}