* fix: break circular dependency in messageDispatcher via dependency injection Break the 4-file circular dependency chain: credit-service → reminderScheduler → smsReminderManager → messageDispatcher → credit-service Solution: - Add optional creditCheckFn parameter to messageDispatcher functions - Thread creditCheckFn through the call chain: scheduleWorkflowReminders → scheduleSMSReminder/scheduleWhatsappReminder → messageDispatcher - When creditCheckFn is provided, use it; otherwise fall back to dynamic CreditService import for backward compatibility - This breaks the workflows → billing import while preserving immediate fallback behavior Changes: - messageDispatcher: Accept optional creditCheckFn parameter, use it if provided - smsReminderManager: Thread creditCheckFn through scheduleSMSReminder - whatsappReminderManager: Thread creditCheckFn through scheduleWhatsappReminder - reminderScheduler: Add creditCheckFn to ScheduleWorkflowRemindersArgs and pass through processWorkflowStep All type checks, lint checks, and unit tests pass. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * feat: wire creditCheckFn from all callers to complete circular dependency fix - Add creditCheckFn parameter to WorkflowService.scheduleFormWorkflows - Wire creditCheckFn from all 10 entry points that call workflow scheduling: * formSubmissionUtils.ts (form submissions) * roundRobinManualReassignment.ts (round-robin reassignment) * triggerFormSubmittedNoEventWorkflow.ts (form workflow trigger) * handleBookingRequested.ts (booking requests) * RegularBookingService.ts (2 calls - payment initiated & new bookings) * handleSeats.ts (seated bookings) * handleConfirmation.ts (2 calls - confirmation & payment) * handleMarkNoShow.ts (no-show updates) * confirm.handler.ts (booking rejection) - Update test expectations to use expect.objectContaining() - Fix pre-existing lint warning in handleMarkNoShow.ts (any type) - This completes the messageDispatcher circular dependency fix by ensuring creditCheckFn is actually passed through the call chain, breaking the 4-file circular dependency at runtime Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * fix: use generic type with type guard in logFailedResults to fix type check error - Replace constrained type with generic type parameter - Add proper type guard for rejected promises - Fixes CI type check failure in handleMarkNoShow.ts:385 - Avoids 'any' type while accepting any fulfilled value shape Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * wip * wip * wip * revert * revert * feat: wire creditCheckFn from all remaining callers to eliminate fallbacks - Wire creditCheckFn in packages/sms/sms-manager.ts (can safely import CreditService) - Create makeHandler factory pattern for CRON endpoints (scheduleSMSReminders.ts, scheduleWhatsappReminders.ts) - Wire creditCheckFn from apps/web CRON routes to factories - Add warning log in messageDispatcher when fallback is used - Complete creditCheckFn wiring from all direct callers (activateEventType.handler.ts, util.ts) This eliminates all fallbacks to dynamic import except as a safety net for unforeseen call sites. The circular dependency (workflows ↔ billing) remains acceptable as discussed with user (Option C). Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * test: update formSubmissionUtils tests to expect creditCheckFn parameter The scheduleFormWorkflows function now receives creditCheckFn as a parameter. Updated test assertions to use expect.objectContaining() with creditCheckFn: expect.any(Function) to account for the new dependency injection parameter. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * test: update sms-manager test to expect creditCheckFn parameter The sendSmsOrFallbackEmail function now receives creditCheckFn as a parameter. Updated test assertion to use expect.objectContaining() with creditCheckFn: expect.any(Function) to account for the new dependency injection parameter. Also removed teamId: undefined assertion as the key may be omitted entirely from the actual call. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * feat: make creditCheckFn required to fully break circular dependency This commit completes the circular dependency fix by making creditCheckFn required throughout the call chain, eliminating the dynamic import fallback entirely. Changes: - Make creditCheckFn required in messageDispatcher functions (sendSmsOrFallbackEmail, scheduleSmsOrFallbackEmail) - Remove dynamic import fallback and warning log from messageDispatcher - Make creditCheckFn required in ScheduleTextReminderArgs (smsReminderManager) - Make creditCheckFn required in processWorkflowStep and ScheduleWorkflowRemindersArgs (reminderScheduler) - Add creditCheckFn to SendCancelledRemindersArgs and wire from handleCancelBooking The circular dependency is now fully broken - no more dynamic imports of CreditService from within the workflows package. All callers must explicitly provide creditCheckFn via dependency injection. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * fix: make creditCheckFn required in WorkflowService.scheduleFormWorkflows This commit fixes the CI type check error by making creditCheckFn required in WorkflowService.scheduleFormWorkflows. Previously, creditCheckFn was optional in scheduleFormWorkflows but required in scheduleWorkflowReminders, causing a type mismatch. Changes: - Make creditCheckFn required in scheduleFormWorkflows signature - Update WorkflowService.test.ts to pass mock creditCheckFn in all test cases - Add responseId and routedEventTypeId to test calls for completeness All callers of scheduleFormWorkflows already pass creditCheckFn, so this change is safe and completes the circular dependency fix. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * remove * fix * refactor * refactor * refactor * wip * fix * fix * rm --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
229 lines
6.2 KiB
TypeScript
229 lines
6.2 KiB
TypeScript
import { describe, expect, vi, beforeEach } from "vitest";
|
|
|
|
import { scheduleWorkflowReminders } from "@calcom/features/ee/workflows/lib/reminders/reminderScheduler";
|
|
import { tasker } from "@calcom/features/tasker";
|
|
import { WorkflowTriggerEvents, WorkflowActions, WorkflowTemplates, TimeUnit } from "@calcom/prisma/enums";
|
|
import { test } from "@calcom/web/test/fixtures/fixtures";
|
|
|
|
import { WorkflowService } from "./WorkflowService";
|
|
|
|
vi.mock("@calcom/features/ee/workflows/lib/reminders/reminderScheduler");
|
|
vi.mock("@calcom/features/tasker");
|
|
|
|
const mockScheduleWorkflowReminders = vi.mocked(scheduleWorkflowReminders);
|
|
const mockTasker = vi.mocked(tasker);
|
|
|
|
// Mock the getHideBranding function to return false
|
|
vi.mock("@calcom/features/profile/lib/hideBranding", () => ({
|
|
getHideBranding: vi.fn().mockResolvedValue(false),
|
|
}));
|
|
|
|
describe("WorkflowService.scheduleFormWorkflows", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
const mockForm = {
|
|
id: "form-123",
|
|
userId: 101,
|
|
teamId: null,
|
|
fields: [
|
|
{ type: "email", identifier: "email" },
|
|
{ type: "phone", identifier: "phone" },
|
|
],
|
|
user: {
|
|
email: "formowner@example.com",
|
|
timeFormat: 12,
|
|
locale: "en",
|
|
},
|
|
};
|
|
|
|
const mockResponses = {
|
|
email: {
|
|
value: "submitter@example.com",
|
|
response: "submitter@example.com",
|
|
},
|
|
phone: {
|
|
value: "+1234567890",
|
|
response: "+1234567890",
|
|
},
|
|
};
|
|
|
|
test("should call scheduleWorkflowReminders for FORM_SUBMITTED triggers with correct phone number", async () => {
|
|
const workflows = [
|
|
{
|
|
id: 1,
|
|
name: "Form Submitted",
|
|
userId: 101,
|
|
teamId: null,
|
|
trigger: WorkflowTriggerEvents.FORM_SUBMITTED,
|
|
time: null,
|
|
timeUnit: null,
|
|
steps: [
|
|
{
|
|
id: 1,
|
|
action: WorkflowActions.SMS_ATTENDEE,
|
|
sendTo: null,
|
|
reminderBody: "Thank you!",
|
|
emailSubject: "Form Received",
|
|
template: WorkflowTemplates.CUSTOM,
|
|
verifiedAt: new Date(),
|
|
includeCalendarEvent: false,
|
|
numberVerificationPending: false,
|
|
numberRequired: false,
|
|
},
|
|
],
|
|
},
|
|
];
|
|
|
|
const mockCreditCheckFn = vi.fn().mockResolvedValue(true);
|
|
|
|
await WorkflowService.scheduleFormWorkflows({
|
|
workflows,
|
|
responses: mockResponses,
|
|
form: mockForm,
|
|
responseId: 123,
|
|
routedEventTypeId: null,
|
|
creditCheckFn: mockCreditCheckFn,
|
|
});
|
|
|
|
expect(mockScheduleWorkflowReminders).toHaveBeenCalledWith({
|
|
smsReminderNumber: "+1234567890",
|
|
formData: {
|
|
responses: mockResponses,
|
|
user: { email: "formowner@example.com", timeFormat: 12, locale: "en" },
|
|
routedEventTypeId: null,
|
|
},
|
|
hideBranding: false,
|
|
workflows: [workflows[0]],
|
|
creditCheckFn: mockCreditCheckFn,
|
|
});
|
|
});
|
|
|
|
test("should create task for FORM_SUBMITTED_NO_EVENT triggers", async () => {
|
|
const workflows = [
|
|
{
|
|
id: 2,
|
|
name: "Form Follow-up",
|
|
userId: 101,
|
|
teamId: null,
|
|
trigger: WorkflowTriggerEvents.FORM_SUBMITTED_NO_EVENT,
|
|
time: 30,
|
|
timeUnit: TimeUnit.MINUTE,
|
|
steps: [
|
|
{
|
|
id: 2,
|
|
action: WorkflowActions.EMAIL_ATTENDEE,
|
|
sendTo: null,
|
|
reminderBody: "Follow up message",
|
|
emailSubject: "Follow Up",
|
|
template: WorkflowTemplates.CUSTOM,
|
|
verifiedAt: new Date(),
|
|
includeCalendarEvent: false,
|
|
numberVerificationPending: false,
|
|
numberRequired: false,
|
|
sender: null,
|
|
},
|
|
],
|
|
},
|
|
];
|
|
|
|
mockTasker.create.mockResolvedValue({ id: "task-123" });
|
|
|
|
const mockCreditCheckFn = vi.fn().mockResolvedValue(true);
|
|
|
|
await WorkflowService.scheduleFormWorkflows({
|
|
workflows,
|
|
responses: mockResponses,
|
|
responseId: 123,
|
|
form: mockForm,
|
|
routedEventTypeId: null,
|
|
creditCheckFn: mockCreditCheckFn,
|
|
});
|
|
|
|
expect(mockTasker.create).toHaveBeenCalledWith(
|
|
"triggerFormSubmittedNoEventWorkflow",
|
|
{
|
|
responseId: 123,
|
|
responses: mockResponses,
|
|
smsReminderNumber: "+1234567890",
|
|
hideBranding: false,
|
|
routedEventTypeId: null,
|
|
submittedAt: expect.any(Date),
|
|
form: {
|
|
id: "form-123",
|
|
userId: 101,
|
|
teamId: undefined,
|
|
user: {
|
|
email: "formowner@example.com",
|
|
timeFormat: 12,
|
|
locale: "en",
|
|
},
|
|
},
|
|
workflow: workflows[0],
|
|
},
|
|
{ scheduledAt: expect.any(Date) }
|
|
);
|
|
});
|
|
|
|
test("should handle forms without phone fields by passing null smsReminderNumber", async () => {
|
|
const formWithoutPhone = {
|
|
...mockForm,
|
|
fields: [
|
|
{ type: "email", identifier: "email" },
|
|
{ type: "text", identifier: "name" },
|
|
],
|
|
};
|
|
|
|
const workflows = [
|
|
{
|
|
id: 1,
|
|
name: "Form Submitted",
|
|
userId: 101,
|
|
teamId: null,
|
|
trigger: WorkflowTriggerEvents.FORM_SUBMITTED,
|
|
time: null,
|
|
timeUnit: null,
|
|
steps: [
|
|
{
|
|
id: 1,
|
|
action: WorkflowActions.EMAIL_ATTENDEE,
|
|
sendTo: null,
|
|
reminderBody: "Thank you!",
|
|
emailSubject: "Form Received",
|
|
template: WorkflowTemplates.CUSTOM,
|
|
verifiedAt: new Date(),
|
|
includeCalendarEvent: false,
|
|
numberVerificationPending: false,
|
|
numberRequired: false,
|
|
sender: null,
|
|
},
|
|
],
|
|
},
|
|
];
|
|
|
|
const mockCreditCheckFn = vi.fn().mockResolvedValue(true);
|
|
|
|
await WorkflowService.scheduleFormWorkflows({
|
|
workflows,
|
|
responses: mockResponses,
|
|
form: formWithoutPhone,
|
|
responseId: 123,
|
|
routedEventTypeId: null,
|
|
creditCheckFn: mockCreditCheckFn,
|
|
});
|
|
|
|
expect(mockScheduleWorkflowReminders).toHaveBeenCalledWith({
|
|
smsReminderNumber: null,
|
|
formData: {
|
|
responses: mockResponses,
|
|
user: { email: "formowner@example.com", timeFormat: 12, locale: "en" },
|
|
routedEventTypeId: null,
|
|
},
|
|
hideBranding: false,
|
|
workflows: [workflows[0]],
|
|
creditCheckFn: mockCreditCheckFn,
|
|
});
|
|
});
|
|
});
|