From 77ea190e26f55dfedd5537d4d8295b80d410e2b9 Mon Sep 17 00:00:00 2001 From: Joonatan Kuosa Date: Mon, 13 May 2024 13:09:20 +0300 Subject: [PATCH] fix: non admin users permissions to check birthday --- apps/admin-ui/src/component/BirthDate.tsx | 80 ++++++++++++++----- .../requested/RequestedReservation.tsx | 2 +- .../src/spa/applications/[id]/index.tsx | 2 +- 3 files changed, 61 insertions(+), 23 deletions(-) diff --git a/apps/admin-ui/src/component/BirthDate.tsx b/apps/admin-ui/src/component/BirthDate.tsx index 67e2fc3489..5757e9a49f 100644 --- a/apps/admin-ui/src/component/BirthDate.tsx +++ b/apps/admin-ui/src/component/BirthDate.tsx @@ -7,17 +7,26 @@ import { formatDate } from "@/common/util"; import { HorisontalFlex } from "@/styles/layout"; import { base64encode } from "common/src/helpers"; -type Props = { - userPk: number; -}; - // NOTE separate query because all requests for dateOfBirth are logged // so don't make them automatically or inside other queries -const DATE_OF_BIRTH_QUERY = gql` - query getDateOfBirth($id: ID!) { - user(id: $id) { - pk - dateOfBirth +const RESERVATION_DATE_OF_BIRTH_QUERY = gql` + query getReservationDateOfBirth($id: ID!) { + reservation(id: $id) { + user { + pk + dateOfBirth + } + } + } +`; + +const APPLICATION_DATE_OF_BIRTH_QUERY = gql` + query getApplicationDateOfBirth($id: ID!) { + application(id: $id) { + user { + pk + dateOfBirth + } } } `; @@ -31,29 +40,58 @@ const Button = styled.button` text-decoration: underline; `; +type Props = + | { + reservationPk: number; + } + | { + applicationPk: number; + }; + /// Component for toggling the visibility of the user's birth date -/// @param userPk - pk of the user +/// Queries through reservation or application because of permission checks (most users are not allowed query users api) +/// @param reservationPk - the pk of the reservation +/// @param applicationPk - the pk of the application /// Only makes the query if the user clicks the show button to minimise logging -export function BirthDate({ userPk }: Props): JSX.Element { +export function BirthDate(props: Props): JSX.Element { const [visible, setVisible] = useState(false); - const typename = "UserNode"; - const id = base64encode(`${typename}:${userPk}`); + const reservationPk = "reservationPk" in props ? props.reservationPk : null; + const applicationPk = "applicationPk" in props ? props.applicationPk : null; + const { - data, - loading: isLoading, - error, - } = useQuery(DATE_OF_BIRTH_QUERY, { + data: dataReservation, + loading: isReservationLoading, + error: errorReservation, + } = useQuery(RESERVATION_DATE_OF_BIRTH_QUERY, { variables: { - id, + id: base64encode(`ReservationNode:${reservationPk}`), }, - fetchPolicy: "network-only", - skip: !userPk || !visible, + fetchPolicy: "no-cache", + skip: !reservationPk || !visible, + }); + + const { + data: dataApplication, + loading: isApplicationLoading, + error: errorApplication, + } = useQuery(APPLICATION_DATE_OF_BIRTH_QUERY, { + variables: { + id: base64encode(`ApplicationNode:${applicationPk}`), + }, + fetchPolicy: "no-cache", + skip: !applicationPk || !visible, }); const { t } = useTranslation(); - const dateOfBirth = data?.user?.dateOfBirth; + const data = "reservationPk" in props ? dataReservation : dataApplication; + const isLoading = + "reservationPk" in props ? isReservationLoading : isApplicationLoading; + const error = "reservationPk" in props ? errorReservation : errorApplication; + + const user = data?.reservation?.user || data?.application?.user; + const dateOfBirth = user?.dateOfBirth; const hideLabel = t("RequestedReservation.hideBirthDate"); const showLabel = t("RequestedReservation.showBirthDate"); diff --git a/apps/admin-ui/src/component/reservations/requested/RequestedReservation.tsx b/apps/admin-ui/src/component/reservations/requested/RequestedReservation.tsx index 2e11b5d234..b38e80c270 100644 --- a/apps/admin-ui/src/component/reservations/requested/RequestedReservation.tsx +++ b/apps/admin-ui/src/component/reservations/requested/RequestedReservation.tsx @@ -590,7 +590,7 @@ const RequestedReservation = ({ /> } + data={} /> } + value={} />

{t("Application.contactPersonInformation")}