o
    ̿Si-0                     @   s  d dl Z d dlZd dlZd dlZd dlmZmZ zd dlZeeds-G dd dZ	e	 e_
W n	 ey7   Y nw 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 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$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* e  edgddZ+eddZ,dZ-dZ.dZ/dZ0dZ1e2e3ddZ4G dd de j5Z6de%d e7fd!d"Z8d8d#e)d e7fd$d%Z9d&e:d'e2d(e2d)ee: d*ee; d e7fd+d,Z<d-e:d e7fd.d/Z=d-e:d0e'd e7fd1d2Z>d-e:d e7fd3d4Z?d5e*d e7fd6d7Z@dS )9    N)datetime	timedelta	__about__c                   @   s   e Zd ZeeddZdS )_About__version__0N)__name__
__module____qualname__getattr_bcryptr    r   r   8/var/www/html/Trade-python/app/v1/services/saas/users.pyr      s    r   )CryptContext)ObjectId)DuplicateKeyError)HTTPExceptionstatusRequest)load_dotenv)Optional)create_access_token)str_to_objectid   )send_verification_emailsend_invite_emailsend_forgot_password_emailsend_welcome_email)UserUserBase	UserLogin
UserUpdateUserResponseListEmailActionResetPasswordRequestbcryptauto)schemes
deprecatedi7  )minutesusersrolesaccountssubscriptionsprojectsALLOWED_FREE_AGENTS2c                       s   e Zd Z fddZ  ZS )CustomJSONEncoderc                    s0   t |tr	| S t |trt|S t |S N)
isinstancer   	isoformatr   strsuperdefault)selfobj	__class__r   r   r7   5   s
   

zCustomJSONEncoder.default)r   r	   r
   r7   __classcell__r   r   r:   r   r1   4   s    r1   userreturnc                 C   sR  |t  }|d }|d| jirtddd|d| ji}|r+d|vs+|d | jkr1tdddt| j}i |  |ddd	}t	|d
< d|d< |
dr|d |d< |
dd|d< |t }|dt|
di}	|	r|	
dd|d< |	
dd|d< |	
dd|d< |	
dr|t }
|
dt|	
di}|r|
dd|d< |
dt	|d
< n|t }||d dd}||d< n	 t |d< t |d< |d td| jd|d < z*d!d" | D }td#| jitd$}||}t|j|d< |t| j|d   W n ty   tdddw t tj!|t"d%}||d&S )'Nemailsemail  $User with this email already exists.status_codedetailverificationCodeInvalid verification code.T)hashed_password	is_activeis_verifiedsubscription_agentsr   active_agents
account_idroled   r+   _idaccount_typeactive_subscription_id subscription_idsubscription_statusactive_agents_countsubscription_agents_countACTIVE)rM   r   created_date
last_loginpasswordz^([^@]+)   namec                 S   s   i | ]\}}|d vr||qS ))rQ   rT   rU   rL   rK   r   ).0kvr   r   r   
<dictcomp>n       z'create_user_service.<locals>.<dictcomp>sub)dataexpires_delta)cls)	user_datatoken)#COLLECTION_NAMEfind_oner@   r   rF   pwd_contexthashr[   dictr/   getACCOUNT_COLLECTION_NAMEr   SUBSCRIPTION_COLLECTION_NAMEPROJECT_COLLECTION_NAMEcount_documentsr   utcnowpopresearchgroupitemsr   ACCESS_TOKEN_EXPIRES
insert_oner5   inserted_idadd_taskr   r   jsonloadsdumpsr1   )r=   background_tasksdbusers_collectionemails_collectionemail_entryrH   new_user_dataaccounts_collectionaccount_infosubscription_collectionsubscriptionproject_collectionactive_projects_countinsert_user_datarh   new_userjson_compatible_user_datar   r   r   create_user_service<   s^   




r   email_actionc              	   C   s(  |t  }|d }|t }|t }dd | jdD }|r | js%dddS |r,|dnd }	| jd	v r|d }
|d
|
id u}| jdkrL|rLdddS | jdkrX|sXdddS t	dd}|j
d
|
idd|iidd | jdkry|t|
| n|t|
| d|
 dddS | jdkrd}|	r|dt|	i}|r|dd}g }g }|D ]V}
