* add workflows to bookingScenario * activate sandbox mode for unit/integreation tests * add sendgrid specific code to SendgridProvider * Refactor WIP * remove duplicate sendgridProvider file * first implementation for testing workflows * revert unintended changes * comment out Workflow trigger tests * move sendgrid check after test mode * Update signup.tsx * fix esLint * test webhooks on all tests in fresh-booking.test.ts * fix subjectPattern as title can be different * add workflow tests to reschedule.test.ts * code clean up * code clean up * fix sendgrid credentials missing message * code clean up --------- Co-authored-by: CarinaWolli <wollencarina@gmail.com>
80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import client from "@sendgrid/client";
|
|
import type { MailData } from "@sendgrid/helpers/classes/mail";
|
|
import sgMail from "@sendgrid/mail";
|
|
|
|
import { SENDER_NAME } from "@calcom/lib/constants";
|
|
import { setTestEmail } from "@calcom/lib/testEmails";
|
|
|
|
let sendgridAPIKey: string;
|
|
let senderEmail: string;
|
|
|
|
function assertSendgrid() {
|
|
if (process.env.SENDGRID_API_KEY && process.env.SENDGRID_EMAIL) {
|
|
sendgridAPIKey = process.env.SENDGRID_API_KEY as string;
|
|
senderEmail = process.env.SENDGRID_EMAIL as string;
|
|
sgMail.setApiKey(sendgridAPIKey);
|
|
client.setApiKey(sendgridAPIKey);
|
|
} else {
|
|
console.error("Sendgrid credentials are missing from the .env file");
|
|
}
|
|
}
|
|
|
|
export async function getBatchId() {
|
|
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>,
|
|
addData: { sender?: string | null; includeCalendarEvent?: boolean }
|
|
) {
|
|
assertSendgrid();
|
|
|
|
const testMode = process.env.NEXT_PUBLIC_IS_E2E || process.env.INTEGRATION_TEST_MODE;
|
|
if (testMode) {
|
|
if (!mailData.sendAt) {
|
|
setTestEmail({
|
|
to: mailData.to?.toString() || "",
|
|
from: {
|
|
email: senderEmail,
|
|
name: addData.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 (!sendgridAPIKey) {
|
|
console.info("No sendgrid API key provided, skipping email");
|
|
return Promise.resolve();
|
|
}
|
|
|
|
return sgMail.send({
|
|
to: mailData.to,
|
|
from: {
|
|
email: senderEmail,
|
|
name: addData.sender || SENDER_NAME,
|
|
},
|
|
subject: mailData.subject,
|
|
html: mailData.html || "",
|
|
batchId: mailData.batchId,
|
|
replyTo: mailData.replyTo || senderEmail,
|
|
attachments: mailData.attachments,
|
|
sendAt: mailData.sendAt,
|
|
});
|
|
}
|