Add stations cloud functions and tests

This commit is contained in:
Conor McNamara
2023-02-28 12:23:05 +00:00
parent 4c1a24d865
commit 2633ad564e
8 changed files with 164 additions and 78 deletions

3
.gitignore vendored
View File

@ -64,4 +64,5 @@ node_modules/
# dotenv environment variables file # dotenv environment variables file
.env .env
./src/api/firebase.js ./src/api/firebase.js
firebase.js

File diff suppressed because one or more lines are too long

5
dist/assets/index-2cd109ed.css vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

1
dist/assets/train-solid-e7249eb7.svg vendored Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Pro 6.3.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M96 0C43 0 0 43 0 96V352c0 48 35.2 87.7 81.1 94.9l-46 46C28.1 499.9 33.1 512 43 512H82.7c8.5 0 16.6-3.4 22.6-9.4L160 448H288l54.6 54.6c6 6 14.1 9.4 22.6 9.4H405c10 0 15-12.1 7.9-19.1l-46-46c46-7.1 81.1-46.9 81.1-94.9V96c0-53-43-96-96-96H96zM64 96c0-17.7 14.3-32 32-32H352c17.7 0 32 14.3 32 32v96c0 17.7-14.3 32-32 32H96c-17.7 0-32-14.3-32-32V96zM224 288a48 48 0 1 1 0 96 48 48 0 1 1 0-96z"/></svg>

After

Width:  |  Height:  |  Size: 636 B

4
dist/index.html vendored
View File

@ -5,8 +5,8 @@
<link rel="icon" href="/favicon.ico"> <link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Irish Rail Tracker</title> <title>Irish Rail Tracker</title>
<script type="module" crossorigin src="/assets/index-f45376ca.js"></script> <script type="module" crossorigin src="/assets/index-e189e6d8.js"></script>
<link rel="stylesheet" href="/assets/index-24f118f5.css"> <link rel="stylesheet" href="/assets/index-2cd109ed.css">
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>

View File

@ -18,7 +18,7 @@ exports.getLiveTrainData = functions.https.onRequest((request, response) => {
// fetch the "liveTrainData" collection // fetch the "liveTrainData" collection
admin.firestore().collection('liveTrainData').get().then((snapshot) => { admin.firestore().collection('liveTrainData').get().then((snapshot) => {
if (snapshot.empty) { if (snapshot.empty) {
response.send({data: "Error fetching data from the database"}); response.send({data: "Error fetching live train data from the database"});
return; return;
} }
// iterate through each of the collection's documents // iterate through each of the collection's documents
@ -30,6 +30,70 @@ exports.getLiveTrainData = functions.https.onRequest((request, response) => {
}); });
}) })
// function to fetch station data from the Firestore database
exports.getStationData = functions.https.onRequest((request, response) => {
response.set('Access-Control-Allow-Origin', '*');
response.set('Access-Control-Allow-Credentials', 'true');
let jsonData = [];
cors(request, response, () => {
// fetch the "stations" collection
admin.firestore().collection('stations').get().then((snapshot) => {
if (snapshot.empty) {
response.send({data: "Error fetching station data from the database"})
return;
}
// iterate through each of the collection's documents
snapshot.forEach(doc => {
jsonData.push(doc.data());
});
response.json({data: jsonData});
})
});
})
// function to populate the Firestore database with station data from Irish Rail
exports.postStationData = functions.https.onRequest((request, response) => {
response.set('Access-Control-Allow-Origin', '*');
response.set('Access-Control-Allow-Credentials', 'true');
cors(request, response, () => {
axios.get('http://api.irishrail.ie/realtime/realtime.asmx/getAllStationsXML')
.then((res) => {
// XML to JSON
parseString(res.data, function(err, result) {
let jsonStr = JSON.stringify(result);
let jsonObj = JSON.parse(jsonStr);
let jsonData = jsonObj.ArrayOfObjStation.objStation;
// batch delete all of the "stations" collection's documents
var db = admin.firestore();
admin.firestore().collection('stations').get().then((snapshot) => {
var batchDelete = db.batch();
snapshot.forEach(doc => {
batchDelete.delete(doc.ref);
});
batchDelete.commit().then(function() {
// batch write all station JSON objects to the "stations" collection
var batchWrite = db.batch();
jsonData.forEach((doc) => {
// set the station's ID as the document ID
var docID = db.collection('stations').doc(doc["StationCode"][0]);
batchWrite.set(docID, doc);
});
batchWrite.commit().then(function () {
response.send({data: "Successfully fetched and uploaded station data from Irish Rail"});
});
})
})
})
})
})
})
// function to populate the Firestore database with live train data from Irish Rail // function to populate the Firestore database with live train data from Irish Rail
exports.postLiveTrainData = functions.https.onRequest((request, response) => { exports.postLiveTrainData = functions.https.onRequest((request, response) => {
response.set('Access-Control-Allow-Origin', '*'); response.set('Access-Control-Allow-Origin', '*');
@ -66,7 +130,7 @@ exports.postLiveTrainData = functions.https.onRequest((request, response) => {
}); });
batchWrite.commit().then(function () { batchWrite.commit().then(function () {
response.send({data: "Successfully fetched and uploaded data from Irish Rail"}); response.send({data: "Successfully fetched and uploaded live train data from Irish Rail"});
}); });
}) })
}) })

View File

@ -25,5 +25,25 @@ describe('Firebase cloud function tests', function() {
const result = await chai.request('https://us-central1-irishrailtracker.cloudfunctions.net') const result = await chai.request('https://us-central1-irishrailtracker.cloudfunctions.net')
.get('/postLiveTrainData') .get('/postLiveTrainData')
expect(result.statusCode).to.equal(200); expect(result.statusCode).to.equal(200);
}),
this.timeout(100000);
it('test getting station data', async() => {
const result = await chai.request('https://us-central1-irishrailtracker.cloudfunctions.net')
.get('/getStationData')
expect(result.statusCode).to.equal(200);
expect(result.body.data).to.be.an('Array');
expect(result.body.data[0]).haveOwnProperty('StationDesc');
expect(result.body.data[0]).haveOwnProperty('StationLatitude');
expect(result.body.data[0]).haveOwnProperty('StationLongitude');
expect(result.body.data[0]).haveOwnProperty('StationCode');
expect(result.body.data[0]).haveOwnProperty('StationId');
})
this.timeout(100000);
it('Test updating the database with live station data', async() => {
const result = await chai.request('https://us-central1-irishrailtracker.cloudfunctions.net')
.get('/postStationData')
expect(result.statusCode).to.equal(200);
}) })
}) })