7 Commits

Author SHA1 Message Date
30df915433 Add comprehensive API error handling for sensor attachment
Improved error handling in the attachDeviceToBeneficiary method with:

- Structured ApiResponse return type with detailed error codes
- User-friendly error messages for different failure scenarios:
  - DEPLOYMENT_NOT_FOUND: Beneficiary has no deployment configured
  - UNAUTHORIZED: Missing or expired authentication credentials
  - NOT_FOUND: Sensor or deployment not found (404)
  - SERVER_ERROR: Legacy API server error (500)
  - SERVICE_UNAVAILABLE: Service temporarily unavailable (503+)
  - LEGACY_API_ERROR: Error from Legacy API response body
  - NETWORK_ERROR: Network connectivity issues
  - EXCEPTION: Unexpected errors

- Enhanced error messages in setup-wifi.tsx to display API error details
- Fixed navigator.onLine check to work in test environment
- Added comprehensive test suite with 11 test cases covering all error scenarios

All tests passing (11/11).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-31 16:08:08 -08:00
0cc82b24b0 Add deployment_id lookup mechanism
Implement getDeploymentForBeneficiary() method to resolve beneficiary → deployment_id mapping. This extracts the deployment lookup logic into a reusable, testable method.

Changes:
- Add getDeploymentForBeneficiary() in services/api.ts
  - Fetches beneficiary from WellNuo API
  - Returns deployment_id with proper error handling
  - Returns ApiResponse<number> with typed errors
- Refactor attachDeviceToBeneficiary() to use new method
  - Reduces code duplication
  - Improves separation of concerns
- Add comprehensive test suite
  - Tests successful deployment lookup
  - Tests authentication errors
  - Tests missing deployment scenarios
  - Tests network error handling
  - Tests edge cases (string IDs, 0 values, empty strings)

Benefits:
- Deployment lookup now reusable across codebase
- Better error handling with specific error codes
- Easier to test and maintain
- Follows DRY principle

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-31 16:00:11 -08:00
70f9a91be1 Remove console.log statements from codebase
Removed all console.log, console.error, console.warn, console.info, and console.debug statements from the main source code to clean up production output.

Changes:
- Removed 400+ console statements from TypeScript/TSX files
- Cleaned BLE services (BLEManager.ts, MockBLEManager.ts)
- Cleaned API services, contexts, hooks, and components
- Cleaned WiFi setup and sensor management screens
- Preserved console statements in test files (*.test.ts, __tests__/)
- TypeScript compilation verified successfully

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-29 12:44:16 -08:00
f6ba2a906a Fix race conditions when quickly switching beneficiaries
Implemented request tracking and cancellation to prevent stale API
responses from overwriting current beneficiary data.

Changes:
- Added loadingBeneficiaryIdRef to track which beneficiary is being loaded
- Added AbortController to cancel in-flight requests
- Validate beneficiary ID before applying state updates
- Cleanup on component unmount to prevent memory leaks

This fixes the issue where rapidly switching between beneficiaries
would show wrong data if slower requests completed after faster ones.

🤖 Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-29 12:33:57 -08:00
69c999729f Fix BLE connections not disconnecting on logout
Implemented proper BLE cleanup mechanism on user logout:

**Root Cause:**
- BLE cleanup callback was being set but reference could become stale
- No explicit cleanup call in profile logout handler
- Callback stability issues due to re-renders

**Changes:**
1. app/_layout.tsx:
   - Use useRef pattern to maintain stable callback reference
   - Set callback once with ref that always points to current cleanupBLE
   - Cleanup callback on unmount to prevent memory leaks

2. app/(tabs)/profile/index.tsx:
   - Add explicit cleanupBLE() call in logout handler
   - Import useBLE hook to access cleanup function
   - Ensure cleanup happens before logout completes

3. services/api.ts:
   - Update setOnLogoutBLECleanupCallback signature to accept null
   - Allows proper cleanup of callback on unmount

4. jest.setup.js:
   - Add AsyncStorage mock to prevent test failures

5. Tests:
   - Add comprehensive BLE cleanup tests
   - Test callback pattern and stability
   - Test logout flow with BLE cleanup
   - Test error handling during cleanup

**Result:**
BLE connections now properly disconnect when user logs out,
preventing stale connections and potential resource leaks.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-29 12:19:46 -08:00
521ff52344 Add comprehensive testing and documentation for role-based UI permissions
This commit implements role-based permission testing and documentation for
the beneficiary management system.

The role-based UI was already correctly implemented in BeneficiaryMenu.tsx
(lines 21-25). This commit adds:

- Comprehensive test suite for BeneficiaryMenu role permissions
- Test suite for role-based edit modal functionality
- Detailed documentation in docs/ROLE_BASED_PERMISSIONS.md
- Jest configuration for future testing
- testID added to menu button for testing accessibility

Role Permission Summary:
- Custodian: Full access (all features including remove)
- Guardian: Most features (cannot remove beneficiary)
- Caretaker: Limited access (dashboard, edit nickname, sensors only)

Edit Functionality:
- Custodians can edit full profile (name, address, avatar)
- Guardians/Caretakers can only edit personal nickname (customName)
- Backend validates all permissions server-side for security

Tests verify:
 Menu items filtered correctly by role
 Custodian has full edit capabilities
 Guardian/Caretaker limited to nickname editing only
 Default role is caretaker (security-first approach)
 Navigation routes work correctly

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-29 11:39:18 -08:00
88fc9042a7 Add retry button to error states in equipment and subscription screens
- Added error state with retry functionality to equipment.tsx
  - Display error message when sensor loading fails
  - Provide "Try Again" button to retry loading
  - Clear error on successful retry

- Added error state with retry functionality to subscription.tsx
  - Display error message when beneficiary loading fails
  - Provide "Try Again" button with icon to retry loading
  - Show offline icon and proper error layout

- Added comprehensive tests for error handling
  - ErrorMessage component tests for inline errors
  - FullScreenError component tests
  - Equipment screen error state tests
  - Subscription screen error state tests

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-29 11:27:19 -08:00