feat: Schedule restriction and booker timezone (#21452)
* init migration and fix name for team event availability toggle * show and save restriction schedule * better naming around * fix loading stored restricted schedule instead of default * add the booker timezone checkbox in the form * restriction schedule application babbbyyyy * remove logs * test * typefix * fix * fix?? * fix??? * --- * typefix * functional fix * restrictionschedule logic --1 * timezone adjustment for regular constraint * noice * test suite for restrictionSchedule * auth restrictionschedule for eventtype * remove unnecessary comments * index * type fix * add concurrent * resolve change request * fix unauth erro * type fix * schedule select extracted from select * revert * fix auth vulnerabililty * fix e22 * chore: hide restriction schedule on platform * init review feedback resolutions * fixes * fix type * multiple windows of recurring rule-same day * team feature flag * fix type err * fix type * improvements * fix test * improve * fix error propagation * fix unused var lint * cleanup and using buildDateRanges * travel schedule inclusion * address comment --------- Co-authored-by: supalarry <[email protected]>
This commit is contained in:
1 parent
3bea03f4b1
commit
e160a4add0
28 files changed
+1307
-73
No files matched your search
@@ -94,6 +94,7 @@ export const duplicateHandler = async ({ ctx, input }: DuplicateOptions) => {
|
||||
descriptionAsSafeHTML: _descriptionAsSafeHTML,
|
||||
secondaryEmailId,
|
||||
instantMeetingScheduleId: _instantMeetingScheduleId,
|
||||
restrictionScheduleId: _restrictionScheduleId,
|
||||
...rest
|
||||
} = eventType;
|
||||
|
||||
@@ -113,7 +114,13 @@ export const duplicateHandler = async ({ ctx, input }: DuplicateOptions) => {
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
|
||||
restrictionSchedule: _restrictionScheduleId
|
||||
? {
|
||||
connect: {
|
||||
id: _restrictionScheduleId,
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
recurringEvent: recurringEvent || undefined,
|
||||
bookingLimits: bookingLimits ?? undefined,
|
||||
durationLimits: durationLimits ?? undefined,
|
||||
|
||||
@@ -13,6 +13,8 @@ import { validateIntervalLimitOrder } from "@calcom/lib/intervalLimits/validateI
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { CalVideoSettingsRepository } from "@calcom/lib/server/repository/calVideoSettings";
|
||||
import { MembershipRepository } from "@calcom/lib/server/repository/membership";
|
||||
import { ScheduleRepository } from "@calcom/lib/server/repository/schedule";
|
||||
import { validateBookerLayouts } from "@calcom/lib/validateBookerLayouts";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import { WorkflowTriggerEvents } from "@calcom/prisma/client";
|
||||
@@ -87,6 +89,7 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
description: newDescription,
|
||||
title: newTitle,
|
||||
seatsPerTimeSlot,
|
||||
restrictionScheduleId,
|
||||
calVideoSettings,
|
||||
...rest
|
||||
} = input;
|
||||
@@ -340,6 +343,45 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
};
|
||||
}
|
||||
|
||||
const membershipRepo = new MembershipRepository(ctx.prisma);
|
||||
|
||||
if (restrictionScheduleId) {
|
||||
// Verify that the user owns the restriction schedule or is a team member
|
||||
const scheduleRepo = new ScheduleRepository(ctx.prisma);
|
||||
const restrictionSchedule = await scheduleRepo.findScheduleByIdForOwnershipCheck({
|
||||
scheduleId: restrictionScheduleId,
|
||||
});
|
||||
// If the user doesn't own the schedule, check if they're a team member
|
||||
if (restrictionSchedule?.userId !== ctx.user.id) {
|
||||
if (!teamId || !restrictionSchedule) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "The restriction schedule is not owned by you or your team",
|
||||
});
|
||||
}
|
||||
const hasMembership = await membershipRepo.hasMembership({
|
||||
teamId,
|
||||
userId: restrictionSchedule.userId,
|
||||
});
|
||||
if (!hasMembership) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "The restriction schedule is not owned by you or your team",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
data.restrictionSchedule = {
|
||||
connect: {
|
||||
id: restrictionScheduleId,
|
||||
},
|
||||
};
|
||||
} else if (restrictionScheduleId === null || restrictionScheduleId === 0) {
|
||||
data.restrictionSchedule = {
|
||||
disconnect: true,
|
||||
};
|
||||
}
|
||||
|
||||
if (users?.length) {
|
||||
data.users = {
|
||||
set: [],
|
||||
@@ -349,14 +391,7 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
|
||||
if (teamId && hosts) {
|
||||
// check if all hosts can be assigned (memberships that have accepted invite)
|
||||
const memberships =
|
||||
(await ctx.prisma.membership.findMany({
|
||||
where: {
|
||||
teamId,
|
||||
accepted: true,
|
||||
},
|
||||
})) || [];
|
||||
const teamMemberIds = memberships.map((membership) => membership.userId);
|
||||
const teamMemberIds = await membershipRepo.listAcceptedTeamMemberIds({ teamId });
|
||||
// guard against missing IDs, this may mean a member has just been removed
|
||||
// or this request was forged.
|
||||
// we let this pass through on organization sub-teams
|
||||
|
||||
@@ -11,6 +11,7 @@ import { getShouldServeCache } from "@calcom/features/calendar-cache/lib/getShou
|
||||
import { findQualifiedHostsWithDelegationCredentials } from "@calcom/lib/bookings/findQualifiedHostsWithDelegationCredentials";
|
||||
import { shouldIgnoreContactOwner } from "@calcom/lib/bookings/routing/utils";
|
||||
import { RESERVED_SUBDOMAINS } from "@calcom/lib/constants";
|
||||
import { buildDateRanges } from "@calcom/lib/date-ranges";
|
||||
import { getUTCOffsetByTimezone } from "@calcom/lib/dayjs";
|
||||
import { getDefaultEvent } from "@calcom/lib/defaultEvents";
|
||||
import { getAggregatedAvailability } from "@calcom/lib/getAggregatedAvailability";
|
||||
@@ -36,6 +37,7 @@ import {
|
||||
isTimeViolatingFutureLimit,
|
||||
} from "@calcom/lib/isOutOfBounds";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { isRestrictionScheduleEnabled } from "@calcom/lib/restrictionSchedule";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import { withReporting } from "@calcom/lib/sentryWrapper";
|
||||
import { getTotalBookingDuration } from "@calcom/lib/server/queries/booking";
|
||||
@@ -497,6 +499,94 @@ const _getAvailableSlots = async ({ input, ctx }: GetScheduleOptions): Promise<I
|
||||
|
||||
let availableTimeSlots: typeof timeSlots = [];
|
||||
const bookerClientUid = ctx?.req?.cookies?.uid;
|
||||
const isRestrictionScheduleFeatureEnabled = await isRestrictionScheduleEnabled(eventType.team?.id);
|
||||
if (eventType.restrictionScheduleId && isRestrictionScheduleFeatureEnabled) {
|
||||
const restrictionSchedule = await prisma.schedule.findUnique({
|
||||
where: { id: eventType.restrictionScheduleId },
|
||||
select: {
|
||||
id: true,
|
||||
timeZone: true,
|
||||
userId: true,
|
||||
availability: {
|
||||
select: {
|
||||
days: true,
|
||||
startTime: true,
|
||||
endTime: true,
|
||||
date: true,
|
||||
},
|
||||
},
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
defaultScheduleId: true,
|
||||
travelSchedules: {
|
||||
select: {
|
||||
id: true,
|
||||
timeZone: true,
|
||||
startDate: true,
|
||||
endDate: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (restrictionSchedule) {
|
||||
if (!eventType.useBookerTimezone && !restrictionSchedule.timeZone) {
|
||||
throw new TRPCError({
|
||||
message: "No timezone is set for the restricted schedule",
|
||||
code: "BAD_REQUEST",
|
||||
});
|
||||
}
|
||||
|
||||
const restrictionTimezone = eventType.useBookerTimezone
|
||||
? input.timeZone
|
||||
: restrictionSchedule.timeZone!;
|
||||
const eventLength = input.duration || eventType.length;
|
||||
|
||||
const restrictionAvailability = restrictionSchedule.availability.map((rule) => ({
|
||||
days: rule.days,
|
||||
startTime: rule.startTime,
|
||||
endTime: rule.endTime,
|
||||
date: rule.date,
|
||||
}));
|
||||
|
||||
// Include travel schedules if restriction schedule is the user's default schedule
|
||||
const isDefaultSchedule = restrictionSchedule.user.defaultScheduleId === restrictionSchedule.id;
|
||||
const travelSchedules =
|
||||
isDefaultSchedule && !eventType.useBookerTimezone
|
||||
? restrictionSchedule.user.travelSchedules.map((schedule) => ({
|
||||
startDate: dayjs(schedule.startDate),
|
||||
endDate: schedule.endDate ? dayjs(schedule.endDate) : undefined,
|
||||
timeZone: schedule.timeZone,
|
||||
}))
|
||||
: [];
|
||||
|
||||
const { dateRanges: restrictionRanges } = buildDateRanges({
|
||||
availability: restrictionAvailability,
|
||||
timeZone: restrictionTimezone || "UTC",
|
||||
dateFrom: startTime,
|
||||
dateTo: endTime,
|
||||
travelSchedules,
|
||||
});
|
||||
|
||||
availableTimeSlots = timeSlots.filter((slot) => {
|
||||
const slotStart = slot.time;
|
||||
const slotEnd = slot.time.add(eventLength, "minute");
|
||||
|
||||
return restrictionRanges.some(
|
||||
(range) =>
|
||||
(slotStart.isAfter(range.start) || slotStart.isSame(range.start)) &&
|
||||
(slotEnd.isBefore(range.end) || slotEnd.isSame(range.end))
|
||||
);
|
||||
});
|
||||
} else {
|
||||
availableTimeSlots = timeSlots;
|
||||
}
|
||||
} else {
|
||||
availableTimeSlots = timeSlots;
|
||||
}
|
||||
|
||||
const reservedSlots = await _getReservedSlotsAndCleanupExpired({
|
||||
bookerClientUid,
|
||||
@@ -504,8 +594,6 @@ const _getAvailableSlots = async ({ input, ctx }: GetScheduleOptions): Promise<I
|
||||
usersWithCredentials,
|
||||
});
|
||||
|
||||
availableTimeSlots = timeSlots;
|
||||
|
||||
const availabilityCheckProps = {
|
||||
eventLength: input.duration || eventType.length,
|
||||
currentSeats,
|
||||
|
||||
Reference in new issue
Block a user