# ADR-0102: NGO Multi-Tenant Isolation, Adopter Consent, and Transparency Accounting

The repository's architectural decision, rationale and consequences.

# ADR-0102: NGO Multi-Tenant Isolation, Adopter Consent, and Transparency Accounting

## Status [#status]

Accepted (2026-12-02)

## Context [#context]

Fofo includes both a consumer Shelter Help hub (verified shelters, adoption showcase, weekly needs, external donations, Pro subscription share, public Transparency) and a free multi-tenant NGO web platform (verification, team roles, animals, adoption requests, visits, adopters, finance, needs, timeline, campaigns, supporters).

Strict legal and privacy rules apply (LGPD, GDPR/DSGVO, Apple App Review 3.2.2(iv), advertising disclosure rules):

- NGOs must be strictly isolated per Organization via PostgreSQL RLS.
- Adopter phone/email contact details require explicit per-field dated consent, are visible only to `admin` and `adoption` NGO roles, are never exported, are access-logged on every view, and disappear immediately upon withdrawal.
- Direct donations never pass through Fofo; NGO finance records are explicitly labeled "registered by the NGO".
- Monthly subscription-share Transparency totals must reconcile exactly to `SHARE_PCT` of net Pro revenue per country and month, and unverified NGOs must never receive payouts or publish listings.

## Decision [#decision]

1. **Tenant scoping.** Every NGO operational table (`shelter_org`, `animal_listing`, `adoption_request`, `adopter_consent`, `adoption_visit`, `shelter_ledger_entry`, `shelter_need`, `shelter_campaign`, `supporter`, `supporter_consent`, `shelter_activity_post`, `shelter_alert`) carries `organization_id` with PostgreSQL RLS policies (`app_tenant`). Public read endpoints (verified shelter directory, published animal showcase, public transparency, public supporter gallery) query strictly filtered verified/published projections through audited `asSystem` reasons registered in `SystemReason`.
2. **Role-gated contact projection.** NGO team members hold one of four roles (`admin`, `adoption`, `finance`, `volunteer`). Server procedures strip adopter email/phone before serialization unless: (a) caller holds `admin` or `adoption`, and (b) an active, non-withdrawn `adopter_consent` row permits that exact field. Every contact read writes an audit entry. CSV/PDF exports never select contact columns.
3. **Immutable consent and ledger history.** Consent withdrawals insert a timestamped revocation record (`withdrawn_at`) rather than deleting history; financial corrections append new ledger entries.
4. **Transparency sum invariant.** Monthly payout distribution uses integer minor units (cents) with deterministic remainder allocation so `sum(payout.amount_cents) === Math.round(net_revenue_cents * SHARE_PCT / 100)` per `(month, country)`, verified by automated unit and integration tests.

## Consequences [#consequences]

- Cross-tenant leaks and unauthorized adopter-contact exposure are prevented at both the database (RLS) and service (role + consent check) layers.
- Public Transparency math is auditable and regression-tested on every test run.
