Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle both invalid trigrams and uics as invalid op #10471

Merged
merged 1 commit into from
Feb 4, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
type TrainScheduleResult,
type PathfindingResult,
type SearchResultItemOperationalPoint,
type PathfindingInputError,
} from 'common/api/osrdEditoastApi';
import { useOsrdConfActions } from 'common/osrdContext';
import buildOpSearchQuery from 'modules/operationalPoint/helpers/buildOpSearchQuery';
Expand Down Expand Up @@ -74,8 +75,13 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
const [postSearch] = osrdEditoastApi.endpoints.postSearch.useMutation();

useEffect(() => {
const fetchPathStepsCoordinates = async (trainSchedule: TrainScheduleResult) => {
// get track sections
const fetchPathStepsCoordinates = async (
trainSchedule: TrainScheduleResult,
invalidPathItems: Extract<
PathfindingInputError,
{ error_type: 'invalid_path_items' }
>['items'] = []
) => {
const trackSectionIds: string[] = [];
trainSchedule.path.forEach((step) => {
if ('track' in step) {
Expand All @@ -97,6 +103,8 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
const pathStepsWithCoordinates = trainSchedule.path.map((step, index) => {
let coordinates: Position | undefined;
let name: string | undefined;
let uic: number | undefined;
let trigram: string | undefined;

if ('track' in step) {
const track = tracks[step.track];
Expand All @@ -107,19 +115,22 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
let op: SearchResultItemOperationalPoint | undefined;
if ('uic' in step) {
op = ops.find((o) => o.uic === step.uic);
uic = step.uic;
} else if ('trigram' in step) {
op = ops.find((o) => o.trigram === step.trigram);
} else {
trigram = step.trigram;
} else if ('operational_point' in step) {
op = ops.find((o) => o.obj_id === step.operational_point);
}
coordinates = op?.geographic.coordinates;
name = `${op?.name}`;
name = `${op?.name || uic?.toString() || trigram}`;
}

return {
...computeBasePathStep(trainSchedule, index),
coordinates,
name,
isInvalid: invalidPathItems.some((item) => item.index === index),
};
});

Expand Down Expand Up @@ -153,7 +164,13 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
};
const pathfindingResult = await postPathfindingBlocks(params).unwrap();
if (pathfindingResult.status !== 'success') {
fetchPathStepsCoordinates(trainSchedule);
const invalidPathItems =
pathfindingResult.failed_status === 'pathfinding_input_error' &&
pathfindingResult.error_type === 'invalid_path_items'
? pathfindingResult.items
: [];

fetchPathStepsCoordinates(trainSchedule, invalidPathItems);
return null;
}
const pathPropertiesParams: PostInfraByInfraIdPathPropertiesApiArg = {
Expand Down
21 changes: 5 additions & 16 deletions front/src/modules/pathfinding/hooks/usePathfinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,12 @@ const usePathfinding = (
steps: PathStep[],
invalidPathItems: Extract<PathfindingInputError, { error_type: 'invalid_path_items' }>['items']
) => {
// TODO: we currently only handle invalid pathSteps with trigram. We will have to do it for trackOffset, opId and uic too.
const invalidTrigrams = invalidPathItems
.map((item) => {
if ('trigram' in item.path_item) {
return item.path_item.trigram;
}
return null;
})
.filter((trigram): trigram is string => trigram !== null);
if (invalidTrigrams.length > 0) {
const updatedPathSteps = steps.map((step) => {
if (step && 'trigram' in step && invalidTrigrams.includes(step.trigram)) {
return { ...step, isInvalid: true };
}
return step;
});
const updatedPathSteps = steps.map((step, index) => ({
...step,
isInvalid: invalidPathItems.some((item) => item.index === index),
}));

if (invalidPathItems.length > 0) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
launchPathfinding(updatedPathSteps);
} else {
Expand Down
Loading