perf: Server-Side Data Fetching in App Router: /availability (#16536)
* wip * add method to OrganizationRepository * server side fetching for availability * fix error * server side fetching finish * remove log * fix * fix * fix type * no need to export * fix type * address nit comments
This commit is contained in:
1 parent
7725adedc8
commit
d9777a3011
10 files changed
+208
-92
No files matched your search
@@ -38,19 +38,20 @@ export async function hasEditPermissionForUserID({ ctx, input }: InputOptions) {
|
||||
return teamIdOverlaps;
|
||||
}
|
||||
|
||||
export async function hasReadPermissionsForUserId({ ctx, input }: InputOptions) {
|
||||
const { user } = ctx;
|
||||
|
||||
export async function hasReadPermissionsForUserId({
|
||||
userId,
|
||||
memberId,
|
||||
}: InputOptions["input"] & { userId: number }) {
|
||||
const authedUsersTeams = await prisma.membership.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
userId,
|
||||
accepted: true,
|
||||
},
|
||||
});
|
||||
|
||||
const targetUsersTeams = await prisma.membership.findMany({
|
||||
where: {
|
||||
userId: input.memberId,
|
||||
userId: memberId,
|
||||
accepted: true,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
import { getDefaultScheduleId } from "@calcom/trpc/server/routers/viewer/availability/util";
|
||||
|
||||
import { hasReadPermissionsForUserId } from "../../hasEditPermissionForUser";
|
||||
import {
|
||||
transformAvailabilityForAtom,
|
||||
transformDateOverridesForAtom,
|
||||
transformWorkingHoursForAtom,
|
||||
} from "../../schedules";
|
||||
|
||||
export class ScheduleRepository {
|
||||
static async findScheduleById({ id }: { id: number }) {
|
||||
@@ -17,4 +25,64 @@ export class ScheduleRepository {
|
||||
|
||||
return schedule;
|
||||
}
|
||||
|
||||
static async findDetailedScheduleById({
|
||||
isManagedEventType,
|
||||
scheduleId,
|
||||
userId,
|
||||
defaultScheduleId,
|
||||
timeZone: userTimeZone,
|
||||
}: {
|
||||
timeZone: string;
|
||||
userId: number;
|
||||
defaultScheduleId: number | null;
|
||||
scheduleId?: number;
|
||||
isManagedEventType?: boolean;
|
||||
}) {
|
||||
const schedule = await prisma.schedule.findUnique({
|
||||
where: {
|
||||
id: scheduleId || (await getDefaultScheduleId(userId, prisma)),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
name: true,
|
||||
availability: true,
|
||||
timeZone: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!schedule) {
|
||||
throw new Error("Schedule not found");
|
||||
}
|
||||
const isCurrentUserPartOfTeam = hasReadPermissionsForUserId({ memberId: schedule?.userId, userId });
|
||||
|
||||
const isCurrentUserOwner = schedule?.userId === userId;
|
||||
|
||||
if (!isCurrentUserPartOfTeam && !isCurrentUserOwner) {
|
||||
throw new Error("UNAUTHORIZED");
|
||||
}
|
||||
|
||||
const timeZone = schedule.timeZone || userTimeZone;
|
||||
|
||||
const schedulesCount = await prisma.schedule.count({
|
||||
where: {
|
||||
userId: userId,
|
||||
},
|
||||
});
|
||||
// disabling utc casting while fetching WorkingHours
|
||||
return {
|
||||
id: schedule.id,
|
||||
name: schedule.name,
|
||||
isManaged: schedule.userId !== userId,
|
||||
workingHours: transformWorkingHoursForAtom(schedule),
|
||||
schedule: schedule.availability,
|
||||
availability: transformAvailabilityForAtom(schedule),
|
||||
timeZone,
|
||||
dateOverrides: transformDateOverridesForAtom(schedule, timeZone),
|
||||
isDefault: !scheduleId || defaultScheduleId === schedule.id,
|
||||
isLastSchedule: schedulesCount <= 1,
|
||||
readOnly: schedule.userId !== userId && !isManagedEventType,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
export class TravelScheduleRepository {
|
||||
static async findTravelSchedulesByUserId(userId: number) {
|
||||
const allTravelSchedules = await prisma.travelSchedule.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
startDate: true,
|
||||
endDate: true,
|
||||
timeZone: true,
|
||||
},
|
||||
});
|
||||
|
||||
return allTravelSchedules;
|
||||
}
|
||||
}
|
||||
@@ -238,12 +238,12 @@ export class UserRepository {
|
||||
};
|
||||
}
|
||||
|
||||
static async findById({ id }: { id: number }) {
|
||||
static async findById({ id, select }: { id: number; select?: Prisma.UserSelect }) {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
select: userSelect,
|
||||
select: { ...userSelect, ...select },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { TravelScheduleRepository } from "@calcom/lib/server/repository/travelSchedule";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
type GetTravelSchedulesOptions = {
|
||||
@@ -8,17 +8,5 @@ type GetTravelSchedulesOptions = {
|
||||
};
|
||||
|
||||
export const getTravelSchedulesHandler = async ({ ctx }: GetTravelSchedulesOptions) => {
|
||||
const allTravelSchedules = await prisma.travelSchedule.findMany({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
startDate: true,
|
||||
endDate: true,
|
||||
timeZone: true,
|
||||
},
|
||||
});
|
||||
|
||||
return allTravelSchedules;
|
||||
return await TravelScheduleRepository.findTravelSchedulesByUserId(ctx.user.id);
|
||||
};
|
||||
@@ -1,15 +1,6 @@
|
||||
import {
|
||||
transformAvailabilityForAtom,
|
||||
transformDateOverridesForAtom,
|
||||
transformWorkingHoursForAtom,
|
||||
} from "@calcom/lib";
|
||||
import { hasReadPermissionsForUserId } from "@calcom/lib/hasEditPermissionForUser";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { ScheduleRepository } from "@calcom/lib/server/repository/schedule";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../../trpc";
|
||||
import { getDefaultScheduleId } from "../util";
|
||||
import type { TGetInputSchema } from "./get.schema";
|
||||
|
||||
type GetOptions = {
|
||||
@@ -20,58 +11,11 @@ type GetOptions = {
|
||||
};
|
||||
|
||||
export const getHandler = async ({ ctx, input }: GetOptions) => {
|
||||
const { user } = ctx;
|
||||
|
||||
const schedule = await prisma.schedule.findUnique({
|
||||
where: {
|
||||
id: input.scheduleId || (await getDefaultScheduleId(user.id, prisma)),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
name: true,
|
||||
availability: true,
|
||||
timeZone: true,
|
||||
},
|
||||
return await ScheduleRepository.findDetailedScheduleById({
|
||||
scheduleId: input.scheduleId,
|
||||
isManagedEventType: input.isManagedEventType,
|
||||
userId: ctx.user.id,
|
||||
timeZone: ctx.user.timeZone,
|
||||
defaultScheduleId: ctx.user.defaultScheduleId,
|
||||
});
|
||||
|
||||
if (!schedule) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
});
|
||||
}
|
||||
const isCurrentUserPartOfTeam = hasReadPermissionsForUserId({
|
||||
ctx,
|
||||
input: { memberId: schedule?.userId },
|
||||
});
|
||||
|
||||
const isCurrentUserOwner = schedule?.userId === user.id;
|
||||
|
||||
if (!isCurrentUserPartOfTeam && !isCurrentUserOwner) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
});
|
||||
}
|
||||
|
||||
const timeZone = schedule.timeZone || user.timeZone;
|
||||
|
||||
const schedulesCount = await prisma.schedule.count({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
// disabling utc casting while fetching WorkingHours
|
||||
return {
|
||||
id: schedule.id,
|
||||
name: schedule.name,
|
||||
isManaged: schedule.userId !== user.id,
|
||||
workingHours: transformWorkingHoursForAtom(schedule),
|
||||
schedule: schedule.availability,
|
||||
availability: transformAvailabilityForAtom(schedule),
|
||||
timeZone,
|
||||
dateOverrides: transformDateOverridesForAtom(schedule, timeZone),
|
||||
isDefault: !input.scheduleId || user.defaultScheduleId === schedule.id,
|
||||
isLastSchedule: schedulesCount <= 1,
|
||||
readOnly: schedule.userId !== user.id && !input.isManagedEventType,
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user