- Created list-users.js to display all users with roles and details - Added promote-user-to-admin.js to elevate regular users to admin - Added demote-admin-to-user.js with safety check for last admin - All scripts use proper ES modules and dotenv for environment loading - Scripts validate user existence and current roles before operations - Added detailed documentation to scripts/README.md - Includes user-friendly output with emojis and clear status messages - Tested promotion functionality successfully - Maintains database integrity with proper error handling
51 lines
No EOL
1.4 KiB
JavaScript
Executable file
51 lines
No EOL
1.4 KiB
JavaScript
Executable file
#!/usr/bin/env node
|
|
|
|
import { config } from 'dotenv';
|
|
import { sql } from '@vercel/postgres';
|
|
|
|
// Load environment variables
|
|
config({ path: '.env.local' });
|
|
|
|
async function listUsers() {
|
|
try {
|
|
console.log('📋 Fetching all users from database...\n');
|
|
|
|
const result = await sql`
|
|
SELECT id, email, role, created_at
|
|
FROM users
|
|
ORDER BY created_at DESC
|
|
`;
|
|
|
|
if (result.rows.length === 0) {
|
|
console.log('👤 No users found in database');
|
|
return;
|
|
}
|
|
|
|
console.log(`Found ${result.rows.length} user(s):\n`);
|
|
|
|
result.rows.forEach((user, index) => {
|
|
const roleEmoji = user.role === 'admin' ? '👑' : '👤';
|
|
const createdDate = new Date(user.created_at).toLocaleDateString();
|
|
|
|
console.log(`${index + 1}. ${roleEmoji} ${user.email}`);
|
|
console.log(` ID: ${user.id}`);
|
|
console.log(` Role: ${user.role}`);
|
|
console.log(` Created: ${createdDate}`);
|
|
console.log('');
|
|
});
|
|
|
|
const adminCount = result.rows.filter(user => user.role === 'admin').length;
|
|
const userCount = result.rows.filter(user => user.role === 'user').length;
|
|
|
|
console.log('📊 Summary:');
|
|
console.log(` 👑 Admins: ${adminCount}`);
|
|
console.log(` 👤 Users: ${userCount}`);
|
|
console.log(` 📈 Total: ${result.rows.length}`);
|
|
|
|
} catch (error) {
|
|
console.error('❌ Database error:', error.message);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
listUsers();
|