fix: break circular dependency in messageDispatcher via dependency injection #25342
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This PR breaks two circular dependencies in the workflows and billing modules:
Circular Dependency #1: reminderScheduler ↔ credit-service
Before:
cancelScheduledMessagesAndScheduleEmailsfrom reminderSchedulerAfter:
userIdsWithoutCreditsbefore callingcancelScheduledMessagesAndScheduleEmailsCircular Dependency #2: messageDispatcher ↔ credit-service (4-file cycle)
Before:
After:
creditCheckFnparameter to messageDispatcher functionscreditCheckFnthrough: scheduleWorkflowReminders → processWorkflowStep → scheduleSMSReminder/scheduleWhatsappReminder → messageDispatchercreditCheckFnis provided, use it; otherwise fall back to dynamic CreditService import for backward compatibilityKey Changes
Files Modified:
credit-service.ts: Pre-compute userIdsWithoutCredits before calling cancelScheduledMessagesAndScheduleEmailsreminderScheduler.ts: Accept userIdsWithoutCredits parameter, add creditCheckFn to ScheduleWorkflowRemindersArgs, thread through processWorkflowStepmessageDispatcher.ts: Accept optional creditCheckFn parameter, use it if provided or fall back to dynamic importsmsReminderManager.ts: Thread creditCheckFn through scheduleSMSReminderwhatsappReminderManager.ts: Thread creditCheckFn through scheduleWhatsappReminderFiles Created:
WorkflowReminderRepository.ts: New repository to encapsulate Prisma queries for workflow reminders (fixes repository pattern violations)Testing
All existing tests pass:
Manual Testing Needed:
Human Review Checklist
Critical items to review:
Potential risks:
Mandatory Tasks (DO NOT REMOVE)
How should this be tested?
Environment Setup:
IS_SMS_CREDITS_ENABLED=trueTest Scenarios:
Credit exhaustion (team):
Credit exhaustion (user):
Backward compatibility:
Immediate sends:
Expected Results:
Link to Devin run: https://app.devin.ai/sessions/001329399e164c19b682ea5d209e8f1c
Requested by: [email protected] (@hbjORbj)