diff --git a/format/travellers.js b/format/travellers.js index 2c572073..921fa455 100644 --- a/format/travellers.js +++ b/format/travellers.js @@ -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; }; diff --git a/p/dbnav/journeys-req.js b/p/dbnav/journeys-req.js index 79e10cfb..70216c28 100644 --- a/p/dbnav/journeys-req.js +++ b/p/dbnav/journeys-req.js @@ -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, }; }), },