const { Client } = require('pg'); require('dotenv').config(); const client = new Client({ user: process.env.DB_USER, host: process.env.DB_HOST, database: process.env.DB_NAME, password: process.env.DB_PASSWORD, port: parseInt(process.env.DB_PORT || '5432'), ssl: { rejectUnauthorized: false } }); const TEST_EMAIL = 'test@test.com'; const TEST_CODE = '123456'; async function run() { try { await client.connect(); console.log('Connected to database'); // 1. Check if user exists const userRes = await client.query('SELECT * FROM public.users WHERE email = $1', [TEST_EMAIL]); let userId; if (userRes.rows.length > 0) { console.log('User already exists:', userRes.rows[0]); userId = userRes.rows[0].id; } else { // 2. Create user (assuming id is serial) console.log('Creating new user...'); const insertRes = await client.query(` INSERT INTO public.users (email, name, role, is_active, created_at, updated_at) VALUES ($1, $2, $3, $4, NOW(), NOW()) RETURNING id; `, [TEST_EMAIL, 'Test User', 'user', true]); userId = insertRes.rows[0].id; console.log('User created with ID:', userId); } // 3. Create OTP code // Delete old codes first await client.query('DELETE FROM public.otp_codes WHERE email = $1', [TEST_EMAIL]); const expiresAt = new Date(); expiresAt.setHours(expiresAt.getHours() + 24); // Valid for 24 hours await client.query(` INSERT INTO public.otp_codes (email, code, expires_at, created_at) VALUES ($1, $2, $3, NOW()) `, [TEST_EMAIL, TEST_CODE, expiresAt]); console.log(`OTP code ${TEST_CODE} created for ${TEST_EMAIL}`); console.log('You can now log in with this email and OTP.'); } catch (err) { console.error('Database error:', err); } finally { await client.end(); } } run();