Skip to content

Commit cd0a236

Browse files
committed
refactor: replace static methods of HostRepository and PrismaAttributeRepository
1 parent a91561f commit cd0a236

10 files changed

Lines changed: 40 additions & 35 deletions

File tree

apps/web/app/(use-page-wrapper)/settings/(settings-layout)/teams/[id]/members/page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { unstable_cache } from "next/cache";
55
import { RoleManagementFactory } from "@calcom/features/pbac/services/role-management.factory";
66
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
77
import { PrismaAttributeRepository } from "@calcom/lib/server/repository/PrismaAttributeRepository";
8+
import prisma from "@calcom/prisma";
89
import { viewerTeamsRouter } from "@calcom/trpc/server/routers/viewer/teams/_router";
910

1011
import { TeamMembersView } from "~/teams/team-members-view";
@@ -36,8 +37,10 @@ const getCachedTeamRoles = unstable_cache(
3637
const getCachedTeamAttributes = unstable_cache(
3738
async (organizationId?: number) => {
3839
if (!organizationId) return [];
40+
const attributeRepo = new PrismaAttributeRepository(prisma);
41+
3942
try {
40-
return await PrismaAttributeRepository.findAllByOrgIdWithOptions({ orgId: organizationId });
43+
return await attributeRepo.findAllByOrgIdWithOptions({ orgId: organizationId });
4144
} catch (error) {
4245
return [];
4346
}

apps/web/app/(use-page-wrapper)/settings/organizations/(org-user-only)/members/page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { unstable_cache } from "next/cache";
44

55
import { RoleManagementFactory } from "@calcom/features/pbac/services/role-management.factory";
66
import { PrismaAttributeRepository } from "@calcom/lib/server/repository/PrismaAttributeRepository";
7+
import prisma from "@calcom/prisma";
78
import { viewerOrganizationsRouter } from "@calcom/trpc/server/routers/viewer/organizations/_router";
89

910
import { MembersView } from "~/members/members-view";
@@ -19,7 +20,9 @@ export const generateMetadata = async () =>
1920

2021
const getCachedAttributes = unstable_cache(
2122
async (orgId: number) => {
22-
return await PrismaAttributeRepository.findAllByOrgIdWithOptions({ orgId });
23+
const attributeRepo = new PrismaAttributeRepository(prisma);
24+
25+
return await attributeRepo.findAllByOrgIdWithOptions({ orgId });
2326
},
2427
undefined,
2528
{ revalidate: 3600, tags: ["viewer.attributes.list"] } // Cache for 1 hour

packages/lib/server/repository/PrismaAttributeRepository.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import prisma from "@calcom/prisma";
1+
import type { PrismaClient } from "@calcom/prisma";
22

33
export class PrismaAttributeRepository {
4-
static async findManyByNamesAndOrgIdIncludeOptions({
4+
constructor(private prismaClient: PrismaClient) {}
5+
6+
async findManyByNamesAndOrgIdIncludeOptions({
57
attributeNames,
68
orgId,
79
}: {
810
attributeNames: string[];
911
orgId: number;
1012
}) {
11-
return prisma.attribute.findMany({
13+
return this.prismaClient.attribute.findMany({
1214
where: {
1315
name: { in: attributeNames, mode: "insensitive" },
1416
teamId: orgId,
@@ -25,9 +27,9 @@ export class PrismaAttributeRepository {
2527
});
2628
}
2729

28-
static async findManyByOrgId({ orgId }: { orgId: number }) {
30+
async findManyByOrgId({ orgId }: { orgId: number }) {
2931
// It should be a faster query because of lesser number of attributes record and index on teamId
30-
const result = await prisma.attribute.findMany({
32+
const result = await this.prismaClient.attribute.findMany({
3133
where: {
3234
teamId: orgId,
3335
},
@@ -43,8 +45,8 @@ export class PrismaAttributeRepository {
4345
return result;
4446
}
4547

46-
static async findAllByOrgIdWithOptions({ orgId }: { orgId: number }) {
47-
return await prisma.attribute.findMany({
48+
async findAllByOrgIdWithOptions({ orgId }: { orgId: number }) {
49+
return await this.prismaClient.attribute.findMany({
4850
where: {
4951
teamId: orgId,
5052
},

packages/lib/server/repository/host.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import prisma from "@calcom/prisma";
1+
import type { PrismaClient } from "@calcom/prisma";
22

33
export class HostRepository {
4-
static async updateHostsSchedule(userId: number, oldScheduleId: number, newScheduleId: number) {
5-
return await prisma.host.updateMany({
4+
constructor(private prismaClient: PrismaClient) {}
5+
6+
async updateHostsSchedule(userId: number, oldScheduleId: number, newScheduleId: number) {
7+
return await this.prismaClient.host.updateMany({
68
where: {
79
userId,
810
scheduleId: oldScheduleId,

packages/lib/service/attribute/server/assignValueToUser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const findAttributesByName = async ({
3737
orgId: number;
3838
attributeNames: AttributeName[];
3939
}) => {
40-
const attributesFromDb = await PrismaAttributeRepository.findManyByNamesAndOrgIdIncludeOptions({
40+
const attributeRepo = new PrismaAttributeRepository(prisma);
41+
const attributesFromDb = await attributeRepo.findManyByNamesAndOrgIdIncludeOptions({
4142
attributeNames,
4243
orgId,
4344
});

packages/lib/service/attribute/server/getAttributes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,11 @@ async function _getOrgMembershipToUserIdForTeam({ orgId, teamId }: { orgId: numb
216216
}
217217

218218
async function _queryAllData({ orgId, teamId }: { orgId: number; teamId: number }) {
219+
const attributeRepo = new PrismaAttributeRepository(prisma);
220+
219221
const [orgMembershipToUserIdForTeamMembers, attributesOfTheOrg] = await Promise.all([
220222
_getOrgMembershipToUserIdForTeam({ orgId, teamId }),
221-
PrismaAttributeRepository.findManyByOrgId({ orgId }),
223+
attributeRepo.findManyByOrgId({ orgId }),
222224
]);
223225

224226
const orgMembershipIds = Array.from(orgMembershipToUserIdForTeamMembers.keys());

packages/trpc/server/routers/viewer/attributes/list.handler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PrismaAttributeRepository } from "@calcom/lib/server/repository/PrismaAttributeRepository";
2+
import prisma from "@calcom/prisma";
23

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

@@ -19,8 +20,9 @@ const listHandler = async (opts: GetOptions) => {
1920
message: "You need to be apart of an organization to use this feature",
2021
});
2122
}
23+
const attributeRepo = new PrismaAttributeRepository(prisma);
2224

23-
return await PrismaAttributeRepository.findAllByOrgIdWithOptions({ orgId: org.id });
25+
return await attributeRepo.findAllByOrgIdWithOptions({ orgId: org.id });
2426
};
2527

2628
export default listHandler;

packages/trpc/server/routers/viewer/availability/schedule/delete.handler.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { HostRepository } from "@calcom/lib/server/repository/host";
12
import { prisma } from "@calcom/prisma";
23

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

56
import type { TrpcSessionUser } from "../../../../types";
6-
import { updateHostsWithNewDefaultSchedule } from "../util";
77
import type { TDeleteInputSchema } from "./delete.schema";
88

99
type DeleteOptions = {
@@ -15,6 +15,7 @@ type DeleteOptions = {
1515

1616
export const deleteHandler = async ({ input, ctx }: DeleteOptions) => {
1717
const { user } = ctx;
18+
const hostRepo = new HostRepository(prisma);
1819

1920
const scheduleToDelete = await prisma.schedule.findUnique({
2021
where: {
@@ -46,7 +47,7 @@ export const deleteHandler = async ({ input, ctx }: DeleteOptions) => {
4647
// to throw the error if there arent any other schedules
4748
if (!scheduleToSetAsDefault) throw new TRPCError({ code: "BAD_REQUEST" });
4849

49-
await updateHostsWithNewDefaultSchedule(user.id, input.scheduleId, scheduleToSetAsDefault.id);
50+
await hostRepo.updateHostsSchedule(user.id, input.scheduleId, scheduleToSetAsDefault.id);
5051

5152
await prisma.user.update({
5253
where: {
@@ -57,7 +58,7 @@ export const deleteHandler = async ({ input, ctx }: DeleteOptions) => {
5758
},
5859
});
5960
} else if (user.defaultScheduleId) {
60-
await updateHostsWithNewDefaultSchedule(user.id, input.scheduleId, user.defaultScheduleId);
61+
await hostRepo.updateHostsSchedule(user.id, input.scheduleId, user.defaultScheduleId);
6162
}
6263

6364
await prisma.schedule.delete({

packages/trpc/server/routers/viewer/availability/util.test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { describe, expect, it, vi, beforeEach } from "vitest";
22

3+
import { HostRepository } from "@calcom/lib/server/repository/host";
34
import { PrismaClient } from "@calcom/prisma";
45

5-
import {
6-
getDefaultScheduleId,
7-
hasDefaultSchedule,
8-
setupDefaultSchedule,
9-
updateHostsWithNewDefaultSchedule,
10-
} from "./util";
6+
import { getDefaultScheduleId, hasDefaultSchedule, setupDefaultSchedule } from "./util";
117

128
vi.mock("@calcom/prisma", () => {
139
const mockPrisma = {
@@ -31,9 +27,11 @@ vi.mock("@calcom/prisma", () => {
3127

3228
describe("Availability Utils", () => {
3329
let prisma: PrismaClient;
30+
let hostRepo: HostRepository;
3431

3532
beforeEach(() => {
3633
prisma = new PrismaClient();
34+
hostRepo = new HostRepository(prisma);
3735
vi.clearAllMocks();
3836
});
3937

@@ -158,7 +156,7 @@ describe("Availability Utils", () => {
158156

159157
(prisma.host.updateMany as any).mockResolvedValue(updateResult);
160158

161-
const result = await updateHostsWithNewDefaultSchedule(userId, oldScheduleId, newScheduleId, prisma);
159+
const result = await hostRepo.updateHostsSchedule(userId, oldScheduleId, newScheduleId);
162160

163161
expect(prisma.host.updateMany).toHaveBeenCalledWith({
164162
where: {
@@ -180,7 +178,7 @@ describe("Availability Utils", () => {
180178

181179
(prisma.host.updateMany as any).mockResolvedValue(updateResult);
182180

183-
const result = await updateHostsWithNewDefaultSchedule(userId, oldScheduleId, newScheduleId, prisma);
181+
const result = await hostRepo.updateHostsSchedule(userId, oldScheduleId, newScheduleId);
184182

185183
expect(prisma.host.updateMany).toHaveBeenCalledWith({
186184
where: {

packages/trpc/server/routers/viewer/availability/util.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { User } from "@prisma/client";
22

3-
import { HostRepository } from "@calcom/lib/server/repository/host";
43
import type { PrismaClient } from "@calcom/prisma";
54

65
export const getDefaultScheduleId = async (userId: number, prisma: PrismaClient) => {
@@ -54,11 +53,3 @@ export const setupDefaultSchedule = async (userId: number, scheduleId: number, p
5453
},
5554
});
5655
};
57-
58-
export const updateHostsWithNewDefaultSchedule = async (
59-
userId: number,
60-
defaultScheduleId: number,
61-
scheduleId: number
62-
) => {
63-
return await HostRepository.updateHostsSchedule(userId, defaultScheduleId, scheduleId);
64-
};

0 commit comments

Comments
 (0)