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

Commit 0b984ed

Browse files
committed
fix: type errors from gql api change
1 parent 0232de8 commit 0b984ed

11 files changed

Lines changed: 88 additions & 87 deletions

File tree

apps/admin-ui/src/component/my-units/hooks/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ export function useUnitResources(
117117
};
118118
}
119119

120-
// copy from concatAffectedReservations in helpers.ts because of types
121120
function doesReservationAffectReservationUnit(
122121
reservation: ReservationType,
123122
reservationUnitPk: number

apps/admin-ui/src/component/reservations/hooks/__test__/mocks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ export const mockReservation: ReservationNode = {
265265
bufferTimeBefore: 0,
266266
state: State.Confirmed,
267267
id: base64encode("ReservationNode:1"),
268+
reservationUnit: [],
269+
paymentOrder: [],
268270
workingMemo: "empty",
269271
handlingDetails: "",
270272
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type ReservationType = Omit<
1818
"user"
1919
>;
2020

21-
type CalendarEventType = CalendarEvent<ReservationType>;
21+
export type CalendarEventType = CalendarEvent<ReservationType>;
2222
type Props = {
2323
reservation: ReservationType;
2424
refetch: (focusDate?: Date) => void;

apps/admin-ui/src/component/reservations/requested/hooks/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import {
66
useReservationDenyReasonsQuery,
77
useReservationsByReservationUnitQuery,
88
useRecurringReservationQuery,
9+
ReservationUnitNode,
910
} from "@gql/gql-types";
1011
import { useTranslation } from "react-i18next";
1112
import { toApiDate } from "common/src/common/util";
1213
import { useNotification } from "@/context/NotificationContext";
1314
import { base64encode, filterNonNullable } from "common/src/helpers";
15+
import { type CalendarEventType } from "../Calendar";
1416

1517
export { default as useCheckCollisions } from "./useCheckCollisions";
1618

@@ -25,15 +27,24 @@ const getReservationTitle = (r: CalendarReservationFragment) =>
2527
r.reserveeName ?? "";
2628

2729
function convertReservationToCalendarEvent(
28-
r: CalendarReservationFragment,
30+
// NOTE funky because we are converting affectedReservations also and they don't have reservationUnit
31+
// but these are passed to event handlers that allow changing the reservation that requires a reservationUnit
32+
// affected don't have event handlers so empty reservationUnit is fine
33+
r: CalendarReservationFragment & { reservationUnit?: ReservationUnitNode[] },
2934
blockedName: string
30-
) {
35+
): CalendarEventType {
3136
const title = getEventName(r.type, getReservationTitle(r), blockedName);
37+
38+
const reservationUnit =
39+
"reservationUnit" in r && r.reservationUnit != null
40+
? r.reservationUnit
41+
: [];
3242
return {
3343
title,
3444
event: {
3545
...r,
3646
name: r.name?.trim() !== "" ? r.name : "No name",
47+
reservationUnit,
3748
},
3849
// TODO use zod for datetime conversions
3950
start: new Date(r.begin),

apps/admin-ui/src/spa/recurring-reservations/application-rounds/[id]/review/Filters.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ export function Filters({
8989
}));
9090

9191
// event status is shared on two tabs, but allocated only has two options
92-
const eventStatusArrayLong = Object.values(
93-
ApplicationSectionStatusChoice
94-
).filter((x) => x !== ApplicationSectionStatusChoice.Failed);
92+
const eventStatusArrayLong = Object.values(ApplicationSectionStatusChoice);
9593
// TODO these are "declined" / "approved" but the decline functionality is not implemented
9694
// so disabling the filter for now (there is no backend filter for it nor can it be tested)
9795

apps/admin-ui/src/spa/recurring-reservations/application-rounds/[id]/review/utils.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ export function transformApplicationSectionStatus(
2222
return ApplicationSectionStatusChoice.Unallocated;
2323
case ApplicationSectionStatusChoice.InAllocation:
2424
return ApplicationSectionStatusChoice.InAllocation;
25-
case ApplicationSectionStatusChoice.Failed:
26-
return ApplicationSectionStatusChoice.Failed;
27-
case ApplicationSectionStatusChoice.Reserved:
28-
return ApplicationSectionStatusChoice.Reserved;
25+
case ApplicationSectionStatusChoice.Rejected:
26+
return ApplicationSectionStatusChoice.Rejected;
2927
default:
3028
return undefined;
3129
}

apps/ui/modules/__tests__/reservation.test.ts

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ const reservation: ReservationNode = {
202202
price: "0",
203203
bufferTimeBefore: 0,
204204
bufferTimeAfter: 0,
205+
paymentOrder: [],
205206
begin: addHours(startOfToday(), 34).toISOString(),
206207
end: addHours(startOfToday(), 35).toISOString(),
207208
reservationUnit: [reservationUnit],
@@ -559,7 +560,12 @@ describe("canReservationBeChanged", () => {
559560
});
560561

561562
test("returns true with default data", () => {
562-
expect(canReservationTimeBeChanged({ reservation })).toStrictEqual([true]);
563+
expect(
564+
canReservationTimeBeChanged({
565+
reservation,
566+
reservationUnit,
567+
})
568+
).toStrictEqual([true]);
563569
});
564570

565571
test("returns false with non-confirmed reservation", () => {
@@ -569,6 +575,7 @@ describe("canReservationBeChanged", () => {
569575
...reservation,
570576
state: State.Created,
571577
},
578+
reservationUnit,
572579
})
573580
).toStrictEqual([false, "RESERVATION_MODIFICATION_NOT_ALLOWED"]);
574581
});
@@ -580,76 +587,81 @@ describe("canReservationBeChanged", () => {
580587
...reservation,
581588
begin: addHours(new Date(), -1).toISOString(),
582589
},
590+
reservationUnit,
583591
} as CanReservationBeChangedProps)
584592
).toStrictEqual([false, "RESERVATION_BEGIN_IN_PAST"]);
585593
});
586594

