Files
calendar/packages/features/ee/workflows/lib/reminders/providers/sendgridProvider.ts
T
Keith WilliamsGitHubdevin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>keith@cal.com <keith@cal.com>
6615b885d1 chore: Make app compatible with Fluid Compute (#19841)
* chore: Make app compatible with Fluid Compute

* Removed isCold from me api endpoint

* chore: Remove handler caching for Fluid Compute compatibility (#20692)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: keith@cal.com <keith@cal.com>

* fix client_id issue

* chore: Remove UNSTABLE_HANDLER_CACHE for Fluid Compute compatibility (#20694)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: keith@cal.com <keith@cal.com>

* Fixing types

* Fixing type issues

* chore: Remove importHandler references for Fluid Compute compatibility

Co-Authored-By: keith@cal.com <keith@cal.com>

* chore: Remove importHandler references from attributes router for Fluid Compute compatibility

Co-Authored-By: keith@cal.com <keith@cal.com>

---------

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: keith@cal.com <keith@cal.com>
2025-04-14 18:27:59 -04:00

116 lines
3.1 KiB
TypeScript

/**
* @deprecated use smtp with tasker instead
*/
import client from "@sendgrid/client";
import type { MailData } from "@sendgrid/helpers/classes/mail";
import sgMail from "@sendgrid/mail";
import { v4 as uuidv4 } from "uuid";
import { addHTMLStyles } from "@calcom/emails/templates/workflow-email";
import { SENDER_NAME } from "@calcom/lib/constants";
import { setTestEmail } from "@calcom/lib/testEmails";
const testMode = process.env.NEXT_PUBLIC_IS_E2E || process.env.INTEGRATION_TEST_MODE;
function assertSendgrid() {
if (process.env.SENDGRID_API_KEY && process.env.SENDGRID_EMAIL) {
const sendgridAPIKey = process.env.SENDGRID_API_KEY as string;
sgMail.setApiKey(sendgridAPIKey);
client.setApiKey(sendgridAPIKey);
} else {
console.error("Sendgrid credentials are missing from the .env file");
}
}
export async function getBatchId() {
if (testMode) {
return uuidv4();
}
assertSendgrid();
if (!process.env.SENDGRID_API_KEY) {
console.info("No sendgrid API key provided, returning DUMMY_BATCH_ID");
return "DUMMY_BATCH_ID";
}
const batchIdResponse = await client.request({
url: "/v3/mail/batch",
method: "POST",
});
return batchIdResponse[1].batch_id as string;
}
export function sendSendgridMail(
mailData: Partial<MailData> & { sender?: string | null; includeCalendarEvent?: boolean }
) {
assertSendgrid();
if (testMode) {
if (!mailData.sendAt) {
setTestEmail({
to: mailData.to?.toString() || "",
from: {
email: process.env.SENDGRID_EMAIL as string,
name: mailData.sender || SENDER_NAME,
},
subject: mailData.subject || "",
html: mailData.html || "",
});
}
console.log(
"Skipped Sending Email as process.env.NEXT_PUBLIC_IS_E2E or process.env.INTEGRATION_TEST_MODE is set. Emails are available in globalThis.testEmails"
);
return new Promise((r) => r("Skipped sendEmail for Unit Tests"));
}
if (!process.env.SENDGRID_API_KEY) {
console.info("No sendgrid API key provided, skipping email");
return Promise.resolve();
}
return sgMail.send({
to: mailData.to,
from: {
email: process.env.SENDGRID_EMAIL as string,
name: mailData.sender || SENDER_NAME,
},
subject: mailData.subject,
html: addHTMLStyles(mailData.html),
batchId: mailData.batchId,
replyTo: mailData.replyTo || (process.env.SENDGRID_EMAIL as string),
attachments: mailData.attachments,
sendAt: mailData.sendAt,
});
}
export function cancelScheduledEmail(referenceId: string | null) {
if (!referenceId) {
console.info("No referenceId provided, skip canceling email");
return Promise.resolve();
}
assertSendgrid();
return client.request({
url: "/v3/user/scheduled_sends",
method: "POST",
body: {
batch_id: referenceId,
status: "cancel",
},
});
}
export function deleteScheduledSend(referenceId: string | null) {
if (!referenceId) {
console.info("No referenceId provided, skip deleting scheduledSend");
return Promise.resolve();
}
assertSendgrid();
return client.request({
url: `/v3/user/scheduled_sends/${referenceId}`,
method: "DELETE",
});
}