
    `gj                    "   d 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mZ ddlmZ ddlmZ dd	Zd
dddZddZddZddZd dZd!dZd"d#dZdd
d	 	 	 	 	 	 	 	 	 d$dZd"d%dZ	 	 	 	 	 	 	 	 d&dZddd
dd	 	 	 	 	 	 	 	 	 	 	 	 	 d'dZy)(zCMicrosoft Graph subscription helpers for the Teams pipeline plugin.    )annotations)datetime	timedeltatimezone)Any)GraphSubscription)TeamsPipelineStore!resolve_teams_pipeline_store_path)MicrosoftGraphTokenProvider)MicrosoftGraphClientc                 @    t        j                         } t        |       S N)r   from_envr   )providers    W/root/.hermes/venv/lib/python3.12/site-packages/plugins/teams_pipeline/subscriptions.pybuild_graph_clientr      s    *335H))    F)defaultc                   t        | t              r| S t        | t              r(| j                         j	                         }|dv ry|dv ry|S )N>   1onyestrueT>   0noofffalseF)
isinstanceboolstrstriplower)valuer   lowereds      r   _parse_boolr%      sL    %%++-%%'0011Nr   c                H    	 t        |       S # t        t        f$ r |cY S w xY wr   )int	TypeError
ValueError)r#   r   s     r   
_parse_intr*      s*    5zz" s   
 !!c                 H    t        j                  t        j                        S r   )r   nowr   utc r   r   _utc_nowr/   &   s    <<%%r   c                 r    t               j                  d      j                         j                  dd      S )Nr   microsecond+00:00Z)r/   replace	isoformatr.   r   r   _utc_now_isor7   *   s.    :!,668@@3OOr   c                2   | y t        |       j                         }|sy |j                  d      r|d d  d}t        j                  |      }|j
                   |j                  t        j                        S |j                  t        j                        S )Nr4   r3   )tzinfo)
r    r!   endswithr   fromisoformatr:   r5   r   r-   
astimezone)r#   textparseds      r   _parse_datetimer@   .   s    }u:D}}Ss)F###D)F}}~~X\\~22X\\**r   c                *    t        t        |             S r   )r    r
   paths    r   resolve_store_pathrD   <   s    0677r   Nc                *    t        t        |             S r   )r	   rD   rB   s    r   build_storerF   @   s    0677r   statusrenewedc                   t        j                  |      j                         }t        |j	                  d            }|}||r|t               k  rdnd}||d<   |rt               |d<   | j                  |d   |      S )Nexpiration_datetimeexpiredactiverH   latest_renewal_atsubscription_id)r   	from_dictto_dictr@   getr/   r7   upsert_subscription)storesubscription_payloadrH   rI   
normalized
expirationeffective_statuss          r   sync_graph_subscription_recordrY   D   s     #,,-ABJJLJ 0E!FGJ(2zXZ7O9U]+Jx*6.
&'$$Z0A%BJOOr   c                j    | ddl m}  |dd      } t        | xs d      j                         }|xs d S )Nr   )getenvMSGRAPH_WEBHOOK_CLIENT_STATE )osr[   r    r!   )rawr[   r#   s      r   expected_client_stater`   V   s7    
{3R8	rN  "E=Dr   c               <   t        |j                  d      xs |j                  d      xs d      j                         }|r| j                  |      ry|rGt        |j                  d      xs |j                  d      xs d      j                         }|r||k(  ryy)NrO   idr]   Tclient_stateclientStateF)r    rR   r!   get_subscription)rT   rU   expected_client_state_valuerO   candidate_states        r   is_managed_subscriptionrh   _   s       !23[7K7O7OPT7U[Y[eg  511/B" $$^4e8L8P8PQ^8_ece

%' 	 2MMr      )renew_within_hoursextend_hoursdry_runrc   c                  K   t        dt        |            }t        dt        |            }t        |      }t               }| j	                  d       d {   }	t               }
d}g }g }g }|	D ]I  }t        |t              st        |j                  d      xs d      j                         }|sDt        |||      }|s|j                  |dd       i|
j                  |       	 t        ||       |dz  }t!        |j                  d
            }||j                  |dd       t        ||z
  j#                               }|dk  rh|j%                  |d|j'                         j)                  dd      d       |j                  |d|j'                         j)                  dd      d       F||dz  kD  r|j                  |d|d       et        ||      t+        |      z   j)                  d      j'                         j)                  dd      }||j                  d      |j'                         j)                  dd      |d}|j                  |       |r| j-                  d| d
