Repository: lifeomic/phc-sdk-py

Issue:
Merge patient_ids with "must" query
Currently, `patient_ids` cannot be auto-merged with this type of custom query:

```python
phc.Procedure.get_date_frame(patient_ids=["a", "b"], query_overrides={
    "where": {
        "type": "elasticsearch",
        "query": {
            "bool": {
                "must": [
                    {"term": {"code.coding.code.keyword": "blah"}},
                    {"term": {"code.coding.system.keyword": "http://loinc.org"}}
                ]
            }
        }
    }
})
```

Discussion:

Patch:
diff --git a/phc/easy/query/fhir_dsl_query.py b/phc/easy/query/fhir_dsl_query.py
index c1ecc3b..2d8a5b2 100644
--- a/phc/easy/query/fhir_dsl_query.py
+++ b/phc/easy/query/fhir_dsl_query.py
@@ -8,6 +8,7 @@ FHIR_WHERE = lens.Get("where", {})
 FHIR_WHERE_TYPE = FHIR_WHERE.Get("type", "")
 FHIR_SIMPLE_QUERY = FHIR_WHERE.Get("query", {})
 FHIR_BOOL_QUERY = FHIR_SIMPLE_QUERY.Get("bool", {})
+FHIR_BOOL_MUST_QUERY = FHIR_BOOL_QUERY.Get("must", [])
 
 
 @curry
@@ -45,6 +46,11 @@ def and_query_clause(query: dict, query_clause: dict):
             query
         )
 
+    if len(bool_keys) == 1 and "must" in bool_keys:
+        return FHIR_BOOL_MUST_QUERY.modify(lambda must: [*must, query_clause])(
+            query
+        )
+
     raise ValueError("Could not add clause to query", query_clause, query)
 
 


Test patch:
diff --git a/tests/test_fhir_dsl_query.py b/tests/test_fhir_dsl_query.py
index 0fc3ecf..c71b9cf 100644
--- a/tests/test_fhir_dsl_query.py
+++ b/tests/test_fhir_dsl_query.py
@@ -65,6 +65,35 @@ def test_add_patient_id_with_query_term():
     }
 
 
+def test_add_patient_id_with_bool_must_query():
+    result = build_query(
+        {
+            "where": {
+                "type": "elasticsearch",
+                "query": {
+                    "bool": {"must": [{"term": {"gender.keyword": "male"}}]}
+                },
+            }
+        },
+        patient_ids=["a"],
+        patient_key="id",
+    )
+
+    assert result == {
+        "where": {
+            "type": "elasticsearch",
+            "query": {
+                "bool": {
+                    "must": [
+                        {"term": {"gender.keyword": "male"}},
+                        {"terms": {"id.keyword": ["Patient/a", "a"]}},
+                    ],
+                }
+            },
+        }
+    }
+
+
 def test_add_patient_id_with_bool_should_query():
     result = build_query(
         {