o
    Ah
                  	   @   s   d dl mZ d dlmZ d dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZmZ d dlmZmZ dZd	efd
dZdefddZdedefddZdefddZdededee dee fddZdS )    )HTTPException)datetime)ObjectId)OptionalDictAny)str_to_objectid)GeofenceCreateGeofenceUpdate)	ASCENDING
DESCENDING	geofencesgeofencec                    s   |   }t |d< |d dkr1|dr1|d }|d |d kr)||d  d|gd|d	< |d
 |}t|j|d< |S )Ncreated_dateshapepolygoncoordinatesr   Polygon)typer   locr   id)dictr   utcnowgetappend
insert_onestrinserted_id)r   dbdatacoordsresult r#   >/var/www/html/moveengine/app/v1/services/platform/geofences.pycreate_geofence_service   s   
r%   
account_idc                    s<   |t  dt| i}|stdddt|d |d< |S )N_idi  zGeofence not found)status_codedetailr   )COLLECTION_NAMEfind_oner   r   r   r&   r   r   r#   r#   r$   get_geofence_service%   s   r-   updatec                    sN   dd |j dd D }|t dt| id|iI d H  t| |I d H S )Nc                 S   s   i | ]\}}||qS r#   r#   ).0kvr#   r#   r$   
<dictcomp>-   s    z+update_geofence_service.<locals>.<dictcomp>T)exclude_unsetr'   z$set)r   itemsr*   
update_oner   r-   )r&   r.   r   update_dictr#   r#   r$   update_geofence_service,   s   "r7   c                    s2   t | |I d H }|t dt| iI d H  |S )Nr'   )r-   r*   
delete_oner   r,   r#   r#   r$   delete_geofence_service1   s   r9   skiplimitqr   c                    s   i }|r|dd}d|ig|d< |r||d< |t  || |}g }|D ]}	t|	d |	d< ||	 q(|t  |}
|
|dS )	Ni)z$regexz$optionsnamez$orr   r'   r   )total_countusers)r*   findr:   r;   r   r   count_documents)r:   r;   r<   r   r   filter_queryregex_querycursorr   itemtotalr#   r#   r$   list_geofences_service6   s   

rH   N)fastapir   r   bsonr   typingr   r   r   app.v1.libraries.objectr    app.v1.models.platform.geofencesr	   r
   pymongor   r   r*   r%   r   r-   r7   r9   intrH   r#   r#   r#   r$   <module>   s    	&