|i       d {   }i ||xs i ||d}t        ||dd       |j                  i |d|i       L |j/                         D ]$  }||
v r|j%                  |d t1               d!       & dt3        |      t        |j4                        t7        |	      |t7        |      t7        |      |||||d"S 7 # t        $ r"}|j                  |d	| d       Y d }~d }~ww xY w7 w)#N   z/subscriptionsr   rb   r]   )rf   not_managed_by_teams_pipeline)rO   reasonzfailed_to_sync_local_store: expirationDateTimemissing_expirationrL   r3   r4   )rH   rK   already_expired)rO   rp   rK   i  not_due)rO   rp   expires_in_seconds)hoursr1   resource)rO   rw   current_expirationnew_expirationz/subscriptions/)	json_body)rb   rq   rM   TrG   resultmissing_remote)rH   last_seen_missing_remote_at)successrl   
store_pathremote_subscription_countsynced_subscription_countcandidate_countrenewed_countthreshold_hoursrk   
candidatesrI   skipped)maxr'   r`   r/   collect_paginatedsetr   dictr    rR   r!   rh   appendaddrY   	Exceptionr@   total_secondsrS   r6   r5   r   
patch_jsonlist_subscriptionsr7   r   rC   len)clientrT   rj   rk   rl   rc   r   managed_client_stater,   remote_subscriptions
remote_idssyncedrI   r   r   r_   rO   managedexcrW   seconds_until_expiryry   	candidatepatchedmergeds                            r   maintain_graph_subscriptionsr   u   s     !S!345Oq#l+,L0>
*C!'!9!9:J!KK5JF$&G')J$&G# U9#t$cggdm1r288:)(<

 NN'6= '
	*5#6aKF %SWW-A%BC
NNJ^_`"J$4#C#C#EF!#%%'+5+?+?+A+I+I(TW+X NN'6/+5+?+?+A+I+I(TW+X /D"88NN'6'*> c:.1NNWW X 

)+ggh, 	  /
+","6"6"8"@"@3"O,	
	 	)$))o./+^< * 
 
 iChGMrh/Ygh&ufXtT7)7Xw78kU9n !335 	
j(!!*/;~	
	
 =%**o%()=%>%+z?W*$  S L>  	NN'6 <SEB 	n
sK   AM,L9BM,*L<;FM,?M* B:M,<	M'M"M,"M''M,)returnr   )r#   r   r   r   r   r   )r#   r   r   r'   r   r'   )r   r   )r   r    )r#   r   r   zdatetime | None)rC   
str | Noner   r    r   )rC   r   r   r	   )
rT   r	   rU   dict[str, Any]rH   r   rI   r   r   r   )r_   r   r   r   )rT   r	   rU   r   rf   r   r   r   )r   r   rT   r	   rj   r'   rk   r'   rl   r   rc   r   r   r   )__doc__
__future__r   r   r   r   typingr   plugins.teams_pipeline.modelsr   plugins.teams_pipeline.storer	   r
   tools.microsoft_graph_authr   tools.microsoft_graph_clientr   r   r%   r*   r/   r7   r@   rD   rF   rY   r`   rh   r   r.   r   r   <module>r      s   I " 2 2  ; ^ B =*
 05 	&P+88 PP(P 	P
 P P$( ",	
 
4 !#D D D 	D
 D D D Dr   