forked from r-spacex/SpaceX-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlandpads.js
88 lines (81 loc) · 2.09 KB
/
landpads.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
const got = require('got');
const { logger } = require('../middleware/logger');
const API = process.env.SPACEX_API;
const KEY = process.env.SPACEX_KEY;
const HEALTHCHECK = process.env.LANDPADS_HEALTHCHECK;
/**
* Update landpad attempts/successes
* @return {Promise<void>}
*/
module.exports = async () => {
try {
const landpads = await got.post(`${API}/landpads/query`, {
json: {
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const updates = landpads.docs.map(async (landpad) => {
const [attempts, successes] = await Promise.all([
got.post(`${API}/launches/query`, {
json: {
query: {
cores: {
$elemMatch: {
landpad: landpad.id,
landing_attempt: true,
},
},
upcoming: false,
success: true,
},
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
}),
got.post(`${API}/launches/query`, {
json: {
query: {
cores: {
$elemMatch: {
landpad: landpad.id,
landing_attempt: true,
landing_success: true,
},
},
upcoming: false,
success: true,
},
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
}),
]);
await got.patch(`${API}/landpads/${landpad.id}`, {
json: {
landing_attempts: attempts.totalDocs,
landing_successes: successes.totalDocs,
},
headers: {
'spacex-key': KEY,
},
});
});
await Promise.all(updates);
logger.info('Landpads updated');
if (HEALTHCHECK) {
await got(HEALTHCHECK);
}
} catch (error) {
console.log(`Landpads Error: ${error.message}`);
}
};