Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import prismaMock from "../../../../../../tests/libs/__mocks__/prismaMock";

import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
import { describe, it, expect, vi, beforeEach } from "vitest";

import { TRPCError } from "@trpc/server";

import { duplicateHandler } from "./duplicate.handler";

vi.mock("@calcom/prisma", () => ({
default: prismaMock,
}));
vi.mock("@calcom/lib/server/repository/eventTypeRepository");

describe("duplicateHandler", () => {
const ctx = { user: { id: 1, profile: { id: 1 } } } as any;
const input = { id: 123, slug: "test-event", title: "Test", description: "Test", length: 30, teamId: null };
const eventType = { id: 123, userId: 1, teamId: null, users: [{ id: 1 }] };

beforeEach(() => {
vi.resetAllMocks();
prismaMock.eventType.findUnique.mockResolvedValue(eventType);
});

it("should throw BAD_REQUEST in case of unique constraint violation", async () => {
const { EventTypeRepository } = await import("@calcom/lib/server/repository/eventTypeRepository");
vi.mocked(EventTypeRepository).mockImplementation(
() =>
({
create: vi.fn().mockRejectedValue(
new PrismaClientKnownRequestError("Unique constraint failed", {
code: "P2002",
clientVersion: "mockedVersion",
})
),
} as any)
);

await expect(duplicateHandler({ ctx, input })).rejects.toThrow(
new TRPCError({
code: "BAD_REQUEST",
message: "Unique constraint violation while creating a duplicate event.",
})
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ export const duplicateHandler = async ({ ctx, input }: DuplicateOptions) => {
eventType: newEventType,
};
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
// unique constraint violation
throw new TRPCError({
code: "BAD_REQUEST",
message: "Unique constraint violation while creating a duplicate event.",
});
}
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Error duplicating event type ${error}` });
}
};
Loading