Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 77ea190

Browse files
committed
fix: non admin users permissions to check birthday
1 parent b89a622 commit 77ea190

3 files changed

Lines changed: 61 additions & 23 deletions

File tree

apps/admin-ui/src/component/BirthDate.tsx

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@ import { formatDate } from "@/common/util";
77
import { HorisontalFlex } from "@/styles/layout";
88
import { base64encode } from "common/src/helpers";
99

10-
type Props = {
11-
userPk: number;
12-
};
13-
1410
// NOTE separate query because all requests for dateOfBirth are logged
1511
// so don't make them automatically or inside other queries
16-
const DATE_OF_BIRTH_QUERY = gql`
17-
query getDateOfBirth($id: ID!) {
18-
user(id: $id) {
19-
pk
20-
dateOfBirth
12+
const RESERVATION_DATE_OF_BIRTH_QUERY = gql`
13+
query getReservationDateOfBirth($id: ID!) {
14+
reservation(id: $id) {
15+
user {
16+
pk
17+
dateOfBirth
18+
}
19+
}
20+
}
21+
`;
22+
23+
const APPLICATION_DATE_OF_BIRTH_QUERY = gql`
24+
query getApplicationDateOfBirth($id: ID!) {
25+
application(id: $id) {
26+
user {
27+
pk
28+
dateOfBirth
29+
}
2130
}
2231
}
2332
`;
@@ -31,29 +40,58 @@ const Button = styled.button`
3140
text-decoration: underline;
3241
`;
3342

43+
type Props =
44+
| {
45+
reservationPk: number;
46+
}
47+
| {
48+
applicationPk: number;
49+
};
50+
3451
/// Component for toggling the visibility of the user's birth date
35-
/// @param userPk - pk of the user
52+
/// Queries through reservation or application because of permission checks (most users are not allowed query users api)
53+
/// @param reservationPk - the pk of the reservation
54+
/// @param applicationPk - the pk of the application
3655
/// Only makes the query if the user clicks the show button to minimise logging
37-
export function BirthDate({ userPk }: Props): JSX.Element {
56+
export function BirthDate(props: Props): JSX.Element {
3857
const [visible, setVisible] = useState(false);
3958

40-
const typename = "UserNode";
41-
const id = base64encode(`${typename}:${userPk}`);
59+
const reservationPk = "reservationPk" in props ? props.reservationPk : null;
60+
const applicationPk = "applicationPk" in props ? props.applicationPk : null;
61+
4262
const {
43-
data,
44-
loading: isLoading,
45-
error,
46-
} = useQuery<Query, QueryUserArgs>(DATE_OF_BIRTH_QUERY, {
63+
data: dataReservation,
64+
loading: isReservationLoading,
65+
error: errorReservation,
66+
} = useQuery<Query, QueryUserArgs>(RESERVATION_DATE_OF_BIRTH_QUERY, {
4767
variables: {
48-
id,
68+
id: base64encode(`ReservationNode:${reservationPk}`),
4969
},
50-
fetchPolicy: "network-only",
51-
skip: !userPk || !visible,
70+
fetchPolicy: "no-cache",
71+
skip: !reservationPk || !visible,
72+
});
73+
74+
const {
75+
data: dataApplication,
76+
loading: isApplicationLoading,
77+
error: errorApplication,
78+
} = useQuery<Query, QueryUserArgs>(APPLICATION_DATE_OF_BIRTH_QUERY, {
79+
variables: {
80+
id: base64encode(`ApplicationNode:${applicationPk}`),
81+
},
82+
fetchPolicy: "no-cache",
83+
skip: !applicationPk || !visible,
5284
});
5385

5486
const { t } = useTranslation();
5587

56-
const dateOfBirth = data?.user?.dateOfBirth;
88+
const data = "reservationPk" in props ? dataReservation : dataApplication;
89+
const isLoading =
90+
"reservationPk" in props ? isReservationLoading : isApplicationLoading;
91+
const error = "reservationPk" in props ? errorReservation : errorApplication;
92+
93+
const user = data?.reservation?.user || data?.application?.user;
94+
const dateOfBirth = user?.dateOfBirth;
5795

5896
const hideLabel = t("RequestedReservation.hideBirthDate");
5997
const showLabel = t("RequestedReservation.showBirthDate");

apps/admin-ui/src/component/reservations/requested/RequestedReservation.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ const RequestedReservation = ({
590590
/>
591591
<ApplicationData
592592
label={t("RequestedReservation.birthDate")}
593-
data={<BirthDate userPk={reservation?.user?.pk ?? 0} />}
593+
data={<BirthDate reservationPk={reservation?.pk ?? 0} />}
594594
/>
595595
<ApplicationData
596596
label={t("RequestedReservation.addressStreet")}

apps/admin-ui/src/spa/applications/[id]/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ function ApplicationDetails({
992992
/>
993993
<ValueBox
994994
label={t("Application.headings.userBirthDate")}
995-
value={<BirthDate userPk={application.user?.pk ?? 0} />}
995+
value={<BirthDate applicationPk={application.pk ?? 0} />}
996996
/>
997997
</EventProps>
998998
<H4>{t("Application.contactPersonInformation")}</H4>

0 commit comments

Comments
 (0)