From d7a77896adb2f06ce531dc350b8077177d754fc2 Mon Sep 17 00:00:00 2001 From: Jake Meyer Date: Sun, 3 May 2020 18:34:10 -0500 Subject: [PATCH] add launches arrays to ships and payloads --- jobs/launches.js | 269 ++++++++++++++++++++++++++++++++++ jobs/worker.js | 3 + package.json | 1 + services/v4/launches/model.js | 3 + services/v4/payloads/model.js | 4 + services/v4/ships/model.js | 4 + 6 files changed, 284 insertions(+) create mode 100644 jobs/launches.js diff --git a/jobs/launches.js b/jobs/launches.js new file mode 100644 index 00000000..f5487687 --- /dev/null +++ b/jobs/launches.js @@ -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} + */ +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); + } +}; diff --git a/jobs/worker.js b/jobs/worker.js index 02631a3c..1f11f669 100644 --- a/jobs/worker.js +++ b/jobs/worker.js @@ -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(); diff --git a/package.json b/package.json index 8d41e91d..255da7f0 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "scripts": { "test": "eslint . && jest --silent --verbose", "start": "node server.js", + "worker": "node jobs/worker.js", "lint": "eslint ." }, "repository": { diff --git a/services/v4/launches/model.js b/services/v4/launches/model.js index f227fae0..a0094e3b 100644 --- a/services/v4/launches/model.js +++ b/services/v4/launches/model.js @@ -91,6 +91,9 @@ const launchSchema = new mongoose.Schema({ ships: [ mongoose.ObjectId, ], + capsules: [ + mongoose.ObjectId, + ], payloads: [ mongoose.ObjectId, ], diff --git a/services/v4/payloads/model.js b/services/v4/payloads/model.js index bc7e903d..b92542ee 100644 --- a/services/v4/payloads/model.js +++ b/services/v4/payloads/model.js @@ -119,6 +119,10 @@ const payloadSchema = new mongoose.Schema({ default: null, }, }, + launches: [{ + type: mongoose.ObjectId, + ref: 'Launch', + }], }); const index = { diff --git a/services/v4/ships/model.js b/services/v4/ships/model.js index dd79384c..9c267e94 100644 --- a/services/v4/ships/model.js +++ b/services/v4/ships/model.js @@ -88,6 +88,10 @@ const shipSchema = new mongoose.Schema({ type: String, default: null, }, + launches: [{ + type: mongoose.ObjectId, + ref: 'Launch', + }], }); shipSchema.plugin(mongoosePaginate);