Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@rohilsurana
Copy link
Member

@rohilsurana rohilsurana commented Oct 7, 2025

Preferences APIs Migration to Connect RPC

This PR migrates Preferences management APIs from gRPC to Connect RPC framework, following the established patterns from previous API migrations.

Migration Overview

Converting Preferences APIs from traditional gRPC handlers to buf Connect RPC handlers with proper error handling, request/response wrapping, and comprehensive test coverage.

APIs Migration Progress

✅ Completed APIs (7/X)

API Status Commit Implementation Tests
DescribePreferences ✅ Complete 0f38720 internal/api/v1beta1connect/preferences.go:155-164 3 test cases
CreateOrganizationPreferences ✅ Complete 3821b31 internal/api/v1beta1connect/preferences.go:60-86 4 test cases
ListOrganizationPreferences ✅ Complete 8bc0396 internal/api/v1beta1connect/preferences.go:91-106 4 test cases
CreateUserPreferences ✅ Complete e382f38 internal/api/v1beta1connect/preferences.go:106-132 4 test cases
ListUserPreferences ✅ Complete a998a96 internal/api/v1beta1connect/preferences.go:134-150 4 test cases
CreateCurrentUserPreferences ✅ Complete 43e4393 internal/api/v1beta1connect/preferences.go:161-194 5 test cases
ListCurrentUserPreferences ✅ Complete aad5a22 internal/api/v1beta1connect/preferences.go:196-220 5 test cases

🔄 Remaining APIs

  • Additional Preferences APIs to be identified and migrated

Technical Changes

API Implementation Updates

  • Updated function signatures to use Connect RPC types:
    • Request: *connect.Request[frontierv1beta1.RequestType]
    • Response: (*connect.Response[frontierv1beta1.ResponseType], error)
  • Changed request field access from request.GetField() to request.Msg.GetField()
  • Updated response wrapping with connect.NewResponse()
  • Converted error handling to Connect error codes

Test Migration

  • Enhanced test suite for Connect RPC patterns
  • Comprehensive test coverage with error scenarios
  • Updated mock service expectations to match Connect RPC patterns

Key Implementation Details

ListCurrentUserPreferences

  • Authentication-Aware: Uses GetLoggedInPrincipal to get current user context
  • User-Scoped Filtering: Lists preferences using preference.Filter{UserID: principal.ID}
  • Enhanced Error Handling: Improved error mapping over original gRPC implementation:
    • preference.ErrInvalidFilterInvalidArgument (filter validation)
    • Authentication errors → proper Connect error propagation
    • Other errors → InternalServerError
  • Transformation: Uses existing transformPreferenceToPB() for each preference
  • Response Structure: Returns preferences list in response wrapper
  • Test Coverage: 5 comprehensive test cases:
    1. Success scenario with single preference
    2. Empty preferences list handling (proper nil vs empty slice handling)
    3. Authentication failure handling
    4. Preference service error handling
    5. Multiple preferences listing for current user

CreateCurrentUserPreferences

  • Authentication-Aware: Uses GetLoggedInPrincipal to get current user context
  • Enhanced Error Handling: Improved error mapping over original gRPC implementation:
    • preference.ErrTraitNotFoundInvalidArgument (trait validation)
    • preference.ErrInvalidValueInvalidArgument (value validation)
    • Authentication errors → proper Connect error propagation
    • Other errors → InternalServerError
  • Batch Processing: Supports multiple preference creation for current user
  • Resource Context: Uses ResourceID: principal.ID and ResourceType: schema.UserPrincipal
  • Test Coverage: 5 comprehensive test cases:
    1. Success scenario with single preference
    2. Authentication failure handling
    3. Trait not found error handling
    4. Invalid value error handling
    5. Multiple preferences creation (batch operation)

ListUserPreferences

  • User-Scoped Filtering: Lists preferences for specific user using preference.Filter{UserID: req.Msg.GetId()}
  • Enhanced Error Handling: Improved error mapping over original gRPC implementation:
    • preference.ErrInvalidFilterInvalidArgument (filter validation)
    • Other errors → InternalServerError
  • Transformation: Uses existing transformPreferenceToPB() for each preference
  • Response Structure: Returns user preferences list in response wrapper
  • Test Coverage: 4 comprehensive test cases:
    1. Success scenario with single preference
    2. Empty preferences list handling
    3. Service error handling (internal error)
    4. Multiple preferences listing with comprehensive user data

CreateUserPreferences

  • User-Scoped Creation: Creates preferences for specific user using ResourceID: req.Msg.GetId() and ResourceType: schema.UserPrincipal
  • Enhanced Error Handling: Improved error mapping over original gRPC implementation:
    • preference.ErrTraitNotFoundInvalidArgument (trait validation)
    • Other errors → InternalServerError
  • Batch Processing: Supports multiple preference creation in single request
  • Transformation: Uses existing transformPreferenceToPB() for response formatting
  • Test Coverage: 4 comprehensive test cases:
    1. Success scenario with single preference
    2. Service error handling (internal error)
    3. Multiple preferences creation (batch operation)
    4. Empty preferences list handling

