Skip to content

Wiring organizations entity with Backend Database, with a TSV fallback - #785

Draft
Shikhar16078 wants to merge 1 commit into
migration/backendfrom
migration/backend-organization
Draft

Wiring organizations entity with Backend Database, with a TSV fallback#785
Shikhar16078 wants to merge 1 commit into
migration/backendfrom
migration/backend-organization

Conversation

@Shikhar16078

Copy link
Copy Markdown
Contributor

Summary: Wires the Organization entity onto the PostgREST API introduced by the backend migration, following the same pattern established for Territory: try the API first when VITE_API_URL is configured, fall back to organizations.tsv on any failure. This is one entity's slice of the wider migration.

Changes

  • User experience
    • None visible. Same organization data renders regardless of source; only where the data comes from changes.
  • Logical changes
    • loadOrganizations() now tries loadOrganizationsFromApi() first when the API is enabled, and falls back to the TSV file (with a console warning) if that fails.
    • New loadOrganizationsFromApi.ts maps the organization + embedded entity rows from PostgREST onto OrganizationData, matching parseOrganizationLine's exact behavior — including its quirks (hqID stays '' rather than undefined when absent; names uses a truthy filter, dropping empty strings as well as null/undefined).
    • Organizations turned out simpler to move than territories: no derived/rolled-up values to withhold, IDs already arrive in the exact string shape the frontend expects (org.-prefixed ids, bare territory codes), and there are no supplemental TSV files to skip.
  • Data
    • No data files changed. Reads the same organization/entity tables the ETL already populates from organizations.tsv.
  • Refactors
    • Extracted a shared orUndefined() helper into apiConfig.ts (Territory's loader keeps its own private copy, left untouched — out of scope for this PR).

Out of scope/Future work: Territory still has no fallback to TSV on this branch if its API call fails — that work is on a separate, unmerged branch. This PR does not touch Territory. Two known, pre-existing issues are also explicitly not addressed here: a React StrictMode double-invoke race in DataProvider.tsx, and an API response of [] being read as a successful (if empty) load. Both belong in their own PR.

Test Plan

How to test the changes in this PR: Start the local backend (brew services start postgresql@18, then postgrest backend/tools/postgrest.conf), set VITE_API_URL=http://localhost:3000 in .env, run npm run dev, and load any Organization page/detail view. Stop PostgREST and reload — organizations should still populate via the TSV fallback, with Organization API load failed; falling back to TSV files. in the console and no "Error loading data" alert.

Checklist

Summary

  • Clear description of what and why
  • Scope kept focused; note follow-ups if any
  • Set yourself as assignee

Testing

  • npm run lint
  • npm run build — passes for this PR's own files; a pre-existing type error in a teammate's loadLanguagesFromApi.test.ts currently blocks the full tsc -b on migration/backend itself, unrelated to this change
  • npm run test — 434 passed, 0 failed, 11 skipped (gated live-backend parity tests)
    • Tests added or updated for changed logic (loadOrganizationsFromApi.test.ts, loadOrganizationsParity.test.ts, loadOrganizations.test.ts)
  • npm run dev -- tried out the website directly
    • Write comments on manual testing: confirmed both the API-success path and the API-down TSV fallback path manually, including the console warning firing correctly on failure.

Changes

Visual changes

  • Purely new views can just include the "after" screenshot. (No new views — data source only.)
  • Since more views can be reproduced by just sharing the URL -- add links to the relevant page and/or conditions to reproduce the view.

Data changes

  • TSV, SVG, etc. edits in public/data/
  • Corresponding readmes updated in public/data/
  • Load/connect/compute updates in src/features/data/ including how we aggregate data or compute derived values

Internal changes

  • Logical changes
  • Refactors, moving files around
  • If you notice any changes that require explanations, make sure to include the explanations in the code as well.

Docs

  • Code is self-documenting, or if not, comments are added where needed.
  • Updated markdown readme files documenting how the code behaves or how to develop in case there are any relevant changes to make (docs/api-data-source.md).

Wires the Organization entity onto the same API/TSV pattern territories
use: with VITE_API_URL set, try the API first; on any failure, fall
back to organizations.tsv instead of leaving the app stuck. Unlike
territories, organizations have no supplemental TSV files to skip and
no derived values to withhold, so the fallback needs no external
tracker - it's entirely self-contained in loadOrganizations().

loadOrganizationsFromApi.ts maps the API response onto OrganizationData,
matching the TSV parser's exact behavior field-for-field (including two
of its quirks: an empty Headquarters column stays '' rather than
becoming undefined, and an empty endonym is dropped from `names` the
same way a falsy check would). Verified with a live parity test against
all 57 organizations in the database, plus unit tests for the mapping
and the fallback/success paths.
@Shikhar16078
Shikhar16078 force-pushed the migration/backend-organization branch from 0b31c9e to e8af43f Compare September 8, 2026 19:27
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Cloudflare Pages preview

@Shikhar16078 Shikhar16078 changed the title Wiring organizations entity with Backend, with a TSV fallback Wiring organizations entity with Backend Database, with a TSV fallback Sep 8, 2026
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.

1 participant