587595
test("handles cancellation rule check", () => {
596+
const reservationUnit1: ReservationUnitNode = {
597+
...reservationUnit,
598+
cancellationRule: null,
599+
};
588600
expect(
589601
canReservationTimeBeChanged({
590602
reservation: {
591603
...reservation,
592-
reservationUnit: [
593-
{
594-
...reservationUnit,
595-
cancellationRule: null,
596-
},
597-
],
604+
reservationUnit: [reservationUnit1],
598605
},
606+
reservationUnit: reservationUnit1,
599607
} as CanReservationBeChangedProps)
600608
).toStrictEqual([false, "RESERVATION_MODIFICATION_NOT_ALLOWED"]);
601609

610+
const reservationUnit2: ReservationUnitNode = {
611+
...reservationUnit,
612+
cancellationRule: {
613+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
614+
...reservationUnit.cancellationRule!,
615+
needsHandling: true,
616+
},
617+
};
602618
expect(
603619
canReservationTimeBeChanged({
604620
reservation: {
605621
...reservation,
606-
reservationUnit: [
607-
{
608-
...reservationUnit,
609-
cancellationRule: {
610-
needsHandling: true,
611-
},
612-
},
613-
],
622+
reservationUnit: [reservationUnit2],
614623
},
624+
reservationUnit: reservationUnit2,
615625
} as CanReservationBeChangedProps)
616626
).toStrictEqual([false, "RESERVATION_MODIFICATION_NOT_ALLOWED"]);
617627
});
618628

619629
test("handles cancellation rule buffer check", () => {
630+
const reservationUnit1: ReservationUnitNode = {
631+
...reservationUnit,
632+
cancellationRule: {
633+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
634+
...reservationUnit.cancellationRule!,
635+
canBeCancelledTimeBefore: 3000,
636+
},
637+
};
620638
expect(
621639
canReservationTimeBeChanged({
622640
reservation: {
623641
...reservation,
624642
begin: addHours(new Date(), 1).toISOString(),
625-
reservationUnit: [
626-
{
627-
...reservationUnit,
628-
cancellationRule: {
629-
...reservationUnit.cancellationRule,
630-
canBeCancelledTimeBefore: 3000,
631-
},
632-
},
633-
],
643+
reservationUnit: [reservationUnit1],
634644
},
645+
reservationUnit: reservationUnit1,
635646
} as CanReservationBeChangedProps)
636647
).toStrictEqual([true]);
637648

649+
const reservationUnit2: ReservationUnitNode = {
650+
...reservationUnit,
651+
cancellationRule: {
652+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
653+
...reservationUnit.cancellationRule!,
654+
canBeCancelledTimeBefore: 3601,
655+
},
656+
};
638657
expect(
639658
canReservationTimeBeChanged({
640659
reservation: {
641660
...reservation,
642661
begin: addHours(new Date(), 1).toISOString(),
643-
reservationUnit: [
644-
{
645-
...reservationUnit,
646-
cancellationRule: {
647-
...reservationUnit.cancellationRule,
648-
canBeCancelledTimeBefore: 3601,
649-
},
650-
},
651-
],
662+
reservationUnit: [reservationUnit2],
652663
},
664+
reservationUnit: reservationUnit2,
653665
} as CanReservationBeChangedProps)
654666
).toStrictEqual([false, "CANCELLATION_TIME_PAST"]);
655667
});
@@ -658,6 +670,7 @@ describe("canReservationBeChanged", () => {
658670
expect(
659671
canReservationTimeBeChanged({
660672
reservation: { ...reservation, isHandled: true },
673+
reservationUnit,
661674
} as CanReservationBeChangedProps)
662675
).toStrictEqual([false, "RESERVATION_MODIFICATION_NOT_ALLOWED"]);
663676
});
@@ -667,6 +680,7 @@ describe("canReservationBeChanged", () => {
667680
canReservationTimeBeChanged({
668681
reservation,
669682
newReservation: { ...reservation, price: "2.02" },
683+
reservationUnit,
670684
} as CanReservationBeChangedProps)
671685
).toStrictEqual([false, "RESERVATION_MODIFICATION_NOT_ALLOWED"]);
672686
});

