File size: 4,491 Bytes
2dddd1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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);
});