From caacd21351e098612dbffba63b52c691bea8a2b1 Mon Sep 17 00:00:00 2001
From: Hana Snow <hsnow@broadinstitute.org>
Date: Wed, 21 Jun 2023 17:15:56 -0400
Subject: [PATCH 1/3] parse family fields from individual update

---
 seqr/views/apis/individual_api_tests.py |  4 ++--
 seqr/views/utils/individual_utils.py    | 13 +++++++++++--
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/seqr/views/apis/individual_api_tests.py b/seqr/views/apis/individual_api_tests.py
index 6765f18181..cede8d7757 100644
--- a/seqr/views/apis/individual_api_tests.py
+++ b/seqr/views/apis/individual_api_tests.py
@@ -669,8 +669,8 @@ def _send_request_data(data):
         self.assertEqual(len(response_json['familiesByGuid']), 1)
         family_guid = next(iter(response_json['familiesByGuid'].keys()))
         self.assertEqual(response_json['familiesByGuid'][family_guid]['familyId'], 'PED073')
-        self.assertEqual(response_json['familiesByGuid'][family_guid]['codedPhenotype'], None)
-        self.assertEqual(response_json['familiesByGuid'][family_guid]['mondoId'], None)
+        self.assertEqual(response_json['familiesByGuid'][family_guid]['codedPhenotype'], 'Perinatal death')
+        self.assertEqual(response_json['familiesByGuid'][family_guid]['mondoId'],  'MONDO:0100086')
         self.assertSetEqual(set(
             response_json['familiesByGuid'][family_guid]['individualGuids']),
             set(response_json['individualsByGuid'].keys())
diff --git a/seqr/views/utils/individual_utils.py b/seqr/views/utils/individual_utils.py
index 967922faea..545e2e262f 100644
--- a/seqr/views/utils/individual_utils.py
+++ b/seqr/views/utils/individual_utils.py
@@ -6,7 +6,8 @@
 from matchmaker.models import MatchmakerSubmission, MatchmakerResult
 from seqr.models import Sample, IgvSample, Individual, Family, FamilyNote
 from seqr.utils.middleware import ErrorsWarningsException
-from seqr.views.utils.json_to_orm_utils import update_individual_from_json, update_individual_parents, create_model_from_json
+from seqr.views.utils.json_to_orm_utils import update_individual_from_json, update_individual_parents, create_model_from_json, \
+    update_family_from_json
 from seqr.views.utils.orm_to_json_utils import _get_json_for_individuals, _get_json_for_families, get_json_for_family_notes
 from seqr.views.utils.pedigree_info_utils import JsonConstants
 
@@ -82,7 +83,7 @@ def add_or_update_individuals_and_families(project, individual_records, user, ge
                 updated_family_ids.add(individual.family_id)
 
     updated_family_models = Family.objects.filter(id__in=updated_family_ids)
-    _remove_pedigree_images(updated_family_models, user)
+    _remove_pedigree_images(updated_family_models.filter(id__in=updated_family_ids), user)
 
     pedigree_json = None
     if get_update_json:
@@ -143,6 +144,14 @@ def _update_from_record(record, user, families_by_id, individual_lookup, updated
         note = create_model_from_json(FamilyNote, {'note': family_notes, 'note_type': 'C', 'family': family}, user)
         updated_note_ids.append(note.id)
 
+    family_record = {
+        k: record.pop(k) for k in [JsonConstants.CODED_PHENOTYPE_COLUMN, JsonConstants.MONDO_ID_COLUMN] if k in record
+    }
+    if family_record:
+        is_updated = update_family_from_json(family, family_record, user=user)
+        if is_updated:
+            updated_family_ids.add(family.id)
+
     is_updated = update_individual_from_json(individual, record, user=user, allow_unknown_keys=True)
     if is_updated:
         updated_individuals.add(individual)

From 541e37a4931fb9759e88fd048cfb3adf0a142d65 Mon Sep 17 00:00:00 2001
From: Hana Snow <hsnow@broadinstitute.org>
Date: Wed, 21 Jun 2023 17:17:16 -0400
Subject: [PATCH 2/3] clean up

---
 seqr/views/utils/individual_utils.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/seqr/views/utils/individual_utils.py b/seqr/views/utils/individual_utils.py
index 545e2e262f..8e58f174c8 100644
--- a/seqr/views/utils/individual_utils.py
+++ b/seqr/views/utils/individual_utils.py
@@ -83,7 +83,7 @@ def add_or_update_individuals_and_families(project, individual_records, user, ge
                 updated_family_ids.add(individual.family_id)
 
     updated_family_models = Family.objects.filter(id__in=updated_family_ids)
-    _remove_pedigree_images(updated_family_models.filter(id__in=updated_family_ids), user)
+    _remove_pedigree_images(updated_family_models, user)
 
     pedigree_json = None
     if get_update_json:

From 74209c3d428271046e578ad7bfa2e8776c94494c Mon Sep 17 00:00:00 2001
From: Hana Snow <hsnow@broadinstitute.org>
Date: Wed, 21 Jun 2023 17:32:57 -0400
Subject: [PATCH 3/3] fix return value assignment

---
 seqr/views/utils/json_to_orm_utils.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/seqr/views/utils/json_to_orm_utils.py b/seqr/views/utils/json_to_orm_utils.py
index 6a7f42ece5..16a044ac34 100644
--- a/seqr/views/utils/json_to_orm_utils.py
+++ b/seqr/views/utils/json_to_orm_utils.py
@@ -21,7 +21,7 @@ def update_family_from_json(family, json, user, allow_unknown_keys=False, immuta
 
     immutable_keys = (immutable_keys or []) + ['pedigree_image', 'assigned_analyst', 'case_review_summary', 'case_review_notes', 'guid']
 
-    update_model_from_json(
+    return update_model_from_json(
         family, json, user=user, allow_unknown_keys=allow_unknown_keys, immutable_keys=immutable_keys,
     )