2025-12-19 01:15:19 +05:30
|
|
|
import { PrismaClient, UserRole, UserStatus, AuthProvider } from '@prisma/client';
|
|
|
|
|
import { PrismaPg } from '@prisma/adapter-pg';
|
|
|
|
|
import { Pool } from 'pg';
|
|
|
|
|
import * as argon2 from 'argon2';
|
|
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
|
const connectionString = process.env.DATABASE_URL;
|
|
|
|
|
const pool = new Pool({ connectionString });
|
|
|
|
|
const adapter = new PrismaPg(pool);
|
|
|
|
|
|
|
|
|
|
const prisma = new PrismaClient({ adapter });
|
|
|
|
|
|
|
|
|
|
console.log('🌱 Starting database seeding...\n');
|
|
|
|
|
|
2026-01-20 12:16:01 +05:30
|
|
|
// =============================================
|
|
|
|
|
// Seed Agent Types
|
|
|
|
|
// =============================================
|
|
|
|
|
console.log('📋 Seeding Agent Types...');
|
|
|
|
|
|
|
|
|
|
const agentTypes = [
|
|
|
|
|
{
|
|
|
|
|
name: 'Professional',
|
|
|
|
|
description: 'Licensed real estate professionals',
|
|
|
|
|
icon: 'briefcase',
|
|
|
|
|
sortOrder: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Lender',
|
|
|
|
|
description: 'Mortgage and loan specialists',
|
|
|
|
|
icon: 'bank',
|
|
|
|
|
sortOrder: 2,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (const agentType of agentTypes) {
|
|
|
|
|
const existing = await prisma.agentType.findUnique({
|
|
|
|
|
where: { name: agentType.name },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (existing) {
|
|
|
|
|
console.log(` ⚠️ Agent type "${agentType.name}" already exists`);
|
|
|
|
|
} else {
|
|
|
|
|
await prisma.agentType.create({
|
|
|
|
|
data: agentType,
|
|
|
|
|
});
|
|
|
|
|
console.log(` ✅ Created agent type: ${agentType.name}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('');
|
|
|
|
|
|
|
|
|
|
// =============================================
|
|
|
|
|
// Seed Admin User
|
|
|
|
|
// =============================================
|
|
|
|
|
console.log('👤 Seeding Admin User...');
|
|
|
|
|
|
2025-12-19 01:15:19 +05:30
|
|
|
const adminEmail = process.env.ADMIN_EMAIL || 'admin@realestate.com';
|
|
|
|
|
const adminPassword = process.env.ADMIN_PASSWORD || 'Admin@123456';
|
|
|
|
|
|
|
|
|
|
// Hash password with Argon2 (more secure than bcrypt)
|
|
|
|
|
const hashedPassword = await argon2.hash(adminPassword);
|
|
|
|
|
|
|
|
|
|
// Check if admin already exists
|
|
|
|
|
const existingAdmin = await prisma.user.findUnique({
|
|
|
|
|
where: { email: adminEmail },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (existingAdmin) {
|
2026-01-20 12:16:01 +05:30
|
|
|
console.log(` ⚠️ Admin user already exists: ${adminEmail}`);
|
2025-12-19 01:15:19 +05:30
|
|
|
} else {
|
|
|
|
|
// Create admin user
|
|
|
|
|
const admin = await prisma.user.create({
|
|
|
|
|
data: {
|
|
|
|
|
email: adminEmail,
|
|
|
|
|
password: hashedPassword,
|
|
|
|
|
role: UserRole.ADMIN,
|
|
|
|
|
status: UserStatus.ACTIVE,
|
|
|
|
|
emailVerified: true,
|
|
|
|
|
emailVerifiedAt: new Date(),
|
|
|
|
|
authProvider: AuthProvider.LOCAL,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 12:16:01 +05:30
|
|
|
console.log(' ✅ Admin user created successfully!');
|
2025-12-19 01:15:19 +05:30
|
|
|
console.log(' ───────────────────────────────');
|
|
|
|
|
console.log(` 📧 Email: ${adminEmail}`);
|
|
|
|
|
console.log(` 🔑 Password: ${adminPassword}`);
|
|
|
|
|
console.log(` 👤 Role: ${admin.role}`);
|
|
|
|
|
console.log(` 🆔 ID: ${admin.id}`);
|
|
|
|
|
console.log(' ───────────────────────────────\n');
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 12:16:01 +05:30
|
|
|
// =============================================
|
2025-12-19 01:15:19 +05:30
|
|
|
// Summary
|
2026-01-20 12:16:01 +05:30
|
|
|
// =============================================
|
2025-12-19 01:15:19 +05:30
|
|
|
const userCount = await prisma.user.count();
|
|
|
|
|
const adminCount = await prisma.user.count({ where: { role: UserRole.ADMIN } });
|
2026-01-20 12:16:01 +05:30
|
|
|
const agentTypeCount = await prisma.agentType.count();
|
2025-12-19 01:15:19 +05:30
|
|
|
|
|
|
|
|
console.log('📊 Database Summary:');
|
2026-01-20 12:16:01 +05:30
|
|
|
console.log(` Total Users: ${userCount}`);
|
|
|
|
|
console.log(` Admins: ${adminCount}`);
|
|
|
|
|
console.log(` Agent Types: ${agentTypeCount}`);
|
2025-12-19 01:15:19 +05:30
|
|
|
console.log('\n🌱 Seeding completed!\n');
|
|
|
|
|
|
|
|
|
|
await prisma.$disconnect();
|
|
|
|
|
await pool.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main()
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
console.error('❌ Seeding failed:', e);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|