feat: #15416 Expand round robin availability beyond default schedule (#15618)

* 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 <[email protected]>
Co-authored-by: Syed Ali Shahbaz <[email protected]>
Co-authored-by: Alex van Andel <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-10-15 17:36:00 +01:00
1 parent c21f0caecf
commit a48d190af3
27 files changed
+600 -67

No files matched your search

@@ -5,6 +5,7 @@ import { ZCreateInputSchema } from "./create.schema";
import { ZDeleteInputSchema } from "./delete.schema";
import { ZScheduleDuplicateSchema } from "./duplicate.schema";
import { ZGetInputSchema } from "./get.schema";
import { ZGetAllByUserIdInputSchema } from "./getAllSchedulesByUserId.schema";
import { ZGetByEventSlugInputSchema } from "./getScheduleByEventTypeSlug.schema";
import { ZGetByUserIdInputSchema } from "./getScheduleByUserId.schema";
import { ZUpdateInputSchema } from "./update.schema";
@@ -16,6 +17,7 @@ type ScheduleRouterHandlerCache = {
update?: typeof import("./update.handler").updateHandler;
duplicate?: typeof import("./duplicate.handler").duplicateHandler;
getScheduleByUserId?: typeof import("./getScheduleByUserId.handler").getScheduleByUserIdHandler;
getAllSchedulesByUserId?: typeof import("./getAllSchedulesByUserId.handler").getAllSchedulesByUserIdHandler;
getScheduleByEventSlug?: typeof import("./getScheduleByEventTypeSlug.handler").getScheduleByEventSlugHandler;
bulkUpdateToDefaultAvailability?: typeof import("./bulkUpdateDefaultAvailability.handler").bulkUpdateToDefaultAvailabilityHandler;
};
@@ -122,6 +124,25 @@ export const scheduleRouter = router({
input,
});
}),
getAllSchedulesByUserId: authedProcedure.input(ZGetAllByUserIdInputSchema).query(async ({ input, ctx }) => {
if (!UNSTABLE_HANDLER_CACHE.getAllSchedulesByUserId) {
UNSTABLE_HANDLER_CACHE.getAllSchedulesByUserId = await import("./getAllSchedulesByUserId.handler").then(
(mod) => mod.getAllSchedulesByUserIdHandler
);
}
// Unreachable code but required for type safety
if (!UNSTABLE_HANDLER_CACHE.getAllSchedulesByUserId) {
throw new Error("Failed to load handler");
}
return UNSTABLE_HANDLER_CACHE.getAllSchedulesByUserId({
ctx,
input,
});
}),
getScheduleByEventSlug: authedProcedure.input(ZGetByEventSlugInputSchema).query(async ({ input, ctx }) => {
if (!UNSTABLE_HANDLER_CACHE.getScheduleByEventSlug) {
UNSTABLE_HANDLER_CACHE.getScheduleByEventSlug = await import(
@@ -0,0 +1,62 @@
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,
};
}),
};
};
@@ -0,0 +1,7 @@
import { z } from "zod";
export const ZGetAllByUserIdInputSchema = z.object({
userId: z.number(),
});
export type TGetAllByUserIdInputSchema = z.infer<typeof ZGetAllByUserIdInputSchema>;
@@ -57,6 +57,7 @@ export const EventTypeUpdateInput = _EventTypeModel
isFixed: z.boolean().optional(),
priority: z.number().min(0).max(4).optional().nullable(),
weight: z.number().min(0).optional().nullable(),
scheduleId: z.number().optional().nullable(),
})
)
.optional(),
@@ -191,6 +191,7 @@ type Host = {
isFixed?: boolean | undefined;
priority?: number | null | undefined;
weight?: number | null | undefined;
scheduleId?: number | null | undefined;
};
type User = {
@@ -230,6 +231,7 @@ export async function addWeightAdjustmentToNewHosts({
weightAdjustment: true,
priority: true,
weight: true,
scheduleId: true,
},
},
},
@@ -249,6 +251,7 @@ export async function addWeightAdjustmentToNewHosts({
id: user.id,
email: user.email,
},
scheduleId: hostData?.scheduleId ?? null,
};
});
@@ -263,6 +266,7 @@ export async function addWeightAdjustmentToNewHosts({
priority: host.priority,
weight: host.weight,
weightAdjustment: host.weightAdjustment,
scheduleId: host.scheduleId,
}));
}
@@ -303,6 +307,7 @@ export async function addWeightAdjustmentToNewHosts({
priority: host.priority,
weight: host.weight,
weightAdjustment: weightAdjustment > 0 ? Math.floor(weightAdjustment) : 0,
scheduleId: host.scheduleId,
};
})
);
@@ -228,6 +228,20 @@ export async function getEventType(
...availabilityUserSelect,
},
},
schedule: {
select: {
availability: {
select: {
date: true,
startTime: true,
endTime: true,
days: true,
},
},
timeZone: true,
id: true,
},
},
},
},
users: {
@@ -444,9 +458,17 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Pro
}
let currentSeats: CurrentSeats | undefined;
const eventHosts =
const eventHosts: {
isFixed: boolean;
email: string;
user: (typeof eventType.hosts)[number]["user"];
}[] =
eventType.hosts?.length && eventType.schedulingType
? eventType.hosts
? eventType.hosts.map((host) => ({
isFixed: host.isFixed,
email: host.user.email,
user: host.user,
}))
: eventType.users.map((user) => {
return {
isFixed: !eventType.schedulingType || eventType.schedulingType === SchedulingType.COLLECTIVE,