fix: fixes for round robin reassignment (#17233)
* fixes scheduled email for new host * fixes emails and calendar event * type fixes * add test to check if caneclation email is called --------- Co-authored-by: CarinaWolli <[email protected]> Co-authored-by: sean <[email protected]>
This commit is contained in:
3 files changed
+140
-41
No files matched your search
@@ -247,4 +247,78 @@ describe("roundRobinManualReassignment test", () => {
|
|||||||
expect(attendees.some((attendee) => attendee.email === currentRRHost.email)).toBe(false);
|
expect(attendees.some((attendee) => attendee.email === currentRRHost.email)).toBe(false);
|
||||||
expect(attendees.some((attendee) => attendee.email === newHost.email)).toBe(true);
|
expect(attendees.some((attendee) => attendee.email === newHost.email)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("sends cancellation email to previous RR host when reassigning", async ({ emails }) => {
|
||||||
|
const roundRobinManualReassignment = (await import("./roundRobinManualReassignment")).default;
|
||||||
|
const EventManager = (await import("@calcom/core/EventManager")).default;
|
||||||
|
|
||||||
|
const eventManagerSpy = vi.spyOn(EventManager.prototype as any, "reschedule");
|
||||||
|
eventManagerSpy.mockResolvedValue({ referencesToCreate: [] });
|
||||||
|
|
||||||
|
const sendRoundRobinCancelledEmailsAndSMSSpy = vi.spyOn(
|
||||||
|
await import("@calcom/emails"),
|
||||||
|
"sendRoundRobinCancelledEmailsAndSMS"
|
||||||
|
);
|
||||||
|
|
||||||
|
const users = testUsers;
|
||||||
|
const originalHost = users[0];
|
||||||
|
const previousRRHost = users[1];
|
||||||
|
const newHost = users[2];
|
||||||
|
|
||||||
|
const { dateString: dateStringPlusOne } = getDate({ dateIncrement: 1 });
|
||||||
|
|
||||||
|
const bookingToReassignUid = "booking-to-reassign-with-previous-rr-host";
|
||||||
|
|
||||||
|
await createBookingScenario(
|
||||||
|
getScenarioData({
|
||||||
|
eventTypes: [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
slug: "round-robin-event",
|
||||||
|
schedulingType: SchedulingType.ROUND_ROBIN,
|
||||||
|
length: 45,
|
||||||
|
users: users.map((user) => ({ id: user.id })),
|
||||||
|
hosts: users.map((user) => ({ userId: user.id, isFixed: false })),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
bookings: [
|
||||||
|
{
|
||||||
|
id: 124,
|
||||||
|
eventTypeId: 1,
|
||||||
|
userId: originalHost.id,
|
||||||
|
uid: bookingToReassignUid,
|
||||||
|
status: BookingStatus.ACCEPTED,
|
||||||
|
startTime: `${dateStringPlusOne}T05:00:00.000Z`,
|
||||||
|
endTime: `${dateStringPlusOne}T05:15:00.000Z`,
|
||||||
|
attendees: [
|
||||||
|
getMockBookingAttendee({
|
||||||
|
id: 2,
|
||||||
|
name: "attendee",
|
||||||
|
email: "[email protected]",
|
||||||
|
locale: "en",
|
||||||
|
timeZone: "Asia/Kolkata",
|
||||||
|
}),
|
||||||
|
getMockBookingAttendee({
|
||||||
|
id: previousRRHost.id,
|
||||||
|
name: previousRRHost.name,
|
||||||
|
email: previousRRHost.email,
|
||||||
|
locale: "en",
|
||||||
|
timeZone: previousRRHost.timeZone,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
organizer: originalHost,
|
||||||
|
usersApartFromOrganizer: users.slice(1),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
await roundRobinManualReassignment({
|
||||||
|
bookingId: 124,
|
||||||
|
newUserId: newHost.id,
|
||||||
|
orgId: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(sendRoundRobinCancelledEmailsAndSMSSpy).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -104,6 +104,24 @@ export const roundRobinManualReassignment = async ({
|
|||||||
const newUserT = await getTranslation(newUser.locale || "en", "common");
|
const newUserT = await getTranslation(newUser.locale || "en", "common");
|
||||||
const originalOrganizerT = await getTranslation(originalOrganizer.locale || "en", "common");
|
const originalOrganizerT = await getTranslation(originalOrganizer.locale || "en", "common");
|
||||||
|
|
||||||
|
const roundRobinHosts = eventType.hosts.filter((host) => !host.isFixed);
|
||||||
|
|
||||||
|
const attendeeEmailsSet = new Set(booking.attendees.map((attendee) => attendee.email));
|
||||||
|
|
||||||
|
// Find the current round robin host assigned
|
||||||
|
const previousRRHost = (() => {
|
||||||
|
for (const host of roundRobinHosts) {
|
||||||
|
if (host.user.id === booking.userId) {
|
||||||
|
return host.user;
|
||||||
|
}
|
||||||
|
if (attendeeEmailsSet.has(host.user.email)) {
|
||||||
|
return host.user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
const previousRRHostT = await getTranslation(previousRRHost?.locale || "en", "common");
|
||||||
|
|
||||||
if (hasOrganizerChanged) {
|
if (hasOrganizerChanged) {
|
||||||
const bookingResponses = booking.responses;
|
const bookingResponses = booking.responses;
|
||||||
const responseSchema = getBookingResponsesSchema({
|
const responseSchema = getBookingResponsesSchema({
|
||||||
@@ -166,24 +184,38 @@ export const roundRobinManualReassignment = async ({
|
|||||||
hasOrganizerChanged,
|
hasOrganizerChanged,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const organizer = hasOrganizerChanged ? newUser : booking.user ?? newUser;
|
||||||
|
|
||||||
|
const organizerT = await getTranslation(organizer?.locale || "en", "common");
|
||||||
|
|
||||||
const teamMembers = await getTeamMembers({
|
const teamMembers = await getTeamMembers({
|
||||||
eventTypeHosts,
|
eventTypeHosts,
|
||||||
attendees: booking.attendees,
|
attendees: booking.attendees,
|
||||||
organizer: newUser,
|
organizer,
|
||||||
previousHost: originalOrganizer,
|
previousHost: previousRRHost || null,
|
||||||
reassignedHost: newUser,
|
reassignedHost: newUser,
|
||||||
});
|
});
|
||||||
|
|
||||||
const attendeePromises = booking.attendees.map(async (attendee) => ({
|
const attendeePromises = [];
|
||||||
email: attendee.email,
|
for (const attendee of booking.attendees) {
|
||||||
name: attendee.name,
|
if (
|
||||||
timeZone: attendee.timeZone,
|
attendee.email === newUser.email ||
|
||||||
language: {
|
attendee.email === previousRRHost?.email ||
|
||||||
translate: await getTranslation(attendee.locale ?? "en", "common"),
|
teamMembers.some((member) => member.email === attendee.email)
|
||||||
locale: attendee.locale ?? "en",
|
) {
|
||||||
},
|
continue;
|
||||||
phoneNumber: attendee.phoneNumber || undefined,
|
}
|
||||||
}));
|
|
||||||
|
attendeePromises.push(
|
||||||
|
getTranslation(attendee.locale ?? "en", "common").then((tAttendee) => ({
|
||||||
|
email: attendee.email,
|
||||||
|
name: attendee.name,
|
||||||
|
timeZone: attendee.timeZone,
|
||||||
|
language: { translate: tAttendee, locale: attendee.locale ?? "en" },
|
||||||
|
phoneNumber: attendee.phoneNumber || undefined,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const attendeeList = await Promise.all(attendeePromises);
|
const attendeeList = await Promise.all(attendeePromises);
|
||||||
|
|
||||||
@@ -194,10 +226,10 @@ export const roundRobinManualReassignment = async ({
|
|||||||
startTime: dayjs(booking.startTime).utc().format(),
|
startTime: dayjs(booking.startTime).utc().format(),
|
||||||
endTime: dayjs(booking.endTime).utc().format(),
|
endTime: dayjs(booking.endTime).utc().format(),
|
||||||
organizer: {
|
organizer: {
|
||||||
email: newUser.email,
|
email: organizer.email,
|
||||||
name: newUser.name || "",
|
name: organizer.name || "",
|
||||||
timeZone: newUser.timeZone,
|
timeZone: organizer.timeZone,
|
||||||
language: { translate: newUserT, locale: newUser.locale || "en" },
|
language: { translate: organizerT, locale: organizer.locale || "en" },
|
||||||
},
|
},
|
||||||
attendees: attendeeList,
|
attendees: attendeeList,
|
||||||
uid: booking.uid,
|
uid: booking.uid,
|
||||||
@@ -242,8 +274,10 @@ export const roundRobinManualReassignment = async ({
|
|||||||
newReferencesToCreate,
|
newReferencesToCreate,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { cancellationReason, ...evtWithoutCancellationReason } = evt;
|
||||||
|
|
||||||
// Send emails
|
// Send emails
|
||||||
await sendRoundRobinScheduledEmailsAndSMS(evt, [
|
await sendRoundRobinScheduledEmailsAndSMS(evtWithoutCancellationReason, [
|
||||||
{
|
{
|
||||||
...newUser,
|
...newUser,
|
||||||
name: newUser.name || "",
|
name: newUser.name || "",
|
||||||
@@ -262,19 +296,21 @@ export const roundRobinManualReassignment = async ({
|
|||||||
language: { translate: originalOrganizerT, locale: originalOrganizer.locale || "en" },
|
language: { translate: originalOrganizerT, locale: originalOrganizer.locale || "en" },
|
||||||
};
|
};
|
||||||
|
|
||||||
await sendRoundRobinCancelledEmailsAndSMS(
|
if (previousRRHost) {
|
||||||
cancelledEvt,
|
await sendRoundRobinCancelledEmailsAndSMS(
|
||||||
[
|
cancelledEvt,
|
||||||
{
|
[
|
||||||
...originalOrganizer,
|
{
|
||||||
name: originalOrganizer.name || "",
|
...previousRRHost,
|
||||||
username: originalOrganizer.username || "",
|
name: previousRRHost.name || "",
|
||||||
timeFormat: getTimeFormatStringFromUserTimeFormat(originalOrganizer.timeFormat),
|
username: previousRRHost.username || "",
|
||||||
language: { translate: originalOrganizerT, locale: originalOrganizer.locale || "en" },
|
timeFormat: getTimeFormatStringFromUserTimeFormat(previousRRHost.timeFormat),
|
||||||
},
|
language: { translate: previousRRHostT, locale: previousRRHost.locale || "en" },
|
||||||
],
|
},
|
||||||
eventType?.metadata as EventTypeMetadata
|
],
|
||||||
);
|
eventType?.metadata as EventTypeMetadata
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (hasOrganizerChanged) {
|
if (hasOrganizerChanged) {
|
||||||
// Handle changing workflows with organizer
|
// Handle changing workflows with organizer
|
||||||
|
|||||||
@@ -151,17 +151,6 @@ export const roundRobinReassignment = async ({
|
|||||||
reassignedHost: reassignedRRHost,
|
reassignedHost: reassignedRRHost,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Assume the RR host was labelled as a team member
|
|
||||||
if (reassignedRRHost.email !== organizer.email) {
|
|
||||||
teamMembers.push({
|
|
||||||
id: reassignedRRHost.id,
|
|
||||||
email: reassignedRRHost.email,
|
|
||||||
name: reassignedRRHost.name || "",
|
|
||||||
timeZone: reassignedRRHost.timeZone,
|
|
||||||
language: { translate: reassignedRRHostT, locale: reassignedRRHost.locale ?? "en" },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const attendeePromises = [];
|
const attendeePromises = [];
|
||||||
for (const attendee of booking.attendees) {
|
for (const attendee of booking.attendees) {
|
||||||
if (
|
if (
|
||||||
|
|||||||
Reference in new issue
Block a user