EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
create-test-portal-user.js
Go to the documentation of this file.
1const { Pool } = require('pg');
2const bcrypt = require('bcrypt');
3const fs = require('fs');
4
5// Manually parse .env file
6const envContent = fs.readFileSync('.env', 'utf8');
7const envVars = {};
8envContent.split('\n').forEach(line => {
9 const match = line.match(/^([^=]+)=(.*)$/);
10 if (match) envVars[match[1]] = match[2];
11});
12
13const dbUrl = envVars.DATABASE_URL.replace('sslmode=require', 'sslmode=no-verify');
14const pool = new Pool({
15 connectionString: dbUrl,
16 ssl: { rejectUnauthorized: false }
17});
18
19async function createTestUser() {
20 const client = await pool.connect();
21
22 try {
23 console.log('🔍 Checking for existing customers...\n');
24
25 // Get first customer
26 const customerResult = await client.query(`
27 SELECT customer_id, tenant_id, name, email
28 FROM customers
29 WHERE status = 'active'
30 LIMIT 1
31 `);
32
33 if (customerResult.rows.length === 0) {
34 console.log('📝 No customers found. Creating test customer...\n');
35
36 const insertResult = await client.query(`
37 INSERT INTO customers (
38 tenant_id,
39 name,
40 email,
41 phone,
42 status,
43 allow_portal_access
44 )
45 VALUES (
46 'root-tenant',
47 'Test Customer Company',
48 'customer@precisewebhosting.com.au',
49 '0400 123 456',
50 'active',
51 true
52 )
53 RETURNING customer_id, tenant_id, name, email
54 `);
55
56 const customer = insertResult.rows[0];
57 console.log('✅ Customer created:');
58 console.log(' ID:', customer.customer_id);
59 console.log(' Name:', customer.name);
60 console.log(' Email:', customer.email);
61 console.log();
62
63 // Create portal user
64 const passwordHash = await bcrypt.hash('password123', 10);
65
66 const userResult = await client.query(`
67 INSERT INTO customer_users (
68 tenant_id,
69 customer_id,
70 email,
71 password_hash,
72 first_name,
73 last_name,
74 phone,
75 is_primary,
76 is_active
77 )
78 VALUES ($1, $2, $3, $4, 'Test', 'User', '0400 123 456', true, true)
79 RETURNING customer_user_id, email, first_name, last_name
80 `, [customer.tenant_id, customer.customer_id, 'test@precisewebhosting.com.au', passwordHash]);
81
82 console.log('✅ Portal user created:');
83 console.log(' Email: test@precisewebhosting.com.au');
84 console.log(' Password: password123');
85 console.log(' Name:', userResult.rows[0].first_name, userResult.rows[0].last_name);
86 console.log();
87
88 } else {
89 const customer = customerResult.rows[0];
90 console.log('✅ Found customer:');
91 console.log(' ID:', customer.customer_id);
92 console.log(' Name:', customer.name);
93 console.log(' Email:', customer.email);
94 console.log();
95
96 // Check if portal user already exists
97 const existingUserResult = await client.query(`
98 SELECT customer_user_id, email, first_name, last_name, is_active
99 FROM customer_users
100 WHERE customer_id = $1
101 `, [customer.customer_id]);
102
103 if (existingUserResult.rows.length > 0) {
104 console.log('⚠️ Portal user already exists:');
105 existingUserResult.rows.forEach((user, index) => {
106 console.log(` ${index + 1}. ${user.email} - ${user.first_name} ${user.last_name} (${user.is_active ? 'Active' : 'Inactive'})`);
107 });
108 console.log();
109 console.log('💡 Use these credentials to log in, or contact admin to reset password.');
110 } else {
111 console.log('📝 Creating portal user for this customer...\n');
112
113 // Update customer to allow portal access
114 await client.query(`
115 UPDATE customers
116 SET allow_portal_access = true
117 WHERE customer_id = $1
118 `, [customer.customer_id]);
119
120 // Create portal user
121 const passwordHash = await bcrypt.hash('password123', 10);
122
123 const userResult = await client.query(`
124 INSERT INTO customer_users (
125 tenant_id,
126 customer_id,
127 email,
128 password_hash,
129 first_name,
130 last_name,
131 phone,
132 is_primary,
133 is_active
134 )
135 VALUES ($1, $2, $3, $4, 'Portal', 'User', '0400 123 456', true, true)
136 RETURNING customer_user_id, email, first_name, last_name
137 `, [customer.tenant_id, customer.customer_id, 'portal@precisewebhosting.com.au', passwordHash]);
138
139 console.log('✅ Portal user created:');
140 console.log(' Email: portal@precisewebhosting.com.au');
141 console.log(' Password: password123');
142 console.log(' Name:', userResult.rows[0].first_name, userResult.rows[0].last_name);
143 console.log();
144 }
145 }
146
147 console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
148 console.log('🎉 Test customer user setup complete!');
149 console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
150 console.log();
151 console.log('🔗 Portal URL: (checking deployment...)');
152 console.log('📧 Test Email: test@precisewebhosting.com.au (or portal@precisewebhosting.com.au)');
153 console.log('🔑 Test Password: password123');
154 console.log();
155
156 } catch (err) {
157 console.error('❌ Error:', err.message);
158 console.error(err.stack);
159 } finally {
160 client.release();
161 await pool.end();
162 }
163}
164
165createTestUser();