Skip to content

Commit f413408

Browse files
committed
front, editoast: turn SearchPayload.object into enum
Signed-off-by: Quentix <quentin.hay-kergrohenn@epita.fr>
1 parent 7809237 commit f413408

10 files changed

Lines changed: 96 additions & 27 deletions

File tree

editoast/openapi.yaml

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

editoast/src/views/search.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ use search::query_into_sql;
230230
use serde::Deserialize;
231231
use serde::Serialize;
232232
use serde_json::value::Value as JsonValue;
233+
use strum::Display;
234+
use strum::FromRepr;
233235
use utoipa::ToSchema;
234236

235237
use crate::error::Result;
@@ -260,6 +262,20 @@ enum SearchQuery {
260262
Array(Vec<Option<SearchQuery>>),
261263
}
262264

265+
/// Object type for query search
266+
#[derive(Debug, Clone, ToSchema, Serialize, Deserialize, FromRepr, Display)]
267+
#[serde(rename_all = "lowercase")]
268+
enum SearchObjectType {
269+
Track,
270+
Signal,
271+
Project,
272+
Study,
273+
Scenario,
274+
TrainSchedule,
275+
OperationalPoint,
276+
User,
277+
}
278+
263279
/// The payload of a search request
264280
#[derive(Debug, Clone, Deserialize, ToSchema)]
265281
#[schema(example = json!({
@@ -268,7 +284,7 @@ enum SearchQuery {
268284
}))]
269285
pub struct SearchPayload {
270286
/// The object kind to query - run `editoast search list` to get all possible values
271-
object: String,
287+
object: SearchObjectType,
272288
/// The query to run
273289
#[schema(value_type = SearchQuery)]
274290
query: JsonValue,
@@ -343,17 +359,15 @@ pub(in crate::views) async fn search(
343359
Query(PaginationQueryParams { page, page_size }): Query<PaginationQueryParams<1000>>,
344360
Json(SearchPayload { object, query, dry }): Json<SearchPayload>,
345361
) -> Result<Json<serde_json::Value>> {
346-
let required_role = match object.as_str() {
347-
"track" | "signal" | "project" | "study" | "scenario" => {
348-
Some(authz::Role::OperationalStudies)
349-
}
350-
"trainschedule" | "operationalpoint" | "user" => None,
351-
_ => {
352-
return Err(SearchApiError::ObjectType {
353-
object_type: object.to_owned(),
354-
}
355-
.into());
356-
}
362+
let required_role = match object {
363+
SearchObjectType::Track
364+
| SearchObjectType::Signal
365+
| SearchObjectType::Project
366+
| SearchObjectType::Study
367+
| SearchObjectType::Scenario => Some(authz::Role::OperationalStudies),
368+
SearchObjectType::TrainSchedule
369+
| SearchObjectType::OperationalPoint
370+
| SearchObjectType::User => None,
357371
};
358372

359373
if let Some(required_role) = required_role
@@ -365,8 +379,10 @@ pub(in crate::views) async fn search(
365379
}
366380

367381
let search_config =
368-
SearchConfigFinder::find(&object).ok_or_else(|| SearchApiError::ObjectType {
369-
object_type: object.to_owned(),
382+
SearchConfigFinder::find(object.to_string().to_lowercase()).ok_or_else(|| {
383+
SearchApiError::ObjectType {
384+
object_type: object.to_string(),
385+
}
370386
})?;
371387
let offset = (page - 1) * page_size;
372388
let (sql, bindings) = query_into_sql(

front/src/applications/operationalStudies/helpers/searchPayload.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { SearchPayload } from 'common/api/osrdEditoastApi';
12
import { toUpper } from 'utils/strings';
23

34
export const tokenClause = (token: string) => [
@@ -17,7 +18,10 @@ export const searchQuery = (debouncedTrimmedInput: string) => [
1718
['search', ['secondary_code'], debouncedTrimmedInput],
1819
];
1920

20-
export const largePayload = (infraId: number | undefined, debouncedTrimmedInput: string) => ({
21+
export const largePayload = (
22+
infraId: number | undefined,
23+
debouncedTrimmedInput: string
24+
): SearchPayload => ({
2125
object: 'operationalpoint',
2226
query: [
2327
'and',
@@ -26,7 +30,10 @@ export const largePayload = (infraId: number | undefined, debouncedTrimmedInput:
2630
],
2731
});
2832

29-
export const exactTrigramPayload = (infraId: number | undefined, firstTokenUpper: string) => ({
33+
export const exactTrigramPayload = (
34+
infraId: number | undefined,
35+
firstTokenUpper: string
36+
): SearchPayload => ({
3037
object: 'operationalpoint',
3138
query: [
3239
'and',
@@ -35,7 +42,10 @@ export const exactTrigramPayload = (infraId: number | undefined, firstTokenUpper
3542
],
3643
});
3744

38-
export const multiPayloadFromTokens = (infraId: number | undefined, tokens: string[]) => {
45+
export const multiPayloadFromTokens = (
46+
infraId: number | undefined,
47+
tokens: string[]
48+
): SearchPayload => {
3949
const multiQuery = buildMultiTokenQuery(tokens);
4050
return {
4151
object: 'operationalpoint',

front/src/applications/operationalStudies/views/Scenario/components/MacroEditor/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { omit } from 'lodash';
44
import {
55
osrdEditoastApi,
66
type MacroNodeResponse,
7+
type SearchPayload,
78
type SearchResultItemOperationalPoint,
89
type SubCategory,
910
type TrainCategory,
@@ -268,7 +269,7 @@ export const fetchStationSecondaryCode = async (
268269
infraId: number,
269270
dispatch: AppDispatch
270271
) => {
271-
const searchPayload = {
272+
const searchPayload: SearchPayload = {
272273
object: 'operationalpoint',
273274
query: ['and', ['=', ['infra_id'], infraId], ['=', ['main_code'], trigram]],
274275
};

front/src/applications/stdcm/hooks/useLinkedTrainSearch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useSelector } from 'react-redux';
66

77
import type {
88
PathItem,
9+
SearchPayload,
910
SearchQuery,
1011
SearchResultItemOperationalPoint,
1112
SearchResultItemTrainSchedule,
@@ -80,7 +81,7 @@ const useLinkedTrainSearch = () => {
8081
] as SearchQuery);
8182

8283
try {
83-
const payloadOP = {
84+
const payloadOP: SearchPayload = {
8485
object: 'operationalpoint',
8586
query: pathItemQuery,
8687
};

front/src/common/Map/Search/MapSearchLine.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import bbox from '@turf/bbox';
44
import { useTranslation } from 'react-i18next';
55

66
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
7-
import type { BoundingBox, SearchResultItemTrack } from 'common/api/osrdEditoastApi';
7+
import type { BoundingBox, SearchPayload, SearchResultItemTrack } from 'common/api/osrdEditoastApi';
88
import InputSNCF from 'common/BootstrapSNCF/InputSNCF';
99
import { computeBBoxViewport } from 'common/Map/WarpedMap/core/helpers';
1010
import { useInfraID } from 'common/osrdContext';
@@ -43,7 +43,7 @@ const MapSearchLine = ({
4343
['search', ['line_name'], debouncedSearchTerm],
4444
['like', ['to_string', ['line_code']], `%${debouncedSearchTerm}%`],
4545
];
46-
const payload = {
46+
const payload: SearchPayload = {
4747
object: 'track',
4848
query: ['and', searchQuery, infraID !== undefined ? ['=', ['infra_id'], infraID] : true],
4949
};

front/src/common/Map/Search/useSearchOperationalPoint.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { useState, useMemo, useCallback } from 'react';
33
import { skipToken } from '@reduxjs/toolkit/query';
44
import { useSelector } from 'react-redux';
55

6-
import { type SearchResultItemOperationalPoint, osrdEditoastApi } from 'common/api/osrdEditoastApi';
6+
import {
7+
type SearchResultItemOperationalPoint,
8+
osrdEditoastApi,
9+
type SearchPayload,
10+
} from 'common/api/osrdEditoastApi';
711
import { useInfraID } from 'common/osrdContext';
812
import { setFailure } from 'reducers/main';
913
import { getOperationalPoints } from 'reducers/osrdconf/stdcmConf/selectors';
@@ -58,7 +62,7 @@ export default function useSearchOperationalPoint({
5862
async (searchQuery: string) => {
5963
if (!infraID) return [];
6064

61-
const payload = {
65+
const payload: SearchPayload = {
6266
object: 'operationalpoint',
6367
query: [
6468
'and',

front/src/common/api/generatedEditoastApi.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4365,14 +4365,23 @@ export type SearchResultItem =
43654365
| SearchResultItemScenario
43664366
| SearchResultItemTrainSchedule
43674367
| SearchResultItemUser;
4368+
export type SearchObjectType =
4369+
| 'track'
4370+
| 'signal'
4371+
| 'project'
4372+
| 'study'
4373+
| 'scenario'
4374+
| 'trainschedule'
4375+
| 'operationalpoint'
4376+
| 'user';
43684377
export type SearchQuery = boolean | number | number | string | object;
43694378
export type SearchPayload = {
43704379
/** Whether to return the SQL query instead of executing it
43714380
43724381
Only available in debug builds. */
43734382
dry?: boolean;
43744383
/** The object kind to query - run `editoast search list` to get all possible values */
4375-
object: string;
4384+
object: SearchObjectType;
43764385
/** The query to run */
43774386
query: SearchQuery;
43784387
};

front/src/modules/pathfinding/components/Pathfinding/TypeAndPath.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useManageTrainScheduleContext } from 'applications/operationalStudies/h
1010
import type {
1111
PathItem,
1212
PostSearchApiArg,
13+
SearchPayload,
1314
SearchResultItemOperationalPoint,
1415
} from 'common/api/osrdEditoastApi';
1516
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
@@ -100,7 +101,7 @@ const TypeAndPath = ({ setDisplayTypeAndPath, isInNewModal = false }: TypeAndPat
100101
const searchOperationalPoints = async () => {
101102
const searchQuery = ['or', ['search', ['name'], debouncedSearchTerm]];
102103

103-
const payload = {
104+
const payload: SearchPayload = {
104105
object: 'operationalpoint',
105106
query: ['and', searchQuery, infraId !== undefined ? ['=', ['infra_id'], infraId] : true],
106107
};

osrd_schemas_auto/osrd_schemas_auto/models.py

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)