const fs = require('fs'); const path = require('path'); const bcrypt = require('bcrypt'); const DatabaseHelper = require('../database/db-helper'); async function initializeDatabase() { console.log('๐Ÿ—„๏ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ดˆ๊ธฐํ™” ์‹œ์ž‘...'); const dbHelper = new DatabaseHelper(); try { // ๋ฐ์ดํ„ฐ ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ const dataDir = path.join(__dirname, '..', 'data'); if (!fs.existsSync(dataDir)) { fs.mkdirSync(dataDir, { recursive: true }); console.log('๐Ÿ“ ๋ฐ์ดํ„ฐ ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ๋จ:', dataDir); } // ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ๋ฐ ํ…Œ์ด๋ธ” ์ƒ์„ฑ await dbHelper.connect(); console.log('โœ… ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์„ฑ๊ณต'); // ๊ธฐ๋ณธ ๊ด€๋ฆฌ์ž ๊ณ„์ • ์ƒ์„ฑ const adminEmail = 'admin@jaryo.com'; const adminPassword = 'Hee150603!'; const existingUser = await dbHelper.getUserByEmail(adminEmail); if (!existingUser) { const saltRounds = 10; const hashedPassword = await bcrypt.hash(adminPassword, saltRounds); const adminData = { email: adminEmail, password_hash: hashedPassword, name: '๊ด€๋ฆฌ์ž', role: 'admin' }; await dbHelper.createUser(adminData); console.log('๐Ÿ‘ค ๊ธฐ๋ณธ ๊ด€๋ฆฌ์ž ๊ณ„์ • ์ƒ์„ฑ๋จ'); console.log('๐Ÿ“ง ์ด๋ฉ”์ผ:', adminEmail); console.log('๐Ÿ”‘ ๋น„๋ฐ€๋ฒˆํ˜ธ:', adminPassword); } else { console.log('๐Ÿ‘ค ๊ด€๋ฆฌ์ž ๊ณ„์ •์ด ์ด๋ฏธ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.'); } // ๊ธฐ๋ณธ ์นดํ…Œ๊ณ ๋ฆฌ ์ƒ์„ฑ const defaultCategories = ['๋ฌธ์„œ', '์ด๋ฏธ์ง€', '๋™์˜์ƒ', 'ํ”„๋ ˆ์  ํ…Œ์ด์…˜', '๊ธฐํƒ€']; for (const categoryName of defaultCategories) { try { await dbHelper.addCategory(categoryName); console.log(`๐Ÿ“‚ ์นดํ…Œ๊ณ ๋ฆฌ ์ƒ์„ฑ๋จ: ${categoryName}`); } catch (error) { if (error.message.includes('UNIQUE constraint failed')) { console.log(`๐Ÿ“‚ ์นดํ…Œ๊ณ ๋ฆฌ ์ด๋ฏธ ์กด์žฌ: ${categoryName}`); } else { console.error(`์นดํ…Œ๊ณ ๋ฆฌ ์ƒ์„ฑ ์˜ค๋ฅ˜ (${categoryName}):`, error.message); } } } console.log('๐ŸŽ‰ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ดˆ๊ธฐํ™” ์™„๋ฃŒ!'); } catch (error) { console.error('โŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ดˆ๊ธฐํ™” ์‹คํŒจ:', error); process.exit(1); } finally { await dbHelper.close(); } } // ์Šคํฌ๋ฆฝํŠธ ์ง์ ‘ ์‹คํ–‰ ์‹œ์—๋งŒ ์ดˆ๊ธฐํ™” ์‹คํ–‰ if (require.main === module) { initializeDatabase().catch(console.error); } module.exports = initializeDatabase;