Skip to content

Commit 40b8c65

Browse files
committed
Get update history data for terminals
1 parent e3434c3 commit 40b8c65

File tree

6 files changed

+249
-4
lines changed

6 files changed

+249
-4
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ describe('Terminal details', () => {
239239
.validityPeriod()
240240
.should('contain', '1.1.2020-1.1.2050');
241241

242+
terminalDetailsPage.versioningRow
243+
.getChangeHistoryLink()
244+
.shouldBeVisible()
245+
.invoke('text')
246+
.should('match', /\d{2}\.\d{2}\.\d{4}\s+\d{2}:\d{2}/); // Matches format: DD.MM.YYYY HH:mm
247+
242248
verifyInitialBasicDetails();
243249
});
244250

cypress/pageObjects/stop-registry/terminals/TerminalVersioningRow.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ export class TerminalVersioningRow {
44

55
getEditValidityButton = () =>
66
cy.getByTestId('TerminalVersioningRow::editTerminalValidityButton');
7+
8+
getChangeHistoryLink = () =>
9+
cy.getByTestId('TerminalVersioningRow::changeHistoryLink');
710
}

ui/src/components/stop-registry/terminals/components/terminal-versions/TerminalVersioningRow.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
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 { TerminalComponentProps } from '../../types';
68
import { EditTerminalValidityButton } from './EditTerminalValidityButton';
79

810
const testIds = {
911
validityPeriod: 'TerminalVersioningRow::validityPeriod',
12+
changeHistoryLink: 'TerminalVersioningRow::changeHistoryLink',
1013
};
1114

