From f42f58deb94990d35a7981713e826ab77c1eb68d Mon Sep 17 00:00:00 2001 From: Conor McNamara Date: Mon, 20 Feb 2023 12:45:51 +0000 Subject: [PATCH] Exclude trains with false coordinates --- functions/index.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/functions/index.js b/functions/index.js index bfa8779..f8a1149 100644 --- a/functions/index.js +++ b/functions/index.js @@ -25,7 +25,6 @@ exports.getLiveTrainData = functions.https.onRequest((request, response) => { snapshot.forEach(doc => { jsonData.push(doc.data()); }); - // response.send(jsonData); response.json({data: jsonData}); }) }); @@ -56,21 +55,25 @@ exports.postLiveTrainData = functions.https.onRequest((request, response) => { batchDelete.commit().then(function() { // batch write all train JSON objects to the "liveTrainData" collection var batchWrite = db.batch(); + jsonData.forEach((doc) => { - // set the train's code as the document ID - var docID = db.collection('liveTrainData').doc(doc["TrainCode"][0]); - batchWrite.set(docID, doc); + // ignore trains with longitudes or latitudes equal zero + if (!(doc["TrainLongitude"] == 0 || doc["TrainLatitude"] == 0)) { + // set the train's code as the document ID + var docID = db.collection('liveTrainData').doc(doc["TrainCode"][0]); + batchWrite.set(docID, doc); + } }); batchWrite.commit().then(function () { - response.send("Successfully fetched and uploaded data from Irish Rail"); + response.send({data: "Successfully fetched and uploaded data from Irish Rail"}); }); }) }) }) }) .catch((error) => {; - response.send("Error fetching data from the IrishRail API"); + response.send({data: "Error fetching data from the IrishRail API"}); }) }); }) \ No newline at end of file