Exclude trains with false coordinates

This commit is contained in:
Conor McNamara
2023-02-20 12:45:51 +00:00
parent 60e0661a01
commit f42f58deb9

View File

@ -25,7 +25,6 @@ exports.getLiveTrainData = functions.https.onRequest((request, response) => {
snapshot.forEach(doc => { snapshot.forEach(doc => {
jsonData.push(doc.data()); jsonData.push(doc.data());
}); });
// response.send(jsonData);
response.json({data: jsonData}); response.json({data: jsonData});
}) })
}); });
@ -56,21 +55,25 @@ exports.postLiveTrainData = functions.https.onRequest((request, response) => {
batchDelete.commit().then(function() { batchDelete.commit().then(function() {
// batch write all train JSON objects to the "liveTrainData" collection // batch write all train JSON objects to the "liveTrainData" collection
var batchWrite = db.batch(); var batchWrite = db.batch();
jsonData.forEach((doc) => { jsonData.forEach((doc) => {
// set the train's code as the document ID // ignore trains with longitudes or latitudes equal zero
var docID = db.collection('liveTrainData').doc(doc["TrainCode"][0]); if (!(doc["TrainLongitude"] == 0 || doc["TrainLatitude"] == 0)) {
batchWrite.set(docID, doc); // 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 () { 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) => {; .catch((error) => {;
response.send("Error fetching data from the IrishRail API"); response.send({data: "Error fetching data from the IrishRail API"});
}) })
}); });
}) })