[CAL-988] Limit total appointment time per day/week/month/year (#7166)
Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: Omar López <[email protected]>
This commit is contained in:
28 files changed
+664
-228
No files matched your search
@@ -1,14 +1,14 @@
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { validateBookingLimitOrder } from "@calcom/lib/isBookingLimits";
|
||||
import { checkBookingLimits, checkLimit } from "@calcom/lib/server";
|
||||
import type { BookingLimit } from "@calcom/types/Calendar";
|
||||
import { validateIntervalLimitOrder } from "@calcom/lib";
|
||||
import { checkBookingLimits, checkBookingLimit } from "@calcom/lib/server";
|
||||
import type { IntervalLimit } from "@calcom/types/Calendar";
|
||||
|
||||
import { prismaMock } from "../../../../tests/config/singleton";
|
||||
|
||||
type Mockdata = {
|
||||
id: number;
|
||||
startDate: Date;
|
||||
bookingLimits: BookingLimit;
|
||||
bookingLimits: IntervalLimit;
|
||||
};
|
||||
|
||||
const MOCK_DATA: Mockdata = {
|
||||
@@ -63,7 +63,7 @@ describe("Check Booking Limits Tests", () => {
|
||||
it("Should handle mutiple limits correctly", async () => {
|
||||
prismaMock.booking.count.mockResolvedValue(1);
|
||||
expect(
|
||||
checkLimit({
|
||||
checkBookingLimit({
|
||||
key: "PER_DAY",
|
||||
limitingNumber: 2,
|
||||
eventStartDate: MOCK_DATA.startDate,
|
||||
@@ -72,7 +72,7 @@ describe("Check Booking Limits Tests", () => {
|
||||
).resolves.not.toThrow();
|
||||
prismaMock.booking.count.mockResolvedValue(3);
|
||||
expect(
|
||||
checkLimit({
|
||||
checkBookingLimit({
|
||||
key: "PER_WEEK",
|
||||
limitingNumber: 2,
|
||||
eventStartDate: MOCK_DATA.startDate,
|
||||
@@ -83,7 +83,7 @@ describe("Check Booking Limits Tests", () => {
|
||||
it("Should return busyTimes when set", async () => {
|
||||
prismaMock.booking.count.mockResolvedValue(2);
|
||||
expect(
|
||||
checkLimit({
|
||||
checkBookingLimit({
|
||||
key: "PER_DAY",
|
||||
limitingNumber: 2,
|
||||
eventStartDate: MOCK_DATA.startDate,
|
||||
@@ -99,21 +99,21 @@ describe("Check Booking Limits Tests", () => {
|
||||
|
||||
describe("Booking limit validation", () => {
|
||||
it("Should validate a correct limit", () => {
|
||||
expect(validateBookingLimitOrder({ PER_DAY: 3, PER_MONTH: 5 })).toBe(true);
|
||||
expect(validateIntervalLimitOrder({ PER_DAY: 3, PER_MONTH: 5 })).toBe(true);
|
||||
});
|
||||
|
||||
it("Should invalidate an incorrect limit", () => {
|
||||
expect(validateBookingLimitOrder({ PER_DAY: 9, PER_MONTH: 5 })).toBe(false);
|
||||
expect(validateIntervalLimitOrder({ PER_DAY: 9, PER_MONTH: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
it("Should validate a correct limit with 'gaps' ", () => {
|
||||
expect(validateBookingLimitOrder({ PER_DAY: 9, PER_YEAR: 25 })).toBe(true);
|
||||
expect(validateIntervalLimitOrder({ PER_DAY: 9, PER_YEAR: 25 })).toBe(true);
|
||||
});
|
||||
|
||||
it("Should validate a correct limit with equal values ", () => {
|
||||
expect(validateBookingLimitOrder({ PER_DAY: 1, PER_YEAR: 1 })).toBe(true);
|
||||
expect(validateIntervalLimitOrder({ PER_DAY: 1, PER_YEAR: 1 })).toBe(true);
|
||||
});
|
||||
it("Should validate a correct with empty", () => {
|
||||
expect(validateBookingLimitOrder({})).toBe(true);
|
||||
expect(validateIntervalLimitOrder({})).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,128 @@
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { validateIntervalLimitOrder } from "@calcom/lib";
|
||||
import { checkDurationLimit, checkDurationLimits } from "@calcom/lib/server";
|
||||
|
||||
import { prismaMock } from "../../../../tests/config/singleton";
|
||||
|
||||
type MockData = {
|
||||
id: number;
|
||||
startDate: Date;
|
||||
};
|
||||
|
||||
const MOCK_DATA: MockData = {
|
||||
id: 1,
|
||||
startDate: dayjs("2022-09-30T09:00:00+01:00").toDate(),
|
||||
};
|
||||
|
||||
// Path: apps/web/test/lib/checkDurationLimits.ts
|
||||
describe("Check Duration Limits Tests", () => {
|
||||
it("Should return no errors if limit is not reached", async () => {
|
||||
prismaMock.$queryRaw.mockResolvedValue([{ totalMinutes: 0 }]);
|
||||
await expect(
|
||||
checkDurationLimits({ PER_DAY: 60 }, MOCK_DATA.startDate, MOCK_DATA.id)
|
||||
).resolves.toBeTruthy();
|
||||
});
|
||||
it("Should throw an error if limit is reached", async () => {
|
||||
prismaMock.$queryRaw.mockResolvedValue([{ totalMinutes: 60 }]);
|
||||
await expect(
|
||||
checkDurationLimits({ PER_DAY: 60 }, MOCK_DATA.startDate, MOCK_DATA.id)
|
||||
).rejects.toThrowError();
|
||||
});
|
||||
it("Should pass with multiple duration limits", async () => {
|
||||
prismaMock.$queryRaw.mockResolvedValue([{ totalMinutes: 30 }]);
|
||||
await expect(
|
||||
checkDurationLimits(
|
||||
{
|
||||
PER_DAY: 60,
|
||||
PER_WEEK: 120,
|
||||
},
|
||||
MOCK_DATA.startDate,
|
||||
MOCK_DATA.id
|
||||
)
|
||||
).resolves.toBeTruthy();
|
||||
});
|
||||
it("Should pass with multiple duration limits with one undefined", async () => {
|
||||
prismaMock.$queryRaw.mockResolvedValue([{ totalMinutes: 30 }]);
|
||||
await expect(
|
||||
checkDurationLimits(
|
||||
{
|
||||
PER_DAY: 60,
|
||||
PER_WEEK: undefined,
|
||||
},
|
||||
MOCK_DATA.startDate,
|
||||
MOCK_DATA.id
|
||||
)
|
||||
).resolves.toBeTruthy();
|
||||
});
|
||||
it("Should return no errors if limit is not reached with multiple bookings", async () => {
|
||||
prismaMock.$queryRaw.mockResolvedValue([{ totalMinutes: 60 }]);
|
||||
await expect(
|
||||
checkDurationLimits(
|
||||
{
|
||||
PER_DAY: 90,
|
||||
PER_WEEK: 120,
|
||||
},
|
||||
MOCK_DATA.startDate,
|
||||
MOCK_DATA.id
|
||||
)
|
||||
).resolves.toBeTruthy();
|
||||
});
|
||||
it("Should throw an error if one of the limit is reached with multiple bookings", async () => {
|
||||
prismaMock.$queryRaw.mockResolvedValue([{ totalMinutes: 90 }]);
|
||||
await expect(
|
||||
checkDurationLimits(
|
||||
{
|
||||
PER_DAY: 60,
|
||||
PER_WEEK: 120,
|
||||
},
|
||||
MOCK_DATA.startDate,
|
||||
MOCK_DATA.id
|
||||
)
|
||||
).rejects.toThrowError();
|
||||
});
|
||||
});
|
||||
|
||||
// Path: apps/web/test/lib/checkDurationLimits.ts
|
||||
describe("Check Duration Limit Tests", () => {
|
||||
it("Should return no busyTimes and no error if limit is not reached", async () => {
|
||||
prismaMock.$queryRaw.mockResolvedValue([{ totalMinutes: 60 }]);
|
||||
await expect(
|
||||
checkDurationLimit({
|
||||
key: "PER_DAY",
|
||||
limitingNumber: 90,
|
||||
eventStartDate: MOCK_DATA.startDate,
|
||||
eventId: MOCK_DATA.id,
|
||||
})
|
||||
).resolves.toBeUndefined();
|
||||
});
|
||||
it("Should return busyTimes when set and limit is reached", async () => {
|
||||
prismaMock.$queryRaw.mockResolvedValue([{ totalMinutes: 60 }]);
|
||||
await expect(
|
||||
checkDurationLimit({
|
||||
key: "PER_DAY",
|
||||
limitingNumber: 60,
|
||||
eventStartDate: MOCK_DATA.startDate,
|
||||
eventId: MOCK_DATA.id,
|
||||
returnBusyTimes: true,
|
||||
})
|
||||
).resolves.toEqual({
|
||||
start: dayjs(MOCK_DATA.startDate).startOf("day").toDate(),
|
||||
end: dayjs(MOCK_DATA.startDate).endOf("day").toDate(),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Duration limit validation", () => {
|
||||
it("Should validate limit where ranges have ascending values", () => {
|
||||
expect(validateIntervalLimitOrder({ PER_DAY: 30, PER_MONTH: 60 })).toBe(true);
|
||||
});
|
||||
it("Should invalidate limit where ranges does not have a strict ascending values", () => {
|
||||
expect(validateIntervalLimitOrder({ PER_DAY: 60, PER_WEEK: 30 })).toBe(false);
|
||||
});
|
||||
it("Should validate a correct limit with 'gaps'", () => {
|
||||
expect(validateIntervalLimitOrder({ PER_DAY: 60, PER_YEAR: 120 })).toBe(true);
|
||||
});
|
||||
it("Should validate empty limit", () => {
|
||||
expect(validateIntervalLimitOrder({})).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user