-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
107 lines (95 loc) · 2.98 KB
/
database.js
File metadata and controls
107 lines (95 loc) · 2.98 KB
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
const sqlite3 = require('sqlite3').verbose();
const path = require('path');
const fs = require('fs');
// Check if we are running in the cloud (Railway usually sets a PORT variable)
const isRailway = process.env.PORT || process.env.RAILWAY_ENVIRONMENT;
// Use environment variable if set. If on Railway, use the protected Volume. Otherwise, use local file.
const DB_PATH = process.env.DB_PATH || (isRailway ? '/app/data/lounge_bookings.db' : path.join(__dirname, 'lounge_bookings.db'));
// Ensure the directory for the database exists
const dbDir = path.dirname(DB_PATH);
if (!fs.existsSync(dbDir)) {
try {
fs.mkdirSync(dbDir, { recursive: true });
console.log(`Created database directory: ${dbDir}`);
} catch (err) {
console.error(`Error creating database directory ${dbDir}:`, err);
}
}
let db = null;
function initialize() {
return new Promise((resolve, reject) => {
db = new sqlite3.Database(DB_PATH, (err) => {
if (err) {
return reject(err);
}
console.log(`Connected to SQLite database at ${DB_PATH}`);
// Create tables
db.serialize(() => {
// Users table
db.run(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
telegram_id TEXT UNIQUE NOT NULL,
telegram_username TEXT,
first_name TEXT,
last_name TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
// Bookings table - UPDATED with lounge_level
db.run(`
CREATE TABLE IF NOT EXISTS bookings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
lounge_level INTEGER NOT NULL,
date TEXT NOT NULL,
time_slot TEXT NOT NULL,
status TEXT DEFAULT 'active',
notes TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
)
`);
// Create unique index for active bookings only - UPDATED to include lounge_level
// This allows Level 9, 10, and 11 to have separate bookings at the same time.
db.run(`
CREATE UNIQUE INDEX IF NOT EXISTS idx_active_bookings
ON bookings(lounge_level, date, time_slot)
WHERE status = 'active'
`, (err) => {
if (err) {
return reject(err);
}
console.log('Database tables initialized with floor levels');
resolve();
});
});
});
});
}
function getDb() {
if (!db) {
throw new Error('Database not initialized. Call initialize() first.');
}
return db;
}
function close() {
return new Promise((resolve, reject) => {
if (db) {
db.close((err) => {
if (err) {
return reject(err);
}
console.log('Database connection closed');
resolve();
});
} else {
resolve();
}
});
}
module.exports = {
initialize,
getDb,
close
};