Skip to content

Commit f713e07

Browse files
committed
Get update history data for stop areas
1 parent 6f51cc7 commit f713e07

File tree

8 files changed

+243
-6
lines changed

8 files changed

+243
-6
lines changed

cypress/e2e/stop-registry/stopAreaDetails.cy.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ describe('Stop area details', () => {
217217
.getValidityPeriod()
218218
.shouldHaveText(validity);
219219

220+
stopAreaDetailsPage.versioningRow
221+
.getChangeHistoryLink()
222+
.shouldBeVisible()
223+
.invoke('text')
224+
.should('match', /\d{2}\.\d{2}\.\d{4}\s+\d{2}:\d{2}/); // Matches format: DD.MM.YYYY HH:mm
225+
220226
const { details } = stopAreaDetailsPage;
221227
details.getName().shouldHaveText(expected.name);
222228
details.getNameSwe().shouldHaveText(expected.nameSwe);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export class StopAreaVersioningRow {
22
getValidityPeriod = () =>
33
cy.getByTestId('StopAreaVersioningRow::validityPeriod');
4+
5+
getChangeHistoryLink = () =>
6+
cy.getByTestId('StopAreaVersioningRow::changeHistoryLink');
47
}

ui/src/components/forms/stop-area/useUpsertStopArea.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export const useUpsertStopArea = () => {
130130
const tryHandleApolloError = useStopAreaDetailsApolloErrorHandler();
131131
const [upsertStopAreaMutation] = useUpsertStopAreaMutation({
132132
awaitRefetchQueries: true,
133-
refetchQueries: ['GetStopAreasByLocation'],
133+
refetchQueries: ['GetStopAreasByLocation', 'GetLatestStopPlaceChange'],
134134
});
135135

136136
/**
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { FC } from 'react';
22
import { useTranslation } from 'react-i18next';
3+
import { Link } from 'react-router';
34
import { twMerge } from 'tailwind-merge';
4-
import { mapToShortDate } from '../../../../../time';
5+
import { Path, routeDetails } from '../../../../../router/routeDetails';
6+
import { mapToShortDate, mapUTCToDateTime } from '../../../../../time';
57
import { StopAreaComponentProps } from '../types';
68

79
const testIds = {
810
validityPeriod: 'StopAreaVersioningRow::validityPeriod',
11+
changeHistoryLink: 'StopAreaVersioningRow::changeHistoryLink',
912
};
1013

1114
export const StopAreaVersioningRow: FC<StopAreaComponentProps> = ({
@@ -15,18 +18,26 @@ export const StopAreaVersioningRow: FC<StopAreaComponentProps> = ({
1518
const { t } = useTranslation();
1619

1720
return (
18-
<div className={twMerge('flex items-center', className)}>
21+
<div className={twMerge('my-4 flex items-center gap-2', className)}>
1922
<h2>{t('stopAreaDetails.title')}</h2>
2023

2124
<div
22-
className="ml-4"
2325
title={t('accessibility:stops.validityPeriod')}
2426
data-testid={testIds.validityPeriod}
2527
>
2628
{mapToShortDate(area.validityStart)}
2729
<span className="mx-1">-</span>
2830
{mapToShortDate(area.validityEnd)}
2931
</div>
32+
33+
<Link
34+
to={routeDetails[Path.stopAreaDetails].getLink(area.privateCode?.value)}
35+
className="ml-auto flex items-center text-base text-tweaked-brand hover:underline"
36+
data-testid={testIds.changeHistoryLink}
37+
>
38+
{mapUTCToDateTime(area.changed)} | {area.changedByUserName ?? 'HSL'}{' '}
39+
<i className="icon-history text-xl" aria-hidden />
40+
</Link>
3041
</div>
3142
);
3243
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { gql } from '@apollo/client';
2+
import {
3+
StopsDatabaseStopPlaceBoolExp,
4+
useGetLatestStopPlaceChangeQuery,
5+
useGetStopPlaceChangeHistoryQuery,
6+
} from '../../../../../generated/graphql';
7+
8+
const GQL_GET_STOP_PLACE_CHANGE_HISTORY = gql`
9+
query GetStopPlaceChangeHistory($where: stops_database_stop_place_bool_exp!) {
10+
stopsDb: stops_database {
11+
stopPlace: stops_database_stop_place(
12+
where: $where
13+
order_by: { version: desc }
14+
) {
15+
changed
16+
changed_by
17+
private_code_value
18+
netex_id
19+
version
20+
version_comment
21+
}
22+
}
23+
}
24+
`;
25+
26+
const GQL_GET_LATEST_STOP_PLACE_CHANGE = gql`
27+
query GetLatestStopPlaceChange($where: stops_database_stop_place_bool_exp!) {
28+
stopsDb: stops_database {
29+
stopPlace: stops_database_stop_place(
30+
where: $where
31+
order_by: { version: desc }
32+
limit: 1
33+
) {
34+
changed
35+
changed_by
36+
private_code_value
37+
netex_id
38+
}
39+
}
40+
}
41+
`;
42+
43+
export function useGetStopPlaceChangeHistory(
44+
where: StopsDatabaseStopPlaceBoolExp,
45+
) {
46+
const { data, ...rest } = useGetStopPlaceChangeHistoryQuery({
47+
variables: { where },
48+
});
49+
50+
const stopPlaceVersions = data?.stopsDb?.stopPlace ?? [];
51+
52+
return {
53+
...rest,
54+
stopPlaceVersions,
55+
};
56+
}
57+
58+
export function useGetLatestStopPlaceChange(
59+
where: StopsDatabaseStopPlaceBoolExp,
60+
) {
61+
const { data, ...rest } = useGetLatestStopPlaceChangeQuery({
62+
variables: { where },
63+
});
64+
65+
const latestStopPlace = data?.stopsDb?.stopPlace?.[0];
66+
67+
const latestStopPlaceChangeData = {
68+
changed: latestStopPlace?.changed ?? null,
69+
changedBy: latestStopPlace?.changed_by ?? null,
70+
};
71+
72+
return { ...rest, latestStopPlaceChangeData };
73+
}

ui/src/components/stop-registry/stop-areas/stop-area-details/useGetStopAreaDetails.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
useGetStopPlaceDetailsQuery,
88
} from '../../../../generated/graphql';
99
import {
10+
useGetUserNames,
1011
useObservationDateQueryParam,
1112
useRequiredParams,
1213
} from '../../../../hooks';
@@ -15,6 +16,7 @@ import {
1516
getStopPlaceDetailsForEnrichment,
1617
getStopPlacesFromQueryResult,
1718
} from '../../../../utils';
19+
import { useGetLatestStopPlaceChange } from './hooks/useGetStopPlaceChangeHistory';
1820

1921
const GQL_GET_STOP_AREA_DETAILS = gql`
2022
query getStopPlaceDetails(
@@ -111,11 +113,19 @@ const GQL_GET_STOP_AREA_DETAILS = gql`
111113

112114
export function getEnrichedStopPlace(
113115
stopPlace: StopPlaceDetailsFragment | null | undefined,
116+
getUserNameById?: (userId: string | null | undefined) => string | null,
117+
stopPlaceChangeData?: {
118+
changed: string | null;
119+
changedBy: string | null;
120+
},
114121
): EnrichedStopPlace | null {
115122
if (!stopPlace) {
116123
return null;
117124
}
118125

126+
const changeData = stopPlaceChangeData;
127+
const changedByUserName = getUserNameById?.(changeData?.changedBy);
128+
119129
const transformedStopPlace = {
120130
...stopPlace,
121131
parentStopPlace: stopPlace.parentStopPlace
@@ -126,11 +136,18 @@ export function getEnrichedStopPlace(
126136
return {
127137
...stopPlace,
128138
...getStopPlaceDetailsForEnrichment(transformedStopPlace),
139+
changed: changeData?.changed,
140+
changedByUserName,
129141
};
130142
}
131143

132144
function useGetStopPlaceDetailsByWhere(
133145
where: StopsDatabaseStopPlaceNewestVersionBoolExp | null,
146+
getUserNameById?: (userId: string | null | undefined) => string | null,
147+
stopPlaceChangeData?: {
148+
changed: string | null;
149+
changedBy: string | null;
150+
},
134151
) {
135152
const { data, ...rest } = useGetStopPlaceDetailsQuery(
136153
where ? { variables: { where } } : { skip: true },
@@ -140,8 +157,9 @@ function useGetStopPlaceDetailsByWhere(
140157
data?.stopsDb?.newestVersion.at(0)?.TiamatStopPlace,
141158
).at(0);
142159
const stopPlaceDetails = useMemo(
143-
() => getEnrichedStopPlace(rawStopPlace),
144-
[rawStopPlace],
160+
() =>
161+
getEnrichedStopPlace(rawStopPlace, getUserNameById, stopPlaceChangeData),
162+
[rawStopPlace, getUserNameById, stopPlaceChangeData],
145163
);
146164

147165
return { ...rest, stopPlaceDetails };
@@ -198,12 +216,22 @@ function useGetStopPlaceDetailsWhereConditionsWithDate(): StopsDatabaseStopPlace
198216
}
199217

200218
export function useGetStopPlaceDetails() {
219+
const { getUserNameById } = useGetUserNames();
220+
221+
const { latestStopPlaceChangeData } = useGetLatestStopPlaceChange(
222+
useGetStopPlaceDetailsWhereConditions(),
223+
);
224+
201225
const validResult = useGetStopPlaceDetailsByWhere(
202226
useGetStopPlaceDetailsWhereConditionsWithDate(),
227+
getUserNameById,
228+
latestStopPlaceChangeData,
203229
);
204230

205231
const fallbackResult = useGetStopPlaceDetailsByWhere(
206232
useGetStopPlaceDetailsWhereConditions(),
233+
getUserNameById,
234+
latestStopPlaceChangeData,
207235
);
208236

209237
const hasValidData = !!validResult.stopPlaceDetails;

ui/src/generated/graphql.tsx

Lines changed: 114 additions & 0 deletions
Large diffs are not rendered by default.

ui/src/types/EnrichedStopRegistryTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export type SharedEnrichmentProperties = {
4646
readonly locationLong?: number | undefined;
4747
readonly validityStart?: string | undefined;
4848
readonly validityEnd?: string | undefined;
49+
readonly changed?: string | null;
50+
readonly changedByUserName?: string | null;
4951
};
5052

5153
export type QuayEnrichmentProperties = {

0 commit comments

Comments
 (0)