refactor: replace static methods of HostRepository and PrismaAttributeRepository (#23175)
This commit is contained in:
1 parent
a91561f3c2
commit
9e9f815809
10 files changed
+40
-35
No files matched your search
+4
-1
@@ -5,6 +5,7 @@ import { unstable_cache } from "next/cache";
|
||||
import { RoleManagementFactory } from "@calcom/features/pbac/services/role-management.factory";
|
||||
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
|
||||
import { PrismaAttributeRepository } from "@calcom/lib/server/repository/PrismaAttributeRepository";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { viewerTeamsRouter } from "@calcom/trpc/server/routers/viewer/teams/_router";
|
||||
|
||||
import { TeamMembersView } from "~/teams/team-members-view";
|
||||
@@ -36,8 +37,10 @@ const getCachedTeamRoles = unstable_cache(
|
||||
const getCachedTeamAttributes = unstable_cache(
|
||||
async (organizationId?: number) => {
|
||||
if (!organizationId) return [];
|
||||
const attributeRepo = new PrismaAttributeRepository(prisma);
|
||||
|
||||
try {
|
||||
return await PrismaAttributeRepository.findAllByOrgIdWithOptions({ orgId: organizationId });
|
||||
return await attributeRepo.findAllByOrgIdWithOptions({ orgId: organizationId });
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
|
||||
+4
-1
@@ -4,6 +4,7 @@ import { unstable_cache } from "next/cache";
|
||||
|
||||
import { RoleManagementFactory } from "@calcom/features/pbac/services/role-management.factory";
|
||||
import { PrismaAttributeRepository } from "@calcom/lib/server/repository/PrismaAttributeRepository";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { viewerOrganizationsRouter } from "@calcom/trpc/server/routers/viewer/organizations/_router";
|
||||
|
||||
import { MembersView } from "~/members/members-view";
|
||||
@@ -19,7 +20,9 @@ export const generateMetadata = async () =>
|
||||
|
||||
const getCachedAttributes = unstable_cache(
|
||||
async (orgId: number) => {
|
||||
return await PrismaAttributeRepository.findAllByOrgIdWithOptions({ orgId });
|
||||
const attributeRepo = new PrismaAttributeRepository(prisma);
|
||||
|
||||
return await attributeRepo.findAllByOrgIdWithOptions({ orgId });
|
||||
},
|
||||
undefined,
|
||||
{ revalidate: 3600, tags: ["viewer.attributes.list"] } // Cache for 1 hour
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
export class PrismaAttributeRepository {
|
||||
static async findManyByNamesAndOrgIdIncludeOptions({
|
||||
constructor(private prismaClient: PrismaClient) {}
|
||||
|
||||
async findManyByNamesAndOrgIdIncludeOptions({
|
||||
attributeNames,
|
||||
orgId,
|
||||
}: {
|
||||
attributeNames: string[];
|
||||
orgId: number;
|
||||
}) {
|
||||
return prisma.attribute.findMany({
|
||||
return this.prismaClient.attribute.findMany({
|
||||
where: {
|
||||
name: { in: attributeNames, mode: "insensitive" },
|
||||
teamId: orgId,
|
||||
@@ -25,9 +27,9 @@ export class PrismaAttributeRepository {
|
||||
});
|
||||
}
|
||||
|
||||
static async findManyByOrgId({ orgId }: { orgId: number }) {
|
||||
async findManyByOrgId({ orgId }: { orgId: number }) {
|
||||
// It should be a faster query because of lesser number of attributes record and index on teamId
|
||||
const result = await prisma.attribute.findMany({
|
||||
const result = await this.prismaClient.attribute.findMany({
|
||||
where: {
|
||||
teamId: orgId,
|
||||
},
|
||||
@@ -43,8 +45,8 @@ export class PrismaAttributeRepository {
|
||||
return result;
|
||||
}
|
||||
|
||||
static async findAllByOrgIdWithOptions({ orgId }: { orgId: number }) {
|
||||
return await prisma.attribute.findMany({
|
||||
async findAllByOrgIdWithOptions({ orgId }: { orgId: number }) {
|
||||
return await this.prismaClient.attribute.findMany({
|
||||
where: {
|
||||
teamId: orgId,
|
||||
},
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
export class HostRepository {
|
||||
static async updateHostsSchedule(userId: number, oldScheduleId: number, newScheduleId: number) {
|
||||
return await prisma.host.updateMany({
|
||||
constructor(private prismaClient: PrismaClient) {}
|
||||
|
||||
async updateHostsSchedule(userId: number, oldScheduleId: number, newScheduleId: number) {
|
||||
return await this.prismaClient.host.updateMany({
|
||||
where: {
|
||||
userId,
|
||||
scheduleId: oldScheduleId,
|
||||
|
||||
@@ -37,7 +37,8 @@ const findAttributesByName = async ({
|
||||
orgId: number;
|
||||
attributeNames: AttributeName[];
|
||||
}) => {
|
||||
const attributesFromDb = await PrismaAttributeRepository.findManyByNamesAndOrgIdIncludeOptions({
|
||||
const attributeRepo = new PrismaAttributeRepository(prisma);
|
||||
const attributesFromDb = await attributeRepo.findManyByNamesAndOrgIdIncludeOptions({
|
||||
attributeNames,
|
||||
orgId,
|
||||
});
|
||||
|
||||
@@ -216,9 +216,11 @@ async function _getOrgMembershipToUserIdForTeam({ orgId, teamId }: { orgId: numb
|
||||
}
|
||||
|
||||
async function _queryAllData({ orgId, teamId }: { orgId: number; teamId: number }) {
|
||||
const attributeRepo = new PrismaAttributeRepository(prisma);
|
||||
|
||||
const [orgMembershipToUserIdForTeamMembers, attributesOfTheOrg] = await Promise.all([
|
||||
_getOrgMembershipToUserIdForTeam({ orgId, teamId }),
|
||||
PrismaAttributeRepository.findManyByOrgId({ orgId }),
|
||||
attributeRepo.findManyByOrgId({ orgId }),
|
||||
]);
|
||||
|
||||
const orgMembershipIds = Array.from(orgMembershipToUserIdForTeamMembers.keys());
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { PrismaAttributeRepository } from "@calcom/lib/server/repository/PrismaAttributeRepository";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
@@ -19,8 +20,9 @@ const listHandler = async (opts: GetOptions) => {
|
||||
message: "You need to be apart of an organization to use this feature",
|
||||
});
|
||||
}
|
||||
const attributeRepo = new PrismaAttributeRepository(prisma);
|
||||
|
||||
return await PrismaAttributeRepository.findAllByOrgIdWithOptions({ orgId: org.id });
|
||||
return await attributeRepo.findAllByOrgIdWithOptions({ orgId: org.id });
|
||||
};
|
||||
|
||||
export default listHandler;
|
||||
@@ -1,9 +1,9 @@
|
||||
import { HostRepository } from "@calcom/lib/server/repository/host";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../../types";
|
||||
import { updateHostsWithNewDefaultSchedule } from "../util";
|
||||
import type { TDeleteInputSchema } from "./delete.schema";
|
||||
|
||||
type DeleteOptions = {
|
||||
@@ -15,6 +15,7 @@ type DeleteOptions = {
|
||||
|
||||
export const deleteHandler = async ({ input, ctx }: DeleteOptions) => {
|
||||
const { user } = ctx;
|
||||
const hostRepo = new HostRepository(prisma);
|
||||
|
||||
const scheduleToDelete = await prisma.schedule.findUnique({
|
||||
where: {
|
||||
@@ -46,7 +47,7 @@ export const deleteHandler = async ({ input, ctx }: DeleteOptions) => {
|
||||
// to throw the error if there arent any other schedules
|
||||
if (!scheduleToSetAsDefault) throw new TRPCError({ code: "BAD_REQUEST" });
|
||||
|
||||
await updateHostsWithNewDefaultSchedule(user.id, input.scheduleId, scheduleToSetAsDefault.id);
|
||||
await hostRepo.updateHostsSchedule(user.id, input.scheduleId, scheduleToSetAsDefault.id);
|
||||
|
||||
await prisma.user.update({
|
||||
where: {
|
||||
@@ -57,7 +58,7 @@ export const deleteHandler = async ({ input, ctx }: DeleteOptions) => {
|
||||
},
|
||||
});
|
||||
} else if (user.defaultScheduleId) {
|
||||
await updateHostsWithNewDefaultSchedule(user.id, input.scheduleId, user.defaultScheduleId);
|
||||
await hostRepo.updateHostsSchedule(user.id, input.scheduleId, user.defaultScheduleId);
|
||||
}
|
||||
|
||||
await prisma.schedule.delete({
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
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,
|
||||
updateHostsWithNewDefaultSchedule,
|
||||
} from "./util";
|
||||
import { getDefaultScheduleId, hasDefaultSchedule, setupDefaultSchedule } from "./util";
|
||||
|
||||
vi.mock("@calcom/prisma", () => {
|
||||
const mockPrisma = {
|
||||
@@ -31,9 +27,11 @@ vi.mock("@calcom/prisma", () => {
|
||||
|
||||
describe("Availability Utils", () => {
|
||||
let prisma: PrismaClient;
|
||||
let hostRepo: HostRepository;
|
||||
|
||||
beforeEach(() => {
|
||||
prisma = new PrismaClient();
|
||||
hostRepo = new HostRepository(prisma);
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
@@ -158,7 +156,7 @@ describe("Availability Utils", () => {
|
||||
|
||||
(prisma.host.updateMany as any).mockResolvedValue(updateResult);
|
||||
|
||||
const result = await updateHostsWithNewDefaultSchedule(userId, oldScheduleId, newScheduleId, prisma);
|
||||
const result = await hostRepo.updateHostsSchedule(userId, oldScheduleId, newScheduleId);
|
||||
|
||||
expect(prisma.host.updateMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
@@ -180,7 +178,7 @@ describe("Availability Utils", () => {
|
||||
|
||||
(prisma.host.updateMany as any).mockResolvedValue(updateResult);
|
||||
|
||||
const result = await updateHostsWithNewDefaultSchedule(userId, oldScheduleId, newScheduleId, prisma);
|
||||
const result = await hostRepo.updateHostsSchedule(userId, oldScheduleId, newScheduleId);
|
||||
|
||||
expect(prisma.host.updateMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import type { User } from "@prisma/client";
|
||||
|
||||
import { HostRepository } from "@calcom/lib/server/repository/host";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
export const getDefaultScheduleId = async (userId: number, prisma: PrismaClient) => {
|
||||
@@ -54,11 +53,3 @@ export const setupDefaultSchedule = async (userId: number, scheduleId: number, p
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const updateHostsWithNewDefaultSchedule = async (
|
||||
userId: number,
|
||||
defaultScheduleId: number,
|
||||
scheduleId: number
|
||||
) => {
|
||||
return await HostRepository.updateHostsSchedule(userId, defaultScheduleId, scheduleId);
|
||||
};
|
||||
Reference in new issue
Block a user