Skip to content

Refactor: break up messages_service.py monolith into domain modules#195

Merged
gregv merged 3 commits intodevelopfrom
refactor/break-up-messages-service-monolith
Mar 6, 2026
Merged

Refactor: break up messages_service.py monolith into domain modules#195
gregv merged 3 commits intodevelopfrom
refactor/break-up-messages-service-monolith

Conversation

@gregv
Copy link
Contributor

@gregv gregv commented Mar 6, 2026

Summary

Breaks up the messages_service.py monolith (originally 3155 lines, ~68 functions) into proper domain modules, reducing it to ~1200 lines (62% reduction).

Phase 1: Foundation

  • Shared utilities (common/utils/firestore_helpers.py): Extracted doc_to_json, doc_to_json_recursive, hash_key, log_execution_time, clear_all_caches
  • Hackathon service (services/hackathons_service.py): Extracted 17 hackathon functions (~600 lines)
  • Nonprofit service (services/nonprofits_service.py): Merged 10 raw-Firestore functions from messages_service
  • Import rewiring: Redirected re-exports in api/teams/, api/newsletters/, api/judging/ to real sources

Phase 2: Teams & Email

  • Teams service (services/teams_service.py): Extracted 8 team functions (~480 lines)
  • Email service (services/email_service.py): Extracted 6 email/lead functions (~260 lines)
  • Deduplication: add_utm() consolidated from 2 copies into email_service.py
  • Import cleanup: Removed unused imports from messages_service

What remains in messages_service.py (~1200 lines):

  • News/Praise (~200 lines)
  • Problem statements (~200 lines, deprecated _old functions)
  • User/Profile (~400 lines, deprecated)
  • Feedback/Giveaway/Onboarding (~200 lines)
  • Firebase init + auth boilerplate (~100 lines)
  • Misc (image upload, etc.)

Files changed

File Action
common/utils/firestore_helpers.py New — shared Firestore utilities
services/hackathons_service.py New — hackathon domain service
services/teams_service.py New — team domain service
services/email_service.py New — email/lead service
services/nonprofits_service.py Expanded — merged raw-Firestore functions
api/messages/messages_service.py Reduced — 3155 → 1200 lines
api/messages/messages_views.py Updated imports
api/judging/judging_service.py Updated imports
api/teams/teams_service.py Updated imports
api/newsletters/newsletter_service.py Updated imports
Test files Updated mock paths

Test plan

  • pytest — all tests pass (same pre-existing failures, no regressions)
  • Manually verify hackathon endpoints
  • Manually verify team CRUD endpoints
  • Manually verify nonprofit endpoints
  • Manually verify email sending (lead signup, nonprofit welcome)

🤖 Generated with Claude Code

gregv and others added 3 commits March 6, 2026 08:57
Extract ~1200 lines from the 3155-line messages_service.py god module:

- Create common/utils/firestore_helpers.py with shared Firestore utilities
  (doc_to_json, doc_to_json_recursive, hash_key, log_execution_time, cache mgmt)
- Create services/hackathons_service.py with 17 hackathon functions (~600 lines)
- Expand services/nonprofits_service.py with 10 raw-Firestore nonprofit functions
- Redirect 9 re-exported imports in teams_service.py to their real sources
- Redirect newsletter_service.py imports to db.db

No API routes, URLs, or behavior changes. All 17 existing tests pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Extract 8 team functions into services/teams_service.py (~480 lines)
- Extract 6 email functions into services/email_service.py (~260 lines)
- Move get_db() import to db.db (eliminate local duplicate)
- Deduplicate add_utm() helper (was copied in hackathons_service)
- Update imports in messages_views, judging_service, teams_service
- Remove unused imports from messages_service (github, slack, oauth)
- messages_service.py reduced from ~1970 to ~1200 lines

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Extract news/praise functions into services/news_service.py (~150 lines)
- Extract feedback functions into services/feedback_service.py (~140 lines)
- Extract giveaway functions into services/giveaway_service.py (~75 lines)
- Extract onboarding feedback into services/onboarding_service.py (~90 lines)
- Clean up unused imports from messages_service.py
- messages_service.py reduced from ~1200 to ~760 lines (3155 → 760 total, 76% reduction)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gregv gregv merged commit 80dbc96 into develop Mar 6, 2026
3 checks passed
@gregv gregv deleted the refactor/break-up-messages-service-monolith branch March 6, 2026 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant