NoLev commited on
Commit
9bb022a
·
verified ·
1 Parent(s): 1196757

Update config/db.js

Browse files
Files changed (1) hide show
  1. config/db.js +80 -23
config/db.js CHANGED
@@ -22,13 +22,14 @@ const pool = mysql.createPool({
22
  async function initializeSchema() {
23
  console.log('Initializing database schema...');
24
  try {
25
- // Create users table
26
  await pool.query(`
27
  CREATE TABLE IF NOT EXISTS users (
28
  id INT AUTO_INCREMENT PRIMARY KEY,
29
  username VARCHAR(255) NOT NULL UNIQUE,
30
  email VARCHAR(255) NOT NULL UNIQUE,
31
  password VARCHAR(255) NOT NULL,
 
32
  full_name VARCHAR(255) NOT NULL,
33
  dob DATE NOT NULL,
34
  address TEXT NOT NULL,
@@ -41,17 +42,6 @@ async function initializeSchema() {
41
  )
42
  `);
43
 
44
- // Create admins table
45
- await pool.query(`
46
- CREATE TABLE IF NOT EXISTS admins (
47
- id INT AUTO_INCREMENT PRIMARY KEY,
48
- username VARCHAR(255) NOT NULL UNIQUE,
49
- email VARCHAR(255) NOT NULL UNIQUE,
50
- password VARCHAR(255) NOT NULL,
51
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
52
- )
53
- `);
54
-
55
  // Create kyc_documents table
56
  await pool.query(`
57
  CREATE TABLE IF NOT EXISTS kyc_documents (
@@ -67,6 +57,49 @@ async function initializeSchema() {
67
  )
68
  `);
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  // Create investment_plans table
71
  await pool.query(`
72
  CREATE TABLE IF NOT EXISTS investment_plans (
@@ -144,20 +177,44 @@ async function initializeSchema() {
144
  const [users] = await pool.query('SELECT COUNT(*) as count FROM users');
145
  if (users[0].count === 0) {
146
  await pool.query(`
147
- INSERT INTO users (username, email, password, full_name, dob, address, country, zip_code, phone_number, kyc_status, balance)
148
  VALUES
149
- ('user1', 'user1@example.com', ?, 'User One', '1990-01-01', '123 Main St', 'USA', '12345', '1234567890', 'approved', 1000.00),
150
- ('user2', 'user2@example.com', ?, 'User Two', '1985-02-02', '456 Elm St', 'Canada', '67890', '0987654321', 'pending', 500.00)
151
- `, [user1Password, user2Password]);
 
152
  }
153
 
154
- // Check if admins exist
155
- const [admins] = await pool.query('SELECT COUNT(*) as count FROM admins');
156
- if (admins[0].count === 0) {
157
  await pool.query(`
158
- INSERT INTO admins (username, email, password)
159
- VALUES ('admin1', 'admin1@example.com', ?)
160
- `, [admin1Password]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  }
162
 
163
  // Check if investment_plans exist
@@ -199,7 +256,7 @@ async function initializeSchema() {
199
  INSERT INTO withdrawals (user_id, amount, status)
200
  VALUES
201
  (1, 200.00, 'approved'),
202
- (2, 100.00, 'approved')
203
  `);
204
  }
205
 
 
22
  async function initializeSchema() {
23
  console.log('Initializing database schema...');
24
  try {
25
+ // Create users table with role column
26
  await pool.query(`
27
  CREATE TABLE IF NOT EXISTS users (
28
  id INT AUTO_INCREMENT PRIMARY KEY,
29
  username VARCHAR(255) NOT NULL UNIQUE,
30
  email VARCHAR(255) NOT NULL UNIQUE,
31
  password VARCHAR(255) NOT NULL,
32
+ role ENUM('user', 'admin') NOT NULL DEFAULT 'user',
33
  full_name VARCHAR(255) NOT NULL,
34
  dob DATE NOT NULL,
35
  address TEXT NOT NULL,
 
42
  )
43
  `);
44
 
 
 
 
 
 
 
 
 
 
 
 
45
  // Create kyc_documents table
46
  await pool.query(`
47
  CREATE TABLE IF NOT EXISTS kyc_documents (
 
57
  )
58
  `);
59
 
60
+ // Create crypto_addresses table
61
+ await pool.query(`
62
+ CREATE TABLE IF NOT EXISTS crypto_addresses (
63
+ id INT AUTO_INCREMENT PRIMARY KEY,
64
+ crypto_type VARCHAR(50) NOT NULL,
65
+ address VARCHAR(255) NOT NULL,
66
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
67
+ )
68
+ `);
69
+
70
+ // Create deposits table
71
+ await pool.query(`
72
+ CREATE TABLE IF NOT EXISTS deposits (
73
+ id INT AUTO_INCREMENT PRIMARY KEY,
74
+ user_id INT NOT NULL,
75
+ crypto_type VARCHAR(50) NOT NULL,
76
+ amount DECIMAL(10,2) NOT NULL,
77
+ status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending',
78
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
79
+ FOREIGN KEY (user_id) REFERENCES users(id)
80
+ )
81
+ `);
82
+
83
+ // Create profile_updates table
84
+ await pool.query(`
85
+ CREATE TABLE IF NOT EXISTS profile_updates (
86
+ id INT AUTO_INCREMENT PRIMARY KEY,
87
+ user_id INT NOT NULL,
88
+ username VARCHAR(255),
89
+ email VARCHAR(255),
90
+ full_name VARCHAR(255),
91
+ dob DATE,
92
+ address TEXT,
93
+ country VARCHAR(100),
94
+ zip_code VARCHAR(20),
95
+ phone_number VARCHAR(20),
96
+ status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending',
97
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
98
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
99
+ FOREIGN KEY (user_id) REFERENCES users(id)
100
+ )
101
+ `);
102
+
103
  // Create investment_plans table
104
  await pool.query(`
105
  CREATE TABLE IF NOT EXISTS investment_plans (
 
177
  const [users] = await pool.query('SELECT COUNT(*) as count FROM users');
178
  if (users[0].count === 0) {
179
  await pool.query(`
180
+ INSERT INTO users (username, email, password, role, full_name, dob, address, country, zip_code, phone_number, kyc_status, balance)
181
  VALUES
182
+ ('user1', 'user1@example.com', ?, 'user', 'User One', '1990-01-01', '123 Main St', 'USA', '12345', '1234567890', 'approved', 1000.00),
183
+ ('user2', 'user2@example.com', ?, 'user', 'User Two', '1985-02-02', '456 Elm St', 'Canada', '67890', '0987654321', 'pending', 500.00),
184
+ ('admin1', 'admin1@example.com', ?, 'admin', 'Admin One', '1980-01-01', '789 Oak St', 'USA', '54321', '1112223333', 'approved', 0.00)
185
+ `, [user1Password, user2Password, admin1Password]);
186
  }
187
 
188
+ // Check if crypto_addresses exist
189
+ const [cryptoAddresses] = await pool.query('SELECT COUNT(*) as count FROM crypto_addresses');
190
+ if (cryptoAddresses[0].count === 0) {
191
  await pool.query(`
192
+ INSERT INTO crypto_addresses (crypto_type, address)
193
+ VALUES
194
+ ('BTC', '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'),
195
+ ('ETH', '0x742d35Cc6634C0532925a3b844Bc454e4438f44e')
196
+ `);
197
+ }
198
+
199
+ // Check if deposits exist
200
+ const [deposits] = await pool.query('SELECT COUNT(*) as count FROM deposits');
201
+ if (deposits[0].count === 0) {
202
+ await pool.query(`
203
+ INSERT INTO deposits (user_id, crypto_type, amount, status)
204
+ VALUES
205
+ (1, 'BTC', 200.00, 'pending'),
206
+ (2, 'ETH', 150.00, 'pending')
207
+ `);
208
+ }
209
+
210
+ // Check if profile_updates exist
211
+ const [profileUpdates] = await pool.query('SELECT COUNT(*) as count FROM profile_updates');
212
+ if (profileUpdates[0].count === 0) {
213
+ await pool.query(`
214
+ INSERT INTO profile_updates (user_id, username, email, full_name, dob, address, country, zip_code, phone_number, status)
215
+ VALUES
216
+ (1, 'user1_updated', 'user1_updated@example.com', 'User One Updated', '1990-01-01', '123 Main St Updated', 'USA', '12345', '1234567890', 'pending')
217
+ `);
218
  }
219
 
220
  // Check if investment_plans exist
 
256
  INSERT INTO withdrawals (user_id, amount, status)
257
  VALUES
258
  (1, 200.00, 'approved'),
259
+ (2, 100.00, 'pending')
260
  `);
261
  }
262