2020-06-11 15:33:45 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const minBy = require('lodash/minBy')
|
|
|
|
const maxBy = require('lodash/maxBy')
|
2020-09-16 16:46:22 +02:00
|
|
|
const last = require('lodash/last')
|
2020-06-11 15:33:45 +02:00
|
|
|
|
|
|
|
const parseTrip = (ctx, t) => { // t = raw trip
|
2022-11-16 15:18:12 +01:00
|
|
|
const {profile, opt, res} = ctx
|
2020-06-11 15:33:45 +02:00
|
|
|
|
|
|
|
// pretend the trip is a leg in a journey
|
|
|
|
const fakeLeg = {
|
|
|
|
type: 'JNY',
|
2021-10-24 16:36:29 +02:00
|
|
|
dep: Array.isArray(t.stopL)
|
|
|
|
? minBy(t.stopL, 'idx') || t.stopL[0]
|
|
|
|
: {},
|
|
|
|
arr: Array.isArray(t.stopL)
|
|
|
|
? maxBy(t.stopL, 'idx') || last(t.stopL)
|
|
|
|
: {},
|
2020-06-11 15:33:45 +02:00
|
|
|
jny: t,
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:44:08 +01:00
|
|
|
// todo: this breaks if the trip starts on a different day
|
|
|
|
// how does HAFAS do this?
|
|
|
|
const today = () => profile.formatDate(profile, Date.now())
|
|
|
|
const date = t.date || today()
|
|
|
|
|
|
|
|
const trip = profile.parseJourneyLeg(ctx, fakeLeg, date)
|
2020-06-11 15:33:45 +02:00
|
|
|
trip.id = trip.tripId
|
|
|
|
delete trip.tripId
|
2021-10-26 14:15:17 +02:00
|
|
|
// todo [breaking]: delete trip.reachable
|
2020-06-11 15:33:45 +02:00
|
|
|
|
2022-11-16 15:18:12 +01:00
|
|
|
if (opt.scheduledDays) {
|
|
|
|
const nrOfStopovers = t.stopL.length
|
|
|
|
// trips seem to use sDaysL[], journeys use sDays
|
|
|
|
const sDaysL = Array.isArray(t.sDaysL) ? t.sDaysL : []
|
|
|
|
const matchingSDays = sDaysL.filter((sDays) => {
|
|
|
|
return sDays.fLocIdx === 0 && sDays.tLocIdx === (nrOfStopovers - 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
// if there are >1 sDays, we don't know how to interpret them
|
|
|
|
const sDays = matchingSDays.length === 1 ? matchingSDays[0] : null
|
|
|
|
// todo [breaking]: rename to scheduledDates
|
|
|
|
trip.scheduledDays = profile.parseScheduledDays(ctx, sDays)
|
|
|
|
}
|
|
|
|
|
2021-10-28 00:25:58 +02:00
|
|
|
if (res.planrtTS) {
|
|
|
|
// todo [breaking]: remove here
|
|
|
|
trip.realtimeDataUpdatedAt = parseInt(res.planrtTS)
|
|
|
|
}
|
|
|
|
|
2020-06-11 15:33:45 +02:00
|
|
|
return trip
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = parseTrip
|