mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09: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}) => {
|
const formatTravellers = ({profile, opt}) => {
|
||||||
if ('age' in opt && 'ageGroup' in opt) {
|
if ('age' in opt && 'ageGroup' in opt) {
|
||||||
throw new TypeError(`\
|
throw new TypeError(`\
|
||||||
opt.age and opt.ageGroup are mutually exclusive.
|
opt.age and opt.ageGroup are mutually exclusive.
|
||||||
Pass in just opt.age, and the age group will calculated automatically.`);
|
Pass in just opt.age, and the age group will calculated automatically.`);
|
||||||
}
|
}
|
||||||
|
let travellers = [];
|
||||||
const tvlrAgeGroup = 'age' in opt
|
if (Array.isArray(opt.loyaltyCard) || Array.isArray(opt.age) || Array.isArray(opt.ageGroup)) {
|
||||||
? profile.ageGroupFromAge(opt.age)
|
const len = opt.loyaltyCard?.length || opt.age?.length || opt.ageGroup?.length;
|
||||||
: opt.ageGroup;
|
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 = {
|
const basicCtrfReq = {
|
||||||
klasse: opt.firstClass === true ? 'KLASSE_1' : 'KLASSE_2',
|
klasse: opt.firstClass === true ? 'KLASSE_1' : 'KLASSE_2',
|
||||||
// todo [breaking]: support multiple travelers
|
reisende: travellers,
|
||||||
reisende: [{
|
|
||||||
typ: profile.ageGroupLabel[tvlrAgeGroup || profile.ageGroup.ADULT],
|
|
||||||
anzahl: 1,
|
|
||||||
alter: 'age' in opt
|
|
||||||
? [String(opt.age)]
|
|
||||||
: [],
|
|
||||||
ermaessigungen: [profile.formatLoyaltyCard(opt.loyaltyCard)],
|
|
||||||
}],
|
|
||||||
};
|
};
|
||||||
return basicCtrfReq;
|
return basicCtrfReq;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
import {getHeaders} from './header.js';
|
import {getHeaders} from './header.js';
|
||||||
|
|
||||||
const formatBaseJourneysReq = (ctx) => {
|
const formatBaseJourneysReq = (ctx) => {
|
||||||
const {opt} = ctx;
|
|
||||||
|
|
||||||
// TODO opt.accessibility
|
// TODO opt.accessibility
|
||||||
// TODO routingMode
|
// TODO routingMode
|
||||||
const travellers = ctx.profile.formatTravellers(ctx);
|
const travellers = ctx.profile.formatTravellers(ctx);
|
||||||
|
@ -19,7 +17,7 @@ const formatBaseJourneysReq = (ctx) => {
|
||||||
t.ermaessigungen[0].art + ' ' + t.ermaessigungen[0].klasse,
|
t.ermaessigungen[0].art + ' ' + t.ermaessigungen[0].klasse,
|
||||||
],
|
],
|
||||||
reisendenTyp: t.typ,
|
reisendenTyp: t.typ,
|
||||||
alter: opt.age,
|
alter: t.alter.length && parseInt(t.alter[0]) || undefined,
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue