Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Workflow action 'email to host' only sends to one host of team e… #14767

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/features/bookings/lib/handleConfirmation.ts
Expand Up @@ -14,6 +14,7 @@ import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import type { PrismaClient } from "@calcom/prisma";
import type { SchedulingType } from "@calcom/prisma/enums";
import { BookingStatus, WebhookTriggerEvents } from "@calcom/prisma/enums";
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
import type { AdditionalInformation, CalendarEvent } from "@calcom/types/Calendar";
Expand Down Expand Up @@ -129,6 +130,7 @@ export async function handleConfirmation(args: {
eventType: {
bookingFields: Prisma.JsonValue | null;
slug: string;
schedulingType: SchedulingType | null;
owner: {
hideBranding?: boolean | null;
} | null;
Expand Down Expand Up @@ -174,6 +176,7 @@ export async function handleConfirmation(args: {
select: {
slug: true,
bookingFields: true,
schedulingType: true,
owner: {
select: {
hideBranding: true,
Expand Down Expand Up @@ -228,6 +231,7 @@ export async function handleConfirmation(args: {
select: {
slug: true,
bookingFields: true,
schedulingType: true,
owner: {
select: {
hideBranding: true,
Expand Down Expand Up @@ -266,7 +270,10 @@ export async function handleConfirmation(args: {
const evtOfBooking = {
...evt,
metadata: { videoCallUrl: meetingUrl },
eventType: { slug: eventTypeSlug },
eventType: {
slug: eventTypeSlug,
schedulingType: updatedBookings[index].eventType?.schedulingType,
},
};
evtOfBooking.startTime = updatedBookings[index].startTime.toISOString();
evtOfBooking.endTime = updatedBookings[index].endTime.toISOString();
Expand Down
6 changes: 5 additions & 1 deletion packages/features/bookings/lib/handleNewBooking.ts
Expand Up @@ -2368,7 +2368,11 @@ async function handler(
loggerWithEventDetails.error("Error while creating booking references", JSON.stringify({ error }));
}

const evtWithMetadata = { ...evt, metadata, eventType: { slug: eventType.slug } };
const evtWithMetadata = {
...evt,
metadata,
eventType: { slug: eventType.slug, schedulingType: eventType.schedulingType },
};

await scheduleMandatoryReminder(
evtWithMetadata,
Expand Down
Expand Up @@ -391,8 +391,7 @@ describe("handleNewBooking", () => {
});

expectWorkflowToBeTriggered({
// emailsToReceive: [organizer.email].concat(otherTeamMembers.map(member => member.email)),
emailsToReceive: [organizer.email],
emailsToReceive: [organizer.email].concat(otherTeamMembers.map((member) => member.email)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add a unit test for this? and make sure to test if all the hosts receive email

Instead of writing a new test uncommented this as this tests for receiving emails for all hosts.

emails,
});
},
Expand Down
11 changes: 10 additions & 1 deletion packages/features/bookings/lib/handleSeats/handleSeats.ts
Expand Up @@ -103,7 +103,16 @@ const handleSeats = async (newSeatedBookingObject: NewSeatedBookingObject) => {
await scheduleWorkflowReminders({
workflows: eventType.workflows,
smsReminderNumber: smsReminderNumber || null,
calendarEvent: { ...evt, ...{ metadata, eventType: { slug: eventType.slug } } },
calendarEvent: {
...evt,
...{
metadata,
eventType: {
slug: eventType.slug,
schedulingType: eventType.schedulingType,
},
},
},
isNotConfirmed: evt.requiresConfirmation || false,
isRescheduleEvent: !!rescheduleUid,
isFirstRecurringEvent: true,
Expand Down
15 changes: 14 additions & 1 deletion packages/features/ee/workflows/api/scheduleEmailReminders.ts
Expand Up @@ -9,7 +9,7 @@ import logger from "@calcom/lib/logger";
import { defaultHandler } from "@calcom/lib/server";
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
import prisma from "@calcom/prisma";
import { WorkflowActions, WorkflowMethods, WorkflowTemplates } from "@calcom/prisma/enums";
import { SchedulingType, WorkflowActions, WorkflowMethods, WorkflowTemplates } from "@calcom/prisma/enums";
import { bookingMetadataSchema } from "@calcom/prisma/zod-utils";

import type { PartialWorkflowReminder } from "../lib/getWorkflowReminders";
Expand Down Expand Up @@ -119,6 +119,19 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
switch (reminder.workflowStep.action) {
case WorkflowActions.EMAIL_HOST:
sendTo = reminder.booking?.userPrimaryEmail ?? reminder.booking.user?.email;
const hosts = reminder.booking.attendees
.filter((attendee) =>
reminder.booking?.eventType?.hosts?.some((host) => host.user.email === attendee.email)
)
.map((host) => host.email);
const schedulingType = reminder.booking.eventType?.schedulingType;

if (
hosts &&
(schedulingType === SchedulingType.COLLECTIVE || schedulingType === SchedulingType.ROUND_ROBIN)
) {
sendTo = sendTo ? [sendTo, ...hosts] : hosts;
}
break;
case WorkflowActions.EMAIL_ATTENDEE:
sendTo = reminder.booking.attendees[0].email;
Expand Down
18 changes: 17 additions & 1 deletion packages/features/ee/workflows/lib/getWorkflowReminders.ts
Expand Up @@ -27,7 +27,14 @@ type PartialBooking =
| "attendees"
| "userPrimaryEmail"
| "smsReminderNumber"
> & { eventType: (Partial<EventType> & { team: { parentId?: number } }) | null } & {
> & {
eventType:
| (Partial<EventType> & {
team: { parentId?: number };
hosts: { user: { email: string } }[] | undefined;
})
| null;
} & {
user: Partial<User> | null;
})
| null;
Expand Down Expand Up @@ -159,6 +166,15 @@ export const select: Prisma.WorkflowReminderSelect = {
bookingFields: true,
title: true,
slug: true,
hosts: {
select: {
user: {
select: {
email: true,
},
},
},
},
recurringEvent: true,
team: {
select: {
Expand Down
Expand Up @@ -7,6 +7,7 @@ import {
} from "@calcom/features/ee/workflows/lib/actionHelperFunctions";
import { checkSMSRateLimit } from "@calcom/lib/checkRateLimitAndThrowError";
import { SENDER_NAME } from "@calcom/lib/constants";
import { SchedulingType } from "@calcom/prisma/enums";
import { WorkflowActions, WorkflowMethods, WorkflowTriggerEvents } from "@calcom/prisma/enums";
import type { CalendarEvent } from "@calcom/types/Calendar";

Expand All @@ -17,7 +18,11 @@ import { deleteScheduledWhatsappReminder, scheduleWhatsappReminder } from "./wha

type ExtendedCalendarEvent = CalendarEvent & {
metadata?: { videoCallUrl: string | undefined };
eventType: { slug?: string };
eventType: {
slug?: string;
schedulingType?: SchedulingType | null;
hosts?: { user: { email: string } }[];
};
};

type ProcessWorkflowStepParams = {
Expand Down Expand Up @@ -90,6 +95,13 @@ const processWorkflowStep = async (
break;
case WorkflowActions.EMAIL_HOST:
sendTo = [evt.organizer?.email || ""];

const schedulingType = evt.eventType.schedulingType;
const isTeamEvent =
schedulingType === SchedulingType.ROUND_ROBIN || schedulingType === SchedulingType.COLLECTIVE;
if (isTeamEvent && evt.team?.members) {
sendTo = sendTo.concat(evt.team.members.map((member) => member.email));
}
break;
case WorkflowActions.EMAIL_ATTENDEE:
const attendees = !!emailAttendeeSendToOverride
Expand Down
Expand Up @@ -13,7 +13,7 @@ import {
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
import { prisma } from "@calcom/prisma";
import { BookingStatus } from "@calcom/prisma/client";
import { MembershipRole, WorkflowActions, WorkflowMethods } from "@calcom/prisma/enums";
import { MembershipRole, SchedulingType, WorkflowActions, WorkflowMethods } from "@calcom/prisma/enums";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";

import { TRPCError } from "@trpc/server";
Expand Down Expand Up @@ -149,7 +149,21 @@ export const activateEventTypeHandler = async ({ ctx, input }: ActivateEventType
},
include: {
attendees: true,
eventType: true,
eventType: {
select: {
schedulingType: true,
slug: true,
hosts: {
select: {
user: {
select: {
email: true,
},
},
},
},
},
},
user: true,
},
});
Expand Down Expand Up @@ -181,6 +195,8 @@ export const activateEventTypeHandler = async ({ ctx, input }: ActivateEventType
language: { locale: booking?.user?.locale || defaultLocale },
eventType: {
slug: booking.eventType?.slug,
schedulingType: booking.eventType?.schedulingType,
hosts: booking.eventType?.hosts,
},
};
for (const step of eventTypeWorkflow.steps) {
Expand All @@ -194,6 +210,19 @@ export const activateEventTypeHandler = async ({ ctx, input }: ActivateEventType
switch (step.action) {
case WorkflowActions.EMAIL_HOST:
sendTo = [bookingInfo.organizer?.email];
const schedulingType = bookingInfo.eventType?.schedulingType;
const hosts = bookingInfo.attendees
.filter((attendee) =>
bookingInfo.eventType.hosts?.some((host) => host.user.email === attendee.email)
)
.map((host) => host.email);
if (
(schedulingType === SchedulingType.ROUND_ROBIN ||
schedulingType === SchedulingType.COLLECTIVE) &&
hosts
) {
sendTo = sendTo.concat(hosts);
}
break;
case WorkflowActions.EMAIL_ATTENDEE:
sendTo = bookingInfo.attendees.map((attendee) => attendee.email);
Expand Down