Files
calendar/packages/features/tasker/tasks/sendWorkflowEmails.ts
T
Lauris SkraucisandGitHub edf9cd70dd fix: hide cal branding on platform workflows (#27385)
* fix: hide cal branding on platform workflows

* refactor: rely on existing with platform variables code

* revert: comment

* fix e2e

* chore: remove unit results
2026-02-09 13:46:52 -03:00

92 lines
2.9 KiB
TypeScript

import { z } from "zod";
import { sendCustomWorkflowEmail } from "@calcom/emails/workflow-email-service";
import { CalendarEventBuilder } from "@calcom/features/CalendarEventBuilder";
import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
import { BookingSeatRepository } from "@calcom/features/bookings/repositories/BookingSeatRepository";
import { EmailWorkflowService } from "@calcom/features/ee/workflows/lib/service/EmailWorkflowService";
import { WorkflowReminderRepository } from "@calcom/features/ee/workflows/repositories/WorkflowReminderRepository";
import { bookingMetadataSchema } from "@calcom/prisma/zod-utils";
import { prisma } from "@calcom/prisma";
export const ZSendWorkflowEmailsSchemaEager = z.object({
to: z.array(z.string()),
subject: z.string(),
html: z.string(),
replyTo: z.string().optional(),
sender: z.string().nullable().optional(),
attachments: z
.array(
z
.object({
content: z.string(),
filename: z.string(),
})
.passthrough()
)
.optional(),
});
const ZSendWorkflowEmailsSchemaLazy = z.object({
bookingUid: z.string(),
workflowReminderId: z.number(),
});
export const ZSendWorkflowEmailsSchema = z.union([
ZSendWorkflowEmailsSchemaEager,
ZSendWorkflowEmailsSchemaLazy,
]);
export async function sendWorkflowEmails(payload: string): Promise<void> {
const mailData = ZSendWorkflowEmailsSchema.parse(JSON.parse(payload));
// Generate workflow body to send
if ("bookingUid" in mailData && "workflowReminderId" in mailData) {
const bookingRepository = new BookingRepository(prisma);
const booking = await bookingRepository.getBookingForCalEventBuilderFromUid(mailData.bookingUid);
if (!booking) {
throw new Error("Booking not found");
}
const bookingMetadata = bookingMetadataSchema.parse(booking.metadata || {});
const calendarEvent = (await CalendarEventBuilder.fromBooking(booking, {
platformClientId: bookingMetadata?.platformClientId,
})).build();
if (!calendarEvent) {
throw new Error("Calendar event could not be built");
}
// Check if videoCallUrl exists in booking metadata and add it to evt.metadata
const metadata = bookingMetadata?.videoCallUrl
? {
videoCallUrl: bookingMetadata.videoCallUrl,
}
: undefined;
const evtWithMetadata = { ...calendarEvent, metadata };
const workflowReminderRepository = new WorkflowReminderRepository(prisma);
const bookingSeatRepository = new BookingSeatRepository(prisma);
const emailWorkflowService = new EmailWorkflowService(workflowReminderRepository, bookingSeatRepository);
await emailWorkflowService.handleSendEmailWorkflowTask({
evt: evtWithMetadata,
workflowReminderId: mailData.workflowReminderId,
});
return;
}
await Promise.all(
mailData.to.map((to) =>
sendCustomWorkflowEmail({
...mailData,
to,
})
)
);
}