forked from latetedemelon/actual-simplefin-sync
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsync.js
119 lines (99 loc) · 3.72 KB
/
sync.js
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
const simpleFIN = require('./simpleFIN')
const api = require('@actual-app/api');
const actualInjected = require('@actual-app/api/dist/injected');
let _accessKey
let _linkedAccounts
let _startDate
let _serverUrl
let _serverPassword
let _budgetId
let _budgetEncryption
let _sendNotes
async function sync () {
const { mkdir } = require('fs').promises;
budgetspath = __dirname+'/budgets'
try {
await mkdir(budgetspath);
} catch (e) {}
await api.init({
dataDir: budgetspath,
serverURL: _serverUrl,
password: _serverPassword,
});
console.log('Downloading budget')
try {
await api.downloadBudget(_budgetId, {password: _budgetEncryption});
} catch (e) {
console.log(e.message)
throw e
}
console.log('Budget downloaded')
console.log('Getting all accounts and transactions from ActualBudget')
const allAccounts = await api.getAccounts()
console.log('Getting all transactions from SimpleFIN')
const allTrans = await simpleFIN.getTransactions(_accessKey, _startDate)
console.log('_____________________________________________________')
console.log('| Account | Added | Updated |')
console.log('+---------------------------+-----------+-----------+')
for (const simpleFINAccountId in _linkedAccounts) {
const accountId = _linkedAccounts[simpleFINAccountId]
const transactions = allTrans.accounts.find(f => f.id === simpleFINAccountId).transactions
.map(m => {
return {
account: accountId,
date: new Date(m.posted * 1000).toISOString().split('T')[0],
amount: parseInt(m.amount.replace('.', '')),
payee_name: m.payee,
notes: m.description,
imported_payee: m.payee,
imported_id: m.id
}
})
try {
const importedTransactions = await api.importTransactions(accountId, transactions)
const accountName = allAccounts.find(f => f.id === accountId).name
console.log(`| ${accountName.padEnd(25, ' ')} | ${importedTransactions.added.length.toString().padStart(9, ' ')} | ${importedTransactions.updated.length.toString().padStart(9, ' ')} |`)
if( _sendNotes == 'yes' ) {
const balanceDate = new Date(allTrans.accounts.find(f => f.id == simpleFINAccountId)['balance-date'] * 1000);
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
const balance = allTrans.accounts.find(f => f.id == simpleFINAccountId).balance
const accountNote = "Transactions synced at " + balanceDate.toLocaleString() + " with balance " + formatter.format(balance);
const noteId = 'account-' + accountId;
await actualInjected.send('notes-save', { id: noteId, note: accountNote });
}
} catch (ex) {
console.log(ex)
throw ex
}
}
console.log('¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯')
console.log('Re-downloading budget to force sync.')
try {
await api.downloadBudget(_budgetId, {password:_budgetEncryption});
} catch (e) {
console.log(e.message)
throw e
}
await api.shutdown()
}
async function run (accessKey, budgetId, budgetEncryption, linkedAccounts, startDate, serverUrl, serverPassword, sendNotes) {
_accessKey = accessKey
_linkedAccounts = linkedAccounts
_startDate = startDate
_serverUrl = serverUrl
_serverPassword = serverPassword
_budgetId = budgetId
_budgetEncryption = budgetEncryption
_sendNotes = sendNotes
if(!_serverUrl || !_serverPassword) {
throw new Error('Server URL or password not set')
} else {
console.log('Server information set')
}
console.log(`Budget ID: ${budgetId}`)
await sync()
}
module.exports = { run }