-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
55 lines (48 loc) · 2.01 KB
/
index.ts
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
import { loadUserSession, token, userId } from './api/auth';
import { fetchPockets } from './api/pockets';
import { fetchAllTransactions, transactionsCompareFn } from './api/transactions';
import { BalanceHistory } from './models/balance-history.interface';
import { Transaction } from './models/transactions.interface';
import { balanceHistoryCompareFn, generatePocketBalanceHistory } from './utils/balance-history';
import { saveBalanceHistoryCsv, saveTransactionsCsv } from './utils/fs/csv';
async function main() {
// Try to restore the session saved to the file
await loadUserSession();
if (userId && token) {
const pockets = await fetchPockets(userId, token);
const now = new Date();
const transactions: Transaction[] = [];
const balanceHistory: BalanceHistory[] = [];
for (const pocket of pockets) {
console.log('fetching transactions for pocket', pocket.name);
const transactionsForPocket = await fetchAllTransactions(pocket.pocket_id, userId, token, true);
transactions.push(...transactionsForPocket);
const pocketBalanceHistory = generatePocketBalanceHistory(
pocket.creation_date,
now.toISOString(),
pocket,
transactionsForPocket,
true
);
balanceHistory.push(...pocketBalanceHistory);
}
console.log('total transactions', transactions.length);
transactions.sort(transactionsCompareFn);
const uniqueObjMap: { [key: string]: Transaction } = {};
for (const transaction of transactions) {
if (!uniqueObjMap[transaction.trn_id]) {
uniqueObjMap[transaction.pocket_id + '+' + transaction.trn_id] = transaction;
}
}
const uniqueTransactions = Object.values(uniqueObjMap);
console.log('unique transactions', uniqueTransactions.length);
// Map transactions to csv
saveTransactionsCsv(pockets, transactions);
saveBalanceHistoryCsv(balanceHistory.sort(balanceHistoryCompareFn));
}
}
main()
.catch((error) => console.error(error))
.finally(async () => {
process.exit();
});