o
    V
hz                     @   s  d dl m Z  d dlmZ d dlmZ d dlmZmZ d dlm	Z	 dZ
dedefd	d
Zdd ZdedejdefddZ				d#dejdedede	e de	e dededefddZdedejdefddZdededejdefdd Zdedejdefd!d"ZdS )$    )datetime)DuplicateKeyError)database)
RoleCreate
RoleUpdate)Optionalroles	role_typereturnc                 C   s   |dkrdnd}|dkrdnd}| d }| d|i}|du rR| t }|dkr+d	d
ind	dd
ii}||ddd}d}	|D ]}
|
dd}	qA|||	d |jd|idddiid
d}|d }||krstd| d| d|S )ad  
    Generate the next role_id based on the role type.
    
    For SaaS roles, the counter (_id = "role_id_saas") must not exceed 99.
    For Platform roles, the counter (_id = "role_id_platform") must not exceed 999999.
    If the counter document is missing, it will be initialized based on the
    highest existing role_id in the roles collection.
    saasrole_id_saasrole_id_platformc   i?B counters_idNis_saas_onlyT$nerole_id   r   )r   sequence_valuez$incr   )return_documentzRole ID for z% roles exceeded the maximum limit of .)	find_oneCOLLECTION_NAMEfindsortlimitget
insert_onefind_one_and_update
ValueError)dbr	   
counter_id	max_limitcounters_collectioncounterroles_collectionquerymax_role_cursorstarting_valueroleupdatednew_role_id r.   6/var/www/html/moveengine/app/v1/services/saas/roles.pyget_next_role_id	   s*   	
r0   c                 C   s   | rdd |   D S | S )Nc                 S   s   i | ]	\}}t ||qS r.   )str).0kvr.   r.   r/   
<dictcomp>/   s    z-_convert_permissions_keys.<locals>.<dictcomp>)items)permissionsr.   r.   r/   _convert_permissions_keys-   s   r8   r+   r"   c                 C   s   |t  }|  }d|v r|d rt|d |d< t |d< t |d< |dr+dnd}|ds9t|||d< z|| W |S  tyL   t	dw )	Nr7   created_dateupdated_dater   r   platformr   z%A role with this name already exists.)
r   dictr8   r   utcnowr   r0   r   r   r!   )r+   r"   r'   	role_datar	   r.   r.   r/   create_role_service1   s   
r?   NFskipr   
account_idsearchis_system_defaultr   c                 C   s   | t  }d|i}|r||d< ndddiiddidd ig|d< |r'|dd|d	< |r1|s1d
di|d< ||||}	g }
|	D ]}|d|d< |d  |d< |d  |d< |
| q@||}||
dS )NrC   rA   z$existsF z$ori)z$regexz$optionsnamer   Tr   r   idr9   r:   )total_countr   )r   r   r@   r   r   	isoformatappendcount_documents)r"   r@   r   rA   rB   rC   r   r'   r(   roles_cursorr   r+   rH   r.   r.   r/   list_roles_serviceH   s$   	


rM   r   c                 C   sL   |t  }|d| i}|r$|d|d< |d  |d< |d  |d< |S Nr   rG   r9   r:   )r   r   r   rI   r   r"   r'   r+   r.   r.   r/   get_role_servicem   s   rP   role_updatec                 C   s   |t  }|jdd}d|v r|d rt|d |d< t |d< |d| id|i}|jdkr1d S |d| i}|rQ|d|d< |d	 	 |d	< |d 	 |d< |S )
NT)exclude_unsetr7   r:   r   z$setr   rG   r9   )
r   r<   r8   r   r=   
update_onematched_countr   r   rI   )r   rQ   r"   r'   update_dataresultr+   r.   r.   r/   update_role_servicew   s   
rW   c                 C   s^   |t  }|d| i}|sd S |d| i |d|d< |d  |d< |d  |d< |S rN   )r   r   
delete_oner   rI   rO   r.   r.   r/   delete_role_service   s   rY   )NNFF)r   pymongo.errorsr   app.dbr   app.v1.models.saas.rolemodelr   r   typingr   r   r1   intr0   r8   MongoDBr<   r?   boolrM   rP   rW   rY   r.   r.   r.   r/   <module>   sB    $
%