apps/ui/modules/reservation.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ type IsWithinCancellationPeriodReservationT = Pick<
110110
reservationUnit?: Array<{
111111
cancellationRule?: Pick<
112112
NonNullable<ReservationUnitNode["cancellationRule"]>,
113-
"canBeCancelledTimeBefore"
113+
"canBeCancelledTimeBefore" | "needsHandling"
114114
> | null;
115115
}> | null;
116116
};
@@ -140,10 +140,8 @@ function isReservationWithinCancellationPeriod(
140140
}
141141

142142
export function canUserCancelReservation(
143-
reservation: Pick<
144-
NonNullable<ReservationNodeT>,
145-
"state" | "reservationUnit" | "begin"
146-
>,
143+
reservation: IsWithinCancellationPeriodReservationT &
144+
Pick<NonNullable<ReservationNodeT>, "state">,
147145
skipTimeCheck = false
148146
): boolean {
149147
const reservationUnit = reservation.reservationUnit?.[0];
@@ -383,7 +381,7 @@ export const canReservationTimeBeChanged = ({
383381
reservationUnit,
384382
activeApplicationRounds = [],
385383
}: CanReservationBeChangedProps): [boolean, string?] => {
386-
if (reservation == null) {
384+
if (reservation == null || reservationUnit == null) {
387385
return [false];
388386
}
389387
// existing reservation state is not CONFIRMED

apps/ui/pages/reservations/[id]/edit.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ import {
1212
type ReservationQuery,
1313
type ReservationQueryVariables,
1414
} from "@gql/gql-types";
15-
import {
16-
base64encode,
17-
concatAffectedReservations,
18-
filterNonNullable,
19-
} from "common/src/helpers";
15+
import { base64encode, filterNonNullable } from "common/src/helpers";
2016
import { toApiDate } from "common/src/common/util";
2117
import { addYears } from "date-fns";
2218
import { RELATED_RESERVATION_STATES } from "common/src/const";
@@ -71,16 +67,25 @@ export async function getServerSideProps(ctx: GetServerSidePropsContext) {
7167

7268
const timespans = filterNonNullable(reservationUnit?.reservableTimeSpans);
7369
const reservableTimeSpans = timespans;
70+
const doesReservationAffectReservationUnit = (
71+
res: (typeof affectingReservations)[0],
72+
reservationUnitPk: number
73+
) => {
74+
return res.affectedReservationUnits?.some(
75+
(pk_) => pk_ === reservationUnitPk
76+
);
77+
};
7478
const reservationSet = filterNonNullable(
7579
reservationUnitData?.reservationUnit?.reservationSet
7680
);
7781
const affectingReservations = filterNonNullable(
7882
reservationUnitData?.affectingReservations
7983
);
80-
const reservations = concatAffectedReservations(
81-
reservationSet,
82-
affectingReservations,
83-
resUnitPk ?? 0
84+
85+
const reservations = reservationSet?.concat(
86+
affectingReservations?.filter((y) =>
87+
doesReservationAffectReservationUnit(y, resUnitPk ?? 0)
88+
) ?? []
8489
);
8590

8691
// TODO check for nulls and return notFound if necessary

packages/common/src/helpers.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ImageFragment, Maybe, ReservationNode } from "../gql/gql-types";
1+
import type { ImageFragment, Maybe } from "../gql/gql-types";
22
import { pixel } from "./common/style";
33

44
export function filterNonNullable<T>(
@@ -87,30 +87,3 @@ function getImageSourceWithoutDefault(
8787
return null;
8888
}
8989
}
90-
91-
// concat is necessary because if the reservation is only for one reservationUnit it's not included in the affectingReservations
92-
// NOTE concat is questionable (it creates duplicates), but if there is no common spaces the affecingReservations is empty
93-
// i.e. the reservationUnit doesn't have a space but has reservations (might be other cases too)
94-
// NOTE some users could be changed to use regular concat instead (if there is only a single reservationUnit the filter check is not needed).
95-
export function concatAffectedReservations(
96-
reservationSet: ReservationNode[],
97-
affectingReservations: ReservationNode[],
98-
reservationUnitPk: number
99-
) {
100-
return filterNonNullable(
101-
reservationSet?.concat(
102-
affectingReservations?.filter((y) =>
103-
doesReservationAffectReservationUnit(y, reservationUnitPk)
104-
) ?? []
105-
)
106-
);
107-
}
108-
109-
function doesReservationAffectReservationUnit(
110-
reservation: ReservationNode,
111-
reservationUnitPk: number
112-
) {
113-
return reservation.affectedReservationUnits?.some(
114-
(pk) => pk === reservationUnitPk
115-
);
116-
}

0 commit comments

Comments
 (0)