Skip to main content
    Studio workPharmaceutical sales

    Rep-scoped referral analytics with isolation enforced three layers deep

    Power BI exports loaded on a schedule into a PHI-isolated schema, with each sales rep seeing exactly their own referrals through authentication, repository, and Postgres row-level security.

    Client
    Specialty pharmacy sales organization
    Stack
    TypeScriptpnpm monorepoReactExpressDrizzleNeon PostgresPostgres RLSPower BI exports
    Abstract editorial illustration of referral data partitioned into separate lanes

    Situation

    Where things stood

    A specialty pharmacy sales team needed each representative to see referral activity for their own accounts, and leadership to see everything, from data that arrived as Power BI workbook exports.

    The data contained protected health information. A rep seeing another rep's referrals was a compliance problem before it was a business one.

    Constraints

    What the build had to respect

    • PHI had to be isolated from everything else in the database
    • Isolation had to hold even if application code had a bug
    • Reps and territories did not map one to one; people covered for each other and territories changed
    • The source remained the existing Power BI export loop

    What we built

    The system, piece by piece

    1. 01

      PHI-isolated schema

      Referral data lives in its own Postgres schema with its own roles, separate from the rest of the platform, so access can be reasoned about and audited on its own.

    2. 02

      Three layers of isolation

      Authentication establishes who the user is. The repository layer scopes every query to that user's reps. Postgres row-level security enforces the same rule inside the database, so even a mistaken query cannot return another rep's rows.

    3. 03

      Many-to-many user and rep mapping

      A user can be mapped to several reps and a rep to several users, which matches how coverage actually works. The mapping is versioned and the RLS policies read from it.

    4. 04

      Scheduled workbook import

      The existing Power BI exports are picked up, validated, and loaded on a schedule with each import recorded, so every number on screen traces to a file and a load time.

    Shipped

    What went live

    • Production analytics application on a pnpm TypeScript monorepo
    • PHI-isolated schema inside the organization's managed Postgres
    • Row-level security policies verified with automated tests
    • Rep, manager, and leadership views on shared metric definitions
    • Security hardening sweep with critical findings fixed before go-live

    What changed

    For the people running the business

    • Reps work from current referral data without waiting for a manually filtered report
    • Leadership can state exactly who can see which records, and prove it in the database
    • Territory coverage changes are a mapping update instead of a code change

    Referral volumes and revenue figures are confidential to the client.