@opentripplanner/core-utils 14.2.3 → 14.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,6 +17,7 @@ import {
17
17
  import bikeRentalItinerary from "./__mocks__/bike-rental-itinerary.json";
18
18
  import tncItinerary from "./__mocks__/tnc-itinerary.json";
19
19
  import fareProductItinerary from "./__mocks__/fare-products-itinerary.json";
20
+ import complexItinerary from "./__mocks__/complex-fares.json";
20
21
  import flexItinerary from "../../../itinerary-body/src/__mocks__/itineraries/flex-itinerary.json";
21
22
  import faresv2Itinerary from "../../../itinerary-body/src/__mocks__/itineraries/fares-v2-fare-components.json";
22
23
 
@@ -262,6 +263,10 @@ describe("util > itinerary", () => {
262
263
  );
263
264
  expect(complexResult.amount).toEqual(2.8 * 2);
264
265
  });
266
+ it("should calculate the total cost of an itinerary with multiple v2 fares & transfers", () => {
267
+ const result = getItineraryCost(complexItinerary.legs, "0", "ADULT");
268
+ expect(result.amount).toEqual(3.0);
269
+ });
265
270
  it("should calculate the individual leg cost of a fares v2 legs", () => {
266
271
  const firstLegResult = getLegCost(faresv2Itinerary.legs[1], "3", "ADULT");
267
272
  expect(firstLegResult.price?.amount).toEqual(2);
package/src/itinerary.ts CHANGED
@@ -713,7 +713,7 @@ export function getItineraryCost(
713
713
  riderCategoryId: string | string[] | null
714
714
  ): Money | undefined {
715
715
  // TODO: Better input type handling
716
- if (Array.isArray(mediumId) && Array.isArray(riderCategoryId)) {
716
+ if (Array.isArray(mediumId) || Array.isArray(riderCategoryId)) {
717
717
  if (mediumId?.length !== riderCategoryId.length) {
718
718
  console.warn(
719
719
  "Invalid input types, only using first item. medium id list and rider category list must have same number of items"
@@ -739,8 +739,15 @@ export function getItineraryCost(
739
739
  // Only legs with fares (no walking legs)
740
740
  .filter(leg => leg.fareProducts?.length > 0)
741
741
  // Get the leg cost object of each leg
742
- // @ts-expect-error TS doesn't like our check in the if statement above
743
- .map(leg => getLegCost(leg, mediumId, riderCategoryId))
742
+ .map((leg, index, arr) =>
743
+ getLegCost(
744
+ leg,
745
+ mediumId,
746
+ riderCategoryId,
747
+ // We need to include the seen fare ids by gathering all previous leg fare product ids
748
+ arr.splice(0, index).flatMap(l => l?.fareProducts.map(fp => fp?.id))
749
+ )
750
+ )
744
751
  .filter(cost => cost.appliedFareProduct?.legPrice !== undefined)
745
752
  // Filter out duplicate use IDs
746
753
  // One fare product can be used on multiple legs,