From 9ca473e64ceebbf375edaa065a3982fd2a75be99 Mon Sep 17 00:00:00 2001 From: Joonatan Kuosa Date: Wed, 24 Apr 2024 08:28:24 +0300 Subject: [PATCH] fix: recurring reservation collision checks --- .../component/my-units/ReservationUnitCalendar.tsx | 4 ++-- apps/admin-ui/src/component/my-units/queries.tsx | 6 ++++-- .../component/reservations/requested/hooks/index.ts | 4 ++-- .../reservations/requested/hooks/queries.tsx | 13 +++++++------ .../requested/hooks/useCheckCollisions.ts | 4 ++-- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/apps/admin-ui/src/component/my-units/ReservationUnitCalendar.tsx b/apps/admin-ui/src/component/my-units/ReservationUnitCalendar.tsx index e754ef78bf..83d72c506d 100644 --- a/apps/admin-ui/src/component/my-units/ReservationUnitCalendar.tsx +++ b/apps/admin-ui/src/component/my-units/ReservationUnitCalendar.tsx @@ -17,7 +17,7 @@ import { getEventBuffers } from "common/src/calendar/util"; import { reservationUrl } from "@/common/urls"; import { useNotification } from "@/context/NotificationContext"; import Legend from "../reservations/requested/Legend"; -import { RESERVATIONS_BY_RESERVATIONUNITS } from "./queries"; +import { RESERVATION_UNIT_CALENDAR_QUERY } from "./queries"; import eventStyleGetter, { legend } from "./eventStyleGetter"; import { PUBLIC_URL } from "@/common/const"; import { getReserveeName } from "../reservations/requested/util"; @@ -96,7 +96,7 @@ export function ReservationUnitCalendar({ const { data, loading: isLoading } = useQuery< Query, ReservationUnitWithAffectingArgs - >(RESERVATIONS_BY_RESERVATIONUNITS, { + >(RESERVATION_UNIT_CALENDAR_QUERY, { fetchPolicy: "network-only", skip: reservationUnitPk === 0, variables: { diff --git a/apps/admin-ui/src/component/my-units/queries.tsx b/apps/admin-ui/src/component/my-units/queries.tsx index 79f242eef8..ec5507a125 100644 --- a/apps/admin-ui/src/component/my-units/queries.tsx +++ b/apps/admin-ui/src/component/my-units/queries.tsx @@ -17,9 +17,11 @@ export const RECURRING_RESERVATION_UNIT_QUERY = gql` } `; -export const RESERVATIONS_BY_RESERVATIONUNITS = gql` +// TODO the fragments that this uses should be combined with the other tab page (my-units) +// This is only used in the ReservationUnitCalendar component. +export const RESERVATION_UNIT_CALENDAR_QUERY = gql` ${RESERVATIONUNIT_RESERVATIONS_FRAGMENT} - query ReservationUnitReservations( + query ReservationUnitCalendar( $id: ID! $pk: Int! $state: [String] diff --git a/apps/admin-ui/src/component/reservations/requested/hooks/index.ts b/apps/admin-ui/src/component/reservations/requested/hooks/index.ts index 7674ac4667..813185d4e2 100644 --- a/apps/admin-ui/src/component/reservations/requested/hooks/index.ts +++ b/apps/admin-ui/src/component/reservations/requested/hooks/index.ts @@ -13,7 +13,7 @@ import { useQuery } from "@apollo/client"; import { toApiDate } from "common/src/common/util"; import { RECURRING_RESERVATION_QUERY, - RESERVATIONS_BY_RESERVATIONUNIT, + RESERVATIONS_BY_RESERVATIONUNITS, SINGLE_RESERVATION_QUERY, } from "./queries"; import { useNotification } from "@/context/NotificationContext"; @@ -75,7 +75,7 @@ export function useReservationData( const typename = "ReservationUnitNode"; const id = base64encode(`${typename}:${reservationUnitPk}`); const { data, ...rest } = useQuery( - RESERVATIONS_BY_RESERVATIONUNIT, + RESERVATIONS_BY_RESERVATIONUNITS, { fetchPolicy: "no-cache", skip: !reservationUnitPk, diff --git a/apps/admin-ui/src/component/reservations/requested/hooks/queries.tsx b/apps/admin-ui/src/component/reservations/requested/hooks/queries.tsx index 0ef511ed7c..5fc759750c 100644 --- a/apps/admin-ui/src/component/reservations/requested/hooks/queries.tsx +++ b/apps/admin-ui/src/component/reservations/requested/hooks/queries.tsx @@ -1,5 +1,4 @@ import { gql } from "@apollo/client"; - import { RESERVATION_COMMON_FRAGMENT, RESERVATION_META_FRAGMENT, @@ -22,16 +21,15 @@ const CALENDAR_RESERVATION_FRAGMENT = gql` type bufferTimeBefore bufferTimeAfter - recurringReservation { - pk - } affectedReservationUnits } `; -export const RESERVATIONS_BY_RESERVATIONUNIT = gql` +// TODO there is two versions of this query. +// This is used in the hooks (collision checks). +export const RESERVATIONS_BY_RESERVATIONUNITS = gql` ${CALENDAR_RESERVATION_FRAGMENT} - query reservationUnit( + query ReservationsByReservationUnit( $id: ID! $pk: Int! $beginDate: Date @@ -117,6 +115,9 @@ export const RECURRING_RESERVATION_QUERY = gql` begin end state + reservationUnit { + pk + } } } } diff --git a/apps/admin-ui/src/component/reservations/requested/hooks/useCheckCollisions.ts b/apps/admin-ui/src/component/reservations/requested/hooks/useCheckCollisions.ts index 3c8271f83b..4f986eddda 100644 --- a/apps/admin-ui/src/component/reservations/requested/hooks/useCheckCollisions.ts +++ b/apps/admin-ui/src/component/reservations/requested/hooks/useCheckCollisions.ts @@ -2,7 +2,7 @@ import { type Query, ReservationTypeChoice } from "common/types/gql-types"; import { useQuery } from "@apollo/client"; import { useNotification } from "@/context/NotificationContext"; import { doesIntervalCollide, reservationToInterval } from "@/helpers"; -import { RESERVATIONS_BY_RESERVATIONUNIT } from "./queries"; +import { RESERVATIONS_BY_RESERVATIONUNITS } from "./queries"; import { base64encode, concatAffectedReservations, @@ -37,7 +37,7 @@ function useCheckCollisions({ const typename = "ReservationUnitNode"; const id = base64encode(`${typename}:${reservationUnitPk}`); const { data, loading } = useQuery( - RESERVATIONS_BY_RESERVATIONUNIT, + RESERVATIONS_BY_RESERVATIONUNITS, { fetchPolicy: "no-cache", skip: !reservationUnitPk || !start || !end,