|d
|
id u}|r||
 qt	dd}|d| ji}|r|dd nd }|
|| jd!}| jr| j|d< |j
d
|
id|idd |t|
|| j|| ||
 qd"t| d#||dd$S td%d&d')(Nr?   c                 S   s   g | ]
}|  r|  qS r   )strip)r^   r@   r   r   r   
<listcomp>   s    z3send_email_verification_service.<locals>.<listcomp>,z!Email(s) and action are required.r   )messager   rM   )signupforgotr@   r   rB   r\   r   zEmail not found.   i i?B $setrF   T)upsertzVerification code sent to .r   inviteMOVEXrP   account_namerole_idr]   r   )r@   rF   rN   zInvitations sent to z	 user(s).)r   invitedskippedr   rA   zInvalid action.rC   )ri   ro   COLLECTION_ROLEr?   splitactionrn   rj   randomrandint
update_oner|   r   r   r   appendrN   rM   r   lenr   )r   r   r   current_userr   r   r   roles_collection
email_listrM   r@   user_existsverification_coder   account_detailsr   r   roledetails	role_name
email_datar   r   r   send_email_verification_service|   sp   








r   rM   skiplimitqrI   c                 C   s   |t  }i }td | dkr|ddkrtddd| dkr#| |d< |r7|d	d
}	d|	id|	id|	ig|d< |d ur?||d< t||||}
|
D ]"}t|d |d< dD ]}||v rot	|| t
ro||  ||< qZqN||}td ||
dS )Nzhello BoOSSallr+   1i  z Not permitted to view all users.rC   rM   i)z$regexz$optionsr]   r@   mobilez$orrI   rP   user_id)rY   rZ   date_of_birthzhey boss)total_countr*   )ri   printrn   r   listfindr   r   r5   r3   r   r4   rr   )rM   r   r   r   rI   r   r   r   queryregex_queryr*   r=   fieldr   r   r   r   get_users_service   s.   


r   r   c                 C   s:   |t  dt| i}|rt|d |d< |dd  |S )NrP   idrH   )ri   rj   r   r5   rt   )r   r   r=   r   r   r   read_user_service   s
   r   rg   c                 C   s   |t  }|dt| i}|stddddd |  D }|dt| id|i}|jdkr7tddd|dt| i}|rJ|d	d  |S tdd
d)NrP     User not foundrC   c                 S   s   i | ]\}}|d ur||qS r2   r   )r^   keyvaluer   r   r   ra      rb   z'update_user_service.<locals>.<dictcomp>r   r   rH   zUser not found after update)	ri   rj   r   r   rm   rx   r   matched_countrt   )r   rg   r   r   existing_userupdate_dataresultupdated_userr   r   r   update_user_service   s   
r   c                 C   s@   |t  }|dt| i}|stddd|dt| i |S )NrP   r   r   rC   )ri   rj   r   r   
delete_one)r   r   r   r=   r   r   r   delete_user_service   s   r   request_bodyc           
      C   s   | j }| j}| j}|t }|d }|d|i}|s tddd|d|i}|r5d|vs5t|d |kr;tdddt|}	|	d	|d	 id
d|	ii |	d|idddii ddiS )Nr?   r@   r   r   rC   rF   rA   rG   rP   r   rH   z$unsetrS   r   zPassword reset successfully)
r@   rF   r[   ri   rj   r   r5   rk   rl   r   )
r   r   r@   coder[   r   r   r=   r   rH   r   r   r   reset_password_service  s   
r   r2   )Ar}   osru   r   r   r   r%   r   hasattrr   r   	Exceptionpasslib.contextr   bsonr   pymongo.errorsr   fastapir   r   r   dotenvr   typingr   app.v1.dependencies.authr   app.v1.libraries.objectr   libraries.email_templatesr   r   r   r   models.saas.usersmodelr   r   r    r!   r"   r#   r$   rk   ry   ri   r   ro   rp   rq   intgetenvr/   JSONEncoderr1   rm   r   r   r5   boolr   r   r   r   r   r   r   r   r   <module>   sR    
$
@*S