| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- import { PrismaClient, UserRole } from '@prisma/client'
- import bcrypt from 'bcryptjs'
- const prisma = new PrismaClient()
- async function main() {
- console.log('🌱 Iniciando seed de la base de datos...')
- // Crear usuario administrador
- const hashedPassword = await bcrypt.hash('admin123', 12)
-
- const adminUser = await prisma.user.upsert({
- where: { email: 'admin@universidad.edu' },
- update: {},
- create: {
- email: 'admin@universidad.edu',
- password: hashedPassword,
- role: UserRole.ADMIN,
- },
- })
- console.log('✅ Usuario administrador creado:', adminUser.email)
- // Crear periodo académico
- const period = await prisma.period.upsert({
- where: { id: 'period-2024' },
- update: {},
- create: {
- id: 'period-2024',
- name: 'Periodo 2024',
- startDate: new Date('2024-01-01'),
- endDate: new Date('2024-12-31'),
- isActive: true,
- },
- })
- console.log('✅ Periodo académico creado:', period.name)
- // Crear parciales
- const partials = [
- { id: 'partial-1', name: 'Primer Parcial', periodId: period.id },
- { id: 'partial-2', name: 'Segundo Parcial', periodId: period.id },
- { id: 'partial-3', name: 'Tercer Parcial', periodId: period.id },
- ]
- for (const partial of partials) {
- await prisma.partial.upsert({
- where: {
- id: partial.id,
- },
- update: {},
- create: partial,
- })
- console.log(`✅ Parcial creado: ${partial.name}`)
- }
- // Crear clases de ejemplo
- const classes = [
- {
- name: 'Matemáticas I',
- code: 'MAT101',
- description: 'Fundamentos de matemáticas',
- periodId: period.id,
- },
- {
- name: 'Programación I',
- code: 'PRG101',
- description: 'Introducción a la programación',
- periodId: period.id,
- },
- {
- name: 'Física I',
- code: 'FIS101',
- description: 'Fundamentos de física',
- periodId: period.id,
- },
- ]
- for (const classData of classes) {
- const createdClass = await prisma.class.upsert({
- where: { code: classData.code },
- update: {},
- create: classData,
- })
- // Crear secciones para cada clase
- const sections = ['A', 'B']
- for (const sectionName of sections) {
- await prisma.section.upsert({
- where: {
- id: `${createdClass.code}-${sectionName}`
- },
- update: {},
- create: {
- id: `${createdClass.code}-${sectionName}`,
- name: sectionName,
- classId: createdClass.id,
- },
- })
- console.log(`✅ Sección creada: ${sectionName} para ${createdClass.name}`)
- }
- }
- console.log('✅ Clases y secciones creadas')
- // Crear profesor de ejemplo
- const teacherPassword = await bcrypt.hash('teacher123', 12)
- const teacherUser = await prisma.user.upsert({
- where: { email: 'profesor@universidad.edu' },
- update: {},
- create: {
- email: 'profesor@universidad.edu',
- password: teacherPassword,
- role: UserRole.TEACHER,
- },
- })
- const teacher = await prisma.teacher.upsert({
- where: { userId: teacherUser.id },
- update: {},
- create: {
- userId: teacherUser.id,
- firstName: 'Juan',
- lastName: 'Pérez',
- cedula: '1234567890',
- email: 'profesor@universidad.edu',
- phone: '0987654321',
- },
- })
- console.log('✅ Profesor creado:', teacher.firstName, teacher.lastName)
- // Crear estudiante de ejemplo
- const studentPassword = await bcrypt.hash('student123', 12)
- const studentUser = await prisma.user.upsert({
- where: { email: 'estudiante@universidad.edu' },
- update: {},
- create: {
- email: 'estudiante@universidad.edu',
- password: studentPassword,
- role: UserRole.STUDENT,
- },
- })
- const student = await prisma.student.upsert({
- where: { userId: studentUser.id },
- update: {},
- create: {
- userId: studentUser.id,
- firstName: 'María',
- lastName: 'González',
- cedula: '0987654321',
- email: 'estudiante@universidad.edu',
- phone: '0123456789',
- admissionNumber: 'EST2024001',
- },
- })
- console.log('✅ Estudiante creado:', student.firstName, student.lastName)
- console.log('🎉 Seed completado exitosamente!')
- console.log('\n📋 Credenciales de acceso:')
- console.log('Administrador: admin@universidad.edu / admin123')
- console.log('Profesor: profesor@universidad.edu / teacher123')
- console.log('Estudiante: estudiante@universidad.edu / student123')
- }
- main()
- .catch((e) => {
- console.error('❌ Error durante el seed:', e)
- process.exit(1)
- })
- .finally(async () => {
- await prisma.$disconnect()
- })
|