File size: 5,023 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
119
120
121
122
123
124
125
const sqlite3 = require('sqlite3');
const { open } = require('sqlite');
const path = require('path');
const { subMonths, addDays, startOfMonth } = require('date-fns');

async function seed() {
  const dbPath = path.resolve(__dirname, 'database.sqlite');
  const db = await open({
    filename: dbPath,
    driver: sqlite3.Database
  });

  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');
  if (wallets.length === 0) {
    console.error('No wallets found in DB.');
    return;
  }

  const findWalletByCurrency = (curr) => wallets.find(w => w.currency === curr) || wallets[0];
  const usdWallet = wallets.find(w => w.currency === 'USD') || wallets[0];
  const iqdWallet = wallets.find(w => w.currency === 'IQD') || wallets[0];
  const rmbWallet = wallets.find(w => w.currency === 'RMB') || wallets[0];

  const startDate = startOfMonth(subMonths(new Date(), 24)); // 2 Years
  const today = new Date();

  console.log('Seeding 2 years of data...');

  let currentDate = startDate;
  let baseSalary = 3000;

  while (currentDate <= today) {
    const dateStr = currentDate.toISOString();

    // 1. Monthly Salary (USD) - with variability
    if (currentDate.getDate() === 1) {
      // Increase salary every 12 months
      const monthsSinceStart = Math.floor((currentDate - startDate) / (1000 * 60 * 60 * 24 * 30));
      if (monthsSinceStart > 0 && monthsSinceStart % 12 === 0) {
        baseSalary += 500;
        console.log(`Salary increase at month ${monthsSinceStart} to ${baseSalary}`);
      }

      const salaryAmount = baseSalary + (Math.random() > 0.8 ? 500 : 0); // Occasional bonus
      await db.run(
        'INSERT INTO transactions (type, amount, currency, wallet_id, category, note, date) VALUES (?, ?, ?, ?, ?, ?, ?)',
        ['income', salaryAmount, 'USD', usdWallet.id, 'salary', monthsSinceStart % 12 === 0 && monthsSinceStart > 0 ? 'Salary Increase + Payment' : 'Monthly Salary', dateStr]
      );
    }

    // 2. Daily Expenses - More variety
    const expenseCount = Math.floor(Math.random() * 4) + 1; // 1-4 expenses per day
    const categories = ['food', 'market', 'transport', 'cafe', 'health', 'gift', '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)];
        const wallet = findWalletByCurrency(curr);
        let amount;

        if (curr === 'USD') {
            amount = Math.random() * 60 + 5;
        } else if (curr === 'IQD') {
            amount = Math.random() * 60000 + 5000;
        } else {
            amount = Math.random() * 300 + 20;
        }

        await db.run(
          'INSERT INTO transactions (type, amount, currency, wallet_id, category, note, date) VALUES (?, ?, ?, ?, ?, ?, ?)',
          ['expense', amount, curr, wallet.id, cat, `Daily ${cat}`, dateStr]
        );
    }

    // 3. Rent & Utilities (USD)
    if (currentDate.getDate() === 15) {
        await db.run(
          'INSERT INTO transactions (type, amount, currency, wallet_id, category, note, date) VALUES (?, ?, ?, ?, ?, ?, ?)',
          ['expense', 850 + (Math.random() * 50), 'USD', usdWallet.id, 'electricity', 'Rent & Utilities', dateStr]
        );
    }

    // 4. Frequent Exchanges (Varying amounts)
    if (Math.random() > 0.85) {
        // Exchange USD to IQD or RMB
        const targetCurr = Math.random() > 0.5 ? 'IQD' : 'RMB';
        const targetWallet = findWalletByCurrency(targetCurr);
        const usdAmount = 100 + Math.floor(Math.random() * 400);
        const rate = targetCurr === 'IQD' ? 1500 + (Math.random() * 50) : 6.8 + (Math.random() * 0.5);
        const targetAmount = usdAmount * rate;

        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', targetAmount, targetCurr, rate, dateStr, `Exchanged USD to ${targetCurr}`, usdWallet.id, targetWallet.id]
        );
    }

    currentDate = addDays(currentDate, 1);
  }

  // 5. Some historical loans
  await db.run(
    'INSERT INTO loans (person, type, amount, currency, paid, date, note) VALUES (?, ?, ?, ?, ?, ?, ?)',
    ['Karwan', 'borrowed_from_me', 2000, 'USD', 1200, subMonths(today, 18).toISOString(), 'Long term loan']
  );
  await db.run(
    'INSERT INTO loans (person, type, amount, currency, paid, date, note) VALUES (?, ?, ?, ?, ?, ?, ?)',
    ['Uncle', 'borrowed_from_me', 1500000, 'IQD', 1500000, subMonths(today, 6).toISOString(), 'Car help (Paid back)']
  );

  console.log('Seeding 2 years completed successfully.');
  await db.close();
}

seed().catch(err => {
  console.error('Error seeding data:', err);
  process.exit(1);
});