Circle V2 API Docs
    Preparing search index...

    Function dedupeStayRowsByExternalId

    • 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.

      Parameters

      • rows: readonly (
            { start_date: string; treatment_episode_id: string } & {
                created_at?: DbDate;
                end_date?: string | null;
                external_id?: string | null;
                facility_level_of_care_id?:
                    | string & { __brand: "FacilityLevelOfCareId" }
                    | null;
                facility_program_id?: string & { __brand: "FacilityProgramId" } | null;
                id?: ProgramStayId;
                integration_id?: string | null;
                legacy_admission_id?: number & { __brand: "PatientAdmissionId" } | null;
                updated_at?: DbDate;
            }
        )[]

      Returns (
          { start_date: string; treatment_episode_id: string } & {
              created_at?: DbDate;
              end_date?: string | null;
              external_id?: string | null;
              facility_level_of_care_id?:
                  | string & { __brand: "FacilityLevelOfCareId" }
                  | null;
              facility_program_id?: string & { __brand: "FacilityProgramId" } | null;
              id?: ProgramStayId;
              integration_id?: string | null;
              legacy_admission_id?: number & { __brand: "PatientAdmissionId" } | null;
              updated_at?: DbDate;
          }
      )[]