forked from r-spacex/SpaceX-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunches.js
266 lines (233 loc) · 5.83 KB
/
launches.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const _ = require('lodash');
const got = require('got');
const { logger } = require('../middleware/logger');
const SPACEX_API = 'https://stage.spacexdata.com/v4';
const KEY = process.env.SPACEX_KEY;
const HEALTHCHECK = process.env.LAUNCHES_HEALTHCHECK;
/**
* Update launch arrays
* @return {Promise<void>}
*/
module.exports = async () => {
const launches = await got.post(`${SPACEX_API}/launches/query`, {
json: {
query: {
upcoming: false,
},
options: {
sort: {
flight_number: 'asc',
},
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const results = {
capsule: false,
core: false,
crew: false,
landpad: false,
launchpad: false,
payload: false,
ship: false,
};
// Update capsule launches
const capsules = await got.post(`${SPACEX_API}/capsules/query`, {
json: {
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const capsuleLaunches = capsules.docs.map(async (capsule) => {
const launchIds = launches.docs.filter((launch) => {
if (launch.capsules.includes(capsule.id)) {
return true;
}
return false;
}).map((launch) => launch.id);
await got.patch(`${SPACEX_API}/capsules/${capsule.id}`, {
json: {
launches: launchIds,
},
headers: {
'spacex-key': KEY,
},
});
results.capsule = true;
});
await Promise.all(capsuleLaunches);
// Update core launches
const cores = await got.post(`${SPACEX_API}/cores/query`, {
json: {
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const coreLaunches = cores.docs.map(async (core) => {
const launchIds = launches.docs.filter((launch) => {
if (launch.cores.filter((c) => c.core === core.id).length > 0) {
return true;
}
return false;
}).map((launch) => launch.id);
await got.patch(`${SPACEX_API}/cores/${core.id}`, {
json: {
launches: launchIds,
},
headers: {
'spacex-key': KEY,
},
});
results.core = true;
});
await Promise.all(coreLaunches);
// Update crew launches
const crewMembers = await got.post(`${SPACEX_API}/crew/query`, {
json: {
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const crewLaunches = crewMembers.docs.map(async (crew) => {
const launchIds = launches.docs.filter((launch) => {
if (launch.crew.includes(crew.id)) {
return true;
}
return false;
}).map((launch) => launch.id);
await got.patch(`${SPACEX_API}/crew/${crew.id}`, {
json: {
launches: launchIds,
},
headers: {
'spacex-key': KEY,
},
});
results.crew = true;
});
await Promise.all(crewLaunches);
// Update landpad launches
const landpads = await got.post(`${SPACEX_API}/landpads/query`, {
json: {
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const landpadLaunches = landpads.docs.map(async (landpad) => {
const launchIds = launches.docs.filter((launch) => {
if (launch.cores.filter((c) => c.landpad === landpad.id).length > 0) {
return true;
}
return false;
}).map((launch) => launch.id);
await got.patch(`${SPACEX_API}/landpads/${landpad.id}`, {
json: {
launches: launchIds,
},
headers: {
'spacex-key': KEY,
},
});
results.landpad = true;
});
await Promise.all(landpadLaunches);
// Update launchpad launches
const launchpads = await got.post(`${SPACEX_API}/launchpads/query`, {
json: {
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const launchpadLaunches = launchpads.docs.map(async (launchpad) => {
const launchIds = launches.docs.filter((launch) => {
if (launch.launchpad === launchpad.id) {
return true;
}
return false;
}).map((launch) => launch.id);
await got.patch(`${SPACEX_API}/launchpads/${launchpad.id}`, {
json: {
launches: launchIds,
},
headers: {
'spacex-key': KEY,
},
});
results.launchpad = true;
});
await Promise.all(launchpadLaunches);
// Update payload launches
const payloads = await got.post(`${SPACEX_API}/payloads/query`, {
json: {
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const payloadLaunches = payloads.docs.map(async (payload) => {
const launchId = _.find(launches.docs, (launch) => launch.payloads.includes(payload.id));
if (launchId && launchId.id) {
await got.patch(`${SPACEX_API}/payloads/${payload.id}`, {
json: {
launch: launchId.id,
},
headers: {
'spacex-key': KEY,
},
});
results.payload = true;
}
});
await Promise.all(payloadLaunches);
// Update ship launches
const ships = await got.post(`${SPACEX_API}/ships/query`, {
json: {
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const shipLaunches = ships.docs.map(async (ship) => {
const launchIds = launches.docs.filter((launch) => {
if (launch.ships.includes(ship.id)) {
return true;
}
return false;
}).map((launch) => launch.id);
await got.patch(`${SPACEX_API}/ships/${ship.id}`, {
json: {
launches: launchIds,
},
headers: {
'spacex-key': KEY,
},
});
results.ship = true;
});
await Promise.all(shipLaunches);
logger.info(results);
if (HEALTHCHECK) {
await got(HEALTHCHECK);
}
};