Fix/reschedule on dynamic events (#2657)

* Reschedule for dynamic events

* Fix lint

* Handling attendee calendar event cancellation

Co-authored-by: zomars <[email protected]>
This commit is contained in:
alannncandzomars authored and GitHub committed 2022-05-05 01:03:36 +00:00
1 parent 6ec2b20a23
commit df7abdfc06
3 files changed
+135 -34

No files matched your search

+50 -13
View File
@@ -1,4 +1,4 @@
import { Prisma } from "@prisma/client";
import { Prisma, Booking } from "@prisma/client";
import dayjs from "dayjs";
import short from "short-uuid";
import { v5 as uuidv5 } from "uuid";
@@ -80,7 +80,7 @@ export class CalendarEventBuilder implements ICalendarEventBuilder {
}
users.push(eventTypeUser);
}
this.users = users;
this.setUsers(users);
}
public buildAttendeesList() {
@@ -273,17 +273,54 @@ export class CalendarEventBuilder implements ICalendarEventBuilder {
this.calendarEvent.cancellationReason = cancellationReason;
}
public buildRescheduleLink(originalBookingUId: string) {
if (!this.eventType) {
throw new Error("Run buildEventObjectFromInnerClass before this function");
}
const isTeam = !!this.eventType.teamId;
public setUsers(users: User[]) {
this.users = users;
}
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;
public async setUsersFromId(userId: User["id"]) {
let resultUser: User | null;
try {
resultUser = await prisma.user.findUnique({
rejectOnNotFound: true,
where: {
id: userId,
},
...userSelect,
});
this.setUsers([resultUser]);
} catch (error) {
throw new Error("getUsersById.users.notFound");
}
}
public buildRescheduleLink(booking: Partial<Booking>, eventType?: CalendarEventBuilder["eventType"]) {
try {
if (!booking) {
throw new Error("Parameter booking is required to build reschedule link");
}
const isTeam = !!eventType && !!eventType.teamId;
const isDynamic = booking?.dynamicEventSlugRef && booking?.dynamicGroupSlugRef;
let slug = "";
if (isTeam && eventType?.team?.slug) {
slug = `/team/${eventType.team?.slug}`;
} else if (isDynamic) {
const dynamicSlug = isDynamic ? `${booking.dynamicGroupSlugRef}/${booking.dynamicEventSlugRef}` : "";
slug = dynamicSlug;
} else if (eventType?.slug) {
slug = `${this.users[0].username}/${eventType.slug}`;
}
const queryParams = new URLSearchParams();
queryParams.set("rescheduleUid", `${booking.uid}`);
slug = `${slug}?${queryParams.toString()}`;
const rescheduleLink = `${process.env.NEXT_PUBLIC_WEBAPP_URL}/${slug}`;
this.rescheduleLink = rescheduleLink;
} catch (error) {
if (error instanceof Error) {
throw new Error(`buildRescheduleLink.error: ${error.message}`);
}
}
}
}
@@ -11,7 +11,21 @@ export class CalendarEventDirector {
this.builder = builder;
}
public setExistingBooking(booking: Booking) {
public setExistingBooking(
booking: Pick<
Booking,
| "id"
| "uid"
| "title"
| "startTime"
| "endTime"
| "eventTypeId"
| "userId"
| "dynamicEventSlugRef"
| "dynamicGroupSlugRef"
| "location"
>
) {
this.existingBooking = booking;
}
@@ -29,9 +43,23 @@ export class CalendarEventDirector {
this.builder.setCancellationReason(this.cancellationReason);
this.builder.setDescription(this.builder.eventType.description);
this.builder.setNotes(this.existingBooking.description);
this.builder.buildRescheduleLink(this.existingBooking.uid);
this.builder.buildRescheduleLink(this.existingBooking, this.builder.eventType);
} else {
throw new Error("buildForRescheduleEmail.missing.params.required");
}
}
public async buildWithoutEventTypeForRescheduleEmail() {
if (this.existingBooking && this.existingBooking.userId && this.existingBooking.uid) {
await this.builder.setUsersFromId(this.existingBooking.userId);
this.builder.buildAttendeesList();
this.builder.setLocation(this.existingBooking.location);
this.builder.setUId(this.existingBooking.uid);
this.builder.setCancellationReason(this.cancellationReason);
this.builder.setDescription(this.existingBooking.description);
await this.builder.buildRescheduleLink(this.existingBooking);
} else {
throw new Error("buildWithoutEventTypeForRescheduleEmail.missing.params.required");
}
}
}