
    HCi                         d dl mZ d dl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 d dlmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ d dlmZmZmZmZ  e       Z G d d      Z y)    )PathN)datetime)HTTPException
UploadFilestatus)Session)UserRole)
UserUpdate)UserRepository)UserSessionRepository)hash_passwordverify_password)validate_profile_image)get_settings)ERR_FORBIDDENERR_OLD_PASSWORD)ERR_ADMINS_CAN_DELETE_ONLY_STANDARD_USERSERR_EMAIL_REGISTEREDc            	           e Zd ZdZededededefd       Zedede	defd       Z
eded	ededefd
       Zedede	de	defd       Zededededdfd       Zy)UserServicez%Service for user-specific operations.current_userdatasessionreturnc                     |j                  d      j                         D ]  \  }}t        | ||        t        j                         | _        t        j                  ||       S )z*Update profile fields on the current user.T)exclude_unset)
model_dumpitemssetattrr   utcnow
updated_atr   update)r   r   r   fieldvalues        K/var/www/html/story-book/Story-Book-python-api/app/services/user_service.pyupdate_userzUserService.update_user   sU     !OO$O?EEGLE5L%/ H"*//"3$$Wl;;    	new_emailc                 J   | j                   |k(  rddiS t        j                  ||      }|rt        t        j
                  t              || _         t        j                         | _	        t        j                  ||       }t        j                  ||j                         ddiS )zChange user's email address.messagezEmail unchangedstatus_codedetailz0Email updated successfully. Please log in again.)emailr   get_by_emailr   r   HTTP_400_BAD_REQUESTr   r   r!   r"   r#   r   revoke_all_for_userid)r   r)   r   existingusers        r&   change_emailzUserService.change_email"   s     *011!..w	B"77+ 
 '"*//"3$$Wl;11'477CMNNr(   filec                    t        |       t        t        j                        }|j	                  dd       t        j                         j                   d|j                   }||z  }t        |d      5 }|j                  |j                  j                                ddd       t        |      | _        t        j                          | _        t%        j&                  ||       S # 1 sw Y   HxY w)zDValidate and store profile image, update user's profile_image field.T)parentsexist_ok_wbN)r   r   settingsPROFILE_IMAGE_DIRECTORYmkdiruuiduuid4hexfilenameopenwriter7   readstrprofile_imager   r!   r"   r   r#   )r   r7   r   profile_image_directoryunique_name	file_pathbuffers          r&   update_profile_imagez UserService.update_profile_image7   s     	t$"&x'G'G"H%%dT%B))*!DMM?;+k9	)T"fLL)* # &)^""*//"3$$Wl;; #"s   6*C''C0old_passwordnew_passwordc                 2   t        || j                        st        t        j                  t
              t        |      | _        t        j                         | _	        t        j                  ||       }t        j                  ||j                         ddiS )z9Change user's password after validating the old password.r,   r+   z3Password updated successfully. Please log in again.)r   hashed_passwordr   r   r1   r   r   r   r!   r"   r   r#   r   r2   r3   )r   rN   rO   r   r5   s        r&   change_passwordzUserService.change_passwordL   sv     |\-I-IJF,G,GP`aa'4\'B$"*//"3$$Wl;11'477CPQQr(   targetNc                 X   | j                   t        j                  k(  rt        t        j
                  t              | j                   t        j                  k(  r<|j                   t        j                  k7  rt        t        j
                  t              t        j                  ||      S )z6Soft-delete a target user respecting role-based rules.r,   )roler
   USERSr   r   HTTP_403_FORBIDDENr   ADMINr   r   soft_delete_user)r   rS   r   s      r&   delete_userzUserService.delete_user[   sq     

*F,E,Em\\

*v{{djj/HF,E,ENwxx..w??r(   )__name__
__module____qualname____doc__staticmethodr	   r   r   r'   rG   r6   r   rM   rR   rZ    r(   r&   r   r      s    /<$ <j <7 <t < < O4 OC O' O O( <4 <z <G <X\ < <( Rd R# RS R[b R R @$ @ @w @4 @ @r(   r   )!pathlibr   r@   r   fastapir   r   r   sqlmodelr   app.models.userr	   r
   app.schemas.userr   app.repositories.user_repor   "app.repositories.user_session_repor   app.core.securityr   r   app.utils.imager   app.core.configr   app.core.constantsr   r   r   r   r=   r   r`   r(   r&   <module>rl      sM       5 5  & ' 5 D < 2 (  >K@ K@r(   