o
    ȟ1i#                     @   s   d dl m Z mZ d dl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 d dlmZ d dlmZmZ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mZ d dlm Z  d dl!m"Z"m#Z# d dlm$Z$ G dd dZ%dS )    )datetime	timedeltaN)Path)
joinedload)select)	BASE_PATHget_dbsettings)SendForgotPasswordEmail)verify_passwordcreate_access_tokenhash_password)CoreDBErrorGenericError)
send_email)UserUserProfile
UserWalletParticipantParentPasswordResetToken)UserTypesEnum)ProfileResponseSchemaParentSchema)Staffc                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )UserServicec                 C   s   t  L}|jg dd}t|j|d< |j|d< ztdi |}|| |  || |W W  d    S  t	yN } z|
  td| d }~ww 1 sRw   Y  d S )N)passwordconfirm_password	last_namedob)excludehashed_passwordsur_namezCould not create account:  )r   
model_dumpr   r   r   r   addcommitrefresh	Exceptionrollbackr   )selfrequestdbpayloadnew_userer#   r#   /app/services/user.pycreate_user   s"   



zUserService.create_userc                 C   s  t  ~}tttttd}||j }|||j|j	k|j
d |jdk }|d u r2tdddt|j|js?tddd|j|j|j|j|jd}|jdkrW|j|d< n|j|d< t||d	< |jd
v rt|j|d< |jdk|d< nd|d< |W  d    S 1 sw   Y  d S )NadminparentparticipantgueststaffT  zUser not exists!status_codeexc)idemail
first_name	user_typemobile_numberr7   r"   token)r5   r4   age   Zis_above_18F)r   r   r   r   r   r?   querywherer=   username
deleted_atis_	is_activefirstr   r   r   r!   r<   r>   r@   r   r"   r   rB   )r*   r+   r,   modelsmodeluserr-   r#   r#   r0   validate_user_for_login#   s<   




$z#UserService.validate_user_for_loginc                 C   s   t  g}| }|d |d< |ttj|d k }|s5td	i |}|| |  |	| n-|ttj|d ktj
d j|dd}|  |dkrZtdddW d    d S W d    d S 1 smw   Y  d S )
Nr<   user_idfetchsynchronize_sessionr   i  zCould not update profiler9   r#   )r   r$   rD   r   filterrO   rJ   r%   r&   r'   rG   rH   updater   )r*   r+   rM   r,   r-   user_profileZnew_profileresultr#   r#   r0   update_profileB   s,   

	"zUserService.update_profilec                 C   sl   t  )}|ttj|d k }|r#t| W  d    S 	 W d    d S 1 s/w   Y  d S )Nr<   )	r   rD   r   rS   rO   rJ   r   model_validater$   )r*   rM   r,   rU   r#   r#   r0   fetch_profileU   s   $zUserService.fetch_profilec                    s  t |jjj}t  | }td|  }|j I d H }t|d}|	| W d    n1 s4w   Y  t
ttt
td}||j }	t|	 t *}
|
|	|	j|jkjd|idd}|
  |dkrotdd	d
W d    d S 1 szw   Y  d S )Nzuploads/wbr2   profile_imagerP   rQ   r   i  zCould not upload profile imager9   )r   imagefilenamesuffixuuiduuid4r   readopenwriter   r   r   r   r?   printr   rD   rS   r<   rO   rT   r&   r   )r*   r+   rM   file_extensionnew_filenamefile_locationcontentfrK   rL   r,   rV   r#   r#   r0   upload_profile_image]   s4   
"z UserService.upload_profile_imagec                 C   s"   t |jd}|| |  d S )N)rO   )r   r<   r%   r&   )r*   r,   rM   walletr#   r#   r0   create_walletv   s   
zUserService.create_walletc                 C   s^   t  "}|tttjtjdk }dd |D W  d    S 1 s(w   Y  d S )Nz	parent.pyc                 S   s   g | ]}t |qS r#   )r   rX   ).0rM   r#   r#   r0   
<listcomp>~   s    z3UserService.get_all_parent_user.<locals>.<listcomp>)	r   rD   r   optionsr   rU   rS   r?   all)r*   r,   usersr#   r#   r0   get_all_parent_user{   s   &$zUserService.get_all_parent_userc                 C   s   t  i}t|jd}|j}|t||j|jk}| }|s't	ddd|
ttj|jkjdd tt }t ttjd }t|||j|jd}	||	 |  t|	d	}
|
 }|W  d    S 1 sow   Y  d S )
Nvaluer8   User not foundr9   rP   rQ   )minutes)rA   expiryrO   r?   )token_instance)r   r   r?   rL   executer   rE   r=   scalar_one_or_noner   rD   r   rS   rO   r<   deletestrr_   r`   r   utcnowr   r	   reset_password_token_time_outnamer%   r&   r
   r   )r*   r+   r,   Z
user_typesUserTypeModelrV   rM   rA   rw   reset_entryr=   statusr#   r#   r0   send_password_reset_email   s4   



$z%UserService.send_password_reset_emailc                 C   s   t  [}|tttj|jk}t|jdj}|	 }|r#|
 r)tdddt|j}|||j|jkjd|idd |ttj|jkjdd |  	 W d    dS 1 saw   Y  d S )	Nrs     zInvalid or expired tokenr9   r!   rP   rQ   T)r   ry   r   r   rE   rA   r   r?   rL   rz   
is_expiredr   r   new_passwordrD   rS   r<   rO   rT   r{   r&   )r*   r+   r,   r   r   Zreset_tokenr!   r#   r#   r0   reset_password   s>   





$zUserService.reset_passwordc              	      s   t |jdj}t C}|t||j|dk}|	 }|s(t
dddt|j|js5t
dddt|j}||_|| |  W d    d S 1 sQw   Y  d S )Nrs   r<   r   ru   r9   i  zOld password is incorrect)r   r?   rL   r   ry   r   rE   r<   getrz   r   r   old_passwordr!   r   r   r%   r&   )r*   rM   datar   r,   rV   r!   r#   r#   r0   change_password   s     


"zUserService.change_passwordc                 C   s   t  U}| dkr|ttj|k }n| dkr,|ttj|k }ntddd|s:tdddt	|drR|j
 |_
|  |j
W  d    S tdd	d1 s[w   Y  d S )
Nr4   r5   r   zUnsupported user typer9   r8   zItem not foundrI   zCould not disable user )r   lowerrD   r   rS   r<   rJ   r   r   hasattrrI   r&   )r*   rO   r?   r,   itemr#   r#   r0   toggle_user_active   s   

zUserService.toggle_user_activeN)__name__
__module____qualname__r1   rN   rW   rY   rj   rl   rr   r   r   r   r   r#   r#   r#   r0   r      s    r   )&r   r   r_   pathlibr   sqlalchemy.ormr   Zsqlalchemy.futurer   core.configr   r   r	   core.email.configr
   core.security.authenticationr   r   r   core.security.exceptionsr   r   core.utilityr   rK   r   r   r   r   r   r   Zmodels.userr   schema.userr   r   r   r   r#   r#   r#   r0   <module>   s     