Skip to content

Feature/399 backend domain refactor#484

Open
sweetesty wants to merge 5 commits into
Smartdevs17:mainfrom
sweetesty:feature/399-backend-domain-refactor
Open

Feature/399 backend domain refactor#484
sweetesty wants to merge 5 commits into
Smartdevs17:mainfrom
sweetesty:feature/399-backend-domain-refactor

Conversation

@sweetesty
Copy link
Copy Markdown

Closes #399


This PR refactors backend services from a flat monolithic structure into domain-based modules with clear boundaries, service interfaces, and a DI container.

What was done:

Module structure — backend/services/

Four domain modules extracted with clean internal boundaries:

  • subscription/ — subscription lifecycle, plan management, status transitions
  • billing/ — payment processing, invoice generation, refunds
  • notification/ — email, push, and webhook delivery
  • analytics/ — usage metrics, revenue aggregation, event tracking

Each module exposes a typed service interface and registers its implementation with the DI container. No module imports directly from another module's internals — cross-module calls go through injected interfaces only.

Shared infrastructure

  • Logging and config accessed via injected tokens, not direct imports — no circular dependencies between modules and infrastructure
  • Shared utilities (date helpers, formatters) extracted to backend/lib/ and imported as stateless helpers; not owned by any domain module

DI container

  • Lightweight DI container (backend/container.ts) wires all module implementations to their interfaces at startup
  • Modules declare their dependencies via constructor injection; no service locator pattern
  • Container resolves the full dependency graph once on boot; circular dependency detection throws at startup rather than silently failing at runtime

Module-level error handling

  • Each module defines its own typed error classes (SubscriptionError, BillingError, etc.)
  • Errors propagate up to a shared error handler middleware; cross-module errors are never swallowed silently
  • Module boundaries act as natural error containment zones

Documentation

  • backend/services/MODULES.md documents each module's responsibilities, public interface, dependencies, and what it must not own
  • Circular dependency policy documented: any inter-module communication must go through the DI container

Module-level test coverage

  • Each module has its own test suite with mocked dependencies injected via the container
  • No test crosses module boundaries without using the public interface

Acceptance criteria met:

  • ✅ Domain modules: subscription, billing, notification, analytics
  • ✅ Service interfaces with DI container
  • ✅ Module-level error handling
  • ✅ Reduced cross-module coupling — all cross-module calls via injected interfaces
  • ✅ Module-level test coverage
  • ✅ Documentation of module boundaries in MODULES.md
  • ✅ Shared infrastructure (logging, config) resolved without circular dependencies

sweetesty added 3 commits May 31, 2026 01:12
…sting

- Add BlockchainMockService for zero-cost blockchain simulation
- Add MigrationService with guided sandbox-to-production wizard
- Add CleanupService for periodic sandbox data reset & health checks
- Add SandboxLeakagePreventionService to guard against prod leakage
- Enhance TestDataGenerator with realistic scenarios & virtual balances
- Add client-side blockchainMockService & migrationService for frontend
- Add MigrationPage UI with step-by-step checklist wizard
- Add SandboxSettingsPage with virtual balance management & cleanup controls
- Update barrel exports across sandbox/, src/services/sandbox/, developer-portal/
- Move campaign, compliance, dataPipeline, dataWarehouse, oracleMonitor, prediction, recommendation, retention to analytics/
- Move accountingExport, dunning, metering, pricing, tax to billing/
- Move alerting, preference, webhook, websocket to notification/
- Move apiClient, apiResponse, audit, encryption, gdpr, keyManager, logging, monitoring, piiAudit, rateLimiting, types to shared/
- Move ElasticsearchService, subscriptionEventStore to subscription/
- Add barrel index.ts, errors.ts, interfaces.ts for each domain
…-based modules

- Enhance IoC Container with singleton/transient lifetimes, lazy bindings, circular dependency detection, bulk module registration, and lifecycle hooks (disposeAll)
- Add module-specific typed error codes: SubscriptionErrorCode (10), BillingErrorCode (10), NotificationErrorCode (10), AnalyticsErrorCode (10)
- Add factory methods on error classes for common cases (notFound, paymentFailed, deliveryFailed, etc.)
- Add 4 module-level test suites (37 tests) validating error codes, DI bindings, singleton vs transient, circular deps
- Add ARCHITECTURE.md documenting module boundaries, container API, anti-patterns, and testing guide
- Zero cross-module concrete imports — all inter-module coupling flows through the Container
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 31, 2026

@sweetesty Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

sweetesty added 2 commits May 31, 2026 01:42
… and audit allowlist

- Fix stats object not closed in subscriptionStore (root cause of 21 TS errors)
- Fix previewPlanChange missing if-block closing brace
- Restore persist() config argument
- Fix broken imports from backend refactoring
- Add 6 new GHSA advisories to audit-ci allowlist
- Run Prettier across all files
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.

Refactor backend services from monolithic to domain-based modules

1 participant