Repository: Health-Informatics-UoN/Carrot-Mapper

Issue:
🐛 CSV rules output needs a further change
### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current Behavior

The new format for csv output has introduced an error - the 'domain' column contains target field and domain is not present in the output records.

### Expected Behavior

The domain column should contain OMOP domain and OMOP target field should be present in a 'target_field' column which will have to be added / re-instated

### Steps To Reproduce

_No response_

### Environment

```markdown
- OS:
- Other environment details:
```


### I'm part of a Project Team

_No response_

### Anything else?

_No response_

### Are you willing to contribute to resolve this issue?

None


Discussion:
Yup - this is due to #653. In that, `destination_field` has been incorrectly used for `domain` information. It should be using `destination_table` instead. `destination_field` needs to returned under its own heading (this is the euaivalen tof `target_table` above).

This doesn't affect the rendering of the rules webpage, but is also confusing for the future there.

Resolving patch:
diff --git a/app/api/api/views.py b/app/api/api/views.py
--- a/app/api/api/views.py
+++ b/app/api/api/views.py
@@ -912,9 +912,13 @@ def list(self, request):
                 "name": rule["destination_table"].table,
             }
 
+            rule["destination_field"] = {
+                "id": int(str(rule["destination_field"])),
+                "name": rule["destination_field"].field,
+            }
+
             rule["domain"] = {
-                "id": int(str(rule["domain"])),
-                "name": rule["domain"].field,
+                "name": rule["domain"],
             }
 
             rule["source_table"] = {
diff --git a/app/api/mapping/services_rules.py b/app/api/mapping/services_rules.py
--- a/app/api/mapping/services_rules.py
+++ b/app/api/mapping/services_rules.py
@@ -567,7 +567,8 @@ def get_mapping_rules_list(structural_mapping_rules, page_number=None, page_size
                 "rule_id": rule_scan_report_concept_id,
                 "omop_term": concept_name,
                 "destination_table": destination_table,
-                "domain": destination_field,
+                "domain": scan_report_concept.concept.domain_id,
+                "destination_field": destination_field,
                 "source_table": source_table,
                 "source_field": source_field,
                 "term_mapping": term_mapping,
@@ -627,7 +628,7 @@ def get_mapping_rules_json(structural_mapping_rules):
             cdm[table_name][_id] = {}
 
         # make a new mapping spec for the destination table
-        destination_field = rule["domain"].field
+        destination_field = rule["destination_field"].field
         cdm[table_name][_id][destination_field] = {
             "source_table": rule["source_table"].name.replace("\ufeff", ""),
             "source_field": rule["source_field"].name.replace("\ufeff", ""),
@@ -710,7 +711,7 @@ def download_mapping_rules_as_csv(request, qs):
     for content in output:
         # replace the django model objects with string names
         content["destination_table"] = content["destination_table"].table
-        content["domain"] = content["domain"].field
+        content["domain"] = content["domain"]
         content["source_table"] = content["source_table"].name
         content["source_field"] = content["source_field"].name
 


Test patch: