2023-02-16 22:39:57 +00:00
|
|
|
import type { Booking, BookingReference, User } from "@prisma/client";
|
2022-05-06 17:21:30 +00:00
|
|
|
import type { TFunction } from "next-i18next";
|
|
|
|
|
|
|
|
import { CalendarEventBuilder } from "@calcom/core/builders/CalendarEvent/builder";
|
|
|
|
import { CalendarEventDirector } from "@calcom/core/builders/CalendarEvent/director";
|
|
|
|
import { deleteMeeting } from "@calcom/core/videoClient";
|
2022-07-12 14:32:27 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
2022-06-06 17:49:56 +00:00
|
|
|
import { sendRequestRescheduleEmail } from "@calcom/emails";
|
2022-05-06 17:21:30 +00:00
|
|
|
import logger from "@calcom/lib/logger";
|
|
|
|
import { getTranslation } from "@calcom/lib/server/i18n";
|
|
|
|
import prisma from "@calcom/prisma";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { BookingStatus } from "@calcom/prisma/enums";
|
2022-07-28 19:58:26 +00:00
|
|
|
import type { Person } from "@calcom/types/Calendar";
|
2022-05-06 17:21:30 +00:00
|
|
|
|
|
|
|
import { getCalendar } from "../../_utils/getCalendar";
|
|
|
|
|
|
|
|
type PersonAttendeeCommonFields = Pick<User, "id" | "email" | "name" | "locale" | "timeZone" | "username">;
|
|
|
|
|
|
|
|
const Reschedule = async (bookingUid: string, cancellationReason: string) => {
|
2022-08-31 19:44:47 +00:00
|
|
|
const bookingToReschedule = await prisma.booking.findFirstOrThrow({
|
2022-05-06 17:21:30 +00:00
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
uid: true,
|
|
|
|
title: true,
|
|
|
|
startTime: true,
|
|
|
|
endTime: true,
|
|
|
|
userId: true,
|
|
|
|
eventTypeId: true,
|
|
|
|
location: true,
|
|
|
|
attendees: true,
|
|
|
|
references: true,
|
|
|
|
user: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
email: true,
|
|
|
|
name: true,
|
|
|
|
timeZone: true,
|
|
|
|
locale: true,
|
|
|
|
username: true,
|
|
|
|
credentials: true,
|
|
|
|
destinationCalendar: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
uid: bookingUid,
|
|
|
|
NOT: {
|
|
|
|
status: {
|
|
|
|
in: [BookingStatus.CANCELLED, BookingStatus.REJECTED],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (bookingToReschedule && bookingToReschedule.eventTypeId && bookingToReschedule.user) {
|
|
|
|
const userOwner = bookingToReschedule.user;
|
2022-08-31 19:44:47 +00:00
|
|
|
const event = await prisma.eventType.findFirstOrThrow({
|
2022-05-06 17:21:30 +00:00
|
|
|
select: {
|
|
|
|
title: true,
|
|
|
|
users: true,
|
|
|
|
schedulingType: true,
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
id: bookingToReschedule.eventTypeId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await prisma.booking.update({
|
|
|
|
where: {
|
|
|
|
id: bookingToReschedule.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
rescheduled: true,
|
|
|
|
cancellationReason,
|
|
|
|
status: BookingStatus.CANCELLED,
|
|
|
|
updatedAt: dayjs().toISOString(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const [mainAttendee] = bookingToReschedule.attendees;
|
|
|
|
// @NOTE: Should we assume attendees language?
|
|
|
|
const tAttendees = await getTranslation(mainAttendee.locale ?? "en", "common");
|
|
|
|
const usersToPeopleType = (
|
|
|
|
users: PersonAttendeeCommonFields[],
|
|
|
|
selectedLanguage: TFunction
|
|
|
|
): Person[] => {
|
|
|
|
return users?.map((user) => {
|
|
|
|
return {
|
|
|
|
email: user.email || "",
|
|
|
|
name: user.name || "",
|
|
|
|
username: user?.username || "",
|
|
|
|
language: { translate: selectedLanguage, locale: user.locale || "en" },
|
|
|
|
timeZone: user?.timeZone,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const userOwnerTranslation = await getTranslation(userOwner.locale ?? "en", "common");
|
|
|
|
const [userOwnerAsPeopleType] = usersToPeopleType([userOwner], userOwnerTranslation);
|
|
|
|
const builder = new CalendarEventBuilder();
|
|
|
|
builder.init({
|
|
|
|
title: bookingToReschedule.title,
|
|
|
|
type: event.title,
|
|
|
|
startTime: bookingToReschedule.startTime.toISOString(),
|
|
|
|
endTime: bookingToReschedule.endTime.toISOString(),
|
|
|
|
attendees: usersToPeopleType(
|
|
|
|
// username field doesn't exists on attendee but could be in the future
|
|
|
|
bookingToReschedule.attendees as unknown as PersonAttendeeCommonFields[],
|
|
|
|
tAttendees
|
|
|
|
),
|
|
|
|
organizer: userOwnerAsPeopleType,
|
|
|
|
});
|
|
|
|
const director = new CalendarEventDirector();
|
|
|
|
director.setBuilder(builder);
|
|
|
|
director.setExistingBooking(bookingToReschedule as unknown as Booking);
|
|
|
|
director.setCancellationReason(cancellationReason);
|
|
|
|
await director.buildForRescheduleEmail();
|
|
|
|
// Handling calendar and videos cancellation
|
|
|
|
// This can set previous time as available, until virtual calendar is done
|
|
|
|
const credentialsMap = new Map();
|
|
|
|
userOwner.credentials.forEach((credential) => {
|
|
|
|
credentialsMap.set(credential.type, credential);
|
|
|
|
});
|
|
|
|
const bookingRefsFiltered: BookingReference[] = bookingToReschedule.references.filter(
|
|
|
|
(ref) => !!credentialsMap.get(ref.type)
|
|
|
|
);
|
|
|
|
try {
|
2023-04-05 14:55:57 +00:00
|
|
|
bookingRefsFiltered.forEach(async (bookingRef) => {
|
2022-05-06 17:21:30 +00:00
|
|
|
if (bookingRef.uid) {
|
|
|
|
if (bookingRef.type.endsWith("_calendar")) {
|
2023-04-05 14:55:57 +00:00
|
|
|
const calendar = await getCalendar(credentialsMap.get(bookingRef.type));
|
2022-05-06 17:21:30 +00:00
|
|
|
return calendar?.deleteEvent(bookingRef.uid, builder.calendarEvent);
|
|
|
|
} else if (bookingRef.type.endsWith("_video")) {
|
|
|
|
return deleteMeeting(credentialsMap.get(bookingRef.type), bookingRef.uid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
logger.error(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send emails
|
|
|
|
try {
|
|
|
|
await sendRequestRescheduleEmail(builder.calendarEvent, {
|
|
|
|
rescheduleLink: builder.rescheduleLink,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
logger.error(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Reschedule;
|