Dedupe stay rows by external_id (last-wins) before setEpisodeHistory. Degenerate EMR data can
emit duplicate external_ids (Kipu: two program-history entries with the same program+start_date;
Sunwave: two LOC-list entries with the same level_of_care_id+start_on); passing those to the
repo's ON CONFLICT (integration_id, external_id) DO UPDATE throws "ON CONFLICT DO UPDATE command
cannot affect row a second time." Keeping the last occurrence matches the upsert's last-write-wins.
Dedupe stay rows by
external_id(last-wins) beforesetEpisodeHistory. Degenerate EMR data can emit duplicateexternal_ids (Kipu: two program-history entries with the same program+start_date; Sunwave: two LOC-list entries with the same level_of_care_id+start_on); passing those to the repo'sON CONFLICT (integration_id, external_id) DO UPDATEthrows "ON CONFLICT DO UPDATE command cannot affect row a second time." Keeping the last occurrence matches the upsert's last-write-wins.