* add feature to change host schedule * update for type-check:ci * Update SingleForm.tsx for linterr * update as per design review * update for rerendering * migration files * On Delete set null for schedule if schedule is deleted, no need to delete the host * updated schema * added testcases * checking permissions before * updated for comments * update for initial value * updated and cleanup * set initial value * update after merge * updated nit style * update for console warning and added comments * nit/chore * chore * update for typechecks * update for scheduleId param after merge * update after merge or new changes in metadata.config * update after weights functionality * updated description text and column name as per review suggestion * resolved merge conflicts * fix typecheck due to changes in getScheduleSchema * update after merge having refactor * update refactored EventTypeWebWrapper after merge * updated props * updated trpc err code and added log * update due to refactor in EventAvailabilityTab * perf: Optimise TS type + select only the data that's needed for a specific function --------- Co-authored-by: Amit Sharma <74371312+Amit91848@users.noreply.github.com> Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com> Co-authored-by: Alex van Andel <me@alexvanandel.com>
63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import { hasReadPermissionsForUserId } from "@calcom/lib/hasEditPermissionForUser";
|
|
import logger from "@calcom/lib/logger";
|
|
import { prisma } from "@calcom/prisma";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../../trpc";
|
|
import { getDefaultScheduleId } from "../util";
|
|
import type { TGetAllByUserIdInputSchema } from "./getAllSchedulesByUserId.schema";
|
|
|
|
const log = logger.getSubLogger({ prefix: ["getAllSchedulesByUserIdHandler"] });
|
|
|
|
type GetOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TGetAllByUserIdInputSchema;
|
|
};
|
|
|
|
export const getAllSchedulesByUserIdHandler = async ({ ctx, input }: GetOptions) => {
|
|
const { user } = ctx;
|
|
|
|
const isCurrentUserPartOfTeam = hasReadPermissionsForUserId({ memberId: input?.userId, userId: user.id });
|
|
|
|
const isCurrentUserOwner = input?.userId === user.id;
|
|
|
|
if (!isCurrentUserPartOfTeam && !isCurrentUserOwner) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
});
|
|
}
|
|
|
|
const schedules = await prisma.schedule.findMany({
|
|
where: {
|
|
userId: input.userId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
userId: true,
|
|
name: true,
|
|
},
|
|
});
|
|
|
|
if (!schedules) {
|
|
log.error(`No Schedules found for userId:${input.userId}`);
|
|
throw new TRPCError({
|
|
code: "NOT_FOUND",
|
|
});
|
|
}
|
|
|
|
const defaultScheduleId = await getDefaultScheduleId(input.userId, prisma);
|
|
|
|
return {
|
|
schedules: schedules.map((schedule) => {
|
|
return {
|
|
...schedule,
|
|
isDefault: schedule.id === defaultScheduleId,
|
|
readOnly: schedule.userId !== user.id,
|
|
};
|
|
}),
|
|
};
|
|
};
|