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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { html } from 'lit-html';
import { db } from './dataStorage.js';
import { go } from './router.js';
import { settings, subscribeSettings } from './settings.js';
import { showLoader, hideOverlay, showModal, showAlertModal } from './overlays.js';
import { languages } from './languages.js';
import { isEmptyObject, generateSlug, loyaltyCardToString, loyaltyCardFromString } from './helpers.js';
import { formatDateTime } from './formatters.js';
import { getHafasClient, client } from './hafasClient.js';
import { trainsearchToHafas, hafasToTrainsearch } from './refresh_token/index.js';
import { default as ds100 } from './ds100.json';
let ds100R = {};
const journeySettings = () => {
return {
stopovers: true,
polylines: false,
tickets: true,
language: settings.language,
};
if (settings.profile !== 'db') {
params.accessibility = settings.accessibility;
params.walkingSpeed = settings.walkingSpeed;
} else {
const card = loyaltyCardFromString(settings.loyaltyCard)
params.loyaltyCard = card;
params.ageGroup = settings.ageGroup;
}
return params;
};
export const getFrom = journeys => journeys[0].legs[0].origin;
export const getTo = journeys => journeys[0].legs[journeys[0].legs.length-1].destination;
export const addJourneys = async data => {
if (!data) return false;
const historyEntry = {
profile: data.profile,
fromPoint: getFrom(data.journeys),
viaPoint: data.params.via,
toPoint: getTo(data.journeys),
slug: data.slug
};
const journeyEntries = data.journeys.map(j => {
return {
...j,
settings: data.settings,
slug: data.slug,
};
});
if (typeof data.params.loyaltyCard === 'object') data.params.loyaltyCard = loyaltyCardToString(data.params.loyaltyCard);
const journeysOverviewEntry = {
...data,
journeys: data.journeys.map(j => j.refreshToken),
};
await db.addJourneys(journeyEntries, journeysOverviewEntry, historyEntry);
};
const processJourneys = data => { for (const journey of data.journeys) processJourney(journey) };
const processJourney = journey => { for (const leg of journey.legs) processLeg(leg) };
export const processLeg = leg => {
if (leg.plannedDeparture) leg.plannedDeparture = new Date(leg.plannedDeparture);
if (leg.plannedArrival) leg.plannedArrival = new Date(leg.plannedArrival);
if (leg.plannedWhen) leg.plannedWhen = new Date(leg.plannedWhen);
if (leg.departure) leg.departure = new Date(leg.departure);
if (leg.arrival) leg.arrival = new Date(leg.arrival);
if (leg.when) leg.when = new Date(leg.when);
for (const stopover of (leg.stopovers || [])) {
if (stopover.plannedDeparture) stopover.plannedDeparture = new Date(stopover.plannedDeparture);
if (stopover.plannedArrival) stopover.plannedArrival = new Date(stopover.plannedArrival);
if (stopover.plannedWhen) stopover.plannedWhen = new Date(stopover.plannedWhen);
if (stopover.departure) stopover.departure = new Date(stopover.departure);
if (stopover.arrival) stopover.arrival = new Date(stopover.arrival);
if (stopover.when) stopover.when = new Date(stopover.when);
}
};
export const newJourneys = async (params) => {
const { from, to, ...moreOpts } = params;
let data;
data = await client.journeys(from, to, { ...journeySettings(), ...moreOpts });
for (const journey of data.journeys) {
journey.refreshToken = hafasToTrainsearch(journey.refreshToken);
}
data.slug = generateSlug();
data.indexOffset = 0;
data.params = params;
data.settings = journeySettings();
data.profile = settings.profile;
await addJourneys(data);
processJourneys(data);
return data;
};
export const getJourneys = async slug => {
let data = await db.getJourneysOverview(slug);
if (!data) return null;
return {
...data,
journeys: await Promise.all(data.journeys.map(x => getJourney(x, data.profile))),
};
};
export const getMoreJourneys = async (slug, mode) => {
const saved = await db.getJourneysOverview(slug);
const params = { ...saved.params, ...journeySettings() };
if (typeof params.loyaltyCard === 'string') params.loyaltyCard = loyaltyCardFromString(params.loyaltyCard);
params[mode+'Than'] = saved[mode+'Ref'];
let { departure, arrival, from, to, ...moreOpt } = params;
const [newData, ...existingJourneys] = await Promise.all(
[ client.journeys(from, to, moreOpt) ]
.concat(saved.journeys.map(x => getJourney(x, saved.profile)))
);
const res = {
...saved,
...newData,
};
for (const journey of newData.journeys) journey.refreshToken = hafasToTrainsearch(journey.refreshToken);
if (mode === 'earlier') {
res.journeys = newData.journeys.concat(existingJourneys);
res.indexOffset += newData.journeys.length;
// keep old laterRef
res.laterRef = saved.laterRef;
} else {
res.journeys = existingJourneys.concat(newData.journeys);
// keep old earlierRef
res.earlierRef = saved.earlierRef;
}
await addJourneys(res);
};
export const refreshJourneys = async (slug) => {
const saved = await db.getJourneysOverview(slug);
await Promise.all(saved.journeys.map(x => refreshJourney(x, saved.profile || "db")));
};
export const getJourney = async (refreshToken, profile) => {
let journeyObject = await db.getJourney(refreshToken);
if (!journeyObject || JSON.stringify(journeyObject.settings) != JSON.stringify(journeySettings()))
journeyObject = await refreshJourney(refreshToken, profile);
processJourney(journeyObject);
return journeyObject;
};
export const refreshJourney = async (refreshToken, profile) => {
const client = await getHafasClient(profile || settings.profile || "db");
const [saved, data] = await Promise.all([
db.getJourney(refreshToken),
client.refreshJourney(trainsearchToHafas(refreshToken), journeySettings())
]);
const {journey} = data;
journey.settings = journeySettings();
journey.refreshToken = hafasToTrainsearch(journey.refreshToken);
if (saved) journey.slug = saved.slug;
db.updateJourney(journey);
return journey;
};
export const ds100Name = (id) => {
if (!settings.showDS100) return null;
if (!ds100[Number(id)]) return null;
return ds100[Number(id)];
};
export const ds100Reverse = (name) => {
if (!settings.showDS100) return null;
if (isEmptyObject(ds100R)) {
for (let [id, names] of Object.entries(ds100)) {
for (let name of names.split(", ")) ds100R[name] = id;
}
}
if (!ds100R[name]) return null;
return ds100R[name];
};