forked from infiniteluke/actualplaid
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathactual.js
214 lines (183 loc) · 6.37 KB
/
actual.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const { getAppConfigFromEnv } = require("./config");
const actual = require("@actual-app/api");
const fs = require("fs");
const inquirer = require("inquirer");
let { q, runQuery } = require('@actual-app/api');
const appConfig = getAppConfigFromEnv();
/**
*
* @returns {Promise<typeof actual>}
*/
async function initialize(config) {
try {
const tmp_dir = `./temp_data_actual/${config.get("user")}`
fs.mkdirSync(tmp_dir, { recursive: true });
await actual.init({
serverURL: appConfig.ACTUAL_SERVER_URL,
password: appConfig.ACTUAL_SERVER_PASSWORD,
dataDir: tmp_dir
});
let id = config.get("budget_id")
if (!id) {
id = (await inquirer.prompt({
name: "budget_id",
message: `This is your (${config.get('user')}) first time using this user, what is your budget id? (Can be found is advanced settings on Actual as the 'sync-id')`,
})).budget_id
config.set("budget_id", id)
}
await actual.downloadBudget(id);
} catch (e) {
throw new Error(`Actual Budget Error: ${e.message}`);
}
return actual;
}
/**
*
* @param {typeof actual} actualInstance
*/
function listAccounts(actualInstance) {
return actualInstance.getAccounts();
}
/**
* Only works for the past month
* @param {typeof actual} actualInstance
* @param {*} accountId
*/
async function getLastTransactionDate(actualInstance, accountId) {
const monthAgo = new Date();
monthAgo.setMonth(monthAgo.getMonth() - 1);
const transactions = await actualInstance.getTransactions(accountId, monthAgo, new Date());
if (transactions.length === 0) {
return new Date(0);
}
// Transactions of the day are already imported, so start from the next day.
const last = new Date(transactions[0].date);
last.setDate(last.getDate() + 1);
return last;
}
const ABN_AMRO_TRANSACTION_MAPPER = (accountId) => (transaction) => {
const description = transaction.name
let notes = description;
let payee = description;
if (description.includes("TRTP")) {
let splitted = description.split("/");
if (splitted[2].includes("iDEAL") || splitted[2].includes("SEPA OVERBOEKING")) {
payee = splitted[8].trim();
notes = splitted[10].trim();
} else if (splitted[2].includes("SEPA Incasso")) {
payee = splitted[6].trim();
notes = splitted[10].trim();
}
} else if (description.includes("SEPA iDEAL")) {
let splitted = description.split("Naam:");
payee = splitted[1].split("Omschrijving:")[0].trim();
notes = splitted[1].split("Omschrijving:")[1].split("Kenmerk:")[0].trim();
} else if (description.includes("BEA")) {
let splitted = description.split(",");
let info = splitted[1].replace(" Apple Pay", "").replace("Betaalpas", "").replace("PAS544", "").trim();
payee = info;
} else if (description.includes("SEPA Incasso")) {
let splitted = description.split("Naam:");
if (splitted[1].includes("Machtiging:")) {
payee = splitted[1].split("Machtiging:")[0].trim();
} else {
payee = splitted[1].split("Omschrijving:")[0].trim();
}
notes = splitted[1].split("Omschrijving:")[1].split("IBAN:")[0].trim();
} else if (description.includes("SEPA Overboeking")) {
let splitted = description.split("Naam:");
if (splitted.length > 1) {
if (splitted[1].includes("Omschrijving:")) {
payee = splitted[1].split("Omschrijving:")[0].trim();
if (splitted[1].includes("Kenmerk:")) {
notes = splitted[1].split("Omschrijving:")[1].split("Kenmerk:")[0].trim();
} else {
notes = splitted[1].split("Omschrijving:")[1].trim();
}
} else {
payee = splitted[1].trim();
notes = "";
}
} else {
payee = splitted[0];
notes = "";
}
}
let convertedAmount = transaction.amount * 100;
convertedAmount = Math.round(convertedAmount);
convertedAmount *= -1;
return {
account: accountId,
date: transaction.date,
amount: convertedAmount,
payee_name: payee,
imported_payee: payee,
notes: notes,
imported_id: transaction.transaction_id,
}
}
const GENERIC_TRANSACTION_MAPPER = (accountId) => (transaction) => {
//if (transaction.pending) {
// console.error(transaction, accountId)
// throw new Error("Pending transactions are not supported")
//}
let convertedAmount = transaction.amount * 100;
convertedAmount = Math.round(convertedAmount);
convertedAmount *= -1;
return {
account: accountId,
date: transaction.date,
amount: convertedAmount,
payee_name: transaction.merchant_name || transaction.name,
imported_payee: transaction.merchant_name || transaction.name,
notes: transaction.name,
imported_id: transaction.transaction_id,
cleared: !transaction.pending,
}
}
const map = {
"ABN AMRO": ABN_AMRO_TRANSACTION_MAPPER,
}
const transactionMapper = (accountId, bank) => {
if (map[bank]) {
return map[bank](accountId)
} else {
return GENERIC_TRANSACTION_MAPPER(accountId)
}
}
async function importPlaidTransactions(actualInstance, accountId, bank, transactions) {
const mapped = transactions
.map(transactionMapper(accountId, bank))
const actualResult = await actualInstance.importTransactions(
accountId,
mapped
);
console.log("Imported transactions raw data START:")
console.log(transactions)
console.log("ENV")
console.log("Actual logs: ", actualResult);
}
async function getBalance(actualInstance, accountId) {
const balance = await actualInstance.runQuery(q('transactions')
.filter({ account: accountId })
.options({ splits: 'all' })
.calculate({ $sum: '$amount' }),)
return balance.data;
}
/**
*
* @param {typeof actual} actualInstance
*/
async function finalize(actualInstance) {
await actualInstance.sync()
await actualInstance.shutdown();
}
module.exports = {
initialize,
listAccounts,
getLastTransactionDate,
importPlaidTransactions,
transactionMapper,
finalize,
getBalance
}