Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions packages/core-utils/src/query-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { print } from "graphql";
import {
ModeSetting,
ModeSettingValues,
PlanModesInput,
TransportMode
} from "@opentripplanner/types";

Expand All @@ -20,7 +21,7 @@ type OTPQueryParams = {
date?: string;
departArrive?: string;
from: LonLatOutput & { name?: string };
modes: TransportMode[];
modes: PlanModesInput;
modeSettings: ModeSetting[];
numItineraries?: number;
omitCanceled?: boolean;
Expand All @@ -30,6 +31,10 @@ type OTPQueryParams = {
via?: OTPViaLocationInput[]
};

type GenerateCombinationsParams = Omit<OTPQueryParams, "modes"> & {
modes: TransportMode[];
};

type GraphQLQuery = {
query: string;
variables: Record<string, unknown>;
Expand Down Expand Up @@ -78,24 +83,34 @@ export function extractAdditionalModes(

/**
* Generates a list of queries for OTP based on planConnection config
* @param params OTP Query Params
* @param params OTP query params with mode definitions to expand
* @returns Set of parameters to generate queries
*/
export function generateCombinations(params: OTPQueryParams): OTPQueryParams[] {
export function generateCombinations(
params: GenerateCombinationsParams
): OTPQueryParams[] {
const completeModeList = [
...extractAdditionalModes(params.modeSettings, params.modes),
...params.modes
];

// List of the transit *submodes* that are included in the input params
const transitModes = completeModeList
.filter(mode => isTransit(mode.mode) && mode.mode !== "TRANSIT")
const transitModes = completeModeList.filter(
mode => isTransit(mode.mode) && mode.mode !== "TRANSIT"
);

// @ts-expect-error types packagge fail
return completeModeList
.filter(mode => !!mode.input)
// @ts-expect-error types packagge fail
.map(mode => ({ ...params, modes: { ...mode.input, transit: { ...mode?.input?.transit, ...transitModes.length > 0 && { transit: transitModes } } } }));
.map(mode => ({
...params,
modes: {
...mode.input,
transit: {
...mode?.input?.transit,
...(transitModes.length > 0 && { transit: transitModes })
}
}
}));
}

/**
Expand Down
36 changes: 22 additions & 14 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,22 +832,30 @@ export type ModeSetting = (
ModeSettingBase;
export type ModeSettingValues = Record<string, number | string | boolean>;

export type PlanModesInput = {
direct?: string[];
directOnly?: boolean;
transit?: PlanTransitModesInput;
transitOnly?: boolean;
};

export type PlanTransitModesInput = {
access?: string[];
egress?: string[];
transfer?: string[];
transit?: PlanTransitModePreferenceInput[];
};

export type PlanTransitModePreferenceInput = {
mode: string;
cost?: { reluctance: number };
replacement?: { requirement?: string };
};

/**
* TransportModes correspond with the OTP GraphQL TransportMode.
* Could be anything from walk, bike to transit, tram, or bus.
* A mode selected by the UI, with an optional PlanConnection input for a query.
*/
export type TransportMode = { mode: string; input?: ModeInput };

export type ModeInput =
| {
access: string[];
egress: string[];
transfer?: string[]; // Not actually, but for our config yes
transit: { mode: string }[];
}
| {
direct: string[];
};
export type TransportMode = { mode: string; input?: PlanModesInput };

/**
* This is a combination of transportation modes,
Expand Down
Loading