Repository navigation
Refactor local infrastructure services - #1471
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
Refactors several “local infrastructure” utilities into factory-created services to encapsulate state (e.g., initialization flags) and make dependency injection more explicit across modules.
Changes:
- Replaced module-level realtime initializers (weight, macro profile, day diet) with
create*RealtimeService()factories. - Refactored Sentry initialization into a
createSentryService()factory and updated telemetry/container wiring accordingly. - Encapsulated API food import logic behind
createApiFoodImportService()and updated food CRUD to use it.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/modules/weight/infrastructure/weight/supabase/realtime.ts | Converts weight realtime initializer into a service factory with internal init state. |
| src/modules/weight/application/weight/usecases/weightUseCases.ts | Updates weight use-cases to create/use the new realtime service. |
| src/modules/observability/infrastructure/sentry/sentry.ts | Refactors Sentry init into a service factory with injectable deps. |
| src/modules/observability/application/telemetry.ts | Changes telemetry deps to accept a Sentry service instance. |
| src/modules/diet/macro-profile/infrastructure/supabase/realtime.ts | Converts macro profile realtime initializer into a service factory. |
| src/modules/diet/macro-profile/application/usecases/macroProfileState.ts | Updates macro profile state to use the new realtime service. |
| src/modules/diet/food/infrastructure/api/application/apiFood.ts | Wraps API food import logic into a DI-friendly service factory. |
| src/modules/diet/food/application/usecases/foodCrud.ts | Uses the API import service from food CRUD instead of standalone functions. |
| src/modules/diet/day-diet/infrastructure/supabase/realtime.ts | Converts day diet realtime initializer into a service factory. |
| src/modules/diet/day-diet/infrastructure/dayDietRepository.ts | Removes exported DayRepository type re-export from infrastructure layer. |
| src/modules/diet/day-diet/application/usecases/dayUseCases.ts | Switches DayRepository type import to domain and uses new realtime service. |
| src/di/container.tsx | Updates DI wiring to pass Sentry service into telemetry and adjusts bootstrap telemetry. |
| function macrosToDomain(macrosDTO: Json) { | ||
| if ( | ||
| macrosDTO === null || | ||
| !(typeof macrosDTO === 'object') || | ||
| !('carbs' in macrosDTO) || | ||
| !('protein' in macrosDTO) || | ||
| !('fat' in macrosDTO) || | ||
| typeof macrosDTO.carbs !== 'number' || | ||
| typeof macrosDTO.protein !== 'number' || | ||
| typeof macrosDTO.fat !== 'number' | ||
| ) { | ||
| throw new Error( | ||
| 'macrosDTO is missing macros field: ' + JSON.stringify(macrosDTO), | ||
| ) | ||
| } | ||
|
|
||
| return createMacroNutrients({ | ||
| carbsInMg: macrosDTO.carbs * 1000, | ||
| proteinInMg: macrosDTO.protein * 1000, | ||
| fatInMg: macrosDTO.fat * 1000, | ||
| }) | ||
| } | ||
|
|
||
| function macrosToDTO(macroNutrients: MacroNutrients): Json { | ||
| return { | ||
| carbs: macroNutrients.carbsInMg / 1000, | ||
| protein: macroNutrients.proteinInMg / 1000, | ||
| fat: macroNutrients.fatInMg / 1000, | ||
| } | ||
| } | ||
|
|
||
| function itemToDomain(itemDTO: Json): Item { | ||
| if ( | ||
| itemDTO === null || | ||
| !(typeof itemDTO === 'object') || | ||
| !('id' in itemDTO) || |
There was a problem hiding this comment.
There is substantial duplicated DTO <-> domain mapping logic here (e.g., macrosToDomain/macrosToDTO, itemToDomain/itemToDTO) that now appears in multiple Supabase gateways/repositories. This increases the chance of subtle drift/bugs if one copy is updated and others aren’t. Consider extracting these into a shared helper that still preserves encapsulation (e.g., a createSupabaseItemMapper() / createSupabaseMacroMapper() factory module that is the only export).
This pull request refactors several services and use-case factories to use explicit service objects instead of standalone functions, improving dependency injection, testability, and code modularity. The changes affect the telemetry, Sentry integration, food import, day diet, and macro profile modules, introducing service factories and updating usage throughout the application.
Dependency Injection and Service Refactoring
initializeSentry,initializeDayDietRealtime, andinitializeMacroProfileRealtime) with service factories (createSentryService,createDayDietRealtimeService,createMacroProfileRealtimeService) that return objects encapsulating the relevant methods. This enables easier dependency injection and testing. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]Telemetry and Sentry Integration
sentryServiceobject instead of aninitializeSentryfunction, and updates all usages accordingly. The Sentry integration itself is now encapsulated in a service object, allowing for better configuration and testing. [1] [2] [3] [4] [5] [6] [7] [8]Food Import Service Refactor
createApiFoodImportServicefactory, which returns an object with the import methods. This allows for injecting dependencies like repositories, cached search, and error handlers, and updates all consumers to use the new service object. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]Realtime Subscription Service Refactor
createDayDietRealtimeService,createMacroProfileRealtimeService) instead of direct initialization functions. This allows for more flexible and testable realtime handling. [1] [2] [3] [4] [5] [6] [7] [8] [9]Type and Import Cleanups
These changes collectively improve the modularity, testability, and maintainability of the codebase by adopting a more consistent and explicit dependency injection pattern.
Fixes #1403
sub-issues:
Fixes #1412
Fixes #1408
Fixes #1407
Fixes #1405
Fixes #1414