generated from fahricansecer/boilerplate-be
This commit is contained in:
117
prisma/seed-admin.ts
Normal file
117
prisma/seed-admin.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
|
||||
/**
|
||||
* Admin Seed Script
|
||||
*
|
||||
* Admin hesabı ve sınırsız kredi ataması:
|
||||
* - Email: admin@contentgen.ai
|
||||
* - Şifre: Admin123!
|
||||
* - Rol: admin (sınırsız erişim)
|
||||
* - Kredi: 999999
|
||||
*/
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
console.log('🌱 Admin seed başlatılıyor...');
|
||||
|
||||
// 1. Admin rolü oluştur/bul
|
||||
const adminRole = await prisma.role.upsert({
|
||||
where: { name: 'admin' },
|
||||
update: {},
|
||||
create: {
|
||||
name: 'admin',
|
||||
description: 'Sistem yöneticisi — sınırsız erişim',
|
||||
isSystem: true,
|
||||
},
|
||||
});
|
||||
console.log(`✅ Admin rolü: ${adminRole.id}`);
|
||||
|
||||
// 2. User rolü oluştur/bul (her kullanıcıda olmalı)
|
||||
const userRole = await prisma.role.upsert({
|
||||
where: { name: 'user' },
|
||||
update: {},
|
||||
create: {
|
||||
name: 'user',
|
||||
description: 'Standart kullanıcı',
|
||||
isSystem: true,
|
||||
},
|
||||
});
|
||||
|
||||
// 3. Admin kullanıcısı oluştur
|
||||
const hashedPassword = await bcrypt.hash('Admin123!', 12);
|
||||
|
||||
const adminUser = await prisma.user.upsert({
|
||||
where: { email: 'admin@contentgen.ai' },
|
||||
update: {
|
||||
password: hashedPassword,
|
||||
firstName: 'Admin',
|
||||
lastName: 'ContentGen',
|
||||
isActive: true,
|
||||
},
|
||||
create: {
|
||||
email: 'admin@contentgen.ai',
|
||||
password: hashedPassword,
|
||||
firstName: 'Admin',
|
||||
lastName: 'ContentGen',
|
||||
isActive: true,
|
||||
},
|
||||
});
|
||||
console.log(`✅ Admin kullanıcısı: ${adminUser.id} (${adminUser.email})`);
|
||||
|
||||
// 4. Rolleri ata (upsert ile — varsa atlama)
|
||||
for (const role of [adminRole, userRole]) {
|
||||
await prisma.userRole.upsert({
|
||||
where: {
|
||||
userId_roleId: {
|
||||
userId: adminUser.id,
|
||||
roleId: role.id,
|
||||
},
|
||||
},
|
||||
update: {},
|
||||
create: {
|
||||
userId: adminUser.id,
|
||||
roleId: role.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
console.log('✅ Roller atandı: admin + user');
|
||||
|
||||
// 5. Sınırsız kredi yükle (mevcut bakiye kontrol et)
|
||||
const existingTransactions = await prisma.creditTransaction.findMany({
|
||||
where: { userId: adminUser.id },
|
||||
});
|
||||
const currentBalance = existingTransactions.reduce((sum, tx) => sum + tx.amount, 0);
|
||||
|
||||
if (currentBalance < 1000) {
|
||||
const creditAmount = 999999 - currentBalance;
|
||||
await prisma.creditTransaction.create({
|
||||
data: {
|
||||
userId: adminUser.id,
|
||||
amount: creditAmount,
|
||||
type: 'grant',
|
||||
description: 'Admin hesabı — sınırsız kredi',
|
||||
balanceAfter: 999999,
|
||||
},
|
||||
});
|
||||
console.log(`✅ Kredi yüklendi: +${creditAmount} (toplam: 999,999)`);
|
||||
} else {
|
||||
console.log(`ℹ️ Yeterli kredi mevcut: ${currentBalance}`);
|
||||
}
|
||||
|
||||
console.log('\n═══════════════════════════════════════');
|
||||
console.log('🔑 Admin Giriş Bilgileri:');
|
||||
console.log(' Email: admin@contentgen.ai');
|
||||
console.log(' Şifre: Admin123!');
|
||||
console.log('═══════════════════════════════════════\n');
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error('❌ Seed hatası:', e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user