Skip to content

Commit

Permalink
fix: edit missing interval validations
Browse files Browse the repository at this point in the history
  • Loading branch information
joonatank committed May 6, 2024
1 parent 253df37 commit 43738b2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 14 deletions.
6 changes: 6 additions & 0 deletions apps/admin-ui/src/i18n/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,12 @@ const translations: ITranslations = {
"reservationBeginsDate must be before end": [
"Varausajan tulee alkaa ennen kuin se päättyy.",
],
"duration can't be less than reservation start interval": [
"Kesto ei voi olla pienempi kuin varauksen alkamisväli.",
],
"duration must be a multiple of the reservation start interval": [
"Keston on oltava varauksen alkamisvälin kerrannainen",
],
},
},
level: {
Expand Down
18 changes: 15 additions & 3 deletions apps/admin-ui/src/schemas/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReservationStartInterval } from "common/types/gql-types";

export const intervalToNumber = (i: ReservationStartInterval) => {
export function intervalToNumber(i: ReservationStartInterval) {
switch (i) {
case ReservationStartInterval.Interval_15Mins:
return 15;
Expand All @@ -10,7 +10,19 @@ export const intervalToNumber = (i: ReservationStartInterval) => {
return 60;
case ReservationStartInterval.Interval_90Mins:
return 90;
case ReservationStartInterval.Interval_120Mins:
return 120;
case ReservationStartInterval.Interval_180Mins:
return 180;
case ReservationStartInterval.Interval_240Mins:
return 240;
case ReservationStartInterval.Interval_300Mins:
return 300;
case ReservationStartInterval.Interval_360Mins:
return 360;
case ReservationStartInterval.Interval_420Mins:
return 420;
default:
return 0;
throw new Error(`Unknown interval: ${i}`);
}
};
}
66 changes: 56 additions & 10 deletions apps/admin-ui/src/spa/ReservationUnit/edit/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
checkTimeStringFormat,
} from "common/src/schemas/schemaCommon";
import { constructApiDate } from "@/helpers";
import { intervalToNumber } from "@/schemas/utils";

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

Expand Down Expand Up @@ -442,6 +443,61 @@ export const ReservationUnitEditSchema = z
validateSeasonalTimes(v.seasons, ctx);
}

// Drafts require this validation
if (v.minReservationDuration != null && v.maxReservationDuration != null) {
if (v.minReservationDuration > v.maxReservationDuration) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Min reservation duration must be less than max duration",
path: ["maxReservationDuration"],
});
}
}

if (v.minReservationDuration != null) {
const minDurationMinutes = Math.floor(v.minReservationDuration / 60);
if (minDurationMinutes < intervalToNumber(v.reservationStartInterval)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "duration can't be less than reservation start interval",
path: ["minReservationDuration"],
});
}
if (
minDurationMinutes % intervalToNumber(v.reservationStartInterval) !==
0
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
"duration must be a multiple of the reservation start interval",
path: ["minReservationDuration"],
});
}
}

if (v.maxReservationDuration != null) {
const maxDurationMinutes = Math.floor(v.maxReservationDuration / 60);
if (
maxDurationMinutes % intervalToNumber(v.reservationStartInterval) !==
0
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
"duration must be a multiple of the reservation start interval",
path: ["maxReservationDuration"],
});
}
if (maxDurationMinutes < intervalToNumber(v.reservationStartInterval)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "duration can't be less than reservation start interval",
path: ["maxReservationDuration"],
});
}
}

if (v.isDraft) {
return;
}
Expand Down Expand Up @@ -498,16 +554,6 @@ export const ReservationUnitEditSchema = z
}
}

if (v.minReservationDuration != null && v.maxReservationDuration != null) {
if (v.minReservationDuration > v.maxReservationDuration) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Min reservation duration must be less than max duration",
path: ["maxReservationDuration"],
});
}
}

if (v.hasScheduledPublish) {
validateDateTimeInterval({
beginDate: v.hasPublishBegins ? v.publishBeginsDate : "",
Expand Down
29 changes: 28 additions & 1 deletion apps/admin-ui/src/spa/ReservationUnit/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,34 @@ const ReservationUnitEditor = ({
refetch();
return upPk;
} catch (error) {
notifyError(t("ReservationUnitEditor.saveFailed", { error }));
if (
error != null &&
typeof error === "object" &&
"graphQLErrors" in error
) {
const { graphQLErrors } = error;
if (Array.isArray(graphQLErrors) && graphQLErrors.length > 0) {
if ("extensions" in graphQLErrors[0]) {
const { extensions } = graphQLErrors[0];
if ("errors" in extensions) {
const { errors } = extensions;
if (Array.isArray(errors) && errors.length > 0) {
let str = "";
for (const e of errors) {
if ("message" in e) {
str += `${e.message}\n`;
}
}
notifyError(
t("ReservationUnitEditor.saveFailed", { error: str })
);
return undefined;
}
}
}
}
}
notifyError(t("ReservationUnitEditor.saveFailed", { error: "" }));
}
return undefined;
};
Expand Down

0 comments on commit 43738b2

Please sign in to comment.