fix: break circular dependency in messageDispatcher via dependency injection#25342
Closed
fix: break circular dependency in messageDispatcher via dependency injection#25342
Conversation
…ervice - Created WorkflowReminderRepository to handle workflow reminder queries - Refactored cancelScheduledMessagesAndScheduleEmails to accept userIdsWithoutCredits parameter - Moved credit-checking logic from workflows to CreditService - Changed dynamic import to static import in CreditService - Updated tests to pass userIdsWithoutCredits parameter - Removed unused imports (prisma, WorkflowMethods) This creates a one-way dependency (billing -> workflows) and follows the repository pattern by removing direct Prisma usage from reminderScheduler. Co-Authored-By: benny@cal.com <sldisek783@gmail.com>
- Use MembershipRepository.listAcceptedTeamMemberIds instead of findAllAcceptedPublishedTeamMemberships - Re-add prisma import to reminderScheduler.ts for UserRepository - Update WorkflowReminderRepository to use WorkflowActions enum instead of string Co-Authored-By: benny@cal.com <sldisek783@gmail.com>
…jection 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>
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Contributor
|
Closing this PR in favor of #25343. This PR accidentally included commits from PR #25312 (reminderScheduler fix) because I branched from the wrong base. The user explicitly requested the messageDispatcher fix to be in a separate PR from the reminderScheduler fix. PR #25343 is a clean branch from main with only the messageDispatcher circular dependency fix. |
Contributor
|
Superseded by #25343 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: benny@cal.com (@hbjORbj)