
    pi                        S SK JrJr  S SKJrJrJr  S SKJrJ	r	J
r
Jr  S SKJr  SSKJr  SSKJr  S\S	\
\\4   4S
 jrS\S\S	\\
\\4      4S jrS\S\S	\\
\\4      4S jrS\S\S\S	\\
\\4      4S jrS\S\S	\4S jrS\S\S	\\
\\4      4S jr  SS\S\S\S\S\	\   S\	\   S	\4S jjrS\S\S\S	\4S jr S S\S\S\S\S\	\   S	\4S jjrS\S\4S jrg)!    )Session
joinedload)and_or_desc)ListOptionalDictAny)datetime   )Notification)Usernreturnc                    U (       d  gU R                   (       a  U R                   R                  OSnU R                  (       a  U R                  R                  OSnU R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  UUU R                  U R                  S.$ )zHConvert Notification ORM object to dictionary for template compatibilityNSistemaUsuario)id
created_attitlebody	sender_idrecipient_idreservation_idis_readsender_namerecipient_namedeleted_by_senderdeleted_by_recipient)sendername	recipientr   r   r   r   r   r   r   r   r   r    )r   r   r   s      C/home/mario/Escritorio/thel4footbal/app/models/notification_crud.pynotification_to_dictr%   	   s    #$88!((--K)*Q[[%%)N ddll[[**99"(00 ! 6 6     dbuser_idc           	         U R                  [        5      R                  [        [        R                  5      5      R                  [        R                  U:H  [        [        R                  S:H  [        R                  R                  S5      5      5      R                  [        [        R                  5      5      R                  5       nU Vs/ s H  n[        U5      PM     sn$ s  snf )z%Get received notifications for a userr   N)queryr   optionsr   r!   filterr   r   r    is_order_byr   r   allr%   r'   r(   notificationsr   s       r$   get_notifications_for_userr2       s    HH\*22<&&'f!!W,L--2L4U4U4Y4YZ^4_` htL++,-cce  .;;] #];;;   C%r   c           	         U R                  [        5      R                  [        [        R                  5      5      R                  [        R                  U:H  [        [        R                  S:H  [        R                  R                  S5      5      5      R                  [        [        R                  5      5      R                  5       nU Vs/ s H  n[        U5      PM     sn$ s  snf )z Get notifications sent by a userr   N)r*   r   r+   r   r#   r,   r   r   r   r-   r.   r   r   r/   r%   )r'   r   r1   r   s       r$   get_notifications_sent_by_userr5   +   s    HH\*22<))*f)+L**a/1O1O1S1STX1YZ htL++,-cce  .;;] #];;;r3   r   c                 j   U R                  [        5      R                  [        [        R                  5      5      R                  [        R                  U:H  [        R                  U:H  [        [        R                  S:H  [        R                  R                  S5      5      [        [        R                  S:H  [        R                  R                  S5      5      5      R                  [        [        R                  5      5      R                  5       nU Vs/ s H  n[!        U5      PM     sn$ s  snf )z8Get notifications sent by a user to a specific recipientr   N)r*   r   r+   r   r#   r,   r   r   r   r   r-   r    r.   r   r   r/   r%   )r'   r   r   r1   r   s        r$   &get_notifications_sent_by_user_to_userr7   6   s    HH\*22<))*f)+!!\1L**a/1O1O1S1STX1YZL--2L4U4U4Y4YZ^4_`	
 htL++,-cce  .;;] #];;;s   D0c           
         U R                  [        5      R                  [        R                  U:H  [        R                  S:H  [        [        R                  S:H  [        R                  R                  S5      5      5      R                  5       $ )z%Count unread notifications for a userr   N)	r*   r   r,   r   r   r   r    r-   countr'   r(   s     r$   get_unread_notifications_countr;   C   sj    88L!((!!W,!L--2L4U4U4Y4YZ^4_` eg	r&   c           
         U R                  [        5      R                  [        [        R                  5      5      R                  [        R                  U:H  [        R                  S:H  [        [        R                  S:H  [        R                  R                  S5      5      5      R                  [        [        R                  5      5      R                  5       nU Vs/ s H  n[        U5      PM     sn$ s  snf )z#Get unread notifications for a userr   N)r*   r   r+   r   r!   r,   r   r   r   r    r-   r.   r   r   r/   r%   r0   s       r$   !get_unread_notifications_for_userr=   K   s    HH\*22<&&'f!!W,!L--2L4U4U4Y4YZ^4_` htL++,-cce  .;;] #];;;s   C7Nr   r   r   c                     [        UUUUUS[        R                  " 5       R                  S5      S9nU R	                  U5        U R                  5         U R                  U5        U$ )zCreate a new notificationr   z%Y-%m-%dT%H:%M:%S)r   r   r   r   r   r   r   )r   r   nowstrftimeaddcommitrefresh)r'   r   r   r   r   r   notifications          r$   create_notificationrE   W   s^      !%<<>**+>?L FF<IIKJJ|r&   notification_idc                     U R                  [        5      R                  [        R                  U:H  [        R                  U:H  5      R                  5       nU(       a  SUl        U R                  5         gg)z+Mark a notification as read for a recipient   TF)r*   r   r,   r   r   firstr   rB   )r'   rF   r(   rD   s       r$   mark_notification_as_readrJ   n   s]    88L)00?*!!W, eg 
  
		r&   	user_roletarget_recipient_idc                    U R                  [        5      R                  [        R                  U:H  5      R	                  5       nU(       d  gUR
                  nUR                  nX':X  a  SUl        ORUb.  X&:X  a)  Ub  [        U5      U:X  a  SUl	        SUl        O)SUl	        O!US;   a  Ub  [        U5      U:X  a  SUl        OgU R                  5         g)zo
Delete notification logic based on user role and ownership.
Returns True if deleted/updated, False otherwise.
FrH   )docenteadminT)r*   r   r,   r   rI   r   r   r    intr   rB   )r'   rF   r(   rK   rL   rD   r   r   s           r$   delete_notificationrQ   {   s     88L)00O1STZZ\L&&I,,L ,-)		7#7*s3F/G</W-.L*01L--.L*	*	*/B/NSVWjSko{S{-.	*IIKr&   c                     U R                  [        5      R                  [        [        R                  U:H  [        R
                  U:H  5      5      R                  SS9  U R                  5         g)z1Delete all notifications sent or received by userF)synchronize_sessionN)r*   r   r,   r   r   r   deleterB   r:   s     r$   delete_notifications_by_userrU      sP     HH\!!L""g-|/H/HG/STff'IIKr&   )NN)N)sqlalchemy.ormr   r   
sqlalchemyr   r   r   typingr   r	   r
   r   r   models.notificationr   models.userr   strr%   rP   r2   r5   r7   r;   r=   rE   boolrJ   rQ   rU    r&   r$   <module>r^      s   . & & , ,  . L T#s(^ .	<7 	<S 	<T$sCx.=Q 	<	<w 	<3 	<4SRUXCW 	<<w <3 <VY <^bcghkmphpcq^r <w   
<' 
<C 
<DcSVhDX 
<"  $$(  	
 } SM .' C # RV $ *.!!! ! 	!
 "#! 
!FW s r&   