o
    ̿Si                  
   @   s  d dl m Z  d dlmZ d dlmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZ 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Z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f
ddZ dedejdefddZ!dS )    )datetime)parser)OptionalListDictAny)DuplicateKeyError)HTTPException)ObjectId)database)
send_email)str_to_objectid)AccountCreateAccountUpdate)get_next_sequence_valueaccountsusersaccountdbreturnc              
   C   s  |   }t |d< d|v r|d r|d j|d< d|v r(|d r(|d j|d< d|v r7|d r7|d j|d< td||d< |t }z||}W n: tym } zdt|v r[t	ddt|v ret	d	t	d
d }~w t
y } ztddt| dd }~ww t|j}||d< |t }z|dt| jidd|ii}W n t
y } ztdt| tddt| dd }~ww |jdkrtdddd}	d| j d}
t|	|
| j |S )Ncreated_datesubscription_statussubscription_typeaccount_typeaccounts_idemailz'Account with this email already exists.phonez.Account with this phone number already exists.zDuplicate key error.i  zDatabase error: status_codedetail_id$set
account_idz$Error updating user with account_id:zFailed to update user: r   z&Failed to update user with account_id.z#Your Organization has been created!zHello zQ,

Thank you for signing up for our platform. We're excited to have you on board!)dictr   utcnowvaluer   COLLECTION_NAME
insert_oner   str
ValueError	Exceptionr	   inserted_idUSER_COLLECTION_NAME
update_oner   user_idprintmodified_count	full_namer   r   )r   r   account_dataaccounts_collectionresulter"   users_collectionupdate_resultsubjectbody r:   ;/var/www/html/Trade-python/app/v1/services/saas/accounts.pycreate_account_service   sT   


r<   r"   c                 C   sJ   |t  }|dt| i}|stdddt|d |d< |d |d< |S Nr      Account not foundr   r"   )r&   find_oner   r	   r(   r"   r   
collectionr   r:   r:   r;   get_account_serviceH   s   rC   account_updatecurrent_userc                 C   s  |t  }|t }|dt| i}|stddd|dt|di}|s-tddd|ddkrCt|d| krCtd	d
d|jdd}dD ]}	|	|v r_t||	 dr_||	 j	||	< qK|
dt| id|i |dt| i}
|
rt|
d |
d< |
d |
d< |
S tddd)Nr    r>   r?   r   zUser not foundroles   r"   i  z%Not authorized to update this accountT)exclude_unset)r   r   r   r%   r!   zAccount not found after update)r&   r,   r@   r   r	   getr(   r#   hasattrr%   r-   )r"   rD   rE   r   rB   r6   existing_accountuserupdateskeyupdated_accountr:   r:   r;   update_account_serviceQ   s,    rP   c                 C   s\   |t  }|dt| i}|stddd|dt| i t|d |d< |d |d< |S r=   )r&   r@   r   r	   
delete_oner(   rA   r:   r:   r;   delete_account_servicem   s   rR   N)"r   dateutilr   typingr   r   r   r   pymongo.errorsr   fastapir	   bsonr
   app.dbr   app.v1.integrations.SMTPmailerr   app.v1.libraries.objectr   app.v1.models.saas.accountmodelr   r   app.v1.services.sequencer   r&   r,   MongoDBr#   r<   r(   rC   rP   rR   r:   r:   r:   r;   <module>   s"    7 	