seed.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { PrismaClient, UserRole } from '@prisma/client'
  2. import bcrypt from 'bcryptjs'
  3. const prisma = new PrismaClient()
  4. async function main() {
  5. console.log('🌱 Iniciando seed de la base de datos...')
  6. // Crear usuario administrador
  7. const hashedPassword = await bcrypt.hash('admin123', 12)
  8. const adminUser = await prisma.user.upsert({
  9. where: { email: 'admin@universidad.edu' },
  10. update: {},
  11. create: {
  12. email: 'admin@universidad.edu',
  13. password: hashedPassword,
  14. role: UserRole.ADMIN,
  15. },
  16. })
  17. console.log('✅ Usuario administrador creado:', adminUser.email)
  18. // Crear periodo académico
  19. const period = await prisma.period.upsert({
  20. where: { id: 'period-2024' },
  21. update: {},
  22. create: {
  23. id: 'period-2024',
  24. name: 'Periodo 2024',
  25. startDate: new Date('2024-01-01'),
  26. endDate: new Date('2024-12-31'),
  27. isActive: true,
  28. },
  29. })
  30. console.log('✅ Periodo académico creado:', period.name)
  31. // Crear parciales
  32. const partials = [
  33. { id: 'partial-1', name: 'Primer Parcial', periodId: period.id },
  34. { id: 'partial-2', name: 'Segundo Parcial', periodId: period.id },
  35. { id: 'partial-3', name: 'Tercer Parcial', periodId: period.id },
  36. ]
  37. for (const partial of partials) {
  38. await prisma.partial.upsert({
  39. where: {
  40. id: partial.id,
  41. },
  42. update: {},
  43. create: partial,
  44. })
  45. console.log(`✅ Parcial creado: ${partial.name}`)
  46. }
  47. // Crear clases de ejemplo
  48. const classes = [
  49. {
  50. name: 'Matemáticas I',
  51. code: 'MAT101',
  52. description: 'Fundamentos de matemáticas',
  53. periodId: period.id,
  54. },
  55. {
  56. name: 'Programación I',
  57. code: 'PRG101',
  58. description: 'Introducción a la programación',
  59. periodId: period.id,
  60. },
  61. {
  62. name: 'Física I',
  63. code: 'FIS101',
  64. description: 'Fundamentos de física',
  65. periodId: period.id,
  66. },
  67. ]
  68. for (const classData of classes) {
  69. const createdClass = await prisma.class.upsert({
  70. where: { code: classData.code },
  71. update: {},
  72. create: classData,
  73. })
  74. // Crear secciones para cada clase
  75. const sections = ['A', 'B']
  76. for (const sectionName of sections) {
  77. await prisma.section.upsert({
  78. where: {
  79. id: `${createdClass.code}-${sectionName}`
  80. },
  81. update: {},
  82. create: {
  83. id: `${createdClass.code}-${sectionName}`,
  84. name: sectionName,
  85. classId: createdClass.id,
  86. },
  87. })
  88. console.log(`✅ Sección creada: ${sectionName} para ${createdClass.name}`)
  89. }
  90. }
  91. console.log('✅ Clases y secciones creadas')
  92. // Crear profesor de ejemplo
  93. const teacherPassword = await bcrypt.hash('teacher123', 12)
  94. const teacherUser = await prisma.user.upsert({
  95. where: { email: 'profesor@universidad.edu' },
  96. update: {},
  97. create: {
  98. email: 'profesor@universidad.edu',
  99. password: teacherPassword,
  100. role: UserRole.TEACHER,
  101. },
  102. })
  103. const teacher = await prisma.teacher.upsert({
  104. where: { userId: teacherUser.id },
  105. update: {},
  106. create: {
  107. userId: teacherUser.id,
  108. firstName: 'Juan',
  109. lastName: 'Pérez',
  110. cedula: '1234567890',
  111. email: 'profesor@universidad.edu',
  112. phone: '0987654321',
  113. },
  114. })
  115. console.log('✅ Profesor creado:', teacher.firstName, teacher.lastName)
  116. // Crear estudiante de ejemplo
  117. const studentPassword = await bcrypt.hash('student123', 12)
  118. const studentUser = await prisma.user.upsert({
  119. where: { email: 'estudiante@universidad.edu' },
  120. update: {},
  121. create: {
  122. email: 'estudiante@universidad.edu',
  123. password: studentPassword,
  124. role: UserRole.STUDENT,
  125. },
  126. })
  127. const student = await prisma.student.upsert({
  128. where: { userId: studentUser.id },
  129. update: {},
  130. create: {
  131. userId: studentUser.id,
  132. firstName: 'María',
  133. lastName: 'González',
  134. cedula: '0987654321',
  135. email: 'estudiante@universidad.edu',
  136. phone: '0123456789',
  137. admissionNumber: 'EST2024001',
  138. },
  139. })
  140. console.log('✅ Estudiante creado:', student.firstName, student.lastName)
  141. console.log('🎉 Seed completado exitosamente!')
  142. console.log('\n📋 Credenciales de acceso:')
  143. console.log('Administrador: admin@universidad.edu / admin123')
  144. console.log('Profesor: profesor@universidad.edu / teacher123')
  145. console.log('Estudiante: estudiante@universidad.edu / student123')
  146. }
  147. main()
  148. .catch((e) => {
  149. console.error('❌ Error durante el seed:', e)
  150. process.exit(1)
  151. })
  152. .finally(async () => {
  153. await prisma.$disconnect()
  154. })