const { PrismaClient } = require('@prisma/client'); const bcrypt = require('bcrypt'); const prisma = new PrismaClient(); async function main() { console.log('ðŸŒą Starting database seeding...'); // Clear existing data (in development only) if (process.env.NODE_ENV === 'development') { console.log('🗑ïļ Clearing existing data...'); await prisma.quizAttempt.deleteMany(); await prisma.quizQuestion.deleteMany(); await prisma.quiz.deleteMany(); await prisma.lessonProgress.deleteMany(); await prisma.attachment.deleteMany(); await prisma.lessonPrerequisite.deleteMany(); await prisma.lesson.deleteMany(); await prisma.chapter.deleteMany(); await prisma.enrollment.deleteMany(); await prisma.courseInstructor.deleteMany(); await prisma.course.deleteMany(); await prisma.category.deleteMany(); await prisma.certificate.deleteMany(); await prisma.profile.deleteMany(); await prisma.user.deleteMany(); await prisma.role.deleteMany(); } // Seed Roles console.log('ðŸ‘Ĩ Seeding roles...'); const roles = await Promise.all([ prisma.role.create({ data: { code: 'ADMIN', name: { th: 'āļœāļđāđ‰āļ”āļđāđāļĨāļĢāļ°āļšāļš', en: 'Administrator' }, description: { th: 'āļĄāļĩāļŠāļīāļ—āļ˜āļīāđŒāđ€āļ‚āđ‰āļēāļ–āļķāļ‡āļ—āļļāļāļŸāļąāļ‡āļāđŒāļŠāļąāļ™', en: 'Full system access' } } }), prisma.role.create({ data: { code: 'INSTRUCTOR', name: { th: 'āļœāļđāđ‰āļŠāļ­āļ™', en: 'Instructor' }, description: { th: 'āļŠāļēāļĄāļēāļĢāļ–āļŠāļĢāđ‰āļēāļ‡āđāļĨāļ°āļˆāļąāļ”āļāļēāļĢāļ„āļ­āļĢāđŒāļŠāđ€āļĢāļĩāļĒāļ™', en: 'Can create and manage courses' } } }), prisma.role.create({ data: { code: 'STUDENT', name: { th: 'āļ™āļąāļāđ€āļĢāļĩāļĒāļ™', en: 'Student' }, description: { th: 'āļŠāļēāļĄāļēāļĢāļ–āļĨāļ‡āļ—āļ°āđ€āļšāļĩāļĒāļ™āđāļĨāļ°āđ€āļĢāļĩāļĒāļ™āļ„āļ­āļĢāđŒāļŠ', en: 'Can enroll and learn courses' } } }) ]); // Seed Users console.log('ðŸ‘Ī Seeding users...'); const hashedPassword = await bcrypt.hash('admin123', 10); const admin = await prisma.user.create({ data: { username: 'admin', email: 'admin@elearning.local', password: hashedPassword, role_id: roles[0].id, profile: { create: { first_name: 'Admin', last_name: 'User', bio: { th: 'āļœāļđāđ‰āļ”āļđāđāļĨāļĢāļ°āļšāļš', en: 'System Administrator' } } } } }); const instructor = await prisma.user.create({ data: { username: 'instructor', email: 'instructor@elearning.local', password: hashedPassword, role_id: roles[1].id, profile: { create: { first_name: 'John', last_name: 'Doe', bio: { th: 'āļœāļđāđ‰āļŠāļ­āļ™āļĄāļ·āļ­āļ­āļēāļŠāļĩāļž', en: 'Professional Instructor' } } } } }); const student = await prisma.user.create({ data: { username: 'student', email: 'student@elearning.local', password: hashedPassword, role_id: roles[2].id, profile: { create: { first_name: 'Jane', last_name: 'Smith', bio: { th: 'āļ™āļąāļāđ€āļĢāļĩāļĒāļ™āļ—āļĩāđˆāļāļĢāļ°āļ•āļ·āļ­āļĢāļ·āļ­āļĢāđ‰āļ™', en: 'Eager learner' } } } } }); // Seed Categories console.log('📚 Seeding categories...'); const categories = await Promise.all([ prisma.category.create({ data: { code: 'PROGRAMMING', name: { th: 'āļāļēāļĢāđ€āļ‚āļĩāļĒāļ™āđ‚āļ›āļĢāđāļāļĢāļĄ', en: 'Programming' }, description: { th: 'āđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļāļēāļĢāđ€āļ‚āļĩāļĒāļ™āđ‚āļ›āļĢāđāļāļĢāļĄāđāļĨāļ°āļžāļąāļ’āļ™āļēāļ‹āļ­āļŸāļ•āđŒāđāļ§āļĢāđŒ', en: 'Learn programming and software development' } } }), prisma.category.create({ data: { code: 'DESIGN', name: { th: 'āļāļēāļĢāļ­āļ­āļāđāļšāļš', en: 'Design' }, description: { th: 'āđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļāļēāļĢāļ­āļ­āļāđāļšāļšāļāļĢāļēāļŸāļīāļāđāļĨāļ° UI/UX', en: 'Learn graphic design and UI/UX' } } }), prisma.category.create({ data: { code: 'BUSINESS', name: { th: 'āļ˜āļļāļĢāļāļīāļˆ', en: 'Business' }, description: { th: 'āđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļāļēāļĢāļšāļĢāļīāļŦāļēāļĢāļ˜āļļāļĢāļāļīāļˆāđāļĨāļ°āļāļēāļĢāļ•āļĨāļēāļ”', en: 'Learn business management and marketing' } } }) ]); // Seed Sample Course console.log('🎓 Seeding sample course...'); const course = await prisma.course.create({ data: { title: { th: 'āļžāļ·āđ‰āļ™āļāļēāļ™ JavaScript', en: 'JavaScript Fundamentals' }, description: { th: 'āđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļžāļ·āđ‰āļ™āļāļēāļ™ JavaScript āļ•āļąāđ‰āļ‡āđāļ•āđˆāđ€āļĢāļīāđˆāļĄāļ•āđ‰āļ™', en: 'Learn JavaScript fundamentals from scratch' }, price: 0, is_free: true, have_certificate: true, status: 'APPROVED', category_id: categories[0].id, created_by: instructor.id, instructors: { create: { user_id: instructor.id, is_primary: true } }, chapters: { create: [ { title: { th: 'āļšāļ—āļ—āļĩāđˆ 1: āđ€āļĢāļīāđˆāļĄāļ•āđ‰āļ™', en: 'Chapter 1: Getting Started' }, description: { th: 'āđāļ™āļ°āļ™āļģ JavaScript', en: 'Introduction to JavaScript' }, order: 1, lessons: { create: [ { title: { th: 'JavaScript āļ„āļ·āļ­āļ­āļ°āđ„āļĢ', en: 'What is JavaScript' }, description: { th: 'āđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļ§āđˆāļē JavaScript āļ„āļ·āļ­āļ­āļ°āđ„āļĢ', en: 'Learn what JavaScript is' }, type: 'VIDEO', order: 1, is_preview: true }, { title: { th: 'āļ•āļąāļ§āđāļ›āļĢāđāļĨāļ°āļŠāļ™āļīāļ”āļ‚āđ‰āļ­āļĄāļđāļĨ', en: 'Variables and Data Types' }, description: { th: 'āđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āđ€āļāļĩāđˆāļĒāļ§āļāļąāļšāļ•āļąāļ§āđāļ›āļĢ', en: 'Learn about variables' }, type: 'VIDEO', order: 2 } ] } }, { title: { th: 'āļšāļ—āļ—āļĩāđˆ 2: āļŸāļąāļ‡āļāđŒāļŠāļąāļ™', en: 'Chapter 2: Functions' }, description: { th: 'āđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āđ€āļāļĩāđˆāļĒāļ§āļāļąāļšāļŸāļąāļ‡āļāđŒāļŠāļąāļ™', en: 'Learn about functions' }, order: 2, lessons: { create: [ { title: { th: 'āļāļēāļĢāļŠāļĢāđ‰āļēāļ‡āļŸāļąāļ‡āļāđŒāļŠāļąāļ™', en: 'Creating Functions' }, description: { th: 'āđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļ§āļīāļ˜āļĩāļŠāļĢāđ‰āļēāļ‡āļŸāļąāļ‡āļāđŒāļŠāļąāļ™', en: 'Learn how to create functions' }, type: 'VIDEO', order: 1 } ] } } ] } } }); console.log('✅ Database seeding completed!'); console.log('\n📊 Summary:'); console.log(`- Roles: ${roles.length}`); console.log(`- Users: 3 (admin, instructor, student)`); console.log(`- Categories: ${categories.length}`); console.log(`- Courses: 1`); console.log('\n🔑 Test Credentials:'); console.log('Admin: admin / admin123'); console.log('Instructor: instructor / admin123'); console.log('Student: student / admin123'); } main() .catch((e) => { console.error('❌ Error seeding database:', e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });