forked from zenmoney/ZenPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
29 lines (27 loc) · 1.07 KB
/
index.ts
File metadata and controls
29 lines (27 loc) · 1.07 KB
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
import { Account, ScrapeFunc, Transaction } from '../../types/zenmoney'
import { Auth, fetchAccounts, fetchTransactions, login, Preferences } from './api'
import { convertAccounts, convertTransaction } from './converters'
export const scrape: ScrapeFunc<Preferences> = async ({ preferences, fromDate, toDate }) => {
const auth = await login(preferences, ZenMoney.getData('auth') as Auth)
ZenMoney.setData('auth', auth)
ZenMoney.saveData()
const accounts: Account[] = []
const transactions: Transaction[] = []
await Promise.all(convertAccounts(await fetchAccounts(auth)).map(async ({ account, products }) => {
accounts.push(account)
if (ZenMoney.isAccountSkipped(account.id)) {
return
}
await Promise.all(products.map(async product => {
const apiTransactions = await fetchTransactions(auth, product, fromDate, toDate)
for (const apiTransaction of apiTransactions) {
const transaction = convertTransaction(apiTransaction, account)
transactions.push(transaction)
}
}))
}))
return {
accounts,
transactions
}
}