-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepartureInfoController.js
70 lines (66 loc) · 2.27 KB
/
departureInfoController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const rtt = require('./index');
async function getOriginStationInfo(locations, deptStation, response) {
const locInfoArr = await locations.filter(element => element.crs === deptStation);
const locInfo = locInfoArr[0];
const resp = response;
if (locInfo.displayAs === 'ORIGIN') {
// future work - check if there's existing train coming into same platform here!
resp.arrivalTime = 'n/a - Origin Station';
} else {
resp.arrivalTime = {
timetableArrivalTime: locInfo.gbttBookedArrival,
actualArrivalTime: locInfo.realtimeArrival,
};
}
resp.departTime = {
timetableDepartureTime: locInfo.gbttBookedDeparture,
actualDepartureTime: locInfo.realtimeDeparture,
};
resp.platform = {
platformNumber: locInfo.platform,
'platformConfirmed?': locInfo.platformConfirmed,
};
return resp;
}
async function departureInfoFromService(serviceInfo, service, deptStation, destStation) {
const { locations } = serviceInfo;
let resp = {
requestType: 'when is my next train',
serviceID: service.serviceUid,
yourRequestInfo: {
departureStation: deptStation,
destinationStation: destStation,
},
dateOfTrain: serviceInfo.runDate,
trainRouteInfo: {
originStation: serviceInfo.origin[0].description,
finalStation: serviceInfo.destination[0].description,
},
};
resp = await getOriginStationInfo(locations, deptStation, resp);
return resp;
}
exports.getDepartureInfo = (req, res, next) => {
const deptStation = req.params.dept;
const destStation = req.params.dest;
rtt.routeInfo.getRouteNextService(deptStation, destStation, (service) => {
rtt.serviceInfo.getServiceAll(service, async (serviceInfo) => {
// res.json(serviceInfo);
const resp = await departureInfoFromService(serviceInfo, service, deptStation, destStation);
res.status(202);
res.json(resp);
});
});
};
exports.getDepartureInfoFromService = (req, res, next) => {
const service = {
serviceUid: req.params.serviceID,
};
const deptStation = req.params.dept;
const destStation = req.params.dest;
rtt.serviceInfo.getServiceAll(service, async (serviceInfo) => {
const resp = await departureInfoFromService(serviceInfo, service, deptStation, destStation);
res.status(200);
res.json(resp);
});
};