Spaces:
Sleeping
Sleeping
| import { getDb } from './db'; | |
| async function seed() { | |
| const db = await getDb(); | |
| console.log('Cleaning up existing data...'); | |
| await db.exec('DELETE FROM transactions'); | |
| await db.exec('DELETE FROM exchanges'); | |
| await db.exec('DELETE FROM loans'); | |
| const wallets = await db.all('SELECT * FROM wallets'); | |
| console.log('--- ALL WALLETS IN DB ---'); | |
| wallets.forEach(w => console.log(`- ID: ${w.id}, Name: "${w.name}", Currency: ${w.currency}`)); | |
| console.log('-------------------------'); | |
| const findWallet = (name: string) => { | |
| const found = wallets.find(w => w.name.toLowerCase() === name.toLowerCase()); | |
| return found; | |
| }; | |
| const cashUsd = findWallet('Cash USD'); | |
| const cashDinar = findWallet('Cash Dinar'); | |
| const fib = findWallet('FIB'); | |
| const alipay = findWallet('Alipay'); | |
| const wechat = findWallet('WeChat'); | |
| const usdt = findWallet('USDT'); | |
| if (!cashUsd || !cashDinar || !fib || !alipay || !wechat || !usdt) { | |
| console.error('Required wallets not found. Please ensure wallets are initialized.'); | |
| return; | |
| } | |
| // Calculate 6 months ago manually to avoid date-fns backend issues | |
| const today = new Date(); | |
| const startDate = new Date(today.getFullYear(), today.getMonth() - 6, 1); | |
| console.log('Seeding 6 months of data starting from', startDate.toISOString()); | |
| let currentDate = new Date(startDate); | |
| while (currentDate <= today) { | |
| const dateStr = currentDate.toISOString(); | |
| // 1. Monthly Salary (1st) | |
| if (currentDate.getDate() === 1) { | |
| await db.run( | |
| 'INSERT INTO transactions (type, amount, currency, wallet_id, category, note, date) VALUES (?, ?, ?, ?, ?, ?, ?)', | |
| ['income', 3500, 'USD', cashUsd.id, 'salary', 'Monthly Salary', dateStr] | |
| ); | |
| } | |
| // 2. Daily Expenses | |
| const expenseCount = Math.floor(Math.random() * 3) + 1; | |
| const categories = ['food', 'market', 'transport', 'cafe', 'other']; | |
| const currencies = ['USD', 'IQD', 'RMB']; | |
| for (let i = 0; i < expenseCount; i++) { | |
| const cat = categories[Math.floor(Math.random() * categories.length)]; | |
| const curr = currencies[Math.floor(Math.random() * currencies.length)]; | |
| let wallet: any; | |
| let amount: number; | |
| if (curr === 'USD') { | |
| wallet = Math.random() > 0.8 ? usdt : cashUsd; | |
| amount = Math.random() * 50 + 5; | |
| } else if (curr === 'IQD') { | |
| wallet = Math.random() > 0.5 ? cashDinar : fib; | |
| amount = Math.random() * 50000 + 5000; | |
| } else { | |
| wallet = Math.random() > 0.5 ? alipay : wechat; | |
| amount = Math.random() * 200 + 20; | |
| } | |
| await db.run( | |
| 'INSERT INTO transactions (type, amount, currency, wallet_id, category, note, date, country_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', | |
| ['expense', amount, curr, wallet.id, cat, `Daily ${cat}`, dateStr, curr === 'USD' ? 'US' : curr === 'IQD' ? 'IQ' : 'CN'] | |
| ); | |
| } | |
| // 3. Monthly Rent/Utilities (15th) | |
| if (currentDate.getDate() === 15) { | |
| await db.run( | |
| 'INSERT INTO transactions (type, amount, currency, wallet_id, category, note, date) VALUES (?, ?, ?, ?, ?, ?, ?)', | |
| ['expense', 800, 'USD', cashUsd.id, 'electricity', 'Monthly Rent & Bills', dateStr] | |
| ); | |
| } | |
| // 4. Occasional Exchanges | |
| if (Math.random() > 0.9) { | |
| const usdAmount = 300; | |
| const iqdAmount = usdAmount * 1530; | |
| await db.run( | |
| 'INSERT INTO exchanges (from_amount, from_currency, to_amount, to_currency, rate, date, note, from_wallet_id, to_wallet_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', | |
| [usdAmount, 'USD', iqdAmount, 'IQD', 1530, dateStr, 'USD to IQD Cash', cashUsd.id, cashDinar.id] | |
| ); | |
| } | |
| // Increment day | |
| currentDate.setDate(currentDate.getDate() + 1); | |
| } | |
| // 5. Seed some loans | |
| await db.run( | |
| 'INSERT INTO loans (person, type, amount, currency, paid, date, note) VALUES (?, ?, ?, ?, ?, ?, ?)', | |
| ['Karwan', 'borrowed_from_me', 1000, 'USD', 200, startDate.toISOString(), 'Business loan'] | |
| ); | |
| await db.run( | |
| 'INSERT INTO loans (person, type, amount, currency, paid, date, note) VALUES (?, ?, ?, ?, ?, ?, ?)', | |
| ['Uncle', 'borrowed_from_me', 500, 'USD', 500, new Date(today.getFullYear(), today.getMonth() - 2, 10).toISOString(), 'Personal favor (Fully Paid)'] | |
| ); | |
| console.log('Seeding completed successfully.'); | |
| } | |
| seed().catch(err => { | |
| console.error('Error seeding data:', err); | |
| process.exit(1); | |
| }); | |