Airtable Repair Scripts

sync-patients-from-old.js

Uses Airtable Patients (OLD) as the source of truth for referring-pharmacy links and updates the consolidated Patients table to match.

Default behavior is dry-run:

node scripts/sync-patients-from-old.js

Apply the updates:

node scripts/sync-patients-from-old.js --apply

Optional env vars if your field names differ:

AIRTABLE_OLD_PATIENTS_TABLE_NAME=Patients (OLD)
AIRTABLE_OLD_PATIENTS_NAME_FIELD=Display name
AIRTABLE_OLD_PATIENTS_EMAIL_FIELD=Email
AIRTABLE_OLD_PATIENTS_PHONE_FIELD=Mobile phone
AIRTABLE_OLD_PATIENTS_REFERRING_PHARMACY_FIELD=Referring pharmacies

sync-patients-from-bp.js

Imports a Best Practice patient CSV into Airtable Patients. Default behavior is dry-run:

node scripts/sync-patients-from-bp.js

Apply the updates/creates:

node scripts/sync-patients-from-bp.js --apply

Use a different BP export:

node scripts/sync-patients-from-bp.js --csv "BPS Search Result 2026052750508.csv"

Optional env vars/flags if Airtable field names or category names differ:

AIRTABLE_PATIENTS_CATEGORY_FIELD=Record category
AIRTABLE_PATIENTS_BP_CATEGORY=BP patient import
AIRTABLE_PATIENTS_BP_ID_FIELD=BP patient ID

The script matches existing BP patients by BP ID, email, phone, then unique name. Rows with duplicate BP IDs or exact duplicate source patient details across multiple BP IDs are skipped and reported for manual review. New records with duplicated source names are also skipped by default; pass --allow-duplicate-name-creates only after confirming those duplicated names are genuinely separate patients.

Recommended workflow:

  1. Export the patient search result CSV from BP and place it in the project root.
  2. Confirm Airtable Patients has a single line text field named BP patient ID.
  3. Run a dry-run:
npm run patients:sync-bp -- --csv "BPS Search Result 2026052750508.csv"
  1. Review the summary counts and exception lists.
  2. Apply only after the dry-run looks right:
npm run patients:sync-bp -- --csv "BPS Search Result 2026052750508.csv" --apply
  1. Run the dry-run again. A clean applied safe set should show Updates: 0 and Creates: 0; any remaining rows should be exception buckets.

Exception handling:

The script deliberately does not import DOB, Medicare, address, or other clinical/demographic fields from BP. It only writes Name, Email, Phone, Record category, and BP patient ID.