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

Commit 43738b2

Browse files
committed
fix: edit missing interval validations
1 parent 253df37 commit 43738b2

4 files changed

Lines changed: 105 additions & 14 deletions

File tree

apps/admin-ui/src/i18n/messages.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,12 @@ const translations: ITranslations = {
816816
"reservationBeginsDate must be before end": [
817817
"Varausajan tulee alkaa ennen kuin se päättyy.",
818818
],
819+
"duration can't be less than reservation start interval": [
820+
"Kesto ei voi olla pienempi kuin varauksen alkamisväli.",
821+
],
822+
"duration must be a multiple of the reservation start interval": [
823+
"Keston on oltava varauksen alkamisvälin kerrannainen",
824+
],
819825
},
820826
},
821827
level: {

apps/admin-ui/src/schemas/utils.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReservationStartInterval } from "common/types/gql-types";
22

3-
export const intervalToNumber = (i: ReservationStartInterval) => {
3+
export function intervalToNumber(i: ReservationStartInterval) {
44
switch (i) {
55
case ReservationStartInterval.Interval_15Mins:
66
return 15;
@@ -10,7 +10,19 @@ export const intervalToNumber = (i: ReservationStartInterval) => {
1010
return 60;
1111
case ReservationStartInterval.Interval_90Mins:
1212
return 90;
13+
case ReservationStartInterval.Interval_120Mins:
14+
return 120;
15+
case ReservationStartInterval.Interval_180Mins:
16+
return 180;
17+
case ReservationStartInterval.Interval_240Mins:
18+
return 240;
19+
case ReservationStartInterval.Interval_300Mins:
20+
return 300;
21+
case ReservationStartInterval.Interval_360Mins:
22+
return 360;
23+
case ReservationStartInterval.Interval_420Mins:
24+
return 420;
1325
default:
14-
return 0;
26+
throw new Error(`Unknown interval: ${i}`);
1527
}
16-
};
28+
}

apps/admin-ui/src/spa/ReservationUnit/edit/form.ts

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
checkTimeStringFormat,
2525
} from "common/src/schemas/schemaCommon";
2626
import { constructApiDate } from "@/helpers";
27+
import { intervalToNumber } from "@/schemas/utils";
2728

2829
export const PaymentTypes = ["ONLINE", "INVOICE", "ON_SITE"] as const;
2930

@@ -442,6 +443,61 @@ export const ReservationUnitEditSchema = z
442443
validateSeasonalTimes(v.seasons, ctx);
443444
}
444445

446+
// Drafts require this validation
447+
if (v.minReservationDuration != null && v.maxReservationDuration != null) {
448+
if (v.minReservationDuration > v.maxReservationDuration) {
449+
ctx.addIssue({
450+
code: z.ZodIssueCode.custom,
451+
message: "Min reservation duration must be less than max duration",
452+
path: ["maxReservationDuration"],
453+
});
454+
}
455+
}
456+
457+
if (v.minReservationDuration != null) {
458+
const minDurationMinutes = Math.floor(v.minReservationDuration / 60);
459+
if (minDurationMinutes < intervalToNumber(v.reservationStartInterval)) {
460+
ctx.addIssue({
461+
code: z.ZodIssueCode.custom,
462+
message: "duration can't be less than reservation start interval",
463+
path: ["minReservationDuration"],
464+
});
465+
}
466+
if (
467+
minDurationMinutes % intervalToNumber(v.reservationStartInterval) !==
468+
0
469+
) {
470+
ctx.addIssue({
471+
code: z.ZodIssueCode.custom,
472+
message:
473+
"duration must be a multiple of the reservation start interval",
474+
path: ["minReservationDuration"],
475+
});
476+
}
477+
}
478+
479+
if (v.maxReservationDuration != null) {
480+
const maxDurationMinutes = Math.floor(v.maxReservationDuration / 60);
481+
if (
482+
maxDurationMinutes % intervalToNumber(v.reservationStartInterval) !==
483+
0
484+
) {
485+
ctx.addIssue({
486+
code: z.ZodIssueCode.custom,
487+
message:
488+
"duration must be a multiple of the reservation start interval",
489+
path: ["maxReservationDuration"],
490+
});
491+
}
492+
if (maxDurationMinutes < intervalToNumber(v.reservationStartInterval)) {
493+
ctx.addIssue({
494+
code: z.ZodIssueCode.custom,
495+
message: "duration can't be less than reservation start interval",
496+
path: ["maxReservationDuration"],
497+
});
498+
}
499+
}
500+
445501
if (v.isDraft) {
446502
return;
447503
}
@@ -498,16 +554,6 @@ export const ReservationUnitEditSchema = z
498554
}
499555
}
500556

501-
if (v.minReservationDuration != null && v.maxReservationDuration != null) {
502-
if (v.minReservationDuration > v.maxReservationDuration) {
503-
ctx.addIssue({
504-
code: z.ZodIssueCode.custom,
505-
message: "Min reservation duration must be less than max duration",
506-
path: ["maxReservationDuration"],
507-
});
508-
}
509-
}
510-
511557
if (v.hasScheduledPublish) {
512558
validateDateTimeInterval({
513559
beginDate: v.hasPublishBegins ? v.publishBeginsDate : "",

apps/admin-ui/src/spa/ReservationUnit/edit/index.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,34 @@ const ReservationUnitEditor = ({
20212021
refetch();
20222022
return upPk;
20232023
} catch (error) {
2024-
notifyError(t("ReservationUnitEditor.saveFailed", { error }));
2024+
if (
2025+
error != null &&
2026+
typeof error === "object" &&
2027+
"graphQLErrors" in error
2028+
) {
2029+
const { graphQLErrors } = error;
2030+
if (Array.isArray(graphQLErrors) && graphQLErrors.length > 0) {
2031+
if ("extensions" in graphQLErrors[0]) {
2032+
const { extensions } = graphQLErrors[0];
2033+
if ("errors" in extensions) {
2034+
const { errors } = extensions;
2035+
if (Array.isArray(errors) && errors.length > 0) {
2036+
let str = "";
2037+
for (const e of errors) {
2038+
if ("message" in e) {
2039+
str += `${e.message}\n`;
2040+
}
2041+
}
2042+
notifyError(
2043+
t("ReservationUnitEditor.saveFailed", { error: str })
2044+
);
2045+
return undefined;
2046+
}
2047+
}
2048+
}
2049+
}
2050+
}
2051+
notifyError(t("ReservationUnitEditor.saveFailed", { error: "" }));
20252052
}
20262053
return undefined;
20272054
};

0 commit comments

Comments
 (0)