
    b6i                         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mZ d d
lmZ d dlmZ d dlmZmZmZ  e       Z G d d      Zy)    )PathN)datetime)HTTPException
UploadFilestatus)Session)UserRole)
UserUpdate)UserRepository)hash_passwordverify_password)validate_profile_image)get_settings)ERR_FORBIDDENERR_OLD_PASSWORD)ERR_ADMINS_CAN_DELETE_ONLY_STANDARD_USERSc                       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defd       Z
ed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;;    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writer'   readstrprofile_imager   r   r    r   r!   )r   r'   r   profile_image_directoryunique_name	file_pathbuffers          r$   update_profile_imagez UserService.update_profile_image   s     	t$"&x'G'G"H%%dT%B))*!DMM?;+k9	)T"fLL)* # &)^""*//"3$$Wl;; #"s   6*C''C0old_passwordnew_passwordc                     t        || j                        st        t        j                  t
              t        |      | _        d| _        t        j                         | _
        t        j                  ||       S )z2Change password after validating the old password.status_codedetailN)r   hashed_passwordr   r   HTTP_400_BAD_REQUESTr   r   refresh_tokenr   r   r    r   r!   )r   r>   r?   r   s       r$   change_passwordzUserService.change_password1   s_     |\-I-IJF,G,GP`aa'4\'B$%)""*//"3$$Wl;;r&   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.rA   )roler
   USERSr   r   HTTP_403_FORBIDDENr   ADMINr   r   soft_delete_user)r   rH   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%   r   r=   r7   rG   rO    r&   r$   r   r      s    /<$ <j <7 <t < < <4 <z <G <X\ < <( <d <# <S <[b <gk < < @$ @ @w @4 @ @r&   r   )pathlibr   r0   r   fastapir   r   r   sqlmodelr   app.models.userr	   r
   app.schemas.userr   app.repositories.user_repor   app.core.securityr   r   app.utils.imager   app.core.configr   app.core.constantsr   r   r   r-   r   rU   r&   r$   <module>r`      sB       5 5  & ' 5 < 2 ( i i>1@ 1@r&   