Files
calendar/setupVitest.ts
T
MorganGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>hbjORbj
a73b804d48 refactor: Split EmailManager into focused service files (#24997)
* refactor: Split EmailManager into focused service files

- Created separate service files for different email categories:
  - auth-email-service.ts: Authentication and verification emails
  - organization-email-service.ts: Organization and team emails
  - billing-email-service.ts: Payment and credit-related emails
  - integration-email-service.ts: Integration and app-related emails
  - workflow-email-service.ts: Workflow and custom emails
  - recording-email-service.ts: Recording and transcript emails

- Refactored email-manager.ts to keep only core booking lifecycle functions
- Removed unused imports from email-manager.ts
- Updated index.ts to export from all new service files
- Updated all imports across the codebase to use package root (@calcom/emails)
- Fixed lint warnings in handleChildrenEventTypes.ts

This reduces the import cost of EmailManager by allowing consumers to import only the specific email services they need.

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

* refactor: Update all imports to use direct service file paths

- Update 49 files to import directly from service files instead of barrel file
- Update packages/emails/index.ts to keep only email-manager and renderEmail exports
- Fix dynamic import in passwordResetRequest.ts
- Update renderEmail imports to use direct path
- Update test file to import from specific service module
- Fix ESLint warnings in modified files (unused variables, unused expressions)

This ensures consumers only import the specific email services they need,
reducing import cost by avoiding the barrel file pattern for service files.

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

* fix: Use default import for renderEmail

renderEmail is exported as a default export, not a named export.
Changed from 'import { renderEmail }' to 'import renderEmail'.

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

* fix: Update test mocks to use direct service file imports

- Update handleNoShowFee.test.ts to mock @calcom/emails/billing-email-service
- Update credit-service.test.ts to mock @calcom/emails/billing-email-service
- These tests were failing because they were mocking the barrel file @calcom/emails
  which no longer exports service functions after the refactoring

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

* fix: unit test spy

* fix: unit test mock

* address cubic comments

* fix: type error sendMonthlyDigestEmail

* remove barrel file and sendEmail unused task

* fixup! remove barrel file and sendEmail unused task

* fixup! fixup! remove barrel file and sendEmail unused task

* fix: integration test mock emails

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: hbjORbj <sldisek783@gmail.com>
2025-11-11 14:21:10 +02:00

164 lines
4.1 KiB
TypeScript

import matchers from "@testing-library/jest-dom/matchers";
import ResizeObserver from "resize-observer-polyfill";
import { vi, expect } from "vitest";
import createFetchMock from "vitest-fetch-mock";
import type { CalendarService } from "@calcom/types/Calendar";
global.ResizeObserver = ResizeObserver;
const fetchMocker = createFetchMock(vi);
// sets globalThis.fetch and globalThis.fetchMock to our mocked version
fetchMocker.enableMocks();
expect.extend(matchers);
class MockExchangeCalendarService implements CalendarService {
constructor() {}
async createEvent() {
return {
uid: "mock",
id: "mock",
password: "",
type: "",
url: "",
additionalInfo: {},
};
}
async updateEvent() {
return {
uid: "mock",
id: "mock",
password: "",
type: "",
url: "",
additionalInfo: {},
};
}
async deleteEvent() {}
async getAvailability() {
return [];
}
async listCalendars() {
return [];
}
}
vi.mock("@calcom/exchangecalendar/lib/CalendarService", () => ({
default: MockExchangeCalendarService,
}));
vi.mock("@calcom/exchange2013calendar/lib/CalendarService", () => ({
default: MockExchangeCalendarService,
}));
vi.mock("@calcom/exchange2016calendar/lib/CalendarService", () => ({
default: MockExchangeCalendarService,
}));
const MOCK_PAYMENT_UID = "MOCK_PAYMENT_UID";
class MockPaymentService {
constructor(credentials?: any) {
this.credentials = credentials;
}
private credentials: any;
async create(
payment: any,
bookingId: number,
userId: number,
username: string | null,
bookerName: string | null,
paymentOption: any,
bookerEmail: string,
bookerPhoneNumber?: string | null,
selectedEventTypeTitle?: string,
eventTitle?: string
) {
const { default: prismaMock } = await import("./tests/libs/__mocks__/prisma");
const externalId = "mock_payment_external_id";
const paymentCreateData = {
uid: MOCK_PAYMENT_UID,
appId: null,
bookingId,
fee: 10,
success: false,
refunded: false,
data: {},
externalId,
paymentOption,
amount: payment.amount,
currency: payment.currency,
};
const createdPayment = await prismaMock.payment.create({
data: paymentCreateData,
});
return createdPayment;
}
async collectCard() {
return { success: true };
}
async chargeCard() {
return { success: true };
}
async refund() {
return { success: true };
}
async deletePayment() {
return { success: true };
}
async afterPayment(event: any, booking: any, paymentData: any) {
const { sendAwaitingPaymentEmailAndSMS } = await import("@calcom/emails/email-manager");
await sendAwaitingPaymentEmailAndSMS({
...event,
paymentInfo: {
link: "http://mock-payment.example.com/",
paymentOption: paymentData.paymentOption || "ON_BOOKING",
amount: paymentData.amount,
currency: paymentData.currency,
},
});
return { success: true };
}
}
vi.mock("@calcom/app-store/stripepayment/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/paypal/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/alby/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/hitpay/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/btcpayserver/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/mock-payment-app/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/payment.services.generated", () => ({
PaymentServiceMap: {
stripepayment: Promise.resolve({ PaymentService: MockPaymentService }),
paypal: Promise.resolve({ PaymentService: MockPaymentService }),
alby: Promise.resolve({ PaymentService: MockPaymentService }),
hitpay: Promise.resolve({ PaymentService: MockPaymentService }),
btcpayserver: Promise.resolve({ PaymentService: MockPaymentService }),
"mock-payment-app": Promise.resolve({ PaymentService: MockPaymentService }),
},
}));