Skip to content

Commit

Permalink
Added check on create eventtype to see is user has slug that already …
Browse files Browse the repository at this point in the history
…exists (calcom#2757)

* Added check on create eventtype to see is user has slug that already exists, added error check on onError

* revert yarn.lock back

* Catches prisma known error instead of making an additional query

Co-authored-by: zomars <[email protected]>
  • Loading branch information
Mitchell-Moore and zomars authored May 15, 2022
1 parent 2a53614 commit 016023f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 5 additions & 0 deletions apps/web/components/eventtype/CreateEventType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export default function CreateEventTypeButton(props: Props) {
showToast(message, "error");
}

if (err.data?.code === "BAD_REQUEST") {
const message = `${err.data.code}: URL already exists.`;
showToast(message, "error");
}

if (err.data?.code === "UNAUTHORIZED") {
const message = `${err.data.code}: You are not able to create this event`;
showToast(message, "error");
Expand Down
16 changes: 12 additions & 4 deletions apps/web/server/routers/viewer/eventTypes.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { EventTypeCustomInput, MembershipRole, PeriodType, Prisma } from "@prisma/client";
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
import { z } from "zod";

import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug";
import { _DestinationCalendarModel, _EventTypeCustomInputModel, _EventTypeModel } from "@calcom/prisma/zod";
import { stringOrNumber } from "@calcom/prisma/zod-utils";
import { createEventTypeInput } from "@calcom/prisma/zod/custom/eventtype";
import { RecurringEvent } from "@calcom/types/Calendar";

import { createProtectedRouter } from "@server/createRouter";
import { viewerRouter } from "@server/routers/viewer";
Expand Down Expand Up @@ -150,9 +150,17 @@ export const eventTypesRouter = createProtectedRouter()
data.schedulingType = schedulingType;
}

const eventType = await ctx.prisma.eventType.create({ data });

return { eventType };
try {
const eventType = await ctx.prisma.eventType.create({ data });
return { eventType };
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
if (e.code === "P2002" && Array.isArray(e.meta?.target) && e.meta?.target.includes("slug")) {
throw new TRPCError({ code: "BAD_REQUEST", message: "URL Slug already exists for given user." });
}
}
throw e;
}
},
})
// Prevent non-owners to update/delete a team event
Expand Down

0 comments on commit 016023f

Please sign in to comment.