feat: checkbox to count managed events for team-wide limits (#16923)
Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
1 parent
0d757039f7
commit
b5fa43f086
17 files changed
+266
-61
No files matched your search
@@ -36,6 +36,7 @@ export const getEventTypesFromDB = async (eventTypeId: number) => {
|
||||
name: true,
|
||||
parentId: true,
|
||||
bookingLimits: true,
|
||||
includeManagedEventsInLimits: true,
|
||||
},
|
||||
},
|
||||
bookingFields: true,
|
||||
@@ -73,6 +74,13 @@ export const getEventTypesFromDB = async (eventTypeId: number) => {
|
||||
parent: {
|
||||
select: {
|
||||
teamId: true,
|
||||
team: {
|
||||
select: {
|
||||
id: true,
|
||||
bookingLimits: true,
|
||||
includeManagedEventsInLimits: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
useEventTypeDestinationCalendarEmail: true,
|
||||
|
||||
@@ -85,6 +85,7 @@ describe(
|
||||
team: {
|
||||
id: 1,
|
||||
bookingLimits: { PER_WEEK: 2 },
|
||||
includeManagedEventsInLimits: false,
|
||||
},
|
||||
schedulingType: SchedulingType.COLLECTIVE,
|
||||
},
|
||||
@@ -101,6 +102,27 @@ describe(
|
||||
teamId: 1,
|
||||
schedulingType: SchedulingType.COLLECTIVE,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
slotInterval: eventLength,
|
||||
length: eventLength,
|
||||
hosts: [
|
||||
{
|
||||
userId: 101,
|
||||
},
|
||||
],
|
||||
teamId: 1,
|
||||
schedulingType: SchedulingType.MANAGED,
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
slotInterval: eventLength,
|
||||
length: eventLength,
|
||||
userId: 101,
|
||||
parent: {
|
||||
id: 3,
|
||||
},
|
||||
},
|
||||
],
|
||||
bookings: [
|
||||
{
|
||||
@@ -124,13 +146,21 @@ describe(
|
||||
startTime: `2024-08-06T04:30:00.000Z`,
|
||||
endTime: `2024-08-06T05:00:00.000Z`,
|
||||
},
|
||||
{
|
||||
// managed event type doesn't count, includeManagedEventsInLimits is false
|
||||
eventTypeId: 4,
|
||||
userId: 101,
|
||||
status: BookingStatus.ACCEPTED,
|
||||
startTime: `2024-08-07T04:30:00.000Z`,
|
||||
endTime: `2024-08-07T05:00:00.000Z`,
|
||||
},
|
||||
],
|
||||
organizer,
|
||||
usersApartFromOrganizer: otherTeamMembers,
|
||||
})
|
||||
);
|
||||
|
||||
const mockBookingData1 = getMockRequestDataForBooking({
|
||||
const mockBookingWithinLimit = getMockRequestDataForBooking({
|
||||
data: {
|
||||
start: `2024-08-08T04:00:00.000Z`,
|
||||
end: `2024-08-08T04:30:00.000Z`,
|
||||
@@ -145,7 +175,7 @@ describe(
|
||||
|
||||
const { req: req1 } = createMockNextJsRequest({
|
||||
method: "POST",
|
||||
body: mockBookingData1,
|
||||
body: mockBookingWithinLimit,
|
||||
});
|
||||
|
||||
const createdBooking = await handleNewBooking(req1);
|
||||
@@ -157,7 +187,7 @@ describe(
|
||||
})
|
||||
);
|
||||
|
||||
const mockBookingData2 = getMockRequestDataForBooking({
|
||||
const mockBookingAboveLimit = getMockRequestDataForBooking({
|
||||
data: {
|
||||
start: `2024-08-08T04:00:00.000Z`,
|
||||
end: `2024-08-08T04:30:00.000Z`,
|
||||
@@ -172,7 +202,7 @@ describe(
|
||||
|
||||
const { req: req2 } = createMockNextJsRequest({
|
||||
method: "POST",
|
||||
body: mockBookingData2,
|
||||
body: mockBookingAboveLimit,
|
||||
});
|
||||
|
||||
// this is the third team booking of this week for user 101, limit reached
|
||||
@@ -221,7 +251,7 @@ describe(
|
||||
})
|
||||
);
|
||||
|
||||
const mockBookingData1 = getMockRequestDataForBooking({
|
||||
const mockBookingWithinLimit = getMockRequestDataForBooking({
|
||||
data: {
|
||||
start: `2024-08-07T04:30:00.000Z`,
|
||||
end: `2024-08-07T05:00:00.000Z`,
|
||||
@@ -236,7 +266,7 @@ describe(
|
||||
|
||||
const { req: req1 } = createMockNextJsRequest({
|
||||
method: "POST",
|
||||
body: mockBookingData1,
|
||||
body: mockBookingWithinLimit,
|
||||
});
|
||||
|
||||
const createdBooking = await handleNewBooking(req1);
|
||||
@@ -248,7 +278,7 @@ describe(
|
||||
})
|
||||
);
|
||||
|
||||
const mockBookingData2 = getMockRequestDataForBooking({
|
||||
const mockBookingAboveLimit = getMockRequestDataForBooking({
|
||||
data: {
|
||||
start: `2024-08-07T04:00:00.000Z`,
|
||||
end: `2024-08-07T04:30:00.000Z`,
|
||||
@@ -263,10 +293,10 @@ describe(
|
||||
|
||||
const { req: req2 } = createMockNextJsRequest({
|
||||
method: "POST",
|
||||
body: mockBookingData1,
|
||||
body: mockBookingAboveLimit,
|
||||
});
|
||||
|
||||
// this is the second team booking of this days for user 101, limit reached
|
||||
// this is the second team booking of this day for user 101, limit reached
|
||||
await expect(async () => await handleNewBooking(req2)).rejects.toThrowError(
|
||||
"no_available_users_found_error"
|
||||
);
|
||||
@@ -288,7 +318,8 @@ describe(
|
||||
],
|
||||
team: {
|
||||
id: 1,
|
||||
bookingLimits: { PER_MONTH: 3 },
|
||||
bookingLimits: { PER_MONTH: 4 },
|
||||
includeManagedEventsInLimits: true,
|
||||
},
|
||||
schedulingType: SchedulingType.COLLECTIVE,
|
||||
},
|
||||
@@ -305,6 +336,27 @@ describe(
|
||||
teamId: 1,
|
||||
schedulingType: SchedulingType.COLLECTIVE,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
slotInterval: eventLength,
|
||||
length: eventLength,
|
||||
hosts: [
|
||||
{
|
||||
userId: 101,
|
||||
},
|
||||
],
|
||||
teamId: 1,
|
||||
schedulingType: SchedulingType.MANAGED,
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
slotInterval: eventLength,
|
||||
length: eventLength,
|
||||
userId: 101,
|
||||
parent: {
|
||||
id: 3,
|
||||
},
|
||||
},
|
||||
],
|
||||
bookings: [
|
||||
{
|
||||
@@ -321,13 +373,21 @@ describe(
|
||||
startTime: `2024-08-22T03:30:00.000Z`,
|
||||
endTime: `2024-08-22T04:00:00.000Z`,
|
||||
},
|
||||
{
|
||||
//managed event type also counts towards limits
|
||||
eventTypeId: 4,
|
||||
userId: 101,
|
||||
status: BookingStatus.ACCEPTED,
|
||||
startTime: `2024-08-15T03:30:00.000Z`,
|
||||
endTime: `2024-08-15T04:00:00.000Z`,
|
||||
},
|
||||
],
|
||||
organizer,
|
||||
usersApartFromOrganizer: otherTeamMembers,
|
||||
})
|
||||
);
|
||||
|
||||
const mockBookingData1 = getMockRequestDataForBooking({
|
||||
const mockBookingWithinLimit = getMockRequestDataForBooking({
|
||||
data: {
|
||||
start: `2024-08-29T04:30:00.000Z`,
|
||||
end: `2024-08-29T05:00:00.000Z`,
|
||||
@@ -342,7 +402,7 @@ describe(
|
||||
|
||||
const { req: req1 } = createMockNextJsRequest({
|
||||
method: "POST",
|
||||
body: mockBookingData1,
|
||||
body: mockBookingWithinLimit,
|
||||
});
|
||||
|
||||
const createdBooking = await handleNewBooking(req1);
|
||||
@@ -354,7 +414,7 @@ describe(
|
||||
})
|
||||
);
|
||||
|
||||
const mockBookingData2 = getMockRequestDataForBooking({
|
||||
const mockBookingAboveLimit = getMockRequestDataForBooking({
|
||||
data: {
|
||||
start: `2024-08-25T04:00:00.000Z`,
|
||||
end: `2024-08-25T04:30:00.000Z`,
|
||||
@@ -369,10 +429,10 @@ describe(
|
||||
|
||||
const { req: req2 } = createMockNextJsRequest({
|
||||
method: "POST",
|
||||
body: mockBookingData1,
|
||||
body: mockBookingAboveLimit,
|
||||
});
|
||||
|
||||
// this is the second team booking of this days for user 101, limit reached
|
||||
// this is the firth team booking (incl. managed) of this month for user 101, limit reached
|
||||
await expect(async () => await handleNewBooking(req2)).rejects.toThrowError(
|
||||
"no_available_users_found_error"
|
||||
);
|
||||
@@ -394,7 +454,8 @@ describe(
|
||||
],
|
||||
team: {
|
||||
id: 1,
|
||||
bookingLimits: { PER_YEAR: 2 },
|
||||
bookingLimits: { PER_YEAR: 3 },
|
||||
includeManagedEventsInLimits: true,
|
||||
},
|
||||
schedulingType: SchedulingType.COLLECTIVE,
|
||||
},
|
||||
@@ -411,6 +472,27 @@ describe(
|
||||
teamId: 1,
|
||||
schedulingType: SchedulingType.COLLECTIVE,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
slotInterval: eventLength,
|
||||
length: eventLength,
|
||||
hosts: [
|
||||
{
|
||||
userId: 101,
|
||||
},
|
||||
],
|
||||
teamId: 1,
|
||||
schedulingType: SchedulingType.MANAGED,
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
slotInterval: eventLength,
|
||||
length: eventLength,
|
||||
userId: 101,
|
||||
parent: {
|
||||
id: 3,
|
||||
},
|
||||
},
|
||||
],
|
||||
bookings: [
|
||||
{
|
||||
@@ -418,6 +500,13 @@ describe(
|
||||
userId: 101,
|
||||
status: BookingStatus.ACCEPTED,
|
||||
startTime: `2024-02-03T03:30:00.000Z`,
|
||||
endTime: `2024-02-03T04:00:00.000Z`,
|
||||
},
|
||||
{
|
||||
eventTypeId: 4,
|
||||
userId: 101,
|
||||
status: BookingStatus.ACCEPTED,
|
||||
startTime: `2024-08-03T03:30:00.000Z`,
|
||||
endTime: `2024-08-03T04:00:00.000Z`,
|
||||
},
|
||||
],
|
||||
@@ -426,7 +515,7 @@ describe(
|
||||
})
|
||||
);
|
||||
|
||||
const mockBookingData1 = getMockRequestDataForBooking({
|
||||
const mockBookingWithinLimit = getMockRequestDataForBooking({
|
||||
data: {
|
||||
start: `2024-08-29T04:30:00.000Z`,
|
||||
end: `2024-08-29T05:00:00.000Z`,
|
||||
@@ -441,7 +530,7 @@ describe(
|
||||
|
||||
const { req: req1 } = createMockNextJsRequest({
|
||||
method: "POST",
|
||||
body: mockBookingData1,
|
||||
body: mockBookingWithinLimit,
|
||||
});
|
||||
|
||||
const createdBooking = await handleNewBooking(req1);
|
||||
@@ -453,7 +542,7 @@ describe(
|
||||
})
|
||||
);
|
||||
|
||||
const mockBookingData2 = getMockRequestDataForBooking({
|
||||
const mockBookingAboveLimit = getMockRequestDataForBooking({
|
||||
data: {
|
||||
start: `2024-11-25T04:00:00.000Z`,
|
||||
end: `2024-11-25T04:30:00.000Z`,
|
||||
@@ -468,10 +557,9 @@ describe(
|
||||
|
||||
const { req: req2 } = createMockNextJsRequest({
|
||||
method: "POST",
|
||||
body: mockBookingData1,
|
||||
body: mockBookingAboveLimit,
|
||||
});
|
||||
|
||||
// this is the second team booking of this days for user 101, limit reached
|
||||
await expect(async () => await handleNewBooking(req2)).rejects.toThrowError(
|
||||
"no_available_users_found_error"
|
||||
);
|
||||
|
||||
@@ -14,7 +14,7 @@ import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
import type { IntervalLimit } from "@calcom/types/Calendar";
|
||||
import { Button, Form, SettingsToggle, showToast } from "@calcom/ui";
|
||||
import { Button, CheckboxField, Form, SettingsToggle, showToast } from "@calcom/ui";
|
||||
|
||||
type ProfileViewProps = { team: RouterOutputs["viewer"]["teams"]["getMinimal"] };
|
||||
|
||||
@@ -22,9 +22,10 @@ const BookingLimitsView = ({ team }: ProfileViewProps) => {
|
||||
const { t } = useLocale();
|
||||
const utils = trpc.useUtils();
|
||||
|
||||
const form = useForm<{ bookingLimits?: IntervalLimit }>({
|
||||
const form = useForm<{ bookingLimits?: IntervalLimit; includeManagedEventsInLimits: boolean }>({
|
||||
defaultValues: {
|
||||
bookingLimits: team?.bookingLimits || undefined,
|
||||
includeManagedEventsInLimits: team?.includeManagedEventsInLimits ?? false,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -40,7 +41,10 @@ const BookingLimitsView = ({ team }: ProfileViewProps) => {
|
||||
async onSuccess(res) {
|
||||
await utils.viewer.teams.get.invalidate();
|
||||
if (res) {
|
||||
reset({ bookingLimits: res.bookingLimits });
|
||||
reset({
|
||||
bookingLimits: res.bookingLimits,
|
||||
includeManagedEventsInLimits: res.includeManagedEventsInLimits,
|
||||
});
|
||||
}
|
||||
showToast(t("booking_limits_updated_successfully"), "success");
|
||||
},
|
||||
@@ -83,9 +87,12 @@ const BookingLimitsView = ({ team }: ProfileViewProps) => {
|
||||
});
|
||||
} else {
|
||||
form.setValue("bookingLimits", {});
|
||||
form.setValue("includeManagedEventsInLimits", false);
|
||||
}
|
||||
const bookingLimits = form.getValues("bookingLimits");
|
||||
mutation.mutate({ bookingLimits, id: team.id });
|
||||
const includeManagedEventsInLimits = form.getValues("includeManagedEventsInLimits");
|
||||
|
||||
mutation.mutate({ bookingLimits, includeManagedEventsInLimits, id: team.id });
|
||||
}}
|
||||
switchContainerClassName={classNames(
|
||||
"border-subtle mt-6 rounded-lg border py-6 px-4 sm:px-6",
|
||||
@@ -93,7 +100,21 @@ const BookingLimitsView = ({ team }: ProfileViewProps) => {
|
||||
)}
|
||||
childrenClassName="lg:ml-0">
|
||||
<div className="border-subtle border border-y-0 p-6">
|
||||
<IntervalLimitsManager propertyName="bookingLimits" defaultLimit={1} step={1} />
|
||||
<Controller
|
||||
name="includeManagedEventsInLimits"
|
||||
render={({ field: { value, onChange } }) => (
|
||||
<CheckboxField
|
||||
description={t("count_managed_to_limit")}
|
||||
descriptionAsLabel
|
||||
onChange={(e) => onChange(e)}
|
||||
checked={value}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
|
||||
<div className="pt-6">
|
||||
<IntervalLimitsManager propertyName="bookingLimits" defaultLimit={1} step={1} />
|
||||
</div>
|
||||
</div>
|
||||
<SectionBottomActions className="mb-6" align="end">
|
||||
<Button disabled={isSubmitting || !isDirty} type="submit" color="primary">
|
||||
|
||||
Reference in new issue
Block a user