Skip to content

Commit

Permalink
front: simplify OP matching in updatePathStepsFromOperationalPoints()
Browse files Browse the repository at this point in the history
We already have a matchPathStepAndOp() function to compare a PathStep
and a SuggestedOP. There are two differences:

- The old code checks whether PathStep.ch is set before comparing
  it with SuggestedOP, but that's incorrect. On the editoast side
  the secondary_code field is defined as Option<String>: None
  accepts OPs with any secondary code, "" accepts only OPs without
  any secondary code. On the TypeScript side, undefined accepts OPs
  with any secondary code, and '' accepts only OPs without any
  secondary code. However `'ch' in step` is true for
  `{ ch: undefined }`, which compares SuggestedOP.ch with undefined
  even if an undefined PathStep.ch is supposed to match any OP
  secondary code.
- The old code checks PathStep.secondary_code. However the PathSteps
  given to updatePathStepsFromOperationalPoints() come straight up
  from computeBasePathSteps(), which always leaves secondary_code
  undefined. As a result we never hit this case.

Signed-off-by: Simon Ser <[email protected]>
  • Loading branch information
emersion committed Nov 18, 2024
1 parent d3d369d commit ea909bf
Showing 1 changed file with 8 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
type PathfindingResult,
} from 'common/api/osrdEditoastApi';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { formatSuggestedOperationalPoints, upsertPathStepsInOPs } from 'modules/pathfinding/utils';
import {
formatSuggestedOperationalPoints,
matchPathStepAndOp,
upsertPathStepsInOPs,
} from 'modules/pathfinding/utils';
import { getSupportedElectrification, isThermal } from 'modules/rollingStock/helpers/electric';
import { adjustConfWithTrainToModify } from 'modules/trainschedule/components/ManageTrainSchedule/helpers/adjustConfWithTrainToModify';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
Expand Down Expand Up @@ -83,26 +87,9 @@ export function updatePathStepsFromOperationalPoints(
stepsCoordinates: Position[]
) {
const updatedPathSteps: PathStep[] = pathSteps.map((step, i) => {
const correspondingOp = suggestedOperationalPoints.find((suggestedOp) => {
if ('uic' in step) {
const condition = suggestedOp.uic === step.uic;
if ('ch' in step) {
return condition && suggestedOp.ch === step.ch;
}
// When importing train from open data or from files, secondary_code might not always exist
if (step.secondary_code) {
return condition && suggestedOp.ch === step.secondary_code;
}
return condition;
}
if ('trigram' in step) {
const condition = suggestedOp.trigram === step.trigram;
if (step.secondary_code) {
return condition && suggestedOp.ch === step.secondary_code;
}
}
return false;
});
const correspondingOp = suggestedOperationalPoints.find((suggestedOp) =>
matchPathStepAndOp(step, suggestedOp)
);

const { kp, name, ch } = correspondingOp || step;

Expand Down

0 comments on commit ea909bf

Please sign in to comment.