o
    V
h                     @   st  d dl m Z  d dlmZmZ d dlmZ d dlmZmZm	Z	m
Z
mZ d dlmZ d dlmZmZmZmZ de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ededed
ejdee f
ddZded
ejdee fddZdedeeef d
ejdefddZdedeeeef  d
ejdefddZded
ejdededef
ddZdededed
ejdee f
dd Zd!S )"    )datetime)
CollectionReturnDocument)database)CommentAccountDetailSalesStatusAccountStatsLog)str_to_objectid)OptionalListDictAny
account_idteam_member_idcomment_textdbreturnc                 C   s6   |d }t | ||d }||}t|j|d< |S )Ncrm_comments)r   r   comment_id)r   dict
insert_onestrinserted_id)r   r   r   r   comments_collectioncomment_dataresult r   2/var/www/html/moveengine/app/v1/services/cx/crm.pyadd_comment_service   s
   
r!   
comment_idc                 C   sL   |d }| dt| i}|std|dt| i t|d |d< |S )Nr   r   zComment not found)find_oner   
ValueError
delete_oner   )r"   r   r   r   r   r   r    delete_comment_service   s   r&   skiplimitc                 C   V   |d }| d| idd||}g }|D ]}t|d |d< || q|S )Nr   r   created_dater   findsortr'   r(   r   append)r   r'   r(   r   r   cursorcommentsr   r   r   r    get_comments_service      "r2   c                 C   s.   |d }| d| i}|rt|d |d< |S )Naccount_detailsr   r   )r#   r   )r   r   details_collectiondetailr   r   r    get_account_detail_service'   s
   r7   update_datac                 C   sD   |d }t  |d< |jd| id|idtjd}t|d |d< |S )Nr4   updated_dater   $setTupsertreturn_documentr   )r   utcnowfind_one_and_updater   AFTERr   )r   r8   r   r5   r   r   r   r    update_account_detail_service.   s   rA   pipelinec                 C   sZ   t |dkr
td|d }|t d}|jd| id|idtjd}t|d	 |d	< |S )
N   z0Sales pipeline cannot have more than 5 statuses.r4   )sales_pipeliner9   r   r:   Tr;   r   )lenr$   r   r>   r?   r   r@   r   )r   rB   r   r5   r8   r   r   r   r    update_sales_pipeline_service:   s   rF   c                 C   s   |d }| d| i}|d| iddd}d }|D ]}|d}q|d }	|	 d| i}
|d }|d| i}d }|rPd|v rP|d rP|d d d	}d
}||rY| nd |
||d}|S )Nr   r   r*   r+      usersr4   rD   statusg        )total_commentslast_comment_datetotal_usersmonthly_paymentscurrent_sales_status)count_documentsr-   r.   r(   getr#   	isoformat)r   r   r'   r(   r   rJ   last_comment_cursorrK   r   users_collectionrL   r5   account_detailrN   rM   statsr   r   r    get_account_stats_serviceK   s*   rV   c                 C   r)   )Ncrm_logsr   	timestampr+   r   r,   )r   r'   r(   r   logs_collectionr0   logslogr   r   r    get_logs_servicem   r3   r\   N)r   pymongo.collectionr   r   app.dbr   app.v1.models.cx.crmr   r   r   r	   r
   app.v1.libraries.objectr   typingr   r   r   r   r   MongoDBr   r!   r&   intr2   r7   rA   rF   rV   r\   r   r   r   r    <module>   s     $	$( ("