diff --git a/apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/reschedule-bookings.e2e-spec.ts b/apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/reschedule-bookings.e2e-spec.ts new file mode 100644 index 0000000000..d94ec64948 --- /dev/null +++ b/apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/reschedule-bookings.e2e-spec.ts @@ -0,0 +1,230 @@ +import { bootstrap } from "@/app"; +import { AppModule } from "@/app.module"; +import { CreateBookingOutput_2024_08_13 } from "@/ee/bookings/2024-08-13/outputs/create-booking.output"; +import { RescheduleBookingOutput_2024_08_13 } from "@/ee/bookings/2024-08-13/outputs/reschedule-booking.output"; +import { CreateScheduleInput_2024_04_15 } from "@/ee/schedules/schedules_2024_04_15/inputs/create-schedule.input"; +import { SchedulesModule_2024_04_15 } from "@/ee/schedules/schedules_2024_04_15/schedules.module"; +import { SchedulesService_2024_04_15 } from "@/ee/schedules/schedules_2024_04_15/services/schedules.service"; +import { PermissionsGuard } from "@/modules/auth/guards/permissions/permissions.guard"; +import { PrismaModule } from "@/modules/prisma/prisma.module"; +import { UsersModule } from "@/modules/users/users.module"; +import { INestApplication } from "@nestjs/common"; +import { NestExpressApplication } from "@nestjs/platform-express"; +import { Test } from "@nestjs/testing"; +import * as request from "supertest"; +import { EventTypesRepositoryFixture } from "test/fixtures/repository/event-types.repository.fixture"; +import { UserRepositoryFixture } from "test/fixtures/repository/users.repository.fixture"; +import { randomString } from "test/utils/randomString"; +import { withApiAuth } from "test/utils/withApiAuth"; + +import { CAL_API_VERSION_HEADER, SUCCESS_STATUS, VERSION_2024_08_13 } from "@calcom/platform-constants"; +import type { + CreateBookingInput_2024_08_13, + BookingOutput_2024_08_13, + RescheduleBookingInput_2024_08_13, + GetBookingOutput_2024_08_13, +} from "@calcom/platform-types"; +import type { EventType, User } from "@calcom/prisma/client"; + +describe("Reschedule bookings 2024-08-13", () => { + let app: INestApplication; + + let userRepositoryFixture: UserRepositoryFixture; + let eventTypesRepositoryFixture: EventTypesRepositoryFixture; + let schedulesService: SchedulesService_2024_04_15; + + let normalBookingUser: User; + let normalBookingEventType: EventType; + let normalBookingEventTypeId: number; + const normalBookingUserEmail = `user-bookings-normal-user-${randomString(5)}@api.com`; + const normalBookingEventTypeSlug = `user-bookings-normal-event-type-${randomString(5)}`; + let oldNormalBooking: BookingOutput_2024_08_13; + let newNormalBooking: BookingOutput_2024_08_13; + + const RESCHEDULE_REASON = "Flying to venus that day"; + const RESCHEDULED_BY = `user-venus-bookings-rescheduler-${randomString(5)}@api.com`; + + beforeAll(async () => { + const moduleRef = await withApiAuth( + normalBookingUserEmail, + Test.createTestingModule({ + imports: [AppModule, PrismaModule, UsersModule, SchedulesModule_2024_04_15], + }) + ) + .overrideGuard(PermissionsGuard) + .useValue({ + canActivate: () => true, + }) + .compile(); + + userRepositoryFixture = new UserRepositoryFixture(moduleRef); + eventTypesRepositoryFixture = new EventTypesRepositoryFixture(moduleRef); + schedulesService = moduleRef.get(SchedulesService_2024_04_15); + + // initialize normal booking user, schedule and event type + normalBookingUser = await userRepositoryFixture.create({ + email: normalBookingUserEmail, + username: normalBookingUserEmail, + }); + + const userSchedule: CreateScheduleInput_2024_04_15 = { + name: `user-bookings-normal-schedule-${randomString()}`, + timeZone: "Europe/Rome", + isDefault: true, + }; + await schedulesService.createUserSchedule(normalBookingUser.id, userSchedule); + + normalBookingEventType = await eventTypesRepositoryFixture.create( + { + title: `user-bookings-normal-event-type-${randomString()}`, + slug: normalBookingEventTypeSlug, + length: 60, + }, + normalBookingUser.id + ); + normalBookingEventTypeId = normalBookingEventType.id; + + app = moduleRef.createNestApplication(); + bootstrap(app as NestExpressApplication); + await app.init(); + }); + + describe("reschedule normal booking", () => { + it("should create a booking to be rescheduled", async () => { + const bookingBody: CreateBookingInput_2024_08_13 = { + start: new Date(Date.UTC(2040, 1, 9, 10, 0, 0)).toISOString(), + eventTypeId: normalBookingEventTypeId, + attendee: { + name: `user-venus-bookings-attendee-${randomString(10)}`, + email: `user-venus-bookings-attendee-${randomString(10)}@gmail.com`, + timeZone: "Europe/Rome", + language: "it", + }, + location: "https://meet.google.com/oj12u83128u9312ou3", + }; + + const bookingResponse = await request(app.getHttpServer()) + .post("/v2/bookings") + .send(bookingBody) + .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) + .expect(201); + + const bookingResponseBody: CreateBookingOutput_2024_08_13 = bookingResponse.body; + expect(bookingResponseBody.data).toBeDefined(); + expect(responseDataIsBooking(bookingResponseBody.data)).toBe(true); + if (responseDataIsBooking(bookingResponseBody.data)) { + const created: BookingOutput_2024_08_13 = bookingResponseBody.data; + oldNormalBooking = created; + expect(oldNormalBooking).toBeDefined(); + expect(oldNormalBooking.uid).toBeDefined(); + } else { + throw new Error("should create a booking to be rescheduled - Invalid response data"); + } + }); + + it("should reschedule", async () => { + const body: RescheduleBookingInput_2024_08_13 = { + start: new Date(Date.UTC(2040, 1, 9, 12, 0, 0)).toISOString(), + reschedulingReason: RESCHEDULE_REASON, + rescheduledBy: RESCHEDULED_BY, + }; + + const beforeCreate = new Date(); + return request(app.getHttpServer()) + .post(`/v2/bookings/${oldNormalBooking.uid}/reschedule`) + .send(body) + .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) + .expect(201) + .then(async (response) => { + const afterCreate = new Date(); + const responseBody: RescheduleBookingOutput_2024_08_13 = response.body; + expect(responseBody.status).toEqual(SUCCESS_STATUS); + expect(responseBody.data).toBeDefined(); + + const data: BookingOutput_2024_08_13 = responseBody.data as BookingOutput_2024_08_13; + expect(data.reschedulingReason).toEqual(RESCHEDULE_REASON); + expect(data.rescheduledFromUid).toEqual(oldNormalBooking.uid); + expect(data.rescheduledByEmail).toEqual(RESCHEDULED_BY); + + expect(data.start).toEqual(body.start); + expect(data.end).toEqual(new Date(Date.UTC(2040, 1, 9, 13, 0, 0)).toISOString()); + expect(data.id).toBeDefined(); + expect(data.uid).toBeDefined(); + expect(data.hosts[0].id).toEqual(normalBookingUser.id); + expect(data.hosts[0].username).toEqual(normalBookingUser.username); + expect(data.hosts[0].email).toEqual(normalBookingUser.email); + expect(data.status).toEqual(oldNormalBooking.status); + expect(data.duration).toEqual(oldNormalBooking.duration); + expect(data.eventTypeId).toEqual(oldNormalBooking.eventTypeId); + expect(data.attendees[0]).toEqual(oldNormalBooking.attendees[0]); + expect(data.location).toEqual(oldNormalBooking.location); + expect(data.absentHost).toEqual(oldNormalBooking.absentHost); + expect(data.metadata).toEqual(oldNormalBooking.metadata); + + newNormalBooking = data; + expect(newNormalBooking).toBeDefined(); + + const createdAtDate = new Date(data.createdAt); + expect(createdAtDate.getTime()).toBeGreaterThanOrEqual(beforeCreate.getTime()); + expect(createdAtDate.getTime()).toBeLessThanOrEqual(afterCreate.getTime()); + }); + }); + + it("should fetch old booking and verify rescheduledToUid and rescheduledByEmail and status cancelled", async () => { + return request(app.getHttpServer()) + .get(`/v2/bookings/${oldNormalBooking.uid}`) + .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) + .expect(200) + .then(async (response) => { + const responseBody: GetBookingOutput_2024_08_13 = response.body; + expect(responseBody.status).toEqual(SUCCESS_STATUS); + expect(responseBody.data).toBeDefined(); + expect(responseDataIsBooking(responseBody.data)).toBe(true); + + if (responseDataIsBooking(responseBody.data)) { + const data: BookingOutput_2024_08_13 = responseBody.data; + expect(data.rescheduledToUid).toEqual(newNormalBooking.uid); + expect(data.rescheduledByEmail).toEqual(RESCHEDULED_BY); + expect(data.status).toEqual("cancelled"); + } else { + throw new Error( + "Invalid response data - expected booking but received array of possibly recurring bookings" + ); + } + }); + }); + + it("should fetch new booking and verify rescheduledFromUid, rescheduledByEmail and reschedulingReason", async () => { + return request(app.getHttpServer()) + .get(`/v2/bookings/${newNormalBooking.uid}`) + .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) + .expect(200) + .then(async (response) => { + const responseBody: GetBookingOutput_2024_08_13 = response.body; + expect(responseBody.status).toEqual(SUCCESS_STATUS); + expect(responseBody.data).toBeDefined(); + expect(responseDataIsBooking(responseBody.data)).toBe(true); + + if (responseDataIsBooking(responseBody.data)) { + const data: BookingOutput_2024_08_13 = responseBody.data; + expect(data.rescheduledFromUid).toEqual(oldNormalBooking.uid); + expect(data.rescheduledByEmail).toEqual(RESCHEDULED_BY); + expect(data.reschedulingReason).toEqual(RESCHEDULE_REASON); + expect(data.status).toEqual("accepted"); + } else { + throw new Error( + "Invalid response data - expected booking but received array of possibly recurring bookings" + ); + } + }); + }); + }); + + afterAll(async () => { + await userRepositoryFixture.deleteByEmail(normalBookingUser.email); + }); + + function responseDataIsBooking(data: unknown): data is BookingOutput_2024_08_13 { + return !Array.isArray(data) && typeof data === "object" && data !== null && data && "id" in data; + } +}); diff --git a/apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/user-bookings.e2e-spec.ts b/apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/user-bookings.e2e-spec.ts index dc57ee99c0..65892dd44c 100644 --- a/apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/user-bookings.e2e-spec.ts +++ b/apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/user-bookings.e2e-spec.ts @@ -79,7 +79,6 @@ describe("Bookings Endpoints 2024-08-13", () => { const eventTypeRequiresConfirmationSlug = `user-bookings-event-type-requires-confirmation-${randomString()}`; let createdBooking: BookingOutput_2024_08_13; - let rescheduledBooking: BookingOutput_2024_08_13; let createdRecurringBooking: RecurringBookingOutput_2024_08_13[]; let bookingInThePast: Booking; @@ -1360,139 +1359,217 @@ describe("Bookings Endpoints 2024-08-13", () => { }); }); - describe("reschedule bookings", () => { - it("should reschedule normal booking", async () => { - const body: RescheduleBookingInput_2024_08_13 = { - start: new Date(Date.UTC(2035, 0, 8, 14, 0, 0)).toISOString(), - reschedulingReason: "Flying to mars that day", - }; - - const beforeCreate = new Date(); - return request(app.getHttpServer()) - .post(`/v2/bookings/${createdBooking.uid}/reschedule`) - .send(body) - .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) - .expect(201) - .then(async (response) => { - const afterCreate = new Date(); - const responseBody: RescheduleBookingOutput_2024_08_13 = response.body; - expect(responseBody.status).toEqual(SUCCESS_STATUS); - expect(responseBody.data).toBeDefined(); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - const data: BookingOutput_2024_08_13 = responseBody.data; - expect(data.reschedulingReason).toEqual(body.reschedulingReason); - expect(data.start).toEqual(body.start); - expect(data.end).toEqual(new Date(Date.UTC(2035, 0, 8, 15, 0, 0)).toISOString()); - expect(data.rescheduledFromUid).toEqual(createdBooking.uid); - expect(data.id).toBeDefined(); - expect(data.uid).toBeDefined(); - expect(data.hosts[0].id).toEqual(user.id); - expect(data.hosts[0].username).toEqual(user.username); - expect(data.hosts[0].email).toEqual(user.email); - expect(data.status).toEqual(createdBooking.status); - expect(data.duration).toEqual(createdBooking.duration); - expect(data.eventTypeId).toEqual(createdBooking.eventTypeId); - expect(data.attendees[0]).toEqual(createdBooking.attendees[0]); - expect(data.location).toEqual(createdBooking.location); - expect(data.absentHost).toEqual(createdBooking.absentHost); - expect(data.metadata).toEqual(createdBooking.metadata); - - // When a booking is rescheduled, a new booking is created and the old booking is cancelled. - // We want to make sure the createdAt date of the new booking is between the beforeCreate and afterCreate dates. - const createdAtDate = new Date(data.createdAt); - expect(createdAtDate.getTime()).toBeGreaterThanOrEqual(beforeCreate.getTime()); - expect(createdAtDate.getTime()).toBeLessThanOrEqual(afterCreate.getTime()); - - rescheduledBooking = data; + describe("rescheduling bookings", () => { + describe("cant't reschedule cancelled or rescheduled booking booking", () => { + it("should not be able to reschedule cancelled booking", async () => { + const cancelledBooking = await bookingsRepositoryFixture.create({ + status: "CANCELLED", + user: { + connect: { + id: user.id, + }, + }, + startTime: new Date(Date.UTC(2050, 0, 8, 13, 0, 0)), + endTime: new Date(Date.UTC(2050, 0, 8, 14, 0, 0)), + title: "peer coding lets goo", + uid: `cancelled-booking-${randomString()}`, + eventType: { + connect: { + id: eventTypeId, + }, + }, + location: "integrations:daily", + customInputs: {}, + metadata: {}, + responses: { + name: "Oldie", + email: "oldie@gmail.com", + }, + attendees: { + create: { + email: "oldie@gmail.com", + name: "Oldie", + locale: "lv", + timeZone: "Europe/Rome", + }, + }, }); + + const body: RescheduleBookingInput_2024_08_13 = { + start: new Date(Date.UTC(2040, 0, 9, 14, 0, 0)).toISOString(), + reschedulingReason: "Flying to mars that day", + }; + + const response = await request(app.getHttpServer()) + .post(`/v2/bookings/${cancelledBooking.uid}/reschedule`) + .send(body) + .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) + .expect(400); + + expect(response.body.error.message).toEqual( + `Can't reschedule booking with uid=${cancelledBooking.uid} because it has been cancelled. Please provide uid of a booking that is not cancelled.` + ); + await bookingsRepositoryFixture.deleteById(cancelledBooking.id); + }); + + it("should not be able to reschedule rescheduled booking", async () => { + const rescheduledBooking = await bookingsRepositoryFixture.create({ + status: "CANCELLED", + rescheduled: true, + user: { + connect: { + id: user.id, + }, + }, + startTime: new Date(Date.UTC(2050, 0, 8, 13, 0, 0)), + endTime: new Date(Date.UTC(2050, 0, 8, 14, 0, 0)), + title: "peer coding lets goo", + uid: `cancelled-booking-${randomString()}`, + eventType: { + connect: { + id: eventTypeId, + }, + }, + location: "integrations:daily", + customInputs: {}, + metadata: {}, + responses: { + name: "Oldie", + email: "oldie@gmail.com", + }, + attendees: { + create: { + email: "oldie@gmail.com", + name: "Oldie", + locale: "lv", + timeZone: "Europe/Rome", + }, + }, + }); + + const newBooking = await bookingsRepositoryFixture.create({ + status: "ACCEPTED", + user: { + connect: { + id: user.id, + }, + }, + fromReschedule: rescheduledBooking.uid, + startTime: new Date(Date.UTC(2050, 0, 8, 13, 0, 0)), + endTime: new Date(Date.UTC(2050, 0, 8, 14, 0, 0)), + title: "peer coding lets goo", + uid: `new-booking-${randomString()}`, + eventType: { + connect: { + id: eventTypeId, + }, + }, + location: "integrations:daily", + customInputs: {}, + metadata: {}, + responses: { + name: "Oldie", + email: "oldie@gmail.com", + }, + attendees: { + create: { + email: "oldie@gmail.com", + name: "Oldie", + locale: "lv", + timeZone: "Europe/Rome", + }, + }, + }); + + const body: RescheduleBookingInput_2024_08_13 = { + start: new Date(Date.UTC(2040, 0, 9, 14, 0, 0)).toISOString(), + reschedulingReason: "Flying to mars that day", + }; + + const response = await request(app.getHttpServer()) + .post(`/v2/bookings/${rescheduledBooking.uid}/reschedule`) + .send(body) + .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) + .expect(400); + + expect(response.body.error.message).toEqual( + `Can't reschedule booking with uid=${rescheduledBooking.uid} because it has been cancelled and rescheduled already to booking with uid=${newBooking.uid}. You probably want to reschedule ${newBooking.uid} instead by passing it within the request URL.` + ); + await bookingsRepositoryFixture.deleteById(rescheduledBooking.id); + await bookingsRepositoryFixture.deleteById(newBooking.id); + }); }); - it("should set rescheduled booking status to cancelled", async () => { - return request(app.getHttpServer()) - .get(`/v2/bookings/${createdBooking.uid}`) - .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) - .expect(200) - .then(async (response) => { - const responseBody: RescheduleBookingOutput_2024_08_13 = response.body; - expect(responseBody.status).toEqual(SUCCESS_STATUS); - expect(responseBody.data).toBeDefined(); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - const data: BookingOutput_2024_08_13 = responseBody.data; - expect(data.status).toEqual("cancelled"); - expect(data.rescheduledToUid).toEqual(rescheduledBooking.uid); - }); - }); + describe("recurring booking", () => { + it("should reschedule recurrence of a recurring booking", async () => { + const body: RescheduleBookingInput_2024_08_13 = { + start: new Date(Date.UTC(2035, 0, 9, 14, 0, 0)).toISOString(), + reschedulingReason: "Flying to mars again", + }; - it("should reschedule recurrence of a recurring booking", async () => { - const body: RescheduleBookingInput_2024_08_13 = { - start: new Date(Date.UTC(2035, 0, 9, 14, 0, 0)).toISOString(), - reschedulingReason: "Flying to mars again", - }; + return request(app.getHttpServer()) + .post(`/v2/bookings/${createdRecurringBooking[0].uid}/reschedule`) + .send(body) + .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) + .expect(201) + .then(async (response) => { + const responseBody: RescheduleBookingOutput_2024_08_13 = response.body; + expect(responseBody.status).toEqual(SUCCESS_STATUS); + expect(responseBody.data).toBeDefined(); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + const data: RecurringBookingOutput_2024_08_13 = responseBody.data; + expect(data.id).toBeDefined(); + expect(data.uid).toBeDefined(); + expect(data.hosts[0].id).toEqual(user.id); + expect(data.hosts[0].username).toEqual(user.username); + expect(data.hosts[0].email).toEqual(user.email); + expect(data.status).toEqual(createdRecurringBooking[0].status); + expect(data.start).toEqual(body.start); + expect(data.end).toEqual(new Date(Date.UTC(2035, 0, 9, 15, 0, 0)).toISOString()); + expect(data.duration).toEqual(createdRecurringBooking[0].duration); + expect(data.recurringBookingUid).toEqual(createdRecurringBooking[0].recurringBookingUid); + expect(data.eventTypeId).toEqual(createdRecurringBooking[0].eventTypeId); + expect(data.attendees[0]).toEqual(createdRecurringBooking[0].attendees[0]); + expect(data.location).toEqual(createdRecurringBooking[0].location); + expect(data.absentHost).toEqual(createdRecurringBooking[0].absentHost); + expect(data.metadata).toEqual(createdRecurringBooking[0].metadata); - return request(app.getHttpServer()) - .post(`/v2/bookings/${createdRecurringBooking[0].uid}/reschedule`) - .send(body) - .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) - .expect(201) - .then(async (response) => { - const responseBody: RescheduleBookingOutput_2024_08_13 = response.body; - expect(responseBody.status).toEqual(SUCCESS_STATUS); - expect(responseBody.data).toBeDefined(); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - const data: RecurringBookingOutput_2024_08_13 = responseBody.data; - expect(data.id).toBeDefined(); - expect(data.uid).toBeDefined(); - expect(data.hosts[0].id).toEqual(user.id); - expect(data.hosts[0].username).toEqual(user.username); - expect(data.hosts[0].email).toEqual(user.email); - expect(data.status).toEqual(createdRecurringBooking[0].status); - expect(data.start).toEqual(body.start); - expect(data.end).toEqual(new Date(Date.UTC(2035, 0, 9, 15, 0, 0)).toISOString()); - expect(data.duration).toEqual(createdRecurringBooking[0].duration); - expect(data.recurringBookingUid).toEqual(createdRecurringBooking[0].recurringBookingUid); - expect(data.eventTypeId).toEqual(createdRecurringBooking[0].eventTypeId); - expect(data.attendees[0]).toEqual(createdRecurringBooking[0].attendees[0]); - expect(data.location).toEqual(createdRecurringBooking[0].location); - expect(data.absentHost).toEqual(createdRecurringBooking[0].absentHost); - expect(data.metadata).toEqual(createdRecurringBooking[0].metadata); + const oldBooking = await bookingsRepositoryFixture.getByUid(createdRecurringBooking[0].uid); + expect(oldBooking).toBeDefined(); + expect(oldBooking?.status).toEqual("CANCELLED"); + }); + }); - const oldBooking = await bookingsRepositoryFixture.getByUid(createdRecurringBooking[0].uid); - expect(oldBooking).toBeDefined(); - expect(oldBooking?.status).toEqual("CANCELLED"); - }); - }); + it("should get recurring booking recurrences after rescheduling one", async () => { + const recurringBookingUid = createdRecurringBooking[0].recurringBookingUid; + return request(app.getHttpServer()) + .get(`/v2/bookings/${recurringBookingUid}`) + .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) + .expect(200) + .then(async (response) => { + const responseBody: CreateBookingOutput_2024_08_13 = response.body; + expect(responseBody.status).toEqual(SUCCESS_STATUS); + expect(responseBody.data).toBeDefined(); + expect(responseDataIsRecurringBooking(responseBody.data)).toBe(true); - it("should get recurring booking recurrences after rescheduling one", async () => { - const recurringBookingUid = createdRecurringBooking[0].recurringBookingUid; - return request(app.getHttpServer()) - .get(`/v2/bookings/${recurringBookingUid}`) - .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) - .expect(200) - .then(async (response) => { - const responseBody: CreateBookingOutput_2024_08_13 = response.body; - expect(responseBody.status).toEqual(SUCCESS_STATUS); - expect(responseBody.data).toBeDefined(); - expect(responseDataIsRecurringBooking(responseBody.data)).toBe(true); - - if (responseDataIsRecurringBooking(responseBody.data)) { - const data: RecurringBookingOutput_2024_08_13[] = responseBody.data; - expect(data.length).toEqual(4); - const cancelled = data.find((booking) => booking.status === "cancelled"); - expect(cancelled).toBeDefined(); - const rescheduledNew = data.find( - (booking) => booking.start === new Date(Date.UTC(2035, 0, 9, 14, 0, 0)).toISOString() - ); - expect(rescheduledNew).toBeDefined(); - createdRecurringBooking = data; - } else { - throw new Error( - "Invalid response data - expected recurring booking but received non array response" - ); - } - }); + if (responseDataIsRecurringBooking(responseBody.data)) { + const data: RecurringBookingOutput_2024_08_13[] = responseBody.data; + expect(data.length).toEqual(4); + const cancelled = data.find((booking) => booking.status === "cancelled"); + expect(cancelled).toBeDefined(); + const rescheduledNew = data.find( + (booking) => booking.start === new Date(Date.UTC(2035, 0, 9, 14, 0, 0)).toISOString() + ); + expect(rescheduledNew).toBeDefined(); + createdRecurringBooking = data; + } else { + throw new Error( + "Invalid response data - expected recurring booking but received non array response" + ); + } + }); + }); }); }); @@ -2385,95 +2462,6 @@ describe("Bookings Endpoints 2024-08-13", () => { }); }); - describe("rescheduling booking with invalid booking responses", () => { - it("should reschedule", async () => { - const email = `user-bookings-attendee-${randomString(10)}@gmail.com`; - const username = `user-bookings-attendee-${randomString(10)}`; - const locale = "it"; - const timeZone = "Europe/Rome"; - const attendee = await userRepositoryFixture.create({ - email, - username, - locale, - timeZone, - }); - - const booking = await bookingsRepositoryFixture.create({ - uid: `booking-uid-${randomString(10)}`, - title: "booking title", - startTime: "2060-09-05T11:00:00.000Z", - endTime: "2060-09-05T12:00:00.000Z", - eventType: { - connect: { - id: eventTypeId, - }, - }, - status: "ACCEPTED", - metadata: {}, - responses: "null", - user: { - connect: { - id: user.id, - }, - }, - attendees: { - create: { - email: email, - name: username, - timeZone, - locale, - }, - }, - }); - - const body: RescheduleBookingInput_2024_08_13 = { - start: new Date(Date.UTC(2060, 0, 9, 14, 0, 0)).toISOString(), - reschedulingReason: "Flying to mars that day", - }; - - return request(app.getHttpServer()) - .post(`/v2/bookings/${booking.uid}/reschedule`) - .send(body) - .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) - .expect(201) - .then(async (response) => { - const responseBody: RescheduleBookingOutput_2024_08_13 = response.body; - expect(responseBody.status).toEqual(SUCCESS_STATUS); - expect(responseBody.data).toBeDefined(); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - const data: BookingOutput_2024_08_13 = responseBody.data; - expect(data.reschedulingReason).toEqual(body.reschedulingReason); - expect(data.start).toEqual(body.start); - expect(data.end).toEqual(new Date(Date.UTC(2060, 0, 9, 15, 0, 0)).toISOString()); - expect(data.rescheduledFromUid).toEqual(booking.uid); - expect(data.id).toBeDefined(); - expect(data.uid).toBeDefined(); - expect(data.hosts[0].id).toEqual(user.id); - expect(data.hosts[0].username).toEqual(user.username); - expect(data.hosts[0].email).toEqual(user.email); - expect(data.status).toEqual("accepted"); - expect(data.eventTypeId).toEqual(booking.eventTypeId); - expect(data.attendees.length).toEqual(1); - expect(data.attendees[0]).toEqual({ - name: attendee.username, - email: attendee.email, - timeZone: attendee.timeZone, - language: attendee.locale, - absent: false, - }); - expect(data.bookingFieldsResponses).toEqual({ - name: attendee.username, - email: attendee.email, - rescheduledReason: body.reschedulingReason, - guests: [], - }); - await bookingsRepositoryFixture.deleteById(data.id); - await userRepositoryFixture.delete(attendee.id); - }); - }); - }); - describe("create booking with attendee sms reminder", () => { it("should not be able create a booking if attendee sms reminder is missing", async () => { const body: CreateBookingInput_2024_08_13 = { @@ -2559,146 +2547,6 @@ describe("Bookings Endpoints 2024-08-13", () => { }); }); - describe("cant't reschedule cancelled booking", () => { - it("should not be able to reschedule cancelled booking", async () => { - const cancelledBooking = await bookingsRepositoryFixture.create({ - status: "CANCELLED", - user: { - connect: { - id: user.id, - }, - }, - startTime: new Date(Date.UTC(2050, 0, 8, 13, 0, 0)), - endTime: new Date(Date.UTC(2050, 0, 8, 14, 0, 0)), - title: "peer coding lets goo", - uid: `cancelled-booking-${randomString()}`, - eventType: { - connect: { - id: eventTypeId, - }, - }, - location: "integrations:daily", - customInputs: {}, - metadata: {}, - responses: { - name: "Oldie", - email: "oldie@gmail.com", - }, - attendees: { - create: { - email: "oldie@gmail.com", - name: "Oldie", - locale: "lv", - timeZone: "Europe/Rome", - }, - }, - }); - - const body: RescheduleBookingInput_2024_08_13 = { - start: new Date(Date.UTC(2040, 0, 9, 14, 0, 0)).toISOString(), - reschedulingReason: "Flying to mars that day", - }; - - const response = await request(app.getHttpServer()) - .post(`/v2/bookings/${cancelledBooking.uid}/reschedule`) - .send(body) - .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) - .expect(400); - - expect(response.body.error.message).toEqual( - `Can't reschedule booking with uid=${cancelledBooking.uid} because it has been cancelled. Please provide uid of a booking that is not cancelled.` - ); - await bookingsRepositoryFixture.deleteById(cancelledBooking.id); - }); - - it("should not be able to reschedule rescheduled booking", async () => { - const rescheduledBooking = await bookingsRepositoryFixture.create({ - status: "CANCELLED", - rescheduled: true, - user: { - connect: { - id: user.id, - }, - }, - startTime: new Date(Date.UTC(2050, 0, 8, 13, 0, 0)), - endTime: new Date(Date.UTC(2050, 0, 8, 14, 0, 0)), - title: "peer coding lets goo", - uid: `cancelled-booking-${randomString()}`, - eventType: { - connect: { - id: eventTypeId, - }, - }, - location: "integrations:daily", - customInputs: {}, - metadata: {}, - responses: { - name: "Oldie", - email: "oldie@gmail.com", - }, - attendees: { - create: { - email: "oldie@gmail.com", - name: "Oldie", - locale: "lv", - timeZone: "Europe/Rome", - }, - }, - }); - - const newBooking = await bookingsRepositoryFixture.create({ - status: "ACCEPTED", - user: { - connect: { - id: user.id, - }, - }, - fromReschedule: rescheduledBooking.uid, - startTime: new Date(Date.UTC(2050, 0, 8, 13, 0, 0)), - endTime: new Date(Date.UTC(2050, 0, 8, 14, 0, 0)), - title: "peer coding lets goo", - uid: `new-booking-${randomString()}`, - eventType: { - connect: { - id: eventTypeId, - }, - }, - location: "integrations:daily", - customInputs: {}, - metadata: {}, - responses: { - name: "Oldie", - email: "oldie@gmail.com", - }, - attendees: { - create: { - email: "oldie@gmail.com", - name: "Oldie", - locale: "lv", - timeZone: "Europe/Rome", - }, - }, - }); - - const body: RescheduleBookingInput_2024_08_13 = { - start: new Date(Date.UTC(2040, 0, 9, 14, 0, 0)).toISOString(), - reschedulingReason: "Flying to mars that day", - }; - - const response = await request(app.getHttpServer()) - .post(`/v2/bookings/${rescheduledBooking.uid}/reschedule`) - .send(body) - .set(CAL_API_VERSION_HEADER, VERSION_2024_08_13) - .expect(400); - - expect(response.body.error.message).toEqual( - `Can't reschedule booking with uid=${rescheduledBooking.uid} because it has been cancelled and rescheduled already to booking with uid=${newBooking.uid}. You probably want to reschedule ${newBooking.uid} instead by passing it within the request URL.` - ); - await bookingsRepositoryFixture.deleteById(rescheduledBooking.id); - await bookingsRepositoryFixture.deleteById(newBooking.id); - }); - }); - describe("cant't cancel already cancelled booking", () => { it("should not be able to cancel alraedy cancelled booking", async () => { const cancelledBooking = await bookingsRepositoryFixture.create({ diff --git a/apps/api/v2/src/ee/bookings/2024-08-13/services/input.service.ts b/apps/api/v2/src/ee/bookings/2024-08-13/services/input.service.ts index 272aed1726..5e6e92977e 100644 --- a/apps/api/v2/src/ee/bookings/2024-08-13/services/input.service.ts +++ b/apps/api/v2/src/ee/bookings/2024-08-13/services/input.service.ts @@ -544,12 +544,18 @@ export class InputBookingsService_2024_08_13 { Object.assign(newRequest, { userId, ...oAuthClientParams, platformBookingLocation: location }); newRequest.body = { ...bodyTransformed, + rescheduledBy: request.body.rescheduledBy, noEmail: !oAuthClientParams.arePlatformEmailsEnabled, creationSource: CreationSource.API_V2, }; } else { Object.assign(newRequest, { userId, platformBookingLocation: location }); - newRequest.body = { ...bodyTransformed, noEmail: false, creationSource: CreationSource.API_V2 }; + newRequest.body = { + ...bodyTransformed, + noEmail: false, + creationSource: CreationSource.API_V2, + rescheduledBy: request.body.rescheduledBy, + }; } return newRequest as unknown as BookingRequest; diff --git a/apps/api/v2/src/ee/bookings/2024-08-13/services/output.service.ts b/apps/api/v2/src/ee/bookings/2024-08-13/services/output.service.ts index 4e929d9f9e..ec781308ff 100644 --- a/apps/api/v2/src/ee/bookings/2024-08-13/services/output.service.ts +++ b/apps/api/v2/src/ee/bookings/2024-08-13/services/output.service.ts @@ -105,14 +105,8 @@ export class OutputBookingsService_2024_08_13 { ); const metadata = safeParse(bookingMetadataSchema, databaseBooking.metadata, defaultBookingMetadata); const location = metadata?.videoCallUrl || databaseBooking.location; - const rescheduledToInfo = databaseBooking.rescheduled - ? await this.getRescheduledToInfo(databaseBooking.uid) - : undefined; - - const rescheduledToUid = rescheduledToInfo?.uid; - const rescheduledByEmail = databaseBooking.rescheduled - ? rescheduledToInfo?.rescheduledBy - : databaseBooking.rescheduledBy; + const rescheduledToUid = await this.getRescheduledToUid(databaseBooking); + const rescheduledByEmail = await this.getRescheduledByEmail(databaseBooking); const booking = { id: databaseBooking.id, @@ -168,6 +162,23 @@ export class OutputBookingsService_2024_08_13 { }; } + private async getRescheduledToUid(databaseBooking: DatabaseBooking) { + if (!databaseBooking.rescheduled) return undefined; + const rescheduledTo = await this.bookingsRepository.getByFromReschedule(databaseBooking.uid); + return rescheduledTo?.uid; + } + + private async getRescheduledByEmail(databaseBooking: DatabaseBooking) { + if (databaseBooking.rescheduled) { + return databaseBooking.rescheduledBy; + } + if (databaseBooking.fromReschedule) { + const previousBooking = await this.bookingsRepository.getByUid(databaseBooking.fromReschedule); + return previousBooking?.rescheduledBy ?? databaseBooking.rescheduledBy; + } + return databaseBooking.rescheduledBy; + } + getUserDefinedMetadata(databaseMetadata: DatabaseMetadata) { if (databaseMetadata === null) return {}; @@ -284,14 +295,8 @@ export class OutputBookingsService_2024_08_13 { const duration = dateEnd.diff(dateStart, "minutes").minutes; const metadata = safeParse(bookingMetadataSchema, databaseBooking.metadata, defaultBookingMetadata); const location = metadata?.videoCallUrl || databaseBooking.location; - const rescheduledToInfo = databaseBooking.rescheduled - ? await this.getRescheduledToInfo(databaseBooking.uid) - : undefined; - - const rescheduledToUid = rescheduledToInfo?.uid; - const rescheduledByEmail = databaseBooking.rescheduled - ? rescheduledToInfo?.rescheduledBy - : databaseBooking.rescheduledBy; + const rescheduledToUid = await this.getRescheduledToUid(databaseBooking); + const rescheduledByEmail = await this.getRescheduledByEmail(databaseBooking); const booking = { id: databaseBooking.id,