-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-tag.js
More file actions
54 lines (48 loc) · 1.44 KB
/
auto-tag.js
File metadata and controls
54 lines (48 loc) · 1.44 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
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
const SCRIPT_NOTIFY = "SCRIPT_NOTIFY";
const List = Java.type('java.util.List');
class Script {
get id() {
return "0972510b-70de-4e91-b3d0-b696375474f0";
}
get name() {
return 'Set Tag';
}
get description() {
return `Set Tag automatically if subject starts with the label between brackets.e.g [INTERNET] facture internet`;
}
get enabled() {
return true;
}
get consumeEvent() {
return true;
}
process(payload) {
let event = JSON.parse(payload);
if (event.eventName === 'ExpenseReceived') {
logger.info("receiving expense received with name {}", event.name);
let expense = feeService.findById(event.expenseId);
if (expense.isPresent()) {
expense = expense.get();
if (expense.getTag()) {
logger.info("tag already set somehow. do nothing");
return "OK";
}
const labels = labelService.findAll();
const match = expense.getSubject().match(/\[(.*?)\]/);
if (match?.length >= 2) {
const tag = match[1].toUpperCase();
if (tag?.length) {
for (const label of labels) {
if (label.getName().toUpperCase() === tag) {
logger.info(`set tag ${tag} for expense ${expense.getSubject()}`);
feeService.updateTag(label.getName(), List.of(expense.getId()));
return "OK";
}
}
}
}
}
}
return "OK";
}
}