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
@@ -0,0 +1,167 @@
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest";
|
||||
|
||||
import { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
import { ScheduleRepository } from "./ScheduleRepository";
|
||||
|
||||
vi.mock("@calcom/prisma", () => {
|
||||
const mockPrisma = {
|
||||
user: {
|
||||
findUnique: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
schedule: {
|
||||
findFirst: vi.fn(),
|
||||
},
|
||||
};
|
||||
return {
|
||||
__esModule: true,
|
||||
default: mockPrisma,
|
||||
PrismaClient: vi.fn(() => mockPrisma),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@calcom/lib/hasEditPermissionForUser", () => ({
|
||||
hasReadPermissionsForUserId: vi.fn().mockResolvedValue(true),
|
||||
}));
|
||||
|
||||
describe("ScheduleRepository", () => {
|
||||
let prisma: PrismaClient;
|
||||
let scheduleRepository: ScheduleRepository;
|
||||
|
||||
beforeEach(() => {
|
||||
prisma = new PrismaClient();
|
||||
scheduleRepository = new ScheduleRepository(prisma);
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("constructor", () => {
|
||||
it("should throw error if prismaClient is not provided", () => {
|
||||
expect(() => new ScheduleRepository(null as any)).toThrow(
|
||||
"PrismaClient is required for ScheduleRepository"
|
||||
);
|
||||
expect(() => new ScheduleRepository(undefined as any)).toThrow(
|
||||
"PrismaClient is required for ScheduleRepository"
|
||||
);
|
||||
});
|
||||
|
||||
it("should create instance successfully with valid prismaClient", () => {
|
||||
const repo = new ScheduleRepository(prisma);
|
||||
expect(repo).toBeInstanceOf(ScheduleRepository);
|
||||
});
|
||||
});
|
||||
|
||||
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 scheduleRepository.getDefaultScheduleId(userId);
|
||||
|
||||
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 scheduleRepository.getDefaultScheduleId(userId);
|
||||
|
||||
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(scheduleRepository.getDefaultScheduleId(userId)).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 scheduleRepository.hasDefaultSchedule(user);
|
||||
|
||||
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 scheduleRepository.hasDefaultSchedule(user);
|
||||
|
||||
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 scheduleRepository.hasDefaultSchedule(user);
|
||||
|
||||
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 scheduleRepository.setupDefaultSchedule(userId, scheduleId);
|
||||
|
||||
expect(prisma.user.update).toHaveBeenCalledWith({
|
||||
where: { id: userId },
|
||||
data: { defaultScheduleId: scheduleId },
|
||||
});
|
||||
expect(result).toEqual(updatedUser);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -5,16 +5,19 @@ import {
|
||||
transformWorkingHoursForAtom,
|
||||
} from "@calcom/lib/schedules/transformers";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { getDefaultScheduleId } from "@calcom/trpc/server/routers/viewer/availability/util";
|
||||
import type { User } from "@calcom/prisma/client";
|
||||
|
||||
export type FindDetailedScheduleByIdReturnType = Awaited<
|
||||
ReturnType<typeof ScheduleRepository.findDetailedScheduleById>
|
||||
ReturnType<ScheduleRepository["findDetailedScheduleById"]>
|
||||
>;
|
||||
|
||||
export class ScheduleRepository {
|
||||
// when instantiating, prismaClient injection is required
|
||||
constructor(private prismaClient: PrismaClient) {}
|
||||
constructor(private readonly prismaClient: PrismaClient) {
|
||||
if (!prismaClient) {
|
||||
throw new Error("PrismaClient is required for ScheduleRepository");
|
||||
}
|
||||
}
|
||||
|
||||
async findScheduleByIdForBuildDateRanges({ scheduleId }: { scheduleId: number }) {
|
||||
const schedule = await this.prismaClient.schedule.findUnique({
|
||||
@@ -63,8 +66,8 @@ export class ScheduleRepository {
|
||||
return schedule;
|
||||
}
|
||||
|
||||
static async findScheduleById({ id }: { id: number }) {
|
||||
const schedule = await prisma.schedule.findUnique({
|
||||
async findScheduleById({ id }: { id: number }) {
|
||||
const schedule = await this.prismaClient.schedule.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
@@ -80,7 +83,7 @@ export class ScheduleRepository {
|
||||
return schedule;
|
||||
}
|
||||
|
||||
static async findDetailedScheduleById({
|
||||
async findDetailedScheduleById({
|
||||
isManagedEventType,
|
||||
scheduleId,
|
||||
userId,
|
||||
@@ -93,9 +96,9 @@ export class ScheduleRepository {
|
||||
scheduleId?: number;
|
||||
isManagedEventType?: boolean;
|
||||
}) {
|
||||
const schedule = await prisma.schedule.findUnique({
|
||||
const schedule = await this.prismaClient.schedule.findUnique({
|
||||
where: {
|
||||
id: scheduleId || (await getDefaultScheduleId(userId, prisma)),
|
||||
id: scheduleId || (await this.getDefaultScheduleId(userId)),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
@@ -119,7 +122,7 @@ export class ScheduleRepository {
|
||||
|
||||
const timeZone = schedule.timeZone || userTimeZone;
|
||||
|
||||
const schedulesCount = await prisma.schedule.count({
|
||||
const schedulesCount = await this.prismaClient.schedule.count({
|
||||
where: {
|
||||
userId: userId,
|
||||
},
|
||||
@@ -141,7 +144,7 @@ export class ScheduleRepository {
|
||||
};
|
||||
}
|
||||
|
||||
static async findManyDetailedScheduleByUserId({
|
||||
async findManyDetailedScheduleByUserId({
|
||||
isManagedEventType,
|
||||
userId,
|
||||
defaultScheduleId,
|
||||
@@ -154,7 +157,7 @@ export class ScheduleRepository {
|
||||
|
||||
isManagedEventType?: boolean;
|
||||
}) {
|
||||
const schedules = await prisma.schedule.findMany({
|
||||
const schedules = await this.prismaClient.schedule.findMany({
|
||||
where: {
|
||||
userId: userId,
|
||||
},
|
||||
@@ -203,4 +206,56 @@ export class ScheduleRepository {
|
||||
|
||||
return schedulesFormatted;
|
||||
}
|
||||
|
||||
async getDefaultScheduleId(userId: number) {
|
||||
const user = await this.prismaClient.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 this.prismaClient.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;
|
||||
}
|
||||
|
||||
async hasDefaultSchedule(user: Partial<User>) {
|
||||
const defaultSchedule = await this.prismaClient.schedule.findFirst({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
return !!user.defaultScheduleId || !!defaultSchedule;
|
||||
}
|
||||
|
||||
async setupDefaultSchedule(userId: number, scheduleId: number) {
|
||||
return this.prismaClient.user.update({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
data: {
|
||||
defaultScheduleId: scheduleId,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,10 @@ import { HttpError } from "@calcom/lib/http-error";
|
||||
import { transformScheduleToAvailabilityForAtom } from "@calcom/lib/schedules/transformers/for-atom";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import type { TUpdateInputSchema } from "@calcom/trpc/server/routers/viewer/availability/schedule/update.schema";
|
||||
import { setupDefaultSchedule } from "@calcom/trpc/server/routers/viewer/availability/util";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
|
||||
|
||||
import { ScheduleRepository } from "../repositories/ScheduleRepository";
|
||||
|
||||
interface IUpdateScheduleOptions {
|
||||
input: TUpdateInputSchema;
|
||||
user: Pick<NonNullable<TrpcSessionUser>, "id" | "defaultScheduleId" | "timeZone">;
|
||||
@@ -64,7 +65,9 @@ export class ScheduleService {
|
||||
|
||||
let updatedUser;
|
||||
if (input.isDefault) {
|
||||
const setupDefault = await setupDefaultSchedule(user.id, input.scheduleId, this.prisma);
|
||||
const scheduleRepo = new ScheduleRepository(this.prisma);
|
||||
|
||||
const setupDefault = await scheduleRepo.setupDefaultSchedule(user.id, input.scheduleId);
|
||||
updatedUser = setupDefault;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user