Infrastructure low complexity Shared Component mobile
0
Dependencies
0
Dependents
2
Entities
1
Integrations

Description

Utility that constructs Supabase query filters aligned with the Row Level Security policies for the contacts and peer_mentors tables. Translates the application-level UserRole enum into the correct Supabase filter conditions, ensuring queries respect the RLS boundary and do not over-fetch data.

Feature: Contact List Management

contact-rls-query-builder

Summaries

The Contact RLS Query Builder ensures that every user in the system sees only the data they are authorized to access, directly enforcing organizational privacy and compliance requirements without relying solely on server-side controls. By translating user roles into precise database filters, this utility eliminates the risk of data leakage between organizations or roles — a critical safeguard for nonprofit organizations managing sensitive beneficiary and volunteer information. The business value is twofold: it reduces liability exposure from unauthorized data access, and it enables the product to scale confidently across multiple organizations without expensive custom security audits for each new client. Organizations such as NHF and Blindeforbundet can trust that their data remains siloed even within a shared platform, which is a direct competitive advantage when onboarding privacy-conscious clients.

This is a shared utility component with low complexity, meaning it should be deliverable in a single sprint with minimal risk. However, it is a foundational dependency for all screens that query contacts or peer mentors, so it must be completed and tested before any contact-listing or detail screens can be integrated.

The primary delivery risk is ensuring that the UserRole-to-filter mapping is exhaustively tested for every role variant, including edge cases like coordinators with multi-org access. Testing should include unit tests covering all role permutations and integration tests against a Supabase test database with RLS policies enabled. Any misalignment between the application's role definitions and the actual Supabase RLS policies will cause silent data access failures, so close coordination with the backend/database team is required during implementation and review.

The Contact RLS Query Builder is a pure utility module operating in the mobile execution context, wrapping Supabase's PostgrestFilterBuilder to produce role-scoped query chains. It exposes four primary methods: buildContactQuery and buildPeerMentorQuery accept a SupabaseClient, UserRole, and orgId and return a pre-filtered query builder; applySearchFilter adds ILIKE predicates on name and notes columns; applyOrgScope ensures org-level scoping is always applied. It has no external dependencies, making it straightforward to unit test in isolation. The key implementation concern is maintaining a strict mapping between the UserRole enum values and the Supabase filter expressions — this mapping must stay synchronized with the actual RLS policies defined in Supabase migrations.

Consider using a lookup table or switch statement for clarity and testability. Since this component is shared across features, any breaking change to its interface will propagate broadly.

Responsibilities

  • Map UserRole to appropriate Supabase query filter expressions
  • Apply organization ID scoping to all contact queries
  • Build ILIKE search predicates for name and notes columns
  • Validate query parameters before constructing filter chain

Interfaces

buildContactQuery(SupabaseClient client, UserRole role, String orgId)
buildPeerMentorQuery(SupabaseClient client, UserRole role, String orgId)
applySearchFilter(PostgrestFilterBuilder query, String searchTerm)
applyOrgScope(PostgrestFilterBuilder query, String orgId)

Related Data Entities (2)

Data entities managed by this component

Used Integrations (1)

External integrations and APIs this component relies on

API Contract

View full contract →
REST /api/v1/internal/contact-queries 2 endpoints
POST /api/v1/internal/contact-queries/contacts Build and execute an RLS-scoped contact query
POST /api/v1/internal/contact-queries/peer-mentors Build and execute an RLS-scoped peer mentor query