Feature/reschedule bookings (#2351)
* WIP bookings page ui changes, created api endpoint * Ui changes mobile/desktop * Added translations * Fix lib import and common names * WIP reschedule * WIP * Save wip * [WIP] builder and class for CalendarEvent, email for attende * update rescheduled emails, booking view and availability page view * Working version reschedule * Fix for req.user as array * Added missing translation and refactor dialog to self component * Test for reschedule * update on types * Update lib no required * Update type on createBooking * fix types * remove preview stripe sub * remove unused file * remove unused import * Fix reschedule test * Refactor and cleaning up code * Email reschedule title fixes * Adding calendar delete and recreate placeholder of cancelled * Add translation * Removed logs, notes, fixed types * Fixes process.env types * Use strict compare * Fixes type inference * Type fixing is my middle name * Update apps/web/components/booking/BookingListItem.tsx * Update apps/web/components/dialog/RescheduleDialog.tsx * Update packages/core/builders/CalendarEvent/director.ts * Update apps/web/pages/success.tsx * Updates rescheduling labels * Update packages/core/builders/CalendarEvent/builder.ts * Type fixes * Update packages/core/builders/CalendarEvent/builder.ts * Only validating input blocked once * E2E fixes * Stripe tests fixes Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: zomars <[email protected]>
This commit is contained in:
50 files changed
+2129
-241
No files matched your search
@@ -161,7 +161,11 @@ export default class EventManager {
|
||||
*
|
||||
* @param event
|
||||
*/
|
||||
public async update(event: CalendarEvent, rescheduleUid: string): Promise<CreateUpdateResult> {
|
||||
public async update(
|
||||
event: CalendarEvent,
|
||||
rescheduleUid: string,
|
||||
newBookingId?: number
|
||||
): Promise<CreateUpdateResult> {
|
||||
const evt = processLocation(event);
|
||||
|
||||
if (!rescheduleUid) {
|
||||
@@ -187,6 +191,7 @@ export default class EventManager {
|
||||
},
|
||||
},
|
||||
destinationCalendar: true,
|
||||
payment: true,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -210,6 +215,23 @@ export default class EventManager {
|
||||
// Update all calendar events.
|
||||
results.push(...(await this.updateAllCalendarEvents(evt, booking)));
|
||||
|
||||
const bookingPayment = booking?.payment;
|
||||
|
||||
// Updating all payment to new
|
||||
if (bookingPayment && newBookingId) {
|
||||
const paymentIds = bookingPayment.map((payment) => payment.id);
|
||||
await prisma.payment.updateMany({
|
||||
where: {
|
||||
id: {
|
||||
in: paymentIds,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
bookingId: newBookingId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Now we can delete the old booking and its references.
|
||||
const bookingReferenceDeletes = prisma.bookingReference.deleteMany({
|
||||
where: {
|
||||
@@ -345,4 +367,16 @@ export default class EventManager {
|
||||
return Promise.reject("No suitable credentials given for the requested integration name.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update event to set a cancelled event placeholder on users calendar
|
||||
* remove if virtual calendar is already done and user availability its read from there
|
||||
* and not only in their calendars
|
||||
* @param event
|
||||
* @param booking
|
||||
* @public
|
||||
*/
|
||||
public async updateAndSetCancelledPlaceholder(event: CalendarEvent, booking: PartialBooking) {
|
||||
await this.updateAllCalendarEvents(event, booking);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import dayjs from "dayjs";
|
||||
import short from "short-uuid";
|
||||
import { v5 as uuidv5 } from "uuid";
|
||||
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { CalendarEventClass } from "./class";
|
||||
|
||||
const translator = short();
|
||||
const userSelect = Prisma.validator<Prisma.UserArgs>()({
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
username: true,
|
||||
timeZone: true,
|
||||
credentials: true,
|
||||
bufferTime: true,
|
||||
destinationCalendar: true,
|
||||
locale: true,
|
||||
},
|
||||
});
|
||||
|
||||
type User = Prisma.UserGetPayload<typeof userSelect>;
|
||||
type PersonAttendeeCommonFields = Pick<User, "id" | "email" | "name" | "locale" | "timeZone" | "username">;
|
||||
interface ICalendarEventBuilder {
|
||||
calendarEvent: CalendarEventClass;
|
||||
eventType: Awaited<ReturnType<CalendarEventBuilder["getEventFromEventId"]>>;
|
||||
users: Awaited<ReturnType<CalendarEventBuilder["getUserById"]>>[];
|
||||
attendeesList: PersonAttendeeCommonFields[];
|
||||
teamMembers: Awaited<ReturnType<CalendarEventBuilder["getTeamMembers"]>>;
|
||||
rescheduleLink: string;
|
||||
}
|
||||
|
||||
export class CalendarEventBuilder implements ICalendarEventBuilder {
|
||||
calendarEvent!: CalendarEventClass;
|
||||
eventType!: ICalendarEventBuilder["eventType"];
|
||||
users!: ICalendarEventBuilder["users"];
|
||||
attendeesList: ICalendarEventBuilder["attendeesList"] = [];
|
||||
teamMembers: ICalendarEventBuilder["teamMembers"] = [];
|
||||
rescheduleLink!: string;
|
||||
|
||||
constructor() {
|
||||
this.reset();
|
||||
}
|
||||
|
||||
private reset() {
|
||||
this.calendarEvent = new CalendarEventClass();
|
||||
}
|
||||
|
||||
public init(initProps: CalendarEventClass) {
|
||||
this.calendarEvent = new CalendarEventClass(initProps);
|
||||
}
|
||||
|
||||
public setEventType(eventType: ICalendarEventBuilder["eventType"]) {
|
||||
this.eventType = eventType;
|
||||
}
|
||||
|
||||
public async buildEventObjectFromInnerClass(eventId: number) {
|
||||
const resultEvent = await this.getEventFromEventId(eventId);
|
||||
if (resultEvent) {
|
||||
this.eventType = resultEvent;
|
||||
}
|
||||
}
|
||||
|
||||
public async buildUsersFromInnerClass() {
|
||||
if (!this.eventType) {
|
||||
throw new Error("exec BuildEventObjectFromInnerClass before calling this function");
|
||||
}
|
||||
let users = this.eventType.users;
|
||||
|
||||
/* If this event was pre-relationship migration */
|
||||
if (!users.length && this.eventType.userId) {
|
||||
const eventTypeUser = await this.getUserById(this.eventType.userId);
|
||||
if (!eventTypeUser) {
|
||||
throw new Error("buildUsersFromINnerClass.eventTypeUser.notFound");
|
||||
}
|
||||
users.push(eventTypeUser);
|
||||
}
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
public buildAttendeesList() {
|
||||
// Language Function was set on builder init
|
||||
this.attendeesList = [
|
||||
...(this.calendarEvent.attendees as unknown as PersonAttendeeCommonFields[]),
|
||||
...this.teamMembers,
|
||||
];
|
||||
}
|
||||
|
||||
private async getUserById(userId: number) {
|
||||
let resultUser: User | null;
|
||||
try {
|
||||
resultUser = await prisma.user.findUnique({
|
||||
rejectOnNotFound: true,
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
...userSelect,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error("getUsersById.users.notFound");
|
||||
}
|
||||
return resultUser;
|
||||
}
|
||||
|
||||
private async getEventFromEventId(eventTypeId: number) {
|
||||
let resultEventType;
|
||||
try {
|
||||
resultEventType = await prisma.eventType.findUnique({
|
||||
rejectOnNotFound: true,
|
||||
where: {
|
||||
id: eventTypeId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
users: userSelect,
|
||||
team: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
slug: true,
|
||||
},
|
||||
},
|
||||
slug: true,
|
||||
teamId: true,
|
||||
title: true,
|
||||
length: true,
|
||||
eventName: true,
|
||||
schedulingType: true,
|
||||
periodType: true,
|
||||
periodStartDate: true,
|
||||
periodEndDate: true,
|
||||
periodDays: true,
|
||||
periodCountCalendarDays: true,
|
||||
requiresConfirmation: true,
|
||||
userId: true,
|
||||
price: true,
|
||||
currency: true,
|
||||
metadata: true,
|
||||
destinationCalendar: true,
|
||||
hideCalendarNotes: true,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error("Error while getting eventType");
|
||||
}
|
||||
return resultEventType;
|
||||
}
|
||||
|
||||
public async buildLuckyUsers() {
|
||||
if (!this.eventType && this.users && this.users.length) {
|
||||
throw new Error("exec buildUsersFromInnerClass before calling this function");
|
||||
}
|
||||
|
||||
// @TODO: user?.username gets flagged as null somehow, maybe a filter before map?
|
||||
const filterUsernames = this.users.filter((user) => user && typeof user.username === "string");
|
||||
const userUsernames = filterUsernames.map((user) => user.username) as string[]; // @TODO: hack
|
||||
const users = await prisma.user.findMany({
|
||||
where: {
|
||||
username: { in: userUsernames },
|
||||
eventTypes: {
|
||||
some: {
|
||||
id: this.eventType.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
locale: true,
|
||||
},
|
||||
});
|
||||
|
||||
const userNamesWithBookingCounts = await Promise.all(
|
||||
users.map(async (user) => ({
|
||||
username: user.username,
|
||||
bookingCount: await prisma.booking.count({
|
||||
where: {
|
||||
user: {
|
||||
id: user.id,
|
||||
},
|
||||
startTime: {
|
||||
gt: new Date(),
|
||||
},
|
||||
eventTypeId: this.eventType.id,
|
||||
},
|
||||
}),
|
||||
}))
|
||||
);
|
||||
const luckyUsers = this.getLuckyUsers(this.users, userNamesWithBookingCounts);
|
||||
this.users = luckyUsers;
|
||||
}
|
||||
|
||||
private getLuckyUsers(
|
||||
users: User[],
|
||||
bookingCounts: {
|
||||
username: string | null;
|
||||
bookingCount: number;
|
||||
}[]
|
||||
) {
|
||||
if (!bookingCounts.length) users.slice(0, 1);
|
||||
|
||||
const [firstMostAvailableUser] = bookingCounts.sort((a, b) => (a.bookingCount > b.bookingCount ? 1 : -1));
|
||||
const luckyUser = users.find((user) => user.username === firstMostAvailableUser?.username);
|
||||
return luckyUser ? [luckyUser] : users;
|
||||
}
|
||||
|
||||
public async buildTeamMembers() {
|
||||
this.teamMembers = await this.getTeamMembers();
|
||||
}
|
||||
|
||||
private async getTeamMembers() {
|
||||
// Users[0] its organizer so we are omitting with slice(1)
|
||||
const teamMemberPromises = this.users.slice(1).map(async function (user) {
|
||||
return {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email || "", // @NOTE: Should we change this "" to teamMemberId?
|
||||
name: user.name || "",
|
||||
timeZone: user.timeZone,
|
||||
language: {
|
||||
translate: await getTranslation(user.locale ?? "en", "common"),
|
||||
locale: user.locale ?? "en",
|
||||
},
|
||||
locale: user.locale,
|
||||
} as PersonAttendeeCommonFields;
|
||||
});
|
||||
return await Promise.all(teamMemberPromises);
|
||||
}
|
||||
|
||||
public buildUIDCalendarEvent() {
|
||||
if (this.users && this.users.length > 0) {
|
||||
throw new Error("call buildUsers before calling this function");
|
||||
}
|
||||
const [mainOrganizer] = this.users;
|
||||
const seed = `${mainOrganizer.username}:${dayjs(this.calendarEvent.startTime)
|
||||
.utc()
|
||||
.format()}:${new Date().getTime()}`;
|
||||
const uid = translator.fromUUID(uuidv5(seed, uuidv5.URL));
|
||||
this.calendarEvent.uid = uid;
|
||||
}
|
||||
|
||||
public setLocation(location: CalendarEventClass["location"]) {
|
||||
this.calendarEvent.location = location;
|
||||
}
|
||||
|
||||
public setUId(uid: CalendarEventClass["uid"]) {
|
||||
this.calendarEvent.uid = uid;
|
||||
}
|
||||
|
||||
public setDestinationCalendar(destinationCalendar: CalendarEventClass["destinationCalendar"]) {
|
||||
this.calendarEvent.destinationCalendar = destinationCalendar;
|
||||
}
|
||||
|
||||
public setHideCalendarNotes(hideCalendarNotes: CalendarEventClass["hideCalendarNotes"]) {
|
||||
this.calendarEvent.hideCalendarNotes = hideCalendarNotes;
|
||||
}
|
||||
|
||||
public setDescription(description: CalendarEventClass["description"]) {
|
||||
this.calendarEvent.description = description;
|
||||
}
|
||||
|
||||
public setCancellationReason(cancellationReason: CalendarEventClass["cancellationReason"]) {
|
||||
this.calendarEvent.cancellationReason = cancellationReason;
|
||||
}
|
||||
|
||||
public buildRescheduleLink(originalBookingUId: string) {
|
||||
if (!this.eventType) {
|
||||
throw new Error("Run buildEventObjectFromInnerClass before this function");
|
||||
}
|
||||
const isTeam = !!this.eventType.teamId;
|
||||
|
||||
const queryParams = new URLSearchParams();
|
||||
queryParams.set("rescheduleUid", `${originalBookingUId}`);
|
||||
const rescheduleLink = `${process.env.NEXT_PUBLIC_WEBAPP_URL}/${
|
||||
isTeam ? `/team/${this.eventType.team?.slug}` : this.users[0].username
|
||||
}/${this.eventType.slug}?${queryParams.toString()}`;
|
||||
this.rescheduleLink = rescheduleLink;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { AdditionInformation, CalendarEvent, ConferenceData, Person } from "@calcom/types/Calendar";
|
||||
|
||||
import { DestinationCalendar } from ".prisma/client";
|
||||
|
||||
class CalendarEventClass implements CalendarEvent {
|
||||
type!: string;
|
||||
title!: string;
|
||||
startTime!: string;
|
||||
endTime!: string;
|
||||
organizer!: Person;
|
||||
attendees!: Person[];
|
||||
description?: string | null;
|
||||
team?: { name: string; members: string[] };
|
||||
location?: string | null;
|
||||
conferenceData?: ConferenceData;
|
||||
additionInformation?: AdditionInformation;
|
||||
uid?: string | null;
|
||||
videoCallData?: any;
|
||||
paymentInfo?: any;
|
||||
destinationCalendar?: DestinationCalendar | null;
|
||||
cancellationReason?: string | null;
|
||||
rejectionReason?: string | null;
|
||||
hideCalendarNotes?: boolean;
|
||||
|
||||
constructor(initProps?: CalendarEvent) {
|
||||
// If more parameters are given we update this
|
||||
Object.assign(this, initProps);
|
||||
}
|
||||
}
|
||||
|
||||
export { CalendarEventClass };
|
||||
@@ -0,0 +1,35 @@
|
||||
import { Booking } from "@prisma/client";
|
||||
|
||||
import { CalendarEventBuilder } from "./builder";
|
||||
|
||||
export class CalendarEventDirector {
|
||||
private builder!: CalendarEventBuilder;
|
||||
private existingBooking!: Partial<Booking>;
|
||||
private cancellationReason!: string;
|
||||
|
||||
public setBuilder(builder: CalendarEventBuilder): void {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
public setExistingBooking(booking: Booking) {
|
||||
this.existingBooking = booking;
|
||||
}
|
||||
|
||||
public setCancellationReason(reason: string) {
|
||||
this.cancellationReason = reason;
|
||||
}
|
||||
|
||||
public async buildForRescheduleEmail(): Promise<void> {
|
||||
if (this.existingBooking && this.existingBooking.eventTypeId && this.existingBooking.uid) {
|
||||
await this.builder.buildEventObjectFromInnerClass(this.existingBooking.eventTypeId);
|
||||
await this.builder.buildUsersFromInnerClass();
|
||||
this.builder.buildAttendeesList();
|
||||
this.builder.setLocation(this.existingBooking.location);
|
||||
this.builder.setUId(this.existingBooking.uid);
|
||||
this.builder.setCancellationReason(this.cancellationReason);
|
||||
this.builder.buildRescheduleLink(this.existingBooking.uid);
|
||||
} else {
|
||||
throw new Error("buildForRescheduleEmail.missing.params.required");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,6 @@
|
||||
"compilerOptions": {
|
||||
"baseUrl": "."
|
||||
},
|
||||
"include": ["."],
|
||||
"include": [".", "../types/*.d.ts"],
|
||||
"exclude": ["dist", "build", "node_modules"]
|
||||
}
|
||||
Reference in new issue
Block a user