2024-12-07 16:16:31 +00:00
|
|
|
import {parseRemarks} from './remarks.js';
|
2017-11-11 23:56:09 +01:00
|
|
|
|
2025-01-11 18:39:32 +00:00
|
|
|
const createFakeWalkingLeg = (prevLeg, leg) => {
|
|
|
|
const fakeWalkingLeg = {
|
|
|
|
origin: prevLeg.destination,
|
|
|
|
destination: leg.origin,
|
|
|
|
};
|
|
|
|
fakeWalkingLeg.departure = prevLeg.arrival;
|
|
|
|
fakeWalkingLeg.plannedDeparture = prevLeg.plannedArrival;
|
|
|
|
fakeWalkingLeg.departureDelay = prevLeg.delay;
|
|
|
|
fakeWalkingLeg.arrival = fakeWalkingLeg.departure;
|
|
|
|
fakeWalkingLeg.plannedArrival = fakeWalkingLeg.plannedDeparture;
|
|
|
|
fakeWalkingLeg.arrivalDelay = fakeWalkingLeg.departureDelay;
|
|
|
|
fakeWalkingLeg.public = true;
|
|
|
|
fakeWalkingLeg.walking = true;
|
|
|
|
fakeWalkingLeg.distance = null;
|
|
|
|
return fakeWalkingLeg;
|
2025-01-11 18:41:16 +00:00
|
|
|
};
|
2025-01-11 18:39:32 +00:00
|
|
|
|
2024-12-17 19:41:00 +00:00
|
|
|
const parseLocationsFromCtxRecon = (ctx, j) => {
|
2025-01-03 10:57:24 +00:00
|
|
|
return (j.ctxRecon || j.kontext)
|
2024-12-17 19:41:00 +00:00
|
|
|
.split('$')
|
|
|
|
.map(e => ctx.profile.parseLocation(ctx, {id: e}))
|
|
|
|
.filter(e => e.latitude || e.location?.latitude)
|
|
|
|
.reduce((map, e) => {
|
|
|
|
map[e.id] = e;
|
|
|
|
map[e.name] = e;
|
|
|
|
return map;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
2025-01-20 20:06:29 +00:00
|
|
|
const trimJourneyId = (journeyId) => {
|
2025-01-20 20:32:38 +00:00
|
|
|
if (!journeyId) {
|
|
|
|
return null;
|
|
|
|
}
|
2025-01-20 20:06:29 +00:00
|
|
|
const endOfHafasId = journeyId.lastIndexOf('$');
|
|
|
|
if (endOfHafasId != -1) {
|
2025-01-20 20:32:38 +00:00
|
|
|
return journeyId.substring(0, endOfHafasId + 1);
|
2025-01-20 20:06:29 +00:00
|
|
|
}
|
|
|
|
return journeyId;
|
2025-01-20 20:32:38 +00:00
|
|
|
};
|
2025-01-20 20:06:29 +00:00
|
|
|
|
2025-01-03 10:57:24 +00:00
|
|
|
const parseJourney = (ctx, jj) => { // j = raw journey
|
2024-02-06 22:58:49 +01:00
|
|
|
const {profile, opt} = ctx;
|
2025-01-03 10:57:24 +00:00
|
|
|
const j = jj.verbindung || jj;
|
2024-12-17 19:41:00 +00:00
|
|
|
const fallbackLocations = parseLocationsFromCtxRecon(ctx, j);
|
2024-02-06 22:58:49 +01:00
|
|
|
const legs = [];
|
2024-12-07 16:16:31 +00:00
|
|
|
for (const l of j.verbindungsAbschnitte) {
|
2024-12-17 19:41:00 +00:00
|
|
|
const leg = profile.parseJourneyLeg(ctx, l, null, fallbackLocations);
|
2025-01-11 18:41:16 +00:00
|
|
|
if (legs.length > 0 && !legs[legs.length - 1].walking && !leg.walking) {
|
2025-01-11 18:39:32 +00:00
|
|
|
const fakeWalkingLeg = createFakeWalkingLeg(legs[legs.length - 1], leg);
|
|
|
|
legs.push(fakeWalkingLeg);
|
|
|
|
}
|
2024-02-06 22:58:49 +01:00
|
|
|
legs.push(leg);
|
2023-12-05 15:01:35 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
const res = {
|
|
|
|
type: 'journey',
|
|
|
|
legs,
|
2025-01-20 20:06:29 +00:00
|
|
|
refreshToken: trimJourneyId(j.ctxRecon || j.kontext),
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|
2018-12-02 01:05:19 +01:00
|
|
|
|
2024-12-07 16:16:31 +00:00
|
|
|
// TODO freq
|
2018-06-11 11:29:32 +02:00
|
|
|
|
2024-12-07 16:16:31 +00:00
|
|
|
if (opt.remarks) {
|
|
|
|
res.remarks = parseRemarks(ctx, j);
|
2019-10-20 00:19:11 +02:00
|
|
|
}
|
2018-07-23 20:42:22 +02:00
|
|
|
|
2024-12-07 16:16:31 +00:00
|
|
|
// TODO
|
|
|
|
if (opt.scheduledDays && j.serviceDays) {
|
2022-11-16 15:18:12 +01:00
|
|
|
// todo [breaking]: rename to scheduledDates
|
2024-12-08 21:42:57 +00:00
|
|
|
// res.scheduledDays = profile.parseScheduledDays(ctx, j.serviceDays);
|
2017-11-11 22:35:41 +01:00
|
|
|
}
|
|
|
|
|
2025-01-03 10:57:24 +00:00
|
|
|
res.price = profile.parsePrice(ctx, jj);
|
|
|
|
const tickets = profile.parseTickets(ctx, jj);
|
2024-12-21 15:26:49 +00:00
|
|
|
if (tickets) {
|
|
|
|
res.tickets = tickets;
|
|
|
|
}
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
return res;
|
|
|
|
};
|
2017-11-11 22:35:41 +01:00
|
|
|
|
2022-05-07 16:17:37 +02:00
|
|
|
export {
|
|
|
|
parseJourney,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|