11import json
2+ import os
23from datetime import datetime , timedelta , timezone
34from typing import Any
45
56import boto3
67import fire
78
9+ INCLUDE_PATIENT_IDS = os .environ .get ("INCLUDE_PATIENT_IDS" , "false" ).lower () == "true"
10+
811dynamodb = boto3 .client ("dynamodb" )
912paginator = dynamodb .get_paginator ("scan" )
1013
@@ -23,6 +26,12 @@ def _get_masterids_for_custodians(table_name: str, custodians: str | tuple[str])
2326 f"Getting masterids for custodians { custodian_list } in table { table_name } ...."
2427 )
2528
29+ required_attributes = (
30+ ["id" , "type_id" , "master_identifier" , "custodian" ]
31+ if not INCLUDE_PATIENT_IDS
32+ else ["id" , "type_id" , "master_identifier" , "custodian" , "nhs_number" ]
33+ )
34+
2635 expression_names_str = "," .join (
2736 [f":param{ custodian } " for custodian in custodian_list ]
2837 )
@@ -35,7 +44,7 @@ def _get_masterids_for_custodians(table_name: str, custodians: str | tuple[str])
3544 "PaginationConfig" : {"PageSize" : 50 },
3645 "FilterExpression" : f"custodian IN ({ expression_names_str } )" ,
3746 "ExpressionAttributeValues" : expression_values_list ,
38- "ProjectionExpression" : "id, type_id, master_identifier" ,
47+ "ProjectionExpression" : "," . join ( required_attributes ) ,
3948 }
4049
4150 pointers_info : list [dict [str , str ]] = []
@@ -48,12 +57,15 @@ def _get_masterids_for_custodians(table_name: str, custodians: str | tuple[str])
4857 pointer_id = item .get ("id" , {}).get ("S" , "no-id" )
4958 pointer_type = item .get ("type_id" , {}).get ("S" , "no-type" )
5059 master_id = item .get ("master_identifier" , {}).get ("S" , "no-master-id" )
60+ custodian = item .get ("custodian" , {}).get ("S" , "no-custodian" )
5161
5262 pointers_info .append (
5363 {
5464 "nrl-id" : pointer_id ,
5565 "pointer-type" : pointer_type ,
5666 "master_identifier" : master_id ,
67+ "custodian" : custodian ,
68+ "patient_id" : item .get ("nhs_number" , {}).get ("S" , "no-patient-id" ),
5769 }
5870 )
5971
@@ -70,7 +82,7 @@ def _get_masterids_for_custodians(table_name: str, custodians: str | tuple[str])
7082 print (" Done" ) # noqa
7183
7284 print (f"Writing pointers to file ./pointer-masterids.txt ..." ) # noqa
73- with open (f "pointer-masterids.txt" , "w" ) as f :
85+ with open ("pointer-masterids.txt" , "w" ) as f :
7486 f .write (json .dumps (pointers_info , indent = 2 ))
7587
7688 return {
0 commit comments