refactor: migrate schedule utils from TRPC to ScheduleRepository to break circular dependency (#24764)
* remove util files * wip * wip * wip * wip * wip * wip * add test file * refactors * refactors * fix type error * simplify * host repository test * di
This commit is contained in:
1 parent
eb01e2f9d8
commit
09a4247aa1
13 files changed
+210
-147
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { ScheduleRepository } from "@calcom/features/schedules/repositories/ScheduleRepository";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../types";
|
||||
import { getDefaultScheduleId } from "./util";
|
||||
|
||||
type ListOptions = {
|
||||
ctx: {
|
||||
@@ -37,7 +37,8 @@ export const listHandler = async ({ ctx }: ListOptions) => {
|
||||
|
||||
let defaultScheduleId: number | null;
|
||||
try {
|
||||
defaultScheduleId = await getDefaultScheduleId(user.id, prisma);
|
||||
const scheduleRepository = new ScheduleRepository(prisma);
|
||||
defaultScheduleId = await scheduleRepository.getDefaultScheduleId(user.id);
|
||||
|
||||
if (!user.defaultScheduleId) {
|
||||
await prisma.user.update({
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ScheduleRepository } from "@calcom/features/schedules/repositories/ScheduleRepository";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../../types";
|
||||
import type { TGetInputSchema } from "./get.schema";
|
||||
@@ -11,7 +12,8 @@ type GetOptions = {
|
||||
};
|
||||
|
||||
export const getHandler = async ({ ctx, input }: GetOptions) => {
|
||||
return await ScheduleRepository.findDetailedScheduleById({
|
||||
const scheduleRepo = new ScheduleRepository(prisma);
|
||||
return await scheduleRepo.findDetailedScheduleById({
|
||||
scheduleId: input.scheduleId,
|
||||
isManagedEventType: input.isManagedEventType,
|
||||
userId: ctx.user.id,
|
||||
|
||||
+3
-2
@@ -1,3 +1,4 @@
|
||||
import { ScheduleRepository } from "@calcom/features/schedules/repositories/ScheduleRepository";
|
||||
import { hasReadPermissionsForUserId } from "@calcom/lib/hasEditPermissionForUser";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
@@ -5,7 +6,6 @@ import { prisma } from "@calcom/prisma";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../../types";
|
||||
import { getDefaultScheduleId } from "../util";
|
||||
import type { TGetAllByUserIdInputSchema } from "./getAllSchedulesByUserId.schema";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["getAllSchedulesByUserIdHandler"] });
|
||||
@@ -48,7 +48,8 @@ export const getAllSchedulesByUserIdHandler = async ({ ctx, input }: GetOptions)
|
||||
});
|
||||
}
|
||||
|
||||
const defaultScheduleId = await getDefaultScheduleId(input.userId, prisma);
|
||||
const scheduleRepository = new ScheduleRepository(prisma);
|
||||
const defaultScheduleId = await scheduleRepository.getDefaultScheduleId(input.userId);
|
||||
|
||||
return {
|
||||
schedules: schedules.map((schedule) => {
|
||||
|
||||
@@ -1,195 +0,0 @@
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest";
|
||||
|
||||
import { HostRepository } from "@calcom/lib/server/repository/host";
|
||||
import { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
import { getDefaultScheduleId, hasDefaultSchedule, setupDefaultSchedule } from "./util";
|
||||
|
||||
vi.mock("@calcom/prisma", () => {
|
||||
const mockPrisma = {
|
||||
user: {
|
||||
findUnique: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
schedule: {
|
||||
findFirst: vi.fn(),
|
||||
},
|
||||
host: {
|
||||
updateMany: vi.fn(),
|
||||
},
|
||||
};
|
||||
return {
|
||||
__esModule: true,
|
||||
default: mockPrisma,
|
||||
PrismaClient: vi.fn(() => mockPrisma),
|
||||
};
|
||||
});
|
||||
|
||||
describe("Availability Utils", () => {
|
||||
let prisma: PrismaClient;
|
||||
let hostRepo: HostRepository;
|
||||
|
||||
beforeEach(() => {
|
||||
prisma = new PrismaClient();
|
||||
hostRepo = new HostRepository(prisma);
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("getDefaultScheduleId", () => {
|
||||
it("should return defaultScheduleId if user has one", async () => {
|
||||
const userId = 1;
|
||||
const defaultScheduleId = 123;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(prisma.user.findUnique as any).mockResolvedValue({
|
||||
defaultScheduleId,
|
||||
});
|
||||
|
||||
const result = await getDefaultScheduleId(userId, prisma);
|
||||
|
||||
expect(prisma.user.findUnique).toHaveBeenCalledWith({
|
||||
where: { id: userId },
|
||||
select: { defaultScheduleId: true },
|
||||
});
|
||||
expect(result).toBe(defaultScheduleId);
|
||||
});
|
||||
|
||||
it("should find and return first schedule if user has no defaultScheduleId", async () => {
|
||||
const userId = 1;
|
||||
const scheduleId = 456;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(prisma.user.findUnique as any).mockResolvedValue({
|
||||
defaultScheduleId: null,
|
||||
});
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(prisma.schedule.findFirst as any).mockResolvedValue({
|
||||
id: scheduleId,
|
||||
});
|
||||
|
||||
const result = await getDefaultScheduleId(userId, prisma);
|
||||
|
||||
expect(prisma.user.findUnique).toHaveBeenCalledWith({
|
||||
where: { id: userId },
|
||||
select: { defaultScheduleId: true },
|
||||
});
|
||||
expect(prisma.schedule.findFirst).toHaveBeenCalledWith({
|
||||
where: { userId },
|
||||
select: { id: true },
|
||||
});
|
||||
expect(result).toBe(scheduleId);
|
||||
});
|
||||
|
||||
it("should throw error if no schedules found", async () => {
|
||||
const userId = 1;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(prisma.user.findUnique as any).mockResolvedValue({
|
||||
defaultScheduleId: null,
|
||||
});
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(prisma.schedule.findFirst as any).mockResolvedValue(null);
|
||||
|
||||
await expect(getDefaultScheduleId(userId, prisma)).rejects.toThrow("No schedules found for user");
|
||||
});
|
||||
});
|
||||
|
||||
describe("hasDefaultSchedule", () => {
|
||||
it("should return true if user has defaultScheduleId", async () => {
|
||||
const user = { id: 1, defaultScheduleId: 123 };
|
||||
|
||||
const result = await hasDefaultSchedule(user, prisma);
|
||||
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("should return true if user has a schedule", async () => {
|
||||
const user = { id: 1, defaultScheduleId: null };
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(prisma.schedule.findFirst as any).mockResolvedValue({
|
||||
id: 456,
|
||||
});
|
||||
|
||||
const result = await hasDefaultSchedule(user, prisma);
|
||||
|
||||
expect(prisma.schedule.findFirst).toHaveBeenCalledWith({
|
||||
where: { userId: user.id },
|
||||
});
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false if user has no defaultScheduleId and no schedules", async () => {
|
||||
const user = { id: 1, defaultScheduleId: null };
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(prisma.schedule.findFirst as any).mockResolvedValue(null);
|
||||
|
||||
const result = await hasDefaultSchedule(user, prisma);
|
||||
|
||||
expect(prisma.schedule.findFirst).toHaveBeenCalledWith({
|
||||
where: { userId: user.id },
|
||||
});
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("setupDefaultSchedule", () => {
|
||||
it("should update user with new defaultScheduleId", async () => {
|
||||
const userId = 1;
|
||||
const scheduleId = 123;
|
||||
const updatedUser = { id: userId, defaultScheduleId: scheduleId };
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(prisma.user.update as any).mockResolvedValue(updatedUser);
|
||||
|
||||
const result = await setupDefaultSchedule(userId, scheduleId, prisma);
|
||||
|
||||
expect(prisma.user.update).toHaveBeenCalledWith({
|
||||
where: { id: userId },
|
||||
data: { defaultScheduleId: scheduleId },
|
||||
});
|
||||
expect(result).toEqual(updatedUser);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateHostsWithNewDefaultSchedule", () => {
|
||||
it("should update hosts with new scheduleId", async () => {
|
||||
const userId = 1;
|
||||
const oldScheduleId = 123;
|
||||
const newScheduleId = 456;
|
||||
const updateResult = { count: 2 }; // 2 hosts updated
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(prisma.host.updateMany as any).mockResolvedValue(updateResult);
|
||||
|
||||
const result = await hostRepo.updateHostsSchedule(userId, oldScheduleId, newScheduleId);
|
||||
|
||||
expect(prisma.host.updateMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
userId,
|
||||
scheduleId: oldScheduleId,
|
||||
},
|
||||
data: {
|
||||
scheduleId: newScheduleId,
|
||||
},
|
||||
});
|
||||
expect(result).toEqual(updateResult);
|
||||
});
|
||||
|
||||
it("should handle case with no hosts to update", async () => {
|
||||
const userId = 1;
|
||||
const oldScheduleId = 123;
|
||||
const newScheduleId = 456;
|
||||
const updateResult = { count: 0 }; // No hosts updated
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(prisma.host.updateMany as any).mockResolvedValue(updateResult);
|
||||
|
||||
const result = await hostRepo.updateHostsSchedule(userId, oldScheduleId, newScheduleId);
|
||||
|
||||
expect(prisma.host.updateMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
userId,
|
||||
scheduleId: oldScheduleId,
|
||||
},
|
||||
data: {
|
||||
scheduleId: newScheduleId,
|
||||
},
|
||||
});
|
||||
expect(result).toEqual(updateResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,54 +0,0 @@
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import type { User } from "@calcom/prisma/client";
|
||||
|
||||
export const getDefaultScheduleId = async (userId: number, prisma: PrismaClient) => {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
select: {
|
||||
defaultScheduleId: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (user?.defaultScheduleId) {
|
||||
return user.defaultScheduleId;
|
||||
}
|
||||
|
||||
// If we're returning the default schedule for the first time then we should set it in the user record
|
||||
const defaultSchedule = await prisma.schedule.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!defaultSchedule) {
|
||||
// Handle case where defaultSchedule is null by throwing an error
|
||||
throw new Error("No schedules found for user");
|
||||
}
|
||||
|
||||
return defaultSchedule.id;
|
||||
};
|
||||
|
||||
export const hasDefaultSchedule = async (user: Partial<User>, prisma: PrismaClient) => {
|
||||
const defaultSchedule = await prisma.schedule.findFirst({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
return !!user.defaultScheduleId || !!defaultSchedule;
|
||||
};
|
||||
|
||||
export const setupDefaultSchedule = async (userId: number, scheduleId: number, prisma: PrismaClient) => {
|
||||
return prisma.user.update({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
data: {
|
||||
defaultScheduleId: scheduleId,
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -7,6 +7,7 @@ import { sendChangeOfEmailVerification } from "@calcom/features/auth/lib/verifyE
|
||||
import { updateNewTeamMemberEventTypes } from "@calcom/features/ee/teams/lib/queries";
|
||||
import { FeaturesRepository } from "@calcom/features/flags/features.repository";
|
||||
import { checkUsername } from "@calcom/features/profile/lib/checkUsername";
|
||||
import { ScheduleRepository } from "@calcom/features/schedules/repositories/ScheduleRepository";
|
||||
import hasKeyInMetadata from "@calcom/lib/hasKeyInMetadata";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import logger from "@calcom/lib/logger";
|
||||
@@ -22,7 +23,6 @@ import type { TrpcSessionUser } from "@calcom/trpc/server/types";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import { getDefaultScheduleId } from "../availability/util";
|
||||
import { updateUserMetadataAllowedKeys, type TUpdateProfileInputSchema } from "./updateProfile.schema";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["updateProfile"] });
|
||||
@@ -263,7 +263,8 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
|
||||
|
||||
if (user.timeZone !== data.timeZone && updatedUser.schedules.length > 0) {
|
||||
// on timezone change update timezone of default schedule
|
||||
const defaultScheduleId = await getDefaultScheduleId(user.id, prisma);
|
||||
const scheduleRepository = new ScheduleRepository(prisma);
|
||||
const defaultScheduleId = await scheduleRepository.getDefaultScheduleId(user.id);
|
||||
|
||||
if (!user.defaultScheduleId) {
|
||||
// set default schedule if not already set
|
||||
|
||||
Reference in new issue
Block a user