
    h.              	           d Z ddlZddlZddlmZmZmZ ddlmZ ddl	m
Z
 dede
defd	Zd
edee   dee
   dee   fdZy)zh
Common utility functions for handling object permission updates across
organizations, teams, and keys.
    N)DictOptionalUnion)verbose_proxy_logger)PrismaClient	data_dictprisma_clientreturnc                    K   |t        d      | j                  d      }|rH|j                  j                  j	                  d|i       d{   }|r	 |j                         }|| d<   | S 7 # t        $ r |j                         }Y $w xY ww)a  
    Helper method to attach object_permission to a dictionary if object_permission_id is set.
    
    This function:
    1. Checks if the dictionary has an object_permission_id
    2. If found, queries the database for the corresponding object permission
    3. Converts the object permission to a dictionary format
    4. Attaches it to the input dictionary under the 'object_permission' key
    
    Args:
        data_dict: The dictionary to attach object_permission to
        prisma_client: The database client
        
    Returns:
        Dict: The input dictionary with object_permission attached if found
        
    Raises:
        ValueError: If prisma_client is None
    NPrisma client not foundobject_permission_idwhereobject_permission)
ValueErrorgetdblitellm_objectpermissiontablefind_unique
model_dump	Exceptiondict)r   r	   r   r   s       t/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/management_helpers/object_permission_utils.py attach_object_permission_to_dictr      s     . 233$==)?@"/"2"2"P"P"\"\)+?@ #] #
 
 =$5$@$@$B! .?I)*
  =$5$:$:$<!=s6   ABA-BA/ &B/BB
BB	data_jsonexisting_object_permission_idc                 b  K   |t        d      | j                  dd      }|y|xs t        t        j                               }i }|j
                  j                  j                  d|i       d{   }||j                  dd      }t        |t              rt        j                  |      }t        |t              r|j                  |       |j
                  j                  j                  d|i||d	       d{   }t        j                   d
|        |j"                  S 7 7 *w)a  
    Common logic for handling object permission updates across organizations, teams, and keys.

    This function:
    1. Extracts `object_permission` from data_json
    2. Looks up existing object permission if it exists
    3. Merges new permissions with existing ones
    4. Upserts to the LiteLLM_ObjectPermissionTable
    5. Returns the object_permission_id

    Args:
        data_json: The data dictionary containing the object_permission to update
        existing_object_permission_id: The current object_permission_id from the entity (can be None)
        prisma_client: The database client

    Returns:
        Optional[str]: The object_permission_id after the update/creation, or None if no object_permission to process

    Raises:
        ValueError: If prisma_client is None
    Nr   r   r   r   T)exclude_unsetexclude_none)createupdate)r   datazcreated_object_permission_row: )r   popstruuiduuid4r   r   r   r   
isinstancejsonloadsr   r!   upsertr   debugr   )r   r   r	   new_object_permissionobject_permission_id_to_use existing_object_permissions_dictexisting_object_permissioncreated_object_permission_rows           r   &handle_update_object_permission_commonr1   7   se    4 233 /8mm<OQU.V$ (E (

I .0$ <<HH)+FG I 
 	
  "-+E+P+PT ,Q ,
(
 '- $

+@ A'.(//0EF <<CC)+FG:: D 
 	
 " 
)*G)HI )===G	
,	
s%   A1D/3D+4BD/D-)D/-D/)__doc__r(   r%   typingr   r   r   litellm._loggingr   litellm.proxy.utilsr   r   r$   r1        r   <module>r8      sy   
   ( ( 1 ,&&& 
&RO>O>#+C=O> L)O> c]	O>r7   