Skip to content

Bug/epmedu 4891 fix moderators inconsistency#294

Open
Giorgi217 wants to merge 2 commits intodevelopfrom
bug/EPMEDU-4891-fix-moderators-inconsistency
Open

Bug/epmedu 4891 fix moderators inconsistency#294
Giorgi217 wants to merge 2 commits intodevelopfrom
bug/EPMEDU-4891-fix-moderators-inconsistency

Conversation

@Giorgi217
Copy link
Copy Markdown
Collaborator

@Giorgi217 Giorgi217 commented Mar 24, 2026

Pull Request overview

Fix assigned moderators inconsistency — align with backend relations and ensure correct role-based updates


Problem

Assigned moderators were displayed inconsistently on the Feeding Point Details screen.

  • Initial state often showed incorrect or empty moderators
  • Data was derived from feeding history instead of actual backend relations
  • UI updates were delayed and not synchronized with user roles
  • Moderators visibility logic was not properly aligned with user permissions

Root cause: the implementation relied on indirect data sources (feeding history) and lacked a proper connection to backend user-group relations.


Summary

This PR introduces a dedicated Moderation Directory service and fixes moderator resolution logic to use backend relations directly.

Changes include:

  • Fetch moderators and admins from backend via AdminQueries API
  • Resolve assigned moderators using FeedingPoint user relations
  • Remove incorrect dependency on feeding history
  • Introduce role-based updates for moderators visibility
  • Add token refresh to ensure up-to-date role information
  • Improve async handling and UI update consistency

Root Cause Analysis

1) Incorrect data source (Feeding history)

The previous implementation extracted moderators from:

FeedingHistory.assignedModerators

This approach is unreliable because:

  • History may be outdated or incomplete
  • Not all assigned moderators are guaranteed to appear in history
  • Newly assigned moderators were not reflected immediately

Correct behavior requires using:

FeedingPoint.users


2) Missing backend integration for moderators

There was no dedicated mechanism to fetch:

  • All moderators
  • All administrators

This prevented proper filtering and mapping of assigned users.

Solution:

  • Introduced ModerationDirectoryService to fetch users from Cognito groups:
    • Moderator
    • Administrator

3) Role synchronization issues

User roles were not always up-to-date:

  • Cognito tokens were not refreshed when needed
  • UI could render based on stale role data

Fix:

  • Force token refresh on initialization and on signedIn event
  • Ensure latest cognito:groups claims are used

4) UI update timing and async inconsistencies

Moderators were:

  • Loaded too late
  • Not properly cancelled/reloaded during updates
  • Sometimes shown before role validation completed

Fixes:

  • Introduced cancellable moderatorsTask
  • Added updateModerators() with proper lifecycle handling
  • Ensured updates run on MainActor
  • Trigger updates only when user can moderate

Changes

File What changed
AppContext.swift Added ModerationDirectoryService to dependency container
ModerationDirectoryService.swift Implemented service to fetch moderators/admins from backend
ModerationDirectoryResponse.swift Added models for AdminQueries API response
UserValidationModel.swift Added forced token refresh for role synchronization
FeedingPointDetailsModel.swift Rewrote assigned moderators logic using FeedingPoint relations
FeedingPointDetailsModel.swift Added updateModerators() and async task handling

🧠 How it works (High level)

App fetches moderators/admins from Cognito via AdminQueries API
FeedingPoint relations (users) provide assigned user IDs
Service filters only assigned moderators/admins
Names are mapped and passed to UI

UI updates only when:

  • User has permission
  • Data is fully resolved

Token refresh ensures roles are always up-to-date


How to test

Assigned moderators display

  1. Open Feeding Point Details screen
  2. Verify assigned moderators are displayed correctly
  3. Ensure no delay or incorrect initial state

Role-based visibility

  1. Login as:

    • Moderator / Admin → moderators should be visible
    • Volunteer → moderators should NOT be visible
  2. Verify role updates across sessions:

  • Sign out and sign in with a different role
  • Ensure moderators visibility updates correctly

Dynamic updates

  1. Assign/remove moderator from backend
  2. Trigger update (refresh or subscription)
  3. Verify UI updates correctly

Session refresh

  1. Sign in with different role
  2. Ensure moderators visibility updates correctly without app restart

Simulator Screenshot - iPhone 17 Pro - 2026-03-24 at 06 23 21 Simulator Screenshot - iPhone 17 Pro - 2026-03-24 at 06 24 13 Simulator Screenshot - iPhone 17 Pro - 2026-03-24 at 06 27 02

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.

2 participants