From 6f38d40613306616b45807035516f034b5553e55 Mon Sep 17 00:00:00 2001 From: Lauris Skraucis Date: Thu, 2 Oct 2025 17:58:42 +0200 Subject: [PATCH] refactor: dont allow scheduling team event type without hosts (#24206) Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> --- .../controllers/e2e/team-bookings.e2e-spec.ts | 74 ++++++++++++++++++- .../2024-08-13/services/bookings.service.ts | 14 ++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/team-bookings.e2e-spec.ts b/apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/team-bookings.e2e-spec.ts index da95412f3d..1be96e84e8 100644 --- a/apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/team-bookings.e2e-spec.ts +++ b/apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/team-bookings.e2e-spec.ts @@ -65,6 +65,8 @@ describe("Bookings Endpoints 2024-08-13", () => { let team1EventTypeId: number; let team2EventTypeId: number; let phoneOnlyEventTypeId: number; + let collectiveEventWithoutHostsId: number; + let roundRobinEventWithoutHostsId: number; const team1EventTypeSlug = `team-bookings-event-type-${randomString()}`; const team2EventTypeSlug = `team-bookings-event-type-${randomString()}`; @@ -223,6 +225,28 @@ describe("Bookings Endpoints 2024-08-13", () => { team1EventTypeId = team1EventType.id; + const team1CollectiveEventTypeWithoutHosts = await eventTypesRepositoryFixture.createTeamEventType({ + schedulingType: "COLLECTIVE", + team: { + connect: { id: team1.id }, + }, + title: `team-bookings-2024-08-13-collective-event-type-without-hosts-${randomString()}`, + slug: `team-bookings-2024-08-13-collective-event-type-without-hosts-${randomString()}`, + length: 60, + }); + collectiveEventWithoutHostsId = team1CollectiveEventTypeWithoutHosts.id; + + const team1RoundRobinEventTypeWithoutHosts = await eventTypesRepositoryFixture.createTeamEventType({ + schedulingType: "ROUND_ROBIN", + team: { + connect: { id: team1.id }, + }, + title: `team-bookings-2024-08-13-round-robin-event-type-without-hosts-${randomString()}`, + slug: `team-bookings-2024-08-13-round-robin-event-type-without-hosts-${randomString()}`, + length: 60, + }); + roundRobinEventWithoutHostsId = team1RoundRobinEventTypeWithoutHosts.id; + const phoneOnlyEventType = await eventTypesRepositoryFixture.createTeamEventType({ schedulingType: "ROUND_ROBIN", team: { @@ -363,6 +387,52 @@ describe("Bookings Endpoints 2024-08-13", () => { await app.init(); }); + describe("cant book event types without hosts", () => { + it("should fail to book a collective event type without hosts", async () => { + const body: CreateBookingInput_2024_08_13 = { + start: new Date(Date.UTC(2030, 0, 8, 13, 0, 0)).toISOString(), + eventTypeId: collectiveEventWithoutHostsId, + attendee: { + name: "alice", + timeZone: "Europe/Madrid", + email: "alice@gmail.com", + }, + }; + + const response = await request(app.getHttpServer()) + .post("/v2/bookings") + .send(body) + .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13); + + expect(response.status).toBe(422); + expect(response.body.error.message).toBe( + `Can't book this team event type because it has no hosts. Please, add at least 1 host to event type with id=${collectiveEventWithoutHostsId} belonging to team with id=${team1.id} and try again.` + ); + }); + + it("should fail to book a round robin event type without hosts", async () => { + const body: CreateBookingInput_2024_08_13 = { + start: new Date(Date.UTC(2030, 0, 8, 13, 0, 0)).toISOString(), + eventTypeId: roundRobinEventWithoutHostsId, + attendee: { + name: "alice", + timeZone: "Europe/Madrid", + email: "alice@gmail.com", + }, + }; + + const response = await request(app.getHttpServer()) + .post("/v2/bookings") + .send(body) + .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13); + + expect(response.status).toBe(422); + expect(response.body.error.message).toBe( + `Can't book this team event type because it has no hosts. Please, add at least 1 host to event type with id=${roundRobinEventWithoutHostsId} belonging to team with id=${team1.id} and try again.` + ); + }); + }); + describe("create team bookings", () => { it("should create a team 1 booking", async () => { const body: CreateBookingInput_2024_08_13 = { @@ -765,8 +835,8 @@ describe("Bookings Endpoints 2024-08-13", () => { return client; } - function responseDataIsBooking(data: any): data is BookingOutput_2024_08_13 { - return !Array.isArray(data) && typeof data === "object" && data && "id" in data; + function responseDataIsBooking(data: unknown): data is BookingOutput_2024_08_13 { + return !Array.isArray(data) && typeof data === "object" && data !== null && data && "id" in data; } afterAll(async () => { diff --git a/apps/api/v2/src/ee/bookings/2024-08-13/services/bookings.service.ts b/apps/api/v2/src/ee/bookings/2024-08-13/services/bookings.service.ts index b6093c0be4..b3a9121924 100644 --- a/apps/api/v2/src/ee/bookings/2024-08-13/services/bookings.service.ts +++ b/apps/api/v2/src/ee/bookings/2024-08-13/services/bookings.service.ts @@ -29,6 +29,7 @@ import { Logger, NotFoundException, UnauthorizedException, + UnprocessableEntityException, } from "@nestjs/common"; import { BadRequestException } from "@nestjs/common"; import { Request } from "express"; @@ -132,6 +133,10 @@ export class BookingsService_2024_08_13 { ); } + if (eventType.schedulingType === "COLLECTIVE" || eventType.schedulingType === "ROUND_ROBIN") { + await this.checkEventTypeHasHosts(eventType.id); + } + body.eventTypeId = eventType.id; if ("instant" in body && body.instant) { @@ -159,6 +164,15 @@ export class BookingsService_2024_08_13 { } } + async checkEventTypeHasHosts(eventTypeId: number) { + const eventType = await this.eventTypesRepository.getEventTypeWithHosts(eventTypeId); + if (!eventType?.hosts?.length) { + throw new UnprocessableEntityException( + `Can't book this team event type because it has no hosts. Please, add at least 1 host to event type with id=${eventTypeId} belonging to team with id=${eventType?.teamId} and try again.` + ); + } + } + async checkBookingRequiresAuthenticationSetting( eventType: EventTypeWithOwnerAndTeam, authUser: AuthOptionalUser,