Skip to content

Commit

Permalink
Refactor StopSearch to use the new Data Model
Browse files Browse the repository at this point in the history
  • Loading branch information
Huulivoide committed Jan 30, 2025
1 parent 8c47724 commit 5e466e5
Show file tree
Hide file tree
Showing 10 changed files with 1,949 additions and 661 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,19 @@ const GQL_GET_STOPS_BY_ROUTE_ID_QUERY = gql`
) {
...stop_table_row
stopPlace: newest_stop_place {
id
netex_id
name_lang
name_value
stop_place_alternative_names {
alternative_name {
name_lang
name_value
name_type
}
}
description_lang
description_value
quay: newest_quay {
...stop_table_row_quay_base_details
}
}
}
`;

type RawStopPoint = GetStopsByRouteIdQuery['stopPoints'][number];

function mapStopPlace(rawStopPoint: RawStopPoint): StopPlaceSearchRowDetails {
const rawStopPlace = rawStopPoint.stopPlace;
function mapQuay(rawStopPoint: RawStopPoint): StopPlaceSearchRowDetails {
const rawQuay = rawStopPoint.quay;

if (!rawStopPlace) {
if (!rawQuay) {
return {
netexId: null,
nameFin: null,
Expand All @@ -55,9 +40,9 @@ function mapStopPlace(rawStopPoint: RawStopPoint): StopPlaceSearchRowDetails {
}

return {
netexId: rawStopPlace.netex_id,
nameFin: rawStopPlace.name_value,
nameSwe: rawStopPlace.stop_place_alternative_names.find(
netexId: rawQuay.netex_id,
nameFin: rawQuay.stop_place?.name_value,
nameSwe: rawQuay.stop_place?.stop_place_alternative_names.find(
(alternativeName) =>
alternativeName.alternative_name.name_lang === 'swe' &&
alternativeName.alternative_name.name_type === 'TRANSLATION',
Expand All @@ -68,10 +53,10 @@ function mapStopPlace(rawStopPoint: RawStopPoint): StopPlaceSearchRowDetails {
function mapDataToStopResults(
data: GetStopsByRouteIdQuery,
): Array<StopSearchRow> {
return data.stopPoints.map((rawStopPoint) => {
return data.stopPoints.map((rawQuay) => {
return {
...omit(rawStopPoint, ['stopPlace']),
quay: mapStopPlace(rawStopPoint),
...omit(rawQuay, ['quay']),
quay: mapQuay(rawQuay),
};
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,76 @@
import isEmpty from 'lodash/isEmpty';
import { StopsDatabaseStopPlaceNewestVersionBoolExp } from '../../../../generated/graphql';
import {
StopRegistryMunicipality,
knownPriorityValues,
} from '../../../../types/enums';
import { StopsDatabaseQuayNewestVersionBoolExp } from '../../../../generated/graphql';
import { knownPriorityValues } from '../../../../types/enums';
import {
AllOptionEnum,
buildOptionalSearchConditionGqlFilter,
buildSearchStopByLabelOrNameFilter,
buildTiamatAddressLikeGqlFilter,
buildTiamatMunicipalityGqlFilter,
buildTiamatPrivateCodeLikeGqlFilter,
mapToSqlLikeValue,
} from '../../../../utils';
import { buildSearchStopByLabelOrNameFilter } from '../../utils/buildSearchStopByLabelOrNameFilter';
import { SearchBy, StopSearchFilters } from '../types';

enum TiamatKeyValueKeys {
Address = 'streetAddress',
}

function buildKeyValueLikeFilter(
key: TiamatKeyValueKeys,
value: string,
): StopsDatabaseQuayNewestVersionBoolExp {
return {
quay_key_values: {
key_values_key: { _eq: key },
value: { value_items: { items: { _ilike: value } } },
},
};
}

function buildAddressLikeFilter(value: string) {
return buildKeyValueLikeFilter(TiamatKeyValueKeys.Address, value);
}

function buildSearchStopsQueryFilter(
filters: StopSearchFilters,
): StopsDatabaseStopPlaceNewestVersionBoolExp {
): StopsDatabaseQuayNewestVersionBoolExp {
if (filters.searchBy === SearchBy.LabelOrName) {
return buildSearchStopByLabelOrNameFilter(filters.query);
}

if (filters.searchBy === SearchBy.Address) {
return buildOptionalSearchConditionGqlFilter<
string,
StopsDatabaseStopPlaceNewestVersionBoolExp
>(mapToSqlLikeValue(filters.query), buildTiamatAddressLikeGqlFilter);
StopsDatabaseQuayNewestVersionBoolExp
>(mapToSqlLikeValue(filters.query), buildAddressLikeFilter);
}

return {};
}

// No other type of private code is used in the application
const ELY_NUMBER_TYPE = 'ELY';

function buildPrivateCodeLikeFilter(
value: string,
): StopsDatabaseQuayNewestVersionBoolExp {
return {
private_code_type: { _eq: ELY_NUMBER_TYPE },
private_code_value: { _ilike: value },
};
}

function buildSearchStopsMunicipalityFilter({
municipalities,
}: StopSearchFilters): StopsDatabaseStopPlaceNewestVersionBoolExp {
}: StopSearchFilters): StopsDatabaseQuayNewestVersionBoolExp {
if (municipalities.includes(AllOptionEnum.All)) {
return {};
}

return buildTiamatMunicipalityGqlFilter(
municipalities as Array<StopRegistryMunicipality>,
);
return { stop_place: { topographic_place_id: { _in: municipalities } } };
}

function buildSearchStopsObservationDateFilter({
observationDate,
}: StopSearchFilters): StopsDatabaseStopPlaceNewestVersionBoolExp {
}: StopSearchFilters): StopsDatabaseQuayNewestVersionBoolExp {
const dateString = observationDate.toISODate();
return {
validity_start: { _lte: dateString },
Expand All @@ -59,7 +83,7 @@ function buildSearchStopsObservationDateFilter({

function buildSearchStopsPriorityFilter({
priorities,
}: StopSearchFilters): StopsDatabaseStopPlaceNewestVersionBoolExp {
}: StopSearchFilters): StopsDatabaseQuayNewestVersionBoolExp {
const allSelected =
knownPriorityValues.length === priorities.length &&
knownPriorityValues.every((prio) => priorities.includes(prio));
Expand All @@ -74,13 +98,13 @@ function buildSearchStopsPriorityFilter({

export function buildSearchStopsGqlQueryVariables(
filters: StopSearchFilters,
): StopsDatabaseStopPlaceNewestVersionBoolExp {
): StopsDatabaseQuayNewestVersionBoolExp {
const queryFilter = buildSearchStopsQueryFilter(filters);

const elyNumberFilter = buildOptionalSearchConditionGqlFilter<
string,
StopsDatabaseStopPlaceNewestVersionBoolExp
>(mapToSqlLikeValue(filters.elyNumber), buildTiamatPrivateCodeLikeGqlFilter);
StopsDatabaseQuayNewestVersionBoolExp
>(mapToSqlLikeValue(filters.elyNumber), buildPrivateCodeLikeFilter);

const municipalityFilter = buildSearchStopsMunicipalityFilter(filters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { useNumericSortingCollator } from '../utils';
export type FindStopAreaInfo = FindStopAreaInfoFragment;

const GQL_FIND_STOP_AREAS = gql`
query findStopAreas($query: String!, $validOn: timestamp!) {
query findStopAreas($query: String!, $validOn: String!) {
stops_database {
stops_database_group_of_stop_places(
stopAreas: stops_database_stop_place_newest_version(
where: {
_and: [
{
Expand All @@ -22,17 +22,17 @@ const GQL_FIND_STOP_AREAS = gql`
{ name_value: { _ilike: $query } }
{ short_name_value: { _ilike: $query } }
{
group_of_stop_places_alternative_names: {
stop_place_alternative_names: {
alternative_name: { name_value: { _ilike: $query } }
}
}
]
}
{ from_date: { _lte: $validOn } }
{ validity_start: { _lte: $validOn } }
{
_or: [
{ to_date: { _gte: $validOn } }
{ to_date: { _is_null: true } }
{ validity_end: { _gte: $validOn } }
{ validity_end: { _is_null: true } }
]
}
]
Expand All @@ -44,7 +44,7 @@ const GQL_FIND_STOP_AREAS = gql`
}
}
fragment FindStopAreaInfo on stops_database_group_of_stop_places {
fragment FindStopAreaInfo on stops_database_stop_place_newest_version {
id
netex_id
version
Expand All @@ -68,15 +68,13 @@ export function useFindStopAreas(filters: StopSearchFilters) {
const { data, ...rest } = useFindStopAreasQuery({
variables: {
query: mapToSqlLikeValue(filters.query),
validOn: filters.observationDate,
validOn: filters.observationDate.toString(),
},
});

const stopAreas: ReadonlyArray<FindStopAreaInfo> = useMemo(
() =>
(
data?.stops_database?.stops_database_group_of_stop_places ?? []
).toSorted((a, b) =>
(data?.stops_database?.stopAreas ?? []).toSorted((a, b) =>
labelSortCollator.compare(a.name_value ?? '', b.name_value ?? ''),
),
[data, labelSortCollator],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import { mapQueryResultToStopSearchRows } from '../utils';
const GQL_GET_STOPS_BY_STOP_AREA_ID = gql`
query getStopsByStopAreaId($stopAreaId: bigint!) {
stops_database {
stops: stops_database_stop_place_newest_version(
where: {
group_of_stop_places_members: {
group_of_stop_places_id: { _eq: $stopAreaId }
}
}
order_by: [{ quay_public_code: asc }]
quays: stops_database_quay_newest_version(
where: { stop_place_id: { _eq: $stopAreaId } }
order_by: [{ public_code: asc }]
) {
...stop_table_row_stop_place
...stop_table_row_quay
}
}
}
Expand All @@ -27,11 +23,11 @@ export const useGetStopResultByStopAreaId = (stopAreaId: number | bigint) => {
});

const stopSearchRows: ReadonlyArray<StopSearchRow> = useMemo(() => {
if (!data?.stops_database?.stops) {
if (!data?.stops_database?.quays) {
return [];
}

return mapQueryResultToStopSearchRows(data.stops_database.stops);
return mapQueryResultToStopSearchRows(data.stops_database.quays);
}, [data]);

return {
Expand Down
12 changes: 5 additions & 7 deletions ui/src/components/stop-registry/search/utils/resultMappers.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import {
StopTableRowFragment,
StopTableRowStopPlaceFragment,
StopTableRowQuayFragment,
} from '../../../../generated/graphql';
import { StopSearchRow } from '../types';

const mapResultRowToStopSearchRow = (
stopPlace: StopTableRowStopPlaceFragment,
) => {
const mapResultRowToStopSearchRow = (stopPlace: StopTableRowQuayFragment) => {
return {
...(stopPlace.scheduled_stop_point_instance as StopTableRowFragment),
quay: {
netexId: stopPlace.netex_id,
nameFin: stopPlace.name_value,
nameSwe: stopPlace.stop_place_alternative_names.find(
nameFin: stopPlace.stop_place?.name_value,
nameSwe: stopPlace.stop_place?.stop_place_alternative_names.find(
(alternativeName) =>
alternativeName.alternative_name.name_lang === 'swe' &&
alternativeName.alternative_name.name_type === 'TRANSLATION',
Expand All @@ -22,7 +20,7 @@ const mapResultRowToStopSearchRow = (
};

export const mapQueryResultToStopSearchRows = (
stops: ReadonlyArray<StopTableRowStopPlaceFragment>,
stops: ReadonlyArray<StopTableRowQuayFragment>,
): StopSearchRow[] =>
stops
// Filter out stops which do not have a matching stop in routes and lines
Expand Down
Loading

0 comments on commit 5e466e5

Please sign in to comment.