mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-22 22:59:35 +02:00
support multiple travellers
This commit is contained in:
parent
661024cedb
commit
b431fe65b7
2 changed files with 33 additions and 16 deletions
|
@ -1,25 +1,44 @@
|
|||
const formatTraveller = ({profile}, ageGroup, age, loyaltyCard) => {
|
||||
const tvlrAgeGroup = age
|
||||
? profile.ageGroupFromAge(age)
|
||||
: ageGroup;
|
||||
let r = {
|
||||
typ: profile.ageGroupLabel[tvlrAgeGroup || profile.ageGroup.ADULT],
|
||||
anzahl: 1,
|
||||
alter: age
|
||||
? [String(age)]
|
||||
: [],
|
||||
ermaessigungen: [profile.formatLoyaltyCard(loyaltyCard)],
|
||||
};
|
||||
return r;
|
||||
};
|
||||
|
||||
const validateArr = (field, length) => {
|
||||
return !field || Array.isArray(field) && field.length == length;
|
||||
};
|
||||
|
||||
const formatTravellers = ({profile, opt}) => {
|
||||
if ('age' in opt && 'ageGroup' in opt) {
|
||||
throw new TypeError(`\
|
||||
opt.age and opt.ageGroup are mutually exclusive.
|
||||
Pass in just opt.age, and the age group will calculated automatically.`);
|
||||
}
|
||||
|
||||
const tvlrAgeGroup = 'age' in opt
|
||||
? profile.ageGroupFromAge(opt.age)
|
||||
: opt.ageGroup;
|
||||
let travellers = [];
|
||||
if (Array.isArray(opt.loyaltyCard) || Array.isArray(opt.age) || Array.isArray(opt.ageGroup)) {
|
||||
const len = opt.loyaltyCard?.length || opt.age?.length || opt.ageGroup?.length;
|
||||
if (!validateArr(opt.loyaltyCard, len) || !validateArr(opt.age, len) || !validateArr(opt.ageGroup, len)) {
|
||||
throw new TypeError('If any of loyaltyCard, age or ageGroup are an array, all given must be an array of the same length.');
|
||||
}
|
||||
for (let i = 0; i < len; i++) {
|
||||
travellers.push(formatTraveller({profile}, opt.ageGroup && opt.ageGroup[i], opt.age && opt.age[i], opt.loyaltyCard && opt.loyaltyCard[i]));
|
||||
}
|
||||
} else {
|
||||
travellers.push(formatTraveller({profile}, opt.ageGroup, opt.age, opt.loyaltyCard));
|
||||
}
|
||||
|
||||
const basicCtrfReq = {
|
||||
klasse: opt.firstClass === true ? 'KLASSE_1' : 'KLASSE_2',
|
||||
// todo [breaking]: support multiple travelers
|
||||
reisende: [{
|
||||
typ: profile.ageGroupLabel[tvlrAgeGroup || profile.ageGroup.ADULT],
|
||||
anzahl: 1,
|
||||
alter: 'age' in opt
|
||||
? [String(opt.age)]
|
||||
: [],
|
||||
ermaessigungen: [profile.formatLoyaltyCard(opt.loyaltyCard)],
|
||||
}],
|
||||
reisende: travellers,
|
||||
};
|
||||
return basicCtrfReq;
|
||||
};
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
import {getHeaders} from './header.js';
|
||||
|
||||
const formatBaseJourneysReq = (ctx) => {
|
||||
const {opt} = ctx;
|
||||
|
||||
// TODO opt.accessibility
|
||||
// TODO routingMode
|
||||
const travellers = ctx.profile.formatTravellers(ctx);
|
||||
|
@ -19,7 +17,7 @@ const formatBaseJourneysReq = (ctx) => {
|
|||
t.ermaessigungen[0].art + ' ' + t.ermaessigungen[0].klasse,
|
||||
],
|
||||
reisendenTyp: t.typ,
|
||||
alter: opt.age,
|
||||
alter: t.alter.length && parseInt(t.alter[0]) || undefined,
|
||||
};
|
||||
}),
|
||||
},
|
||||
|
|
Loading…
Add table
Reference in a new issue