diff --git a/index.js b/index.js index 188b77c7..9f062063 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ import isObj from 'lodash/isObject.js'; import sortBy from 'lodash/sortBy.js'; import omit from 'lodash/omit.js'; +import {DateTime} from 'luxon'; import {defaultProfile} from './lib/default-profile.js'; import {validateProfile} from './lib/validate-profile.js'; @@ -276,6 +277,7 @@ const createClient = (profile, userAgent, opt = {}) => { opt = Object.assign({ via: null, // let journeys pass this station? transfers: -1, // maximum nr of transfers + transferTime: 0, // minimum time for a single transfer in minutes bike: false, // only bike-friendly journeys tickets: false, // return tickets? polylines: false, // return leg shapes? @@ -295,7 +297,13 @@ const createClient = (profile, userAgent, opt = {}) => { throw new TypeError('opt.departure is invalid'); } const now = new Date(); - const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()); + let today = DateTime.fromObject({ + year: now.year, month: now.month, day: now.day, + hour: 0, minute: 0, second: 0, millisecond: 0, + }, { + zone: profile.timezone, + locale: profile.locale, + }); if (today > when) { throw new TypeError('opt.departure date older than current date.'); } @@ -304,14 +312,6 @@ const createClient = (profile, userAgent, opt = {}) => { const filters = [ profile.formatProductsFilter({profile}, opt.products || {}), ]; - if ( - opt.accessibility - && profile.filters - && profile.filters.accessibility - && profile.filters.accessibility[opt.accessibility] - ) { - filters.push(profile.filters.accessibility[opt.accessibility]); - } const query = { maxChg: opt.transfers, @@ -326,7 +326,7 @@ const createClient = (profile, userAgent, opt = {}) => { query.outDate = profile.formatDate(profile, when); if (profile.endpoint !== dbProfile.endpoint) { - throw new Error('db profile expected.'); + throw new Error('bestPrices() only works with the DB profile.'); } const {res, common} = await profile.request({profile, opt}, userAgent, { @@ -335,7 +335,7 @@ const createClient = (profile, userAgent, opt = {}) => { req: profile.transformJourneysQuery({profile, opt}, query), }); if (!Array.isArray(res.outConL)) { - return {}; + return null; } // todo: outConGrpL diff --git a/parse/bestprice.js b/parse/bestprice.js index 22cc37cb..52fa8d97 100644 --- a/parse/bestprice.js +++ b/parse/bestprice.js @@ -4,7 +4,7 @@ const parseBestPrice = (ctx, outDaySeg, journeys) => { const bpjourneys = outDaySeg.conRefL ? outDaySeg.conRefL - .map(i => journeys.find(j => j.refreshToken == res.outConL[i].ctxRecon)) + .map(outConLIdx => journeys.find(j => j.refreshToken == res.outConL[outConLIdx].ctxRecon)) .filter(j => Boolean(j)) : []; @@ -13,9 +13,9 @@ const parseBestPrice = (ctx, outDaySeg, journeys) => { const result = { journeys: bpjourneys, - fromDate: profile.parseDateTime(ctx, outDaySeg.fromDate, outDaySeg.fromTime), - toDate: profile.parseDateTime(ctx, outDaySeg.toDate, outDaySeg.toTime), - bestPrice: amount > 0 && currency ? {amount, currency} : undefined, + from: profile.parseDateTime(ctx, outDaySeg.fromDate, outDaySeg.fromTime), + to: profile.parseDateTime(ctx, outDaySeg.toDate, outDaySeg.toTime), + bestPrice: amount > 0 && currency ? {amount, currency} : null, }; return result; diff --git a/test/db-bestprice.js b/test/db-bestprice.js index 86e63651..9cdc9296 100644 --- a/test/db-bestprice.js +++ b/test/db-bestprice.js @@ -17,15 +17,14 @@ const opt = { via: null, transfers: -1, transferTime: 0, - accessibility: 'none', bike: false, tickets: true, polylines: true, remarks: true, - walkingSpeed: 'normal', - startWithWalking: true, departure: '2023-06-15', products: {}, + firstClass: false, + ageGroup: 'E', }; tap.test('parses a bestprice with a DEVI leg correctly (DB)', (t) => { diff --git a/test/fixtures/db-bestprice.js b/test/fixtures/db-bestprice.js index 79314c91..e7c48597 100644 --- a/test/fixtures/db-bestprice.js +++ b/test/fixtures/db-bestprice.js @@ -360,8 +360,8 @@ const json = ` ] } ], - "fromDate": "2023-06-15T10:00:00+02:00", - "toDate": "2023-06-15T13:00:00+02:00", + "from": "2023-06-15T10:00:00+02:00", + "to": "2023-06-15T13:00:00+02:00", "bestPrice": { "amount": 31, "currency": "EUR" @@ -806,8 +806,8 @@ const json = ` ] } ], - "fromDate": "2023-06-15T13:00:00+02:00", - "toDate": "2023-06-15T16:00:00+02:00", + "from": "2023-06-15T13:00:00+02:00", + "to": "2023-06-15T16:00:00+02:00", "bestPrice": { "amount": 29.9, "currency": "EUR" @@ -1506,8 +1506,8 @@ const json = ` ] } ], - "fromDate": "2023-06-15T16:00:00+02:00", - "toDate": "2023-06-15T19:00:00+02:00", + "from": "2023-06-15T16:00:00+02:00", + "to": "2023-06-15T19:00:00+02:00", "bestPrice": { "amount": 28.9, "currency": "EUR" @@ -2293,8 +2293,8 @@ const json = ` ] } ], - "fromDate": "2023-06-15T19:00:00+02:00", - "toDate": "2023-06-16T00:00:00+02:00", + "from": "2023-06-15T19:00:00+02:00", + "to": "2023-06-16T00:00:00+02:00", "bestPrice": { "amount": 9.9, "currency": "EUR" @@ -2302,14 +2302,14 @@ const json = ` }, { "journeys": [], - "fromDate": "2023-06-15T00:00:00+02:00", - "toDate": "2023-06-15T07:00:00+02:00", + "from": "2023-06-15T00:00:00+02:00", + "to": "2023-06-15T07:00:00+02:00", "bestPrice": null }, { "journeys": [], - "fromDate": "2023-06-15T07:00:00+02:00", - "toDate": "2023-06-15T10:00:00+02:00", + "from": "2023-06-15T07:00:00+02:00", + "to": "2023-06-15T10:00:00+02:00", "bestPrice": null } ],