1const { Pool } = require('pg');
2const bcrypt = require('bcrypt');
3const fs = require('fs');
5// Manually parse .env file
6const envContent = fs.readFileSync('.env', 'utf8');
8envContent.split('\n').forEach(line => {
9 const match = line.match(/^([^=]+)=(.*)$/);
10 if (match) envVars[match[1]] = match[2];
13const dbUrl = envVars.DATABASE_URL.replace('sslmode=require', 'sslmode=no-verify');
14const pool = new Pool({
15 connectionString: dbUrl,
16 ssl: { rejectUnauthorized: false }
19async function createTestUser() {
20 const client = await pool.connect();
23 console.log('🔍 Checking for existing customers...\n');
26 const customerResult = await client.query(`
27 SELECT customer_id, tenant_id, name, email
29 WHERE status = 'active'
33 if (customerResult.rows.length === 0) {
34 console.log('📝 No customers found. Creating test customer...\n');
36 const insertResult = await client.query(`
37 INSERT INTO customers (
47 'Test Customer Company',
48 'customer@precisewebhosting.com.au',
53 RETURNING customer_id, tenant_id, name, email
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);
64 const passwordHash = await bcrypt.hash('password123', 10);
66 const userResult = await client.query(`
67 INSERT INTO customer_users (
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]);
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);
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);
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
100 WHERE customer_id = $1
101 `, [customer.customer_id]);
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'})`);
109 console.log('💡 Use these credentials to log in, or contact admin to reset password.');
111 console.log('📝 Creating portal user for this customer...\n');
113 // Update customer to allow portal access
116 SET allow_portal_access = true
117 WHERE customer_id = $1
118 `, [customer.customer_id]);
120 // Create portal user
121 const passwordHash = await bcrypt.hash('password123', 10);
123 const userResult = await client.query(`
124 INSERT INTO customer_users (
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]);
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);
147 console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
148 console.log('🎉 Test customer user setup complete!');
149 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');
157 console.error('❌ Error:', err.message);
158 console.error(err.stack);