-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactuals.js
81 lines (67 loc) · 2.09 KB
/
actuals.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
// actuals
// time_entry
const axios = require('axios');
const fs = require('fs');
async function fetchActualPage(params, apiKey) {
const baseUrl = 'https://app.runn.io/api/v0';
try {
const response = await axios.get(`${baseUrl}/actuals`, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
params: params,
});
return response.data;
} catch (error) {
throw error;
}
}
async function fetchActual(apiKey, options) {
let currentPage = 1;
const params = {};
if (options.per_page)
params.per_page = options.per_page;
if (options.start)
params.start = options.start;
if (options.end)
params.end = options.end;
if (options.page) {
params.page = options.page;
const response = await fetchActualPage(params, apiKey);
return response;
}
const fetchedActual = [];
let dateOfRun = new Date();
const fetchNextPage = async () => {
params.page = currentPage;
try {
const response = await fetchActualPage(params, apiKey);
fetchedActual.push(...response);
if (response.length > 0) {
currentPage++;
if (currentPage % 120 == 0) {
const SecondToComplteMinute = 60000 - (new Date().getTime() / 1000 - dateOfRun.getTime() / 1000);
if (SecondToComplteMinute > 0) {
await new Promise(resolve => setTimeout(resolve, SecondToComplteMinute));
}
dateOfRun = new Date();
}
await fetchNextPage();
}
} catch (error) {
console.error('An error occurred:', error);
}
};
await fetchNextPage();
return fetchedActual;
}
async function actuals(id, options, config) {
try {
const fetchedactual = await fetchActual(config.apikey, options);
return fetchedactual;
} catch (error) {
console.error('An error occurred:', error);
throw error;
}
}
module.exports = actuals;