def get_global_id()

in old_reference/er7_to_json.py [0:0]


def get_global_id(pids):
  dynamodb = boto3.resource('dynamodb')
  table = dynamodb.Table(os.environ['patient_mapping_table'])
  g_id = None
  no_global = []
  
  for pid in pids:
    response = table.query(
      KeyConditionExpression=Key('local_id').eq(pid),
      IndexName='local_index'
    )
    try:
      g_id = response['Items'][0]['uuid'] # If it is there we should only have one
    except:
      no_global.append(pid)
  
  # Create if we didn't find one
  if g_id is None: g_id = str(uuid.uuid4())
  
  # Update any local ids that weren't associated to the global
  for pid in no_global:
    response = table.put_item(
      Item={
        'uuid': g_id,
        'local_id': pid
      }
    )
  return g_id