refactor: create HostRepository in /features/host (#25842)

* mv test

* mv host repository

* update imports
This commit is contained in:
Benny Joo authored and GitHub committed 2025-12-13 11:04:57 +00:00
1 parent d034e7aff7
commit d81993892a
6 files changed
+6 -6

No files matched your search

@@ -4,13 +4,13 @@ import { zodRoutes } from "@calcom/app-store/routing-forms/zod";
import dayjs from "@calcom/dayjs";
import type { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
import { getBusyCalendarTimes } from "@calcom/features/calendars/lib/CalendarManager";
import type { HostRepository } from "@calcom/features/host/repositories/HostRepository";
import { mergeOverlappingRanges } from "@calcom/features/schedules/lib/date-ranges";
import type { UserRepository } from "@calcom/features/users/repositories/UserRepository";
import logger from "@calcom/lib/logger";
import { raqbQueryValueSchema } from "@calcom/lib/raqb/zod";
import { safeStringify } from "@calcom/lib/safeStringify";
import type { PrismaAttributeRepository } from "@calcom/lib/server/repository/PrismaAttributeRepository";
import type { HostRepository } from "@calcom/lib/server/repository/host";
import type { PrismaOOORepository } from "@calcom/lib/server/repository/ooo";
import type { Prisma } from "@calcom/prisma/client";
import type { User, Booking, SelectedCalendar } from "@calcom/prisma/client";
+2 -2
View File
@@ -1,6 +1,6 @@
import { DI_TOKENS } from "@calcom/features/di/tokens";
import { HostRepository } from "@calcom/lib/server/repository/host";
import { moduleLoader as prismaModuleLoader } from "@calcom/features/di/modules/Prisma";
import { DI_TOKENS } from "@calcom/features/di/tokens";
import { HostRepository } from "@calcom/features/host/repositories/HostRepository";
import { createModule, bindModuleToClassOnToken, type ModuleLoader } from "../di";
@@ -0,0 +1,76 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import { PrismaClient } from "@calcom/prisma";
import { HostRepository } from "./HostRepository";
vi.mock("@calcom/prisma", () => {
const mockPrisma = {
host: {
updateMany: vi.fn(),
findMany: vi.fn(),
},
};
return {
__esModule: true,
default: mockPrisma,
PrismaClient: vi.fn(() => mockPrisma),
};
});
describe("HostRepository", () => {
let prisma: PrismaClient;
let hostRepo: HostRepository;
beforeEach(() => {
prisma = new PrismaClient();
hostRepo = new HostRepository(prisma);
vi.clearAllMocks();
});
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);
});
});
});
@@ -0,0 +1,40 @@
import type { PrismaClient } from "@calcom/prisma";
export class HostRepository {
constructor(private prismaClient: PrismaClient) {}
async updateHostsSchedule(userId: number, oldScheduleId: number, newScheduleId: number) {
return await this.prismaClient.host.updateMany({
where: {
userId,
scheduleId: oldScheduleId,
},
data: {
scheduleId: newScheduleId,
},
});
}
async findHostsCreatedInInterval({
eventTypeId,
userIds,
startDate,
}: {
eventTypeId: number;
userIds: number[];
startDate: Date;
}) {
return await this.prismaClient.host.findMany({
where: {
userId: {
in: userIds,
},
eventTypeId,
isFixed: false,
createdAt: {
gte: startDate,
},
},
});
}
}