-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeople.js
122 lines (100 loc) · 3.16 KB
/
people.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
const axios = require('axios');
const fs = require('fs');
async function fetchPeoplePage(params, apiKey) {
const baseUrl = 'https://app.runn.io/api/v0';
try {
const response = await axios.get(`${baseUrl}/people`, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
params: params,
});
return response.data;
} catch (error) {
throw error;
}
}
async function fetchPeople(apiKey, options) {
let currentPage = 1;
const params = {};
if (options.per_page) {
params.per_page = options.per_page;
}
if (options.include_placeholders) {
params.include_placeholders = options.include_placeholders;
}
if (options.include_projects) {
params.include_projects = options.include_projects;
}
const fetchedPeople = [];
let dateOfRun = new Date();
const fetchNextPage = async () => {
params.page = currentPage;
try {
// console.log("currentPage",currentPage);
const response = await fetchPeoplePage(params, apiKey);
fetchedPeople.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 fetchedPeople;
}
async function fetchpeopleById(apiKey, options, id) {
const params = {};
if (options.include_actuals) {
params.include_actuals = options.include_actuals;
}
if (options.include_assignments) {
params.include_assignments = options.include_assignments;
}
if (options.start) {
params.start = options.start;
}
if (options.end) {
params.end = options.end;
}
const baseUrl = `https://app.runn.io/api/v0/`;
try {
const response = await axios.get(`${baseUrl}/people/${id}`, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
params: params,
});
return response.data;
} catch (error) {
throw error;
}
}
async function people(id, options, config) {
try {
const fetchedPeople = [];
if (id.length > 0) {
for (let i = 0; i < id.length; i++) {
const fetchedProject = await fetchpeopleById(config.apikey, options, id[i]);
fetchedPeople.push(fetchedProject);
}
} else {
const people = await fetchPeople(config.apikey, options);
fetchedPeople.push(...people);
}
return fetchedPeople;
} catch (error) {
console.error('An error occurred:', error);
throw error;
}
}
module.exports = people;