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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { db } from './dataStorage.js';
import { settingsState } from './settings.js';
import { getHafasClient, client } from './hafasClient.js';
import { trainsearchToHafas, hafasToTrainsearch } from './refresh_token/index.js';
import { CustomDate } from './helpers.js';
import { t } from './translate.js';
const loyaltyCards = {
NONE: Symbol('no loyalty card'),
BAHNCARD: Symbol('Bahncard'),
VORTEILSCARD: Symbol('VorteilsCard'),
HALBTAXABO: Symbol('HalbtaxAbo'),
VOORDEELURENABO: Symbol('Voordeelurenabo'),
SHCARD: Symbol('SH-Card'),
GENERALABONNEMENT: Symbol('General-Abonnement'),
};
const loyaltyCardsReverse = {
'Symbol(no loyalty card)': 'NONE',
'Symbol(Bahncard)': 'BAHNCARD',
'Symbol(VorteilsCard)': 'VORTEILSCARD',
'Symbol(HalbtaxAbo)': 'HALBTAXABO',
'Symbol(Voordeelurenabo)': 'VOORDEELURENABO',
'Symbol(SH-Card)': 'SHCARD',
'Symbol(General-Abonnement)': 'GENERALABONNEMENT',
};
export const loyaltyCardToString = loyaltyCard =>
loyaltyCardsReverse[loyaltyCard.type.toString()] !== 'NONE' ?
`${loyaltyCardsReverse[loyaltyCard.type.toString()]}-${loyaltyCard.discount}-${loyaltyCard.class}`
: 'NONE';
export const loyaltyCardFromString = string => {
const splitedString = string.split('-');
if (splitedString[0] === 'NONE') return { type: loyaltyCards[splitedString[0]] };
return { type: loyaltyCards[splitedString[0]], discount: splitedString[1], class: splitedString[2] };
};
export const generateSlug = () => {
const len = 8;
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < len; i++)
result += characters.charAt(Math.floor(Math.random() * characters.length));
return result;
};
const journeySettings = () => {
let params = {
stopovers: true,
polylines: false,
tickets: true,
language: t('backendLang'),
};
if (settingsState.profile === 'db') {
params.loyaltyCard = settingsState.loyaltyCard;
params.ageGroup = settingsState.ageGroup;
}
return params;
};
const processJourneys = journeys => journeys.forEach(journey => processJourney(journey));
const processJourney = journey => journey.legs.forEach(leg => processLeg(leg));
export const processLeg = leg => {
const elements = [ 'plannedDeparture', 'plannedArrival', 'plannedWhen', 'departure', 'arrival', 'when' ];
elements.forEach(element => {
if (leg[element]) leg[element] = new CustomDate(leg[element]);
});
if (leg.stopovers) leg.stopovers.forEach(stopover => {
elements.forEach(element => {
if (stopover[element]) stopover[element] = new CustomDate(stopover[element]);
});
});
};
export const getFromPoint = journeys => journeys[0].legs[0].origin;
export const getToPoint = journeys => journeys[0].legs[journeys[0].legs.length-1].destination;
export const newJourneys = async params => {
const { from, to, ...moreOpts } = params;
let data;
const journeySettingsObj = journeySettings();
if (typeof journeySettingsObj.loyaltyCard === 'string') journeySettingsObj.loyaltyCard = loyaltyCardFromString(journeySettingsObj.loyaltyCard);
data = await client.journeys(from, to, { ...moreOpts, ...journeySettingsObj });
data.journeys.forEach((journey, index, journeys) => {
journeys[index].refreshToken = hafasToTrainsearch(journey.refreshToken);
});
data.slug = generateSlug();
data.indexOffset = 0;
data.params = params;
data.settings = journeySettingsObj;
data.profile = settingsState.profile;
await addJourneys(data);
processJourneys(data.journeys);
return data;
};
export const addJourneys = async data => {
if (!data) return false;
if (typeof data.settings.loyaltyCard === 'object') data.settings.loyaltyCard = loyaltyCardToString(data.settings.loyaltyCard);
const journeyEntries = data.journeys.map(j => ({
...j,
settings: data.settings,
slug: data.slug,
}));
const journeysOverviewEntry = {
...data,
journeys: data.journeys.map(j => j.refreshToken),
};
const historyEntry = {
profile: data.profile,
fromPoint: getFromPoint(data.journeys),
viaPoint: data.params.via,
toPoint: getToPoint(data.journeys),
slug: data.slug
};
await db.addJourneys(journeyEntries, journeysOverviewEntry, historyEntry);
};
export const getJourneys = async slug => {
let data = await db.getJourneysOverview(slug);
if (!data) return null;
return {
...data,
journeys: await Promise.all(data.journeys.map(refreshToken => getJourney(data.profile, refreshToken))),
};
};
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(refreshToken => getJourney(saved.profile, refreshToken)))
);
const res = {
...saved,
...newData,
};
newData.journeys.forEach(journey => {
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(refreshToken => refreshJourney(saved.profile, refreshToken)));
};
export const getJourney = async (profile, refreshToken) => {
let journeyObject = await db.getJourney(refreshToken);
if (!journeyObject || JSON.stringify(journeyObject.settings) != JSON.stringify(journeySettings())) journeyObject = await refreshJourney(profile, refreshToken);
processJourney(journeyObject);
return journeyObject;
};
export const refreshJourney = async (profile, refreshToken) => {
const client = await getHafasClient(profile);
const params = journeySettings();
if (params.loyaltyCard) params.loyaltyCard = loyaltyCardFromString(params.loyaltyCard);
const [ saved, data ] = await Promise.all([
db.getJourney(refreshToken),
client.refreshJourney(trainsearchToHafas(refreshToken), params)
]);
const journeyObject = data.journey;
journeyObject.refreshToken = hafasToTrainsearch(journeyObject.refreshToken);
journeyObject.settings = journeySettings();
if (saved) journeyObject.slug = saved.slug;
db.updateJourney(journeyObject);
processJourney(journeyObject);
return journeyObject;
};