Skip to content

Commit

Permalink
add launches arrays to ships and payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewmeyer committed May 3, 2020
1 parent a302a37 commit d7a7789
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 0 deletions.
269 changes: 269 additions & 0 deletions jobs/launches.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@

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}/crew/${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}/crew/${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 launchIds = launches.docs.filter((launch) => {
if (launch.payloads.includes(payload.id)) {
return true;
}
return false;
}).map((launch) => launch.id);

await got.patch(`${SPACEX_API}/payloads/${payload.id}`, {
json: {
launches: launchIds,
},
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);
}
};
3 changes: 3 additions & 0 deletions jobs/worker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

const { CronJob } = require('cron');
const webcast = require('./webcast');
const launches = require('./launches');

const webcastJob = new CronJob('*/10 * * * *', webcast);
const launchesJob = new CronJob('*/10 * * * *', launches);

webcastJob.start();
launchesJob.start();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"test": "eslint . && jest --silent --verbose",
"start": "node server.js",
"worker": "node jobs/worker.js",
"lint": "eslint ."
},
"repository": {
Expand Down
3 changes: 3 additions & 0 deletions services/v4/launches/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ const launchSchema = new mongoose.Schema({
ships: [
mongoose.ObjectId,
],
capsules: [
mongoose.ObjectId,
],
payloads: [
mongoose.ObjectId,
],
Expand Down
4 changes: 4 additions & 0 deletions services/v4/payloads/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ const payloadSchema = new mongoose.Schema({
default: null,
},
},
launches: [{
type: mongoose.ObjectId,
ref: 'Launch',
}],
});

const index = {
Expand Down
4 changes: 4 additions & 0 deletions services/v4/ships/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ const shipSchema = new mongoose.Schema({
type: String,
default: null,
},
launches: [{
type: mongoose.ObjectId,
ref: 'Launch',
}],
});

shipSchema.plugin(mongoosePaginate);
Expand Down

0 comments on commit d7a7789

Please sign in to comment.