Skip to content

Commit

Permalink
refactor: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
Udit-takkar committed Sep 26, 2024
1 parent fb1bafe commit 074b0b2
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 119 deletions.
17 changes: 7 additions & 10 deletions packages/features/bookings/lib/handleNewBooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ import { getCustomInputsResponses } from "./handleNewBooking/getCustomInputsResp
import { getEventTypesFromDB } from "./handleNewBooking/getEventTypesFromDB";
import type { getEventTypeResponse } from "./handleNewBooking/getEventTypesFromDB";
import { getLocationValuesForDb } from "./handleNewBooking/getLocationValuesForDb";
import { getOriginalRescheduledBookingAndSeat } from "./handleNewBooking/getOriginalRescheduledBookingAndSeat";
import { getOriginalRescheduledBooking } from "./handleNewBooking/getOriginalRescheduledBooking";
import { getRequiresConfirmationFlags } from "./handleNewBooking/getRequiresConfirmationFlags";
import { getSeatedBooking } from "./handleNewBooking/getSeatedBooking";
import { getVideoCallDetails } from "./handleNewBooking/getVideoCallDetails";
import { handleAppsStatus } from "./handleNewBooking/handleAppsStatus";
import { loadAndValidateUsers } from "./handleNewBooking/loadAndValidateUsers";
Expand Down Expand Up @@ -291,16 +292,12 @@ async function handler(
reqBodyRescheduleUid: reqBody.rescheduleUid,
});

const {
rescheduleUid,
originalRescheduledBooking: originalBooking,
bookingSeat,
} = await getOriginalRescheduledBookingAndSeat({
reqBodyRescheduleUid: reqBody.rescheduleUid,
seatsPerTimeSlot: eventType.seatsPerTimeSlot,
});
const bookingSeat = reqBody.rescheduleUid ? await getSeatedBooking(reqBody.rescheduleUid) : null;
const rescheduleUid = bookingSeat ? bookingSeat.booking.uid : reqBody.rescheduleUid;

let originalRescheduledBooking = originalBooking;
let originalRescheduledBooking = rescheduleUid
? await getOriginalRescheduledBooking(rescheduleUid, !!eventType.seatsPerTimeSlot)
: null;

let luckyUserResponse;
let isFirstSeat = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,14 @@
import type { Prisma } from "@prisma/client";

import prisma from "@calcom/prisma";
import { BookingStatus } from "@calcom/prisma/enums";
import { BookingRepository } from "@calcom/lib/server/repository/booking";

import { validateOriginalRescheduledBooking } from "./validateOriginalRescheduledBooking";

export async function getOriginalRescheduledBooking(uid: string, seatsEventType?: boolean) {
return prisma.booking.findFirst({
where: {
uid: uid,
status: {
in: [BookingStatus.ACCEPTED, BookingStatus.CANCELLED, BookingStatus.PENDING],
},
},
include: {
attendees: {
select: {
name: true,
email: true,
locale: true,
timeZone: true,
phoneNumber: true,
...(seatsEventType && { bookingSeat: true, id: true }),
},
},
user: {
select: {
id: true,
name: true,
email: true,
locale: true,
timeZone: true,
destinationCalendar: true,
credentials: {
select: {
id: true,
userId: true,
key: true,
type: true,
teamId: true,
appId: true,
invalid: true,
user: {
select: {
email: true,
},
},
},
},
},
},
destinationCalendar: true,
payment: true,
references: true,
workflowReminders: true,
},
});
const originalBooking = await BookingRepository.findOriginalRescheduledBooking(uid, seatsEventType);
validateOriginalRescheduledBooking(originalBooking);

return originalBooking;
}

export type BookingType = Prisma.PromiseReturnType<typeof getOriginalRescheduledBooking>;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import prisma from "@calcom/prisma";

import type { BookingSeat } from "../handleSeats/types";

export const getSeatedBooking = async (bookingSeatUid: string): Promise<BookingSeat | null> => {
// rescheduleUid can be bookingUid and bookingSeatUid
return prisma.bookingSeat.findUnique({
where: {
referenceUid: bookingSeatUid,
},
include: {
booking: true,
attendee: true,
},
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ErrorCode } from "@calcom/lib/errorCodes";
import { HttpError } from "@calcom/lib/http-error";
import { BookingStatus } from "@calcom/prisma/enums";

import { type OriginalRescheduledBooking } from "./getOriginalRescheduledBooking";

export const validateOriginalRescheduledBooking = async (
originalRescheduledBooking: OriginalRescheduledBooking
) => {
if (!originalRescheduledBooking) {
throw new HttpError({ statusCode: 404, message: "Could not find original booking" });
}

if (
originalRescheduledBooking.status === BookingStatus.CANCELLED &&
!originalRescheduledBooking.rescheduled
) {
throw new HttpError({ statusCode: 403, message: ErrorCode.CancelledBookingsCannotBeRescheduled });
}
};
53 changes: 53 additions & 0 deletions packages/lib/server/repository/booking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,57 @@ export class BookingRepository {

return [...collectiveRoundRobinBookingsOwner, ...collectiveRoundRobinBookingsAttendee];
}

static async findOriginalRescheduledBooking(uid: string, seatsEventType?: boolean) {
return await prisma.booking.findFirst({
where: {
uid: uid,
status: {
in: [BookingStatus.ACCEPTED, BookingStatus.CANCELLED, BookingStatus.PENDING],
},
},
include: {
attendees: {
select: {
name: true,
email: true,
locale: true,
timeZone: true,
phoneNumber: true,
...(seatsEventType && { bookingSeat: true, id: true }),
},
},
user: {
select: {
id: true,
name: true,
email: true,
locale: true,
timeZone: true,
destinationCalendar: true,
credentials: {
select: {
id: true,
userId: true,
key: true,
type: true,
teamId: true,
appId: true,
invalid: true,
user: {
select: {
email: true,
},
},
},
},
},
},
destinationCalendar: true,
payment: true,
references: true,
workflowReminders: true,
},
});
}
}

0 comments on commit 074b0b2

Please sign in to comment.