
    /g                         d Z ddlZddlZddlmZ  eej
                  j                  d            Zej                  j                  e       ddl
mZmZ d Zd Zded	efd
Zy)z&
module for db manipulation functions
    N)datetimeROOT_DIR_PHONETRACK_API)mydbmysqlc                     d} 	 t        j                         }|j                  |        t        j                          	 |j                          y# t        j
                  j                  $ r}t        |      |d}~wt        j
                  j                  $ r}t        |      |d}~wt        $ r}t        |      |d}~ww xY w# j                          w xY w)z2
    function to create table if not existent
    a;  
        CREATE TABLE IF NOT EXISTS wp_last_clients_calls_update (
            id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            client_name VARCHAR(150) NOT NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci
    N)r   cursorexecutecommitr   	connectorInterfaceErrorRuntimeErrorDatabaseError	Exceptionclose)sqlmycursorerrs      F/var/www/html/phonetrack_api_data_manager/app/utils/db/db_functions.py$should_create_last_client_call_tabler      s    
C;;= 	 ??)) )3S(??(( )3S( )3S() 	s;   9A C,A88 CB$$C0B<<CC Cc                    t                d}	 t        j                         }|j                  || |g       t        j                          t        d       	 |j                          y# t        j                  j                  $ r}t        |      |d}~wt        j                  j                  $ r}t        |      |d}~wt        $ r}t        |      |d}~ww xY w# j                          w xY w)z8
    function to insert into last_client_call table
    z
        INSERT INTO wp_last_clients_calls_update (
            client_name,
            created_at
        )
        VALUES (
            %s,
            %s
        )
    z+last_client_call table updated successfullyN)r   r   r   r	   r
   printr   r   r   r   r   r   r   )client_namedater   r   r   s        r   insert_last_client_call_tabler   2   s     )*	C;;={D12;< 	 ??)) )3S(??(( )3S( )3S() 	s<   AA' 'CB C0B<<CCCC C.r   r   c                    d}|r| d}| d}|d| d| dz  }|dz  }	 t        j                         }|j                  || f       |j                         }|r|d   nt	        d	d
d
      f	 |j                          S # t        j                  j                  $ r}t        |      |d}~wt        j                  j                  $ r}t        |      |d}~wt        $ r}t        |      |d}~ww xY w# j                          w xY w)z
    function to get the last call date for specific client

    Args:
        client_name: str = client name

    Returns:
        timestamp stringfied
    zh
        SELECT created_at
        FROM wp_last_clients_calls_update
        WHERE client_name = %s
    z	T00:00:00z	T23:59:59zAND created_at BETWEEN 'z' AND 'z' zORDER BY created_at DESCr   i     N)r   r   r	   fetchallr   r   r   r   r   r   r   r   )r   r   r   
date_startdate_endr   datar   s           r   get_last_client_call_dater!   T   s   C
 vY'
V9%)*WXJbII%%C;;={n-  "tAwXdAq%9$;; 	 ??)) )3S(??(( )3S( )3S() 	s<   AA> >C0B'' C0CC0C++C00C3 3D)__doc__ossysr   strenvironget	ROOT_PATHpathappendapp.utils.db.db_connectionr   r   r   r   r!        r   <module>r.      s`    
 
 

89:	 	 >D&3 &c &r-   