This repository was archived by the owner on Apr 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkimai.js
More file actions
140 lines (128 loc) · 4.43 KB
/
kimai.js
File metadata and controls
140 lines (128 loc) · 4.43 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
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
const nodefetch = require('node-fetch');
const fetch = require('fetch-retry')(nodefetch);
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const CronJob = require('cron').CronJob;
const assert = require('assert');
const KIMAI_API_URL = process.env.KIMAI_API_URL;
const KIMAI_API_USER = process.env.KIMAI_API_USER;
const KIMAI_API_TOKEN = process.env.KIMAI_API_TOKEN;
assert(KIMAI_API_URL, "env var KIMAI_API_URL not set");
assert(KIMAI_API_USER, "env var KIMAI_API_USER not set");
assert(KIMAI_API_TOKEN, "env var KIMAI_API_TOKEN not set");
const API_CONCURRENT_READS = 10;
const adapter = new FileSync(process.env.JSONDB_FILE_PATH || 'database/db.json');
const db = low(adapter);
// Set some defaults (required if your JSON file is empty)
db.defaults({ projects:{} })
.write();
const fetchKimai = async (path) => {
const result = await fetch(KIMAI_API_URL + path, {
headers: {
'X-AUTH-USER': KIMAI_API_USER,
'X-AUTH-TOKEN': KIMAI_API_TOKEN,
},
},{
retries: 10,
retryDelay: (attempt, error, response) => Math.pow(2, attempt) * 1000,
});
const json = await result.json();
// console.log(json);
// console.log("fetchKimai done:",path);
return json;
};
const postKimai = async (path,body) => {
console.log("postKimai init:",KIMAI_API_URL + path,body);
const result = await fetch(KIMAI_API_URL + path, {
method: 'POST',
headers: {
'X-AUTH-USER': KIMAI_API_USER,
'X-AUTH-TOKEN': KIMAI_API_TOKEN,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
},{
retries: 3,
retryDelay: (attempt, error, response) => Math.pow(2, attempt) * 1000,
});
if (!result.ok) throw new Error('Post to kimai not successful!')
const json = await result.json();
return json;
};
/**
* get an array of all users that have settings in the DB. Used for Cron function.
*/
const getAllUsers = async () => {
try {
return await db.get('allUsers').value();
} catch (e) {
console.dir(e);
}
}
/**
* Get values from the JSON DB
* @param {string} userId the user ID from LDAP
* @param {string} key
* @param {*} id
*/
const getSettings = async (userId,key,id) => {
try{
//await Parallel.each(project_details, async (proj) => db.set('projects.'+proj.id, proj).write(), 1);
return await db.get(userId+'_'+key+'_'+id).value();
} catch(e){
console.dir(e);
}
};
/**
* Set values in the JSON DB. Automatically saves the dbb value 'allUsers' as an array of all users available.
* @param {*} userId the user ID coming from LDAP
* @param {*} key
* @param {*} id
* @param {*} data
*/
const setSettings = async (userId,key,id,data) => {
try{
const allUsers = await db.get('allUsers').value();
if(!allUsers) {
const users = []
users.push(userId)
await db.set('allUsers', users).write()
} else {
if(!allUsers.includes(userId)){
allUsers.push(userId)
await db.set('allUsers', allUsers).write()
}
}
//await Parallel.each(project_details, async (proj) => db.set('projects.'+proj.id, proj).write(), 1);
return await db.set(userId+'_'+key+'_'+id,data).write();
} catch(e){
console.dir(e);
}
};
const DEPRECATED_getAllProjectDetails = async () => {
return await db.get('projects').value();
};
const DEPRECATED_getProjectDetailsById = async (id) => {
return await db.get('projects').value()[id];
};
const DEPRECATED_init = async () => {
console.log("init the cache");
// sync as soon as file got required
await fetchAllProjectDetails();
// register all cron jobs to keep data up to date
new CronJob('0 */30 * * * *', async () => { //every 10 min
console.log("cron job triggered at", new Date());
await fetchAllProjectDetails();
console.log("sync done");
}).start();
};
//making functions public (marking for export);
exports.API_CONCURRENT_READS = API_CONCURRENT_READS;
exports.fetchKimai = fetchKimai;
exports.postKimai = postKimai;
exports.getSettings = getSettings;
exports.setSettings = setSettings;
exports.getAllUsers = getAllUsers;
exports.DEPRECATED_getAllProjectDetails = DEPRECATED_getAllProjectDetails;
exports.DEPRECATED_getProjectDetailsById = DEPRECATED_getProjectDetailsById;
exports.DEPRECATED_init = DEPRECATED_init;