-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
330 lines (289 loc) · 10.1 KB
/
main.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import Imaps from 'imap-simple';
import MailParser from 'mailparser';
import fetch from 'node-fetch';
import Fs from 'fs/promises';
import _ from 'lodash';
import FormData from 'form-data';
import TelegramBot from 'node-telegram-bot-api';
async function parseMessage(message) {
const out = {};
let all = _.find(message.parts, { "which": "" })
const id = message.attributes.uid;
const idHeader = "Imap-Id: " + id + "\r\n";
const parsedBody = await MailParser.simpleParser(idHeader + all.body, {
skipImageLinks: true,
skipHtmlToText: true,
skipTextToHtml: true,
skipTextLinks: true
});
out.subject = parsedBody.subject;
out.from = parsedBody.from.text;
out.files = [];
for (let i in parsedBody.attachments) {
out.files.push(
{
attachment: parsedBody.attachments[i].content,
name: parsedBody.attachments[i].filename
}
);
}
return out;
}
async function getMailedReceipts(Config) {
const receipts = [];
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const config = {
imap: {
user: Config.imapUser,
password: Config.imapPassword,
host: Config.imapHost,
port: Config.imapPort,
tls: true,
authTimeout: 3000
},
};
const connection = await Imaps.connect(config);
await connection.openBox("INBOX");
let since = new Date();
since.setTime(Date.now() - (60 * 24 * 3600 * 1000)); //2 months
since = since.toISOString();
const searchCriteria = ['ALL', ['SINCE', since]];
const fetchOptions = {
bodies: ['HEADER.FIELDS (SUBJECT FROM)', 'TEXT', ''],
markSeen: false
};
const messages = await connection.search(searchCriteria, fetchOptions);
for (let message of messages) {
message = await parseMessage(message);
if (Config.receiptEmitters.indexOf(message.from.trim()) == -1) continue;
if (message.subject.startsWith(Config.mailPrefix)) {
for (let file of message.files) {
if (!file.name.startsWith(Config.receiptPrefix)) continue;
receipts.push({
name: file.name,
data: file.attachment
});
}
}
}
connection.end();
return receipts;
}
async function healthReport(config, v,log,ping=0){
if(!log)log=v?"OK":"FAIL";
const status=v?"up":"down";
const url=`${config.healthMonitor}/api/push/${config.healthMonitorKey}?status=${status}&msg=${log}&ping=${ping}`
await fetch(url);
}
async function main() {
const config = JSON.parse(await Fs.readFile("./config.json"));
const bot = config.telegramBotToken ? new TelegramBot(config.telegramBotToken, { polling: false }) : undefined;
const botChatId = config.telegramBotChatID;
let logs = "";
const log = (tx) => {
console.log(tx);
logs += tx+"\n";
};
const submitError = () => {
if (logs == "") return;
console.error(logs);
if (bot) bot.sendMessage(botChatId, logs);
logs = "";
};
const submitInfo = () => {
if (logs == "") return;
console.info(logs);
if (bot) bot.sendMessage(botChatId, logs);
logs = "";
};
const resetLog = () => {
logs = "";
};
// log("Started!");
// submitInfo();
loop(config, log, submitInfo, submitError, resetLog);
}
let ERRORS=0;
async function loop(config, log, submitInfo, submitError, resetLog) {
try {
if (await checkAndSubmit(config, log)) {
submitInfo();
console.log("New receipt found.")
healthReport(config,true,"New receipt found");
} else {
resetLog();
console.log("No new receipt found. Sleep for a while.");
healthReport(config,true,"No new receipt found");
}
ERRORS=0;
} catch (eeeee) {
console.error(eeeee)
log("" + eeeee);
healthReport(config,false,eeeee.message)
ERRORS++;
if(ERRORS>=config.numErrorsToTriggerSubmission){
submitError();
ERRORS=0;
}else{
resetLog();
}
}
setTimeout(()=>loop(config,log,submitInfo,submitError,resetLog), config.checkInterval * 1000);
}
async function checkAndSubmit(config, log) {
log("Check for new receipt.");
const OC_QUERY = `
query expenses {
expenses(account: {slug: "${config.collective}"}) {
nodes{
id
status
tags
requiredLegalDocuments
description
createdAt
virtualCard {
name
id
}
items{
description
amount
createdAt
incurredAt
id
url
}
}
}
}
`;
// Find expenses
let resp = await fetch(config.openCollectiveApiEndPoint + "/" + config.openCollectiveApiKey, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
query: OC_QUERY
})
}).then((res) => res.json());
const expenses = resp.data.expenses.nodes.filter(ex => {
return ex.virtualCard&&(
config.virtualCards.includes(ex.virtualCard.name)
||config.virtualCards.includes(ex.virtualCard.id)
);
}); // new -> old
// Find last submitted receipt
const lastReceiptNames=[];
for (const expense of expenses) {
if (expense.items[0].description) {
lastReceiptNames.push(expense.items[0].description);
// break;
}
}
if (lastReceiptNames.length==0) return false;
log("Last receipts " + lastReceiptNames.join(","));
// Find unsent receipts from mail account
let unsentReceipts = []; // old -> new
const mailedReceipts = await getMailedReceipts(config);
for(const lastReceiptName of lastReceiptNames){
if(mailedReceipts.length==1&&mailedReceipts[0].name != lastReceiptName){
unsentReceipts.push(mailedReceipts[0]);
}else{
for (let i = mailedReceipts.length - 2; i >= 0; i--) {
const mailedReceipt = mailedReceipts[i];
if (mailedReceipt.name == lastReceiptName) {
unsentReceipts = mailedReceipts.slice(i + 1);
break;
}
}
}
if (unsentReceipts.length != 0) break;
}
if (unsentReceipts.length == 0) return false;
// Submit receipts
let submit=false;
let receiptI = unsentReceipts.length - 1;
for (const expense of expenses) {
if (!expense.items[0].description) {
if (receiptI < 0) break;
const receipt = unsentReceipts[receiptI];
log("Send " + receipt.name + " to expense " + expense.id + " " + expense.description + " " + expense.createdAt);
const bodyData = new FormData()
bodyData.append("file", receipt.data, receipt.name);
bodyData.append("kind", "EXPENSE_ATTACHED_FILE");
const fileUrl = await fetch(config.openCollectiveUploadEndPoint, {
body: bodyData,
headers: {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.5",
"Cache-Control": "no-cache",
"Origin": config.openCollectiveOrigin,
"Pragma": "no-cache",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "no-cors",
"Sec-Fetch-Site": "same-origin",
"Api-Key": config.openCollectiveApiKey,
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0"
},
method: "POST"
}).then(res => res.text()).then(rest => {
let res;
try {
res = JSON.parse(rest);
} catch (e) {
throw new Error("Can't parse " + rest + " " + e);
}
if (res.status != 200) throw new Error("Error " + JSON.stringify(res));
return res.url;
});
log("File uploaded " + fileUrl);
const mutation = `mutation EditExpense($expense: ExpenseUpdateInput!) {
editExpense(expense: $expense) {
id
tags
items{
amount
id
description
url
}
}
}`
const mutationData = {
"expense": {
"id": expense.id,
"tags": config.tags,
"items": [
{
"id": expense.items[0].id,
"amount": expense.items[0].amount,
"description": receipt.name,
"url": fileUrl
}
]
}
}
log(JSON.stringify(mutationData));
resp = await fetch(config.openCollectiveApiEndPoint + "/" + config.openCollectiveApiKey, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
query: mutation,
variables: mutationData
})
}).then((res) => res.json());
log(JSON.stringify(resp));
submit=true;
receiptI--;
}
}
return submit;
}
main();