|
| 1 | +/** |
| 2 | + * HIS-05 (오프라인 낱말) / HIS-06 (최근 사용 낱말) 테스트용 시드 |
| 3 | + * |
| 4 | + * 사용법: |
| 5 | + * 컨테이너 내부: node prisma/seed-test-his.js |
| 6 | + * USER_ID 지정: USER_ID=xxxx node prisma/seed-test-his.js |
| 7 | + */ |
| 8 | + |
| 9 | +import { PrismaClient } from '@prisma/client'; |
| 10 | + |
| 11 | +const prisma = new PrismaClient(); |
| 12 | + |
| 13 | +async function main() { |
| 14 | + // ── 0. 대상 유저 결정 ────────────────────────────────── |
| 15 | + let userId = process.env.USER_ID; |
| 16 | + |
| 17 | + if (!userId) { |
| 18 | + const user = await prisma.user.findFirst({ orderBy: { createdAt: 'asc' } }); |
| 19 | + if (!user) throw new Error('DB에 유저가 없습니다. 먼저 회원가입하세요.'); |
| 20 | + userId = user.id; |
| 21 | + } |
| 22 | + |
| 23 | + console.log(`🎯 대상 유저 ID: ${userId}\n`); |
| 24 | + |
| 25 | + // ── 1. 기존 테스트 데이터 정리 ──────────────────────── |
| 26 | + await prisma.conversationHistory.deleteMany({ where: { userId } }); |
| 27 | + await prisma.userWord.deleteMany({ where: { userId } }); |
| 28 | + await prisma.userCategory.deleteMany({ where: { userId } }); |
| 29 | + console.log('🗑️ 기존 데이터 삭제 완료'); |
| 30 | + |
| 31 | + // ── 2. UserCategory 생성 ────────────────────────────── |
| 32 | + const category = await prisma.userCategory.create({ |
| 33 | + data: { |
| 34 | + userId, |
| 35 | + categoryName: '일상', |
| 36 | + displayOrder: 1, |
| 37 | + }, |
| 38 | + }); |
| 39 | + console.log(`📁 카테고리 생성: ${category.categoryName}`); |
| 40 | + |
| 41 | + // ── 3. UserWord 생성 (isFavorite 포함) ──────────────── |
| 42 | + const favoriteWords = ['밥', '물', '화장실', '좋아', '싫어']; |
| 43 | + const normalWords = ['학교', '집', '엄마', '아빠', '친구', '선생님', '버스']; |
| 44 | + |
| 45 | + let order = 1; |
| 46 | + |
| 47 | + for (const word of favoriteWords) { |
| 48 | + await prisma.userWord.create({ |
| 49 | + data: { |
| 50 | + userId, |
| 51 | + userCategoryId: category.id, |
| 52 | + partOfSpeech: 'NOUN', |
| 53 | + customWord: word, |
| 54 | + displayOrder: order++, |
| 55 | + isFavorite: true, |
| 56 | + isDeleted: false, |
| 57 | + }, |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + for (const word of normalWords) { |
| 62 | + await prisma.userWord.create({ |
| 63 | + data: { |
| 64 | + userId, |
| 65 | + userCategoryId: category.id, |
| 66 | + partOfSpeech: 'NOUN', |
| 67 | + customWord: word, |
| 68 | + displayOrder: order++, |
| 69 | + isFavorite: false, |
| 70 | + isDeleted: false, |
| 71 | + }, |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + console.log(`✨ UserWord 생성: 즐겨찾기 ${favoriteWords.length}개, 일반 ${normalWords.length}개`); |
| 76 | + |
| 77 | + // ── 4. ConversationHistory 생성 ─────────────────────── |
| 78 | + const now = new Date(); |
| 79 | + |
| 80 | + const daysAgo = (d) => { |
| 81 | + const date = new Date(now); |
| 82 | + date.setDate(date.getDate() - d); |
| 83 | + return date; |
| 84 | + }; |
| 85 | + |
| 86 | + // inputWords 형식: [{word, order}] |
| 87 | + const makeInputWords = (...words) => |
| 88 | + words.map((w, i) => ({ word: w, order: i })); |
| 89 | + |
| 90 | + const histories = [ |
| 91 | + // 최근 1주일 이내 (HIS-06 recent-words) |
| 92 | + { days: 0, words: ['밥', '먹다'], sentence: '밥 먹을게요.' }, |
| 93 | + { days: 1, words: ['물', '주다'], sentence: '물 주세요.' }, |
| 94 | + { days: 2, words: ['화장실', '가다'], sentence: '화장실 가고 싶어요.' }, |
| 95 | + { days: 3, words: ['좋아', '이거'], sentence: '이거 좋아요.' }, |
| 96 | + { days: 4, words: ['엄마', '보고싶다'], sentence: '엄마 보고 싶어요.' }, |
| 97 | + { days: 5, words: ['밥', '주다'], sentence: '밥 주세요.' }, |
| 98 | + { days: 6, words: ['집', '가다'], sentence: '집에 가고 싶어요.' }, |
| 99 | + |
| 100 | + // 최근 3개월 이내 (HIS-05 offline-words 빈도 계산용) |
| 101 | + { days: 10, words: ['밥', '먹다'], sentence: '밥 먹었어요.' }, |
| 102 | + { days: 15, words: ['밥', '주다'], sentence: '밥 주세요.' }, |
| 103 | + { days: 20, words: ['물', '마시다'], sentence: '물 마실게요.' }, |
| 104 | + { days: 25, words: ['화장실', '급하다'], sentence: '화장실이 급해요.' }, |
| 105 | + { days: 30, words: ['학교', '가다'], sentence: '학교 가요.' }, |
| 106 | + { days: 40, words: ['친구', '만나다'], sentence: '친구 만나요.' }, |
| 107 | + { days: 50, words: ['밥', '싫어'], sentence: '밥 싫어요.' }, |
| 108 | + { days: 60, words: ['선생님', '안녕'], sentence: '선생님 안녕하세요.' }, |
| 109 | + { days: 70, words: ['버스', '타다'], sentence: '버스 타요.' }, |
| 110 | + { days: 80, words: ['물', '주다'], sentence: '물 주세요.' }, |
| 111 | + ]; |
| 112 | + |
| 113 | + for (const h of histories) { |
| 114 | + await prisma.conversationHistory.create({ |
| 115 | + data: { |
| 116 | + userId, |
| 117 | + inputWords: makeInputWords(...h.words), |
| 118 | + inputType: 'WORD_ONLY', |
| 119 | + suggestedSentences: [h.sentence], |
| 120 | + selectedSentence: h.sentence, |
| 121 | + isOutputted: true, |
| 122 | + isDeleted: false, |
| 123 | + createdAt: daysAgo(h.days), |
| 124 | + }, |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + console.log(`📝 ConversationHistory 생성: ${histories.length}개`); |
| 129 | + console.log('\n✅ 시드 완료!'); |
| 130 | + console.log('\n테스트:'); |
| 131 | + console.log(' HIS-05 → GET /api/histories/offline-words?limit=30'); |
| 132 | + console.log(' HIS-06 → GET /api/histories/recent-words'); |
| 133 | +} |
| 134 | + |
| 135 | +main() |
| 136 | + .catch((e) => { |
| 137 | + console.error('❌ Seed error:', e); |
| 138 | + process.exit(1); |
| 139 | + }) |
| 140 | + .finally(async () => { |
| 141 | + await prisma.$disconnect(); |
| 142 | + }); |
0 commit comments