@@ -1569,7 +1569,7 @@ async def api_form_read_all_needing_action(
1569
1569
1570
1570
# Read all forms that reference the given form_name and document_id
1571
1571
@app .get ("/api/form/get_linked_refs/{form_name}/{document_id}" , dependencies = [Depends (api_key_auth )])
1572
- async def api_form_read_all_needing_action (
1572
+ async def api_form_get_linked_references (
1573
1573
form_name : str ,
1574
1574
document_id : str ,
1575
1575
background_tasks : BackgroundTasks ,
@@ -1658,8 +1658,22 @@ async def api_form_read_all_needing_action(
1658
1658
)
1659
1659
1660
1660
documents .extend (_documents )
1661
-
1662
- # Placeholder: drop duplicates and sort!
1661
+
1662
+ # Drop duplicates and sort!
1663
+ unique_documents = {}
1664
+ for doc in documents :
1665
+ doc_id = doc ['__metadata__document_id' ]
1666
+
1667
+ # Replace the document if this one is newer
1668
+ if doc_id not in unique_documents :
1669
+ unique_documents [doc_id ] = doc
1670
+
1671
+ # Now we have a dictionary of unique documents; we need to sort them by 'last_modified'
1672
+ sorted_documents = sorted (
1673
+ unique_documents .values (),
1674
+ key = lambda x : datetime .fromisoformat (x ['__metadata__last_modified' ].replace ('Z' , '+00:00' )),
1675
+ reverse = True
1676
+ )
1663
1677
1664
1678
# Write this query to the TransactionLog
1665
1679
if config .COLLECT_USAGE_STATISTICS :
@@ -1675,7 +1689,7 @@ async def api_form_read_all_needing_action(
1675
1689
query_params = {},
1676
1690
)
1677
1691
1678
- return { "documents" : documents }
1692
+ return sorted_documents
1679
1693
1680
1694
1681
1695
0 commit comments