diff --git a/app/lib_redis_helpers.py b/app/lib_redis_helpers.py index 6d05ce8..8038890 100644 --- a/app/lib_redis_helpers.py +++ b/app/lib_redis_helpers.py @@ -171,24 +171,37 @@ def lookup_id_random_pop( ] for prefix in id_prefixes: - key = f'{prefix}_id_random' - if key in obj_data: - # Table name mapping - table = prefix - if prefix == 'address_location': table = 'address' - elif prefix in ['contact_1', 'contact_2']: table = 'contact' - elif prefix == 'event_id_random_only': table = 'event' - elif prefix == 'poc_event_person': table = 'event_person' - elif prefix == 'poc_person': table = 'person' + key_random = f'{prefix}_id_random' + key_id = f'{prefix}_id' + + # Table name mapping + table = prefix + if prefix == 'address_location': table = 'address' + elif prefix in ['contact_1', 'contact_2']: table = 'contact' + elif prefix == 'event_id_random_only': table = 'event' + elif prefix == 'poc_event_person': table = 'event_person' + elif prefix == 'poc_person': table = 'person' + + resolved_id = None + + # Scenario A: Legacy suffix (e.g., account_id_random: "abc") + if key_random in obj_data: + resolved_id = redis_lookup_id_random(record_id_random=obj_data[key_random], table_name=table) + obj_data.pop(key_random) - resolved_id = redis_lookup_id_random(record_id_random=obj_data[key], table_name=table) - obj_data[f'{prefix if not prefix.endswith("_id_random_only") else prefix[:-15]+"_id_only"}'] = resolved_id - # Special case for event_id_random_only - target_id_key = f'{prefix[:-7]}' if prefix.endswith('_random') else f'{prefix}_id' + # Scenario B: Vision naming (e.g., account_id: "abc") + # Only resolve if it's a string of the correct length (random ID format) + elif key_id in obj_data and isinstance(obj_data[key_id], str) and 11 <= len(obj_data[key_id]) <= 22: + resolved_id = redis_lookup_id_random(record_id_random=obj_data[key_id], table_name=table) + + if resolved_id is not None: + # Set the target ID field + target_id_key = key_id if prefix == 'event_id_random_only': target_id_key = 'event_id_only' - obj_data[target_id_key] = resolved_id - obj_data.pop(key) + + # Also set the short prefix version (e.g., obj_data['account'] = 1) for compatibility + obj_data[f'{prefix if not prefix.endswith("_id_random_only") else prefix[:-15]+"_id_only"}'] = resolved_id # Polymorphic links polymorphic = [ @@ -200,12 +213,19 @@ def lookup_id_random_pop( ] for type_key, rand_key, id_key in polymorphic: + # Handle random key if present if type_key in obj_data and rand_key in obj_data: obj_data[id_key] = redis_lookup_id_random( record_id_random=obj_data.get(rand_key), table_name=obj_data.get(type_key) ) obj_data.pop(rand_key) + # Handle Vision naming (id_key contains the string) + elif type_key in obj_data and id_key in obj_data and isinstance(obj_data[id_key], str) and 11 <= len(obj_data[id_key]) <= 22: + obj_data[id_key] = redis_lookup_id_random( + record_id_random=obj_data.get(id_key), + table_name=obj_data.get(type_key) + ) return obj_data diff --git a/app/models/hosted_file_models.py b/app/models/hosted_file_models.py index 565cd4e..3677ae2 100644 --- a/app/models/hosted_file_models.py +++ b/app/models/hosted_file_models.py @@ -15,9 +15,9 @@ class Hosted_File_Base(BaseModel): log.debug(locals()) # --- Standardized Vision IDs (Strings) --- - id: Optional[str] = Field(None, **base_fields['hosted_file_id_random']) - hosted_file_id: Optional[str] = Field(None, **base_fields['hosted_file_id_random']) - account_id: Optional[str] = Field(None, **base_fields['account_id_random']) + id: Optional[Union[int, str]] = Field(None, **base_fields['hosted_file_id_random']) + hosted_file_id: Optional[Union[int, str]] = Field(None, **base_fields['hosted_file_id_random']) + account_id: Optional[Union[int, str]] = Field(None, **base_fields['account_id_random']) hash_sha256: Optional[str] title: Optional[str] @@ -69,17 +69,17 @@ class Hosted_File_Base(BaseModel): """ # 1. Map Random Strings to Clean Names if rid := values.get('id_random') or values.get('hosted_file_id_random'): - values['id'] = rid - values['hosted_file_id'] = rid + # Only set if not already a valid integer + if not isinstance(values.get('id'), int): + values['id'] = rid + if not isinstance(values.get('hosted_file_id'), int): + values['hosted_file_id'] = rid if a_rid := values.get('account_id_random'): - values['account_id'] = a_rid + # Only set if not already a valid integer + if not isinstance(values.get('account_id'), int): + values['account_id'] = a_rid - # 2. Prevent "Collision Population" - for k in ['id', 'account_id']: - if k in values and not isinstance(values[k], str): - del values[k] - return values class Config: diff --git a/app/routers/hosted_file.py b/app/routers/hosted_file.py index c65cee2..a2b3b8b 100644 --- a/app/routers/hosted_file.py +++ b/app/routers/hosted_file.py @@ -512,8 +512,8 @@ async def upload_files( # Going to try and create a new host_file entry... log.warning('For some reason a host_file object entry with the has was not found.') # file_info['id_random'] = None - file_info['account_id'] = account_id - file_info['account_id_random'] = account_id_random + file_info['account_id'] = account_id # This is the integer ID + file_info['account_id_random'] = account_id_random # This is the string ID hosted_file_obj = Hosted_File_Base(**file_info) if hosted_file_obj_result := create_hosted_file_obj(hosted_file_obj_new=hosted_file_obj): hosted_file_id = hosted_file_obj_result