Skip to content

Refactor local infrastructure services - #1471

Merged
marcuscastelo merged 5 commits into
rc/v0.16.0from
refactor/0.16-factories
Mar 24, 2026
Merged

marcuscastelo merged 5 commits into
rc/v0.16.0from
refactor/0.16-factories

Conversation

@marcuscastelo

@marcuscastelo marcuscastelo commented Mar 24, 2026 •

Copy link
Copy Markdown
Owner

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

  • Replaces direct function imports (like initializeSentry, initializeDayDietRealtime, and initializeMacroProfileRealtime) 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

  • Updates the telemetry module to accept a sentryService object instead of an initializeSentry function, 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

  • Refactors the food import logic to use a createApiFoodImportService factory, 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

  • Refactors the day diet and macro profile realtime subscription logic to use service factories (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

  • Removes redundant or relocated type exports and cleans up imports to match the new service-based architecture.

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

Copilot AI review requested due to automatic review settings March 24, 2026 20:58
@marcuscastelo marcuscastelo self-assigned this Mar 24, 2026
@vercel

vercel Bot commented Mar 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
macroflows Ready Ready Preview, Comment Mar 24, 2026 10:45pm

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/di/container.tsx Outdated
Comment thread src/modules/observability/infrastructure/sentry/sentry.ts Outdated
Comment thread src/modules/observability/infrastructure/sentry/sentry.ts
Comment thread src/di/container.tsx Outdated
@github-actions github-actions Bot added backend Backend-specific issue or change complexity-medium Medium implementation complexity epic Issue that groups related issues refactor Code refactoring required subissue labels Mar 24, 2026
@marcuscastelo
marcuscastelo requested a review from Copilot March 24, 2026 21:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Comment thread src/di/container.tsx
Comment thread src/modules/diet/day-diet/infrastructure/supabase/realtime.ts
Comment thread src/modules/diet/macro-profile/infrastructure/supabase/realtime.ts Outdated
Comment thread src/modules/weight/infrastructure/weight/supabase/realtime.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 62 out of 63 changed files in this pull request and generated 4 comments.

Comment thread src/modules/observability/infrastructure/sentry/sentry.ts
Comment thread src/modules/diet/food/infrastructure/api/application/apiFood.ts
Comment on lines +25 to +60
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) ||

Copilot AI Mar 24, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Comment thread src/modules/search/infrastructure/supabase/supabaseCachedSearchGateway.ts Outdated

This branch was successfully deployed

1 active deployment
Preview — 35a464e7 Deployed Mar 24, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend-specific issue or change complexity-medium Medium implementation complexity epic Issue that groups related issues refactor Code refactoring required subissue

Projects

None yet

2 participants