1215
export const TerminalVersioningRow: FC<TerminalComponentProps> = ({
@@ -16,7 +19,7 @@ export const TerminalVersioningRow: FC<TerminalComponentProps> = ({
1619
const { t } = useTranslation();
1720

1821
return (
19-
<div className={twMerge('flex items-center gap-2', className)}>
22+
<div className={twMerge('my-4 flex items-center gap-2', className)}>
2023
<h2>{t('terminalDetails.title')}</h2>
2124

2225
<div
@@ -29,6 +32,18 @@ export const TerminalVersioningRow: FC<TerminalComponentProps> = ({
2932
</div>
3033

3134
<EditTerminalValidityButton terminal={terminal} />
35+
36+
<Link
37+
to={routeDetails[Path.terminalDetails].getLink(
38+
terminal.privateCode?.value,
39+
)}
40+
className="ml-auto flex items-center text-base text-tweaked-brand hover:underline"
41+
data-testid={testIds.changeHistoryLink}
42+
>
43+
{mapUTCToDateTime(terminal.changed)} |{' '}
44+
{terminal.changedByUserName ?? 'HSL'}{' '}
45+
<i className="icon-history text-xl" aria-hidden />
46+
</Link>
3247
</div>
3348
);
3449
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { gql } from '@apollo/client';
2+
import {
3+
StopsDatabaseStopPlaceBoolExp,
4+
useGetLatestParentStopPlaceChangeQuery,
5+
useGetParentStopPlaceChangeHistoryQuery,
6+
} from '../../../../generated/graphql';
7+
8+
const GQL_GET_PARENT_STOP_PLACE_CHANGE_HISTORY = gql`
9+
query GetParentStopPlaceChangeHistory(
10+
$where: stops_database_stop_place_bool_exp!
11+
) {
12+
stopsDb: stops_database {
13+
stopPlace: stops_database_stop_place(
14+
where: $where
15+
order_by: { version: desc }
16+
) {
17+
changed
18+
changed_by
19+
private_code_value
20+
version
21+
version_comment
22+
}
23+
}
24+
}
25+
`;
26+
27+
const GQL_GET_LATEST_PARENT_STOP_PLACE_CHANGE = gql`
28+
query GetLatestParentStopPlaceChange(
29+
$where: stops_database_stop_place_bool_exp!
30+
) {
31+
stopsDb: stops_database {
32+
stopPlace: stops_database_stop_place(
33+
where: $where
34+
order_by: { version: desc }
35+
limit: 1
36+
) {
37+
changed
38+
changed_by
39+
private_code_value
40+
}
41+
}
42+
}
43+
`;
44+
45+
export function useGetParentStopPlaceChangeHistory(
46+
where: StopsDatabaseStopPlaceBoolExp,
47+
) {
48+
const { data, ...rest } = useGetParentStopPlaceChangeHistoryQuery({
49+
variables: { where },
50+
});
51+
52+
const parentStopPlaceVersions = data?.stopsDb?.stopPlace ?? [];
53+
54+
return {
55+
...rest,
56+
parentStopPlaceVersions,
57+
};
58+
}
59+
60+
export function useGetLatestParentStopPlaceChange(
61+
where: StopsDatabaseStopPlaceBoolExp,
62+
) {
63+
const { data, ...rest } = useGetLatestParentStopPlaceChangeQuery({
64+
variables: { where },
65+
});
66+
67+
const latestParentStopPlace = data?.stopsDb?.stopPlace?.[0];
68+
69+
const latestParentStopPlaceChangeData = {
70+
changed: latestParentStopPlace?.changed ?? null,
71+
changedBy: latestParentStopPlace?.changed_by ?? null,
72+
privateCode: latestParentStopPlace?.private_code_value ?? null,
73+
};
74+
75+
return { ...rest, latestParentStopPlaceChangeData };
76+
}

ui/src/components/stop-registry/terminals/hooks/useGetTerminalDetails.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
useGetParentStopPlaceDetailsQuery,
88
} from '../../../../generated/graphql';
99
import {
10+
useGetUserNames,
1011
useObservationDateQueryParam,
1112
useRequiredParams,
1213
} from '../../../../hooks';
@@ -15,6 +16,7 @@ import {
1516
getParentStopPlaceDetailsForEnrichment,
1617
getParentStopPlacesFromQueryResult,
1718
} from '../../../../utils';
19+
import { useGetLatestParentStopPlaceChange } from './useGetParentStopPlaceChangeHistory';
1820

1921
const GQL_GET_PARENT_STOP_PLACE_DETAILS = gql`
2022
query getParentStopPlaceDetails(
@@ -169,21 +171,37 @@ const GQL_GET_PARENT_STOP_PLACE_DETAILS = gql`
169171

170172
export function getEnrichedParentStopPlace(
171173
parentStopPlace: ParentStopPlaceDetailsFragment | null | undefined,
174+
getUserNameById?: (userId: string | null | undefined) => string | null,
175+
parentStopPlaceChangeData?: {
176+
changed: string | null;
177+
changedBy: string | null;
178+
},
172179
): EnrichedParentStopPlace | null {
173180
if (!parentStopPlace) {
174181
return null;
175182
}
176183

184+
const changeData = parentStopPlaceChangeData;
185+
const changedByUserName = getUserNameById?.(changeData?.changedBy);
186+
177187
return {
178188
...parentStopPlace,
179189
...getParentStopPlaceDetailsForEnrichment(
180190
parentStopPlace as StopRegistryParentStopPlace,
181191
),
192+
changed: changeData?.changed,
193+
changedBy: changeData?.changedBy,
194+
changedByUserName,
182195
} as EnrichedParentStopPlace;
183196
}
184197

185198
function useGetParentStopPlaceDetailsByWhere(
186199
where: StopsDatabaseStopPlaceNewestVersionBoolExp | null,
200+
getUserNameById: (userId: string | null | undefined) => string | null,
201+
parentStopPlaceChangeData?: {
202+
changed: string | null;
203+
changedBy: string | null;
204+
},
187205
) {
188206
const { data, ...rest } = useGetParentStopPlaceDetailsQuery(
189207
where ? { variables: { where } } : { skip: true },
@@ -194,8 +212,13 @@ function useGetParentStopPlaceDetailsByWhere(
194212
data?.stopsDb?.newestVersion.at(0)?.TiamatStopPlace,
195213
).at(0);
196214
const parentStopPlaceDetails = useMemo(
197-
() => getEnrichedParentStopPlace(rawParentStopPlace),
198-
[rawParentStopPlace],
215+
() =>
216+
getEnrichedParentStopPlace(
217+
rawParentStopPlace,
218+
getUserNameById,
219+
parentStopPlaceChangeData,
220+
),
221+
[rawParentStopPlace, getUserNameById, parentStopPlaceChangeData],
199222
);
200223

201224
return { ...rest, parentStopPlaceDetails };
@@ -229,12 +252,22 @@ function useGetParentStopPlaceDetailsWhereConditionsWithDate(): StopsDatabaseSto
229252
}
230253

231254
export function useGetParentStopPlaceDetails() {
255+
const { getUserNameById } = useGetUserNames();
256+
257+
const { latestParentStopPlaceChangeData } = useGetLatestParentStopPlaceChange(
258+
useGetParentStopPlaceDetailsWhereConditions(),
259+
);
260+
232261
const validResult = useGetParentStopPlaceDetailsByWhere(
233262
useGetParentStopPlaceDetailsWhereConditionsWithDate(),
263+
getUserNameById,
264+
latestParentStopPlaceChangeData,
234265
);
235266

236267
const fallbackResult = useGetParentStopPlaceDetailsByWhere(
237268
useGetParentStopPlaceDetailsWhereConditions(),
269+
getUserNameById,
270+
latestParentStopPlaceChangeData,
238271
);
239272

240273
const hasValidData = !!validResult.parentStopPlaceDetails;

ui/src/generated/graphql.tsx

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

0 commit comments

Comments
 (0)