
    	h!                     P    S SK Jr  SSKJrJr  SSKrSSKrS SKJr   " S S5      rg)   )get_db_connection    )datetime	timedeltaN)Configc                       \ rS rSr\S 5       r\SS j5       r\S 5       r\S 5       r\S 5       r	\S 5       r
\S	 5       rS
rg)RegistrationToken   c                  X   [        5       n U (       d  [        S5      e U R                  5       nSnUR                  U5        UR                  S5        UR	                  5       (       d  SnUR                  U5        [        S5        U R                  5         U R                  5       (       a!  WR                  5         U R                  5         gg! [         a  n[        SU 35         SnANTSnAff = f! U R                  5       (       a!  WR                  5         U R                  5         f f = f)zKEnsures the registration_tokens table exists and has the email_sent column.z"Could not connect to the database.ai  
            CREATE TABLE IF NOT EXISTS `registration_tokens` (
                `code` VARCHAR(255) PRIMARY KEY,
                `pid` INT NOT NULL,
                `team` INT NULL,
                `expires_at` DATETIME NOT NULL,
                `email_sent` TINYINT(1) NOT NULL DEFAULT 0,
                INDEX (`pid`)
            ) ENGINE=InnoDB;
            z9SHOW COLUMNS FROM `registration_tokens` LIKE 'email_sent'zWALTER TABLE `registration_tokens` ADD COLUMN `email_sent` TINYINT(1) NOT NULL DEFAULT 0z9Column 'email_sent' added to 'registration_tokens' table.z"Error creating or altering table: N)	r   	Exceptioncursorexecutefetchoneprintcommitis_connectedclose)connr   create_queryalter_queryes        */var/www/fran/franai/models/token_model.pycreate_table_if_not_exists,RegistrationToken.create_table_if_not_exists   s     !"@AA	[[]F	L NN<( NNVW??$$w{+QRKKM   ""

 #  	<6qc:;;	<   ""

 #s*   A7C 
C.C)$C1 )C..C1 18D)Nc                    [        5       nU(       d  g UR                  SS9nSnUR                  X@45        UR                  5       n[        R
                  " S5      n[        [        R                  R                  SS5      5      n[        R                  " 5       nU[        US9-   n	U(       a  S	n
UR                  XXU 45        OS
nUR                  XXU	45        UR                  5         UUR                  5       (       a!  UR                  5         UR                  5         $ $ ! [          a^  n[#        SU 35        UR%                  5          SnAUR                  5       (       a!  WR                  5         UR                  5         ggSnAff = f! UR                  5       (       a!  WR                  5         UR                  5         f f = f)z
Checks for a token for a user based on PID. If one exists, it updates it with a 
new code and expiry, and resets the email_sent flag. If not, it creates a new token.
This ensures only one token record exists per user.
NT
dictionaryz;SELECT code FROM registration_tokens WHERE pid = %s LIMIT 1   !REGISTRATION_TOKEN_EXPIRY_MINUTES   )minutesz
                    UPDATE registration_tokens 
                    SET code = %s, expires_at = %s, team = %s, email_sent = 0 
                    WHERE pid = %s
                zUINSERT INTO registration_tokens (code, pid, team, expires_at) VALUES (%s, %s, %s, %s)z"Error creating or updating token: )r   r   r   r   secretstoken_urlsafeintosenvirongetr   get_ph_timer   r   r   r   r   r   rollback)pidteamr   r   select_queryexisting_tokennew_codeexpiry_minutesph_timenew_expires_atupdate_queryinsert_queryr   s                r   createRegistrationToken.create.   s~    !"&	[[D[1F YLNN<0#__.N,,R0H 0SUV!WXN ((*G$y'HHN 
 |c-RS  w|>-RSKKM   ""

 #  	6qc:;MMO  ""

 #	
   ""

 #s*   CD# #
F-FF FF 8Gc                    [        5       nU(       d  g UR                  SS9nSnUR                  X045        UR                  5       nU(       aT  US   [        R
                  " 5       :  a8  UUR                  5       (       a!  UR                  5         UR                  5         $ $  UR                  5       (       a!  UR                  5         UR                  5         gg! [         aN  n[        SU 35         SnAUR                  5       (       a!  WR                  5         UR                  5         ggSnAff = f! UR                  5       (       a!  WR                  5         UR                  5         f f = f)zCFinds a token by its code and checks if it's valid and not expired.NTr   z1SELECT * FROM registration_tokens WHERE code = %s
expires_atzError finding token: )
r   r   r   r   r   nowr   r   r   r   )coder   r   querytokenr   s         r   find_by_codeRegistrationToken.find_by_codea   s'    !"	[[D[1FGENN5'*OO%E|,x||~=   ""

 # 
   ""

 #	  	)!-.  ""

 #		   ""

 #s*   AC 
D2$D-2D5 -D22D5 58E-c                 ,   [        5       nU(       d  g UR                  5       nSnUR                  X045        UR                  5          UR	                  5       (       a!  UR                  5         UR                  5         gg! [         aN  n[        SU 35         SnAUR	                  5       (       a!  WR                  5         UR                  5         ggSnAff = f! UR	                  5       (       a!  WR                  5         UR                  5         f f = f)z9Deletes a token from the database after it has been used.Fz/DELETE FROM registration_tokens WHERE code = %sTzError deleting token: Nr   r   r   r   r   r   r   r   r9   r   r   r:   r   s        r   deleteRegistrationToken.deletez   s     !"	[[]FEENN5'*KKM
   ""

 #	  	*1#./  ""

 #		   ""

 #)   4B   
C
CC CC 8Dc                  (   [        5       n U (       d  g U R                  SS9nSnUR                  U5        UR                  5       nUU R	                  5       (       a!  UR                  5         U R                  5         $ $ ! [         aN  n[        SU 35         SnAU R	                  5       (       a!  WR                  5         U R                  5         ggSnAff = f! U R	                  5       (       a!  WR                  5         U R                  5         f f = f)zIFetches all non-expired tokens with associated user and team information.NTr   ar  
                SELECT 
                    rt.code,
                    rt.pid,
                    rt.expires_at,
                    CONCAT(e.first_name, ' ', e.last_name) as staff_name,
                    ug.group_name as team_name
                FROM 
                    registration_tokens rt
                JOIN 
                    hris_core.employee_information_tbl e ON rt.pid = e.pid
                LEFT JOIN
                    intra.user_group ug ON rt.team = ug.GID
                WHERE 
                    rt.expires_at > NOW()
                ORDER BY
                    rt.expires_at ASC
            zError fetching active tokens: r   r   r   fetchallr   r   r   r   r   r   r:   tokensr   s        r   get_active_tokens#RegistrationToken.get_active_tokens   s     !"	[[D[1FE$ NN5!__&F
   ""

 #	  	21#67  ""

 #		   ""

 #)   3A> >
CCC CC 8Dc                 ,   [        5       nU(       d  g UR                  5       nSnUR                  X045        UR                  5          UR	                  5       (       a!  UR                  5         UR                  5         gg! [         aN  n[        SU 35         SnAUR	                  5       (       a!  WR                  5         UR                  5         ggSnAff = f! UR	                  5       (       a!  WR                  5         UR                  5         f f = f)z+Marks a token as having been sent by email.Fz=UPDATE registration_tokens SET email_sent = 1 WHERE code = %sTzError marking token as sent: Nr?   r@   s        r   mark_as_sentRegistrationToken.mark_as_sent   s     !"	[[]FSENN5'*KKM
   ""

 #	  	1!56  ""

 #		   ""

 #rC   c                  (   [        5       n U (       d  g U R                  SS9nSnUR                  U5        UR                  5       nUU R	                  5       (       a!  UR                  5         U R                  5         $ $ ! [         aN  n[        SU 35         SnAU R	                  5       (       a!  WR                  5         U R                  5         ggSnAff = f! U R	                  5       (       a!  WR                  5         U R                  5         f f = f)z=Fetches all tokens with associated user and team information.NTr   a  
                SELECT 
                    rt.code,
                    rt.pid,
                    rt.expires_at,
                    rt.email_sent,
                    CONCAT(e.first_name, ' ', e.last_name) as staff_name,
                    ug.group_name as team_name,
                    CASE
                        WHEN rt.expires_at > NOW() THEN 1
                        ELSE 0
                    END as is_active
                FROM 
                    registration_tokens rt
                JOIN 
                    hris_core.employee_information_tbl e ON rt.pid = e.pid
                LEFT JOIN
                    intra.user_group ug ON rt.team = ug.GID
                ORDER BY
                    rt.expires_at DESC
            zError fetching all tokens: rE   rG   s        r   get_all_tokens RegistrationToken.get_all_tokens   s     !" 	[[D[1FE* NN5!__&F
   ""

 #	  	/s34  ""

 #		   ""

 #rK    )N)__name__
__module____qualname____firstlineno__staticmethodr   r4   r<   rA   rI   rM   rP   __static_attributes__rR       r   r	   r	      s    # #J 0 0d  0  ( % %N  ( & &rY   r	   )	services.mysql_servicer   r   r   r"   r%   services.configr   r	   rR   rY   r   <module>r\      s     6 (  	 $l lrY   