-
Notifications
You must be signed in to change notification settings - Fork 1
/
mono_actual.js
290 lines (252 loc) · 8.37 KB
/
mono_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// npm install --save @actual-app/api
// npm install --save [email protected]
const actualApi = require('@actual-app/api');
const CACHE_DIR_PATH = '.cache/';
const MONO_URL = 'https://api.monobank.ua';
const DAYS_TO_SYNC = parseInt(process.env.DAYS_TO_SYNC);
const DEFAULT_DAYS_SYNC = DAYS_TO_SYNC < 7 ? DAYS_TO_SYNC : 7;
const MONO_TOKEN = process.env.MONO_TOKEN;
const ACTUAL_URL = process.env.ACTUAL_URL;
const ACTUAL_PASSWORD = process.env.ACTUAL_PASSWORD;
const ACTUAL_SYNC_ID = process.env.ACTUAL_SYNC_ID;
const USE_NODE_CRON = process.env.USE_NODE_CRON;
let ACTUAL_ACCOUNTS = []
function create_cache_dir() {
const fs = require('fs');
const dir = CACHE_DIR_PATH;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
// const example_mono_trans = [
// {
// id: 'aaaa',
// time: 1705091543,
// description: 'test',
// mcc: 4829,
// originalMcc: 4829,
// amount: -100,
// operationAmount: -100,
// currencyCode: 980,
// commissionRate: 0,
// cashbackAmount: 0,
// balance: 536687,
// hold: true,
// receiptId: 'xxxxx'
// }
// ];
async function fetch_data() {
let TOTAL_DAYS_SYNC = DAYS_TO_SYNC;
// MONO
async function getMonoDataFromCards(startDateTimestamp, endDateTimestamp) {
let card_index = 0;
let result = [];
while (true) {
console.log("Parsing card number " + card_index);
const cards_data = process.env["MONO_CARD_" + card_index];
if (!cards_data) {
console.log("Card number " + card_index + " is absent");
break;
}
card_index++;
console.log("splitting " + cards_data);
const array = cards_data.split(":");
console.log("after split " + array);
const mono_card = array[0];
const actual_card = ACTUAL_ACCOUNTS.find((account) => {
if (account.name.toUpperCase() === array[1].toUpperCase()) {
return true;
}
if (account.id.toUpperCase() === array[1].toUpperCase()) {
return true;
}
return false;
});
const actual_id = actual_card.id;
const new_data = await fetchMonoData(mono_card, startDateTimestamp, endDateTimestamp, TOTAL_DAYS_SYNC > 0).catch((error) => {
console.error(error);
});
if (actual_id && new_data) {
result.push({
actual_card: actual_id,
mono_data: new_data
});
}
}
return result;
}
async function fetchMonoData(card, startDateTimestamp, endDateTimestamp, sleepToAllowNextRequest) {
try {
const mono_url = MONO_URL + '/personal/statement/' + card + '/' + startDateTimestamp + '/' + endDateTimestamp;
const response = await fetch(mono_url, {
headers: { 'X-Token': MONO_TOKEN, },
}).catch((error) => {
console.error(error);
});
if (!response.ok) {
throw new Error(mono_url + ' failed: ' + ' ' + response.status + ' ' + response.statusText);
}
const data = await response.json().catch((error) => {
console.error(error);
});
// Mono allows 1 request per 60 seconds
if (sleepToAllowNextRequest) {
// console.log('sleeping for 60 seconds');
await sleep(60 * 1000); // 60 seconds
}
return data;
} catch (error) {
console.error(error);
}
}
async function fetchActualData(startDateIso, endDateIso) {
try {
const actual_data = await actualApi.runQuery(
actualApi.q('transactions')
.filter({
date: [
{ $gte: startDateIso },
{ $lte: endDateIso }
]
})
.select('*')
).catch((error) => {
console.error(error);
});
// actual_data structure
// const actual_data = {
// data: [
// {
// id: '8eb6241f-3d36-48aa-aab6-c2c8e',
// is_parent: false,
// is_child: false,
// parent_id: null,
// account: null,
// category: null,
// amount: -15200,
// payee: '13c21c0a-284a-4689-b4',
// notes: null,
// date: '2024-01-35',
// imported_id: null,
// error: null,
// imported_payee: '...',
// starting_balance_flag: false,
// transfer_id: null,
// sort_order: 1706225650763,
// cleared: true,
// reconciled: false,
// tombstone: false,
// schedule: null
// },
// ],
// dependencies: [ 'transactions', 'accounts', 'categories', 'payees', 'schedules' ]
// }
return actual_data.data
} catch (error) {
console.error(error);
}
}
// START
create_cache_dir();
await actualApi.init({
dataDir: CACHE_DIR_PATH,
serverURL: ACTUAL_URL,
password: ACTUAL_PASSWORD,
}).catch((error) => {
console.error(error);
});
await actualApi.downloadBudget(ACTUAL_SYNC_ID).catch((error) => {
console.error(error);
});
// accounts = [
// {
// "id":"19525deb-b8d8-4681-af43-69ddc3d7110e",
// "name":"name of the budget",
// "offbudget":true,
// "closed":false
// }
// ]
ACTUAL_ACCOUNTS = await actualApi.getAccounts().catch((error) => {
console.error(error);
});
console.log("actual accounts " + JSON.stringify(ACTUAL_ACCOUNTS));
let endDate = new Date();
endDate.setHours(0, 0, 0, 0);
const startDate = new Date();
startDate.setHours(0, 0, 0, 0);
while (TOTAL_DAYS_SYNC > 0) {
const endDateIso = endDate.toISOString().slice(0, 10);
const endDateTimestamp = endDate.getTime();
// set date as end - DAYS_TO_SYNC
startDate.setDate(endDate.getDate() - DEFAULT_DAYS_SYNC);
const startDateIso = startDate.toISOString().slice(0, 10);
const startDateTimestamp = startDate.getTime();
console.log('Sync: ' + startDateIso + ' ' + endDateIso);
let transactions = [];
const actual_data = await fetchActualData(startDateIso, endDateIso).catch((error) => {
console.error(error);
});
console.log("actual data-----------------------------------------------------------")
console.log(actual_data);
console.log("end actual data-----------------------------------------------------------")
if (actual_data) {
const cards_data = await getMonoDataFromCards(startDateTimestamp, endDateTimestamp).catch((error) => {
console.error(error);
});
console.log("mono data-----------------------------------------------------------")
console.log(cards_data);
console.log("end mono data-----------------------------------------------------------")
if (cards_data && cards_data.length > 0) {
// cards_data = {
// actual_card: actual_card
// mono_data: mono_data_array
// }
for (const data of cards_data) {
for (const exp of data.mono_data) {
let create_trans = {};
create_trans.account = data.actual_card;
create_trans.amount = exp.amount;
create_trans.date = new Date(exp.time * 1000).toISOString().slice(0, 10);
create_trans.payee_name = exp.description;
create_trans.imported_id = exp.id
transactions.push(create_trans);
}
if (transactions.length > 0) {
console.log("adding " + transactions.length + " transactions");
console.log("transactions")
console.log(transactions)
console.log("end transactions")
let result = await actualApi.importTransactions(data.actual_card, transactions).catch((error) => {
console.error(error);
});
console.log(result);
} else {
console.log('No new data to be added: ' + transactions.length)
}
transactions = [];
}
}
}
endDate = startDate;
TOTAL_DAYS_SYNC -= DEFAULT_DAYS_SYNC;
}
await actualApi.shutdown();
};
// fetch initial data
(async () => {
await fetch_data();
})()
if (USE_NODE_CRON) {
console.log("Starting cron");
var cron = require('node-cron');
const CRON_ONCE_PER_HOUR = '0 * * * *';
// shedule fetch data for later
cron.schedule(CRON_ONCE_PER_HOUR, async () => {
await fetch_data();
});
}