ListOrganizationPreferences

  • Organization Filtering: Lists preferences for specific organization using preference.Filter{OrgID: req.Msg.GetId()}
  • Enhanced Error Handling: Improved error mapping:
    • preference.ErrInvalidFilterInvalidArgument (filter validation)
    • Other errors → InternalServerError
  • Transformation: Uses existing transformPreferenceToPB() for each preference
  • Response Structure: Returns preferences list in response wrapper
  • Test Coverage: 4 comprehensive test cases:
    1. Success scenario with single preference
    2. Empty preferences list handling
    3. Service error handling (internal error)
    4. Multiple preferences listing

CreateOrganizationPreferences

  • Batch Creation: Creates multiple preferences for an organization in a single request
  • Resource Mapping: Uses ResourceID: req.Msg.GetId() and ResourceType: schema.OrganizationNamespace
  • Error Handling: Comprehensive error mapping:
    • preference.ErrTraitNotFoundInvalidArgument (trait validation failed)
    • Other errors → InternalServerError
  • Transformation: Uses existing transformPreferenceToPB() for each created preference
  • Test Coverage: 4 comprehensive test cases:
    1. Success scenario with single preference
    2. Trait not found error handling
    3. Internal service error handling
    4. Multiple preferences creation (batch operation)

DescribePreferences

  • Simple Trait Listing: Lists all preference traits using preferenceService.Describe(ctx)
  • No Error Handling: Since the service method doesn't return errors
  • Trait Transformation: Uses transformPreferenceTraitToPB() for each trait
  • Input Type Mapping: Maps preference input types to protobuf oneof variants:
    • TraitInputTextPreferenceTrait_Text{}
    • TraitInputSelectPreferenceTrait_Select{}
    • TraitInputCheckboxPreferenceTrait_Checkbox{}
    • And more input types (textarea, combobox, multiselect, number)
  • Test Coverage: 3 comprehensive test cases:
    1. Success scenario with full trait data
    2. Empty traits list handling
    3. Different input types transformation

Verification

Each migrated API goes through:

  • ✅ Build verification (make build)
  • ✅ Test execution (make test)
  • ✅ Lint checking (make lint)
  • ✅ Functionality validation

Current test coverage: Enhanced for v1beta1connect package (29 test cases total)

Files Modified

Implementation Files

  • internal/api/v1beta1connect/preferences.go - Added all seven Preferences Connect RPC handlers
  • internal/api/v1beta1connect/preferences_test.go - Comprehensive test suite with 29 test cases

Migration Methodology

Following established systematic approach:

  1. Analysis - Study original gRPC implementation
  2. API Migration - Convert handler to Connect RPC format
  3. Test Migration - Create comprehensive test coverage
  4. Verification - Build, test, and lint validation
  5. Integration - Commit and push changes

Enhanced Error Handling

The Connect RPC implementations provide improved error handling over the original gRPC handlers:

  • Authentication Validation: Current user operations properly validate authentication with enhanced error propagation
  • Filter Validation: List operations now validate filter parameters (ErrInvalidFilter)
  • Trait Validation: Create operations validate preference traits (ErrTraitNotFound)
  • Value Validation: Enhanced validation for preference values (ErrInvalidValue)
  • Consistent Error Codes: Proper Connect RPC error codes for different failure scenarios
  • Better User Experience: More specific error messages for different validation failures
  • Nil vs Empty Slice Handling: Proper handling of empty preference lists in responses

Next Steps

  • Continue with remaining Preferences APIs migration
  • Update authorization configurations if needed
  • Ensure comprehensive test coverage across all Preferences endpoints

@vercel
Copy link

vercel bot commented Oct 7, 2025

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

Project Deployment Preview Comments Updated (UTC)
frontier Ready Ready Preview Comment Oct 8, 2025 6:08am

@coveralls
Copy link

coveralls commented Oct 7, 2025

Pull Request Test Coverage Report for Build 18335446474

Details

  • 148 of 160 (92.5%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 36.729%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/api/v1beta1connect/preferences.go 148 160 92.5%
Totals Coverage Status
Change from base Build 18334136054: 0.3%
Covered Lines: 14176
Relevant Lines: 38596

💛 - Coveralls

@rohilsurana rohilsurana force-pushed the feature/migrate-frontier-rpc-preferences branch from 40c8677 to dab1e8a Compare October 8, 2025 06:07
@rohilsurana rohilsurana merged commit bcb7187 into main Oct 8, 2025
7 checks passed
@rohilsurana rohilsurana deleted the feature/migrate-frontier-rpc-preferences branch October 8, 2025 06:22
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.

4 participants