
    ^hC                         d dl m Z mZ d dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlZd dlZ ej                  e      Z G d de      Zy)	    )datetimetimezoneN)AdvisorNotifications)BaseRepo)send_sms)get_settingc                   &     e Zd Z fdZddZ xZS )AdvisorRepoc                 $    t         |   |       y N)super__init__)selfdb	__class__s     ?/var/www/html/DP/alpha_backend/app/repositories/advisor_repo.pyr   zAdvisorRepo.__init__   s        c                    |y |j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }t        | j                  d      }t        |t              r'|j                  d      r|j                  d      r|dd	 }t        j                  |      }d
}		 |D ]A  }
| j                  j                  t              j                  t        j                  |
k(        j                         }|r|j                  nd }d| d| d| d| d| 
}|rt        |j                   |j                  |d||d| d| ddt#        j$                  t&        j(                        t#        j$                  t&        j(                              }| j                  j+                  |       | j                  j-                          t/        ||t1        j2                  d             |	dz  }	D y # t4        $ r>}t7        t9        j:                                t7        dt	        |              Y d }~y d }~ww xY w)Ncustomer_namecustomer_phonetransfer_target_nametransfer_target_phonecall_idMISSING_AGENT_INFORM"   r   zMissed Call Alert! A customer,  z , called and was transferred to zt,but the call went unanswered. Kindly return the customer's call at your earliest convenience.
DealerPulse
Call ID: missed_callzMissed Call from z%You have recieved a missed call from z,. Please call them back as soon as possible.F)user_id
advisor_idr   notification_typer   r   titlemessageis_read
created_at
updated_atTWILIO_FROM_NUMBER)	to_numberr%   from_numberz+Failed to send SMS to any of the advisors: )getr	   r   
isinstancestr
startswithendswithastliteral_evalqueryr   filteridfirstphone_numberr   r!   r   nowr   utcaddcommitr   osgetenv	Exceptionprint	traceback
format_exc)r   customer_detailsr   r   r   r   r   notification_phones_idadvisor_idssuccess_countr"   advisoradvisor_numberr%   notificationes                   r   send_missed_call_alertz"AdvisorRepo.send_missed_call_alert   sZ   #(,,_=)--.>?/334JK 0 4 45L M"&&y1!,TWW6L!M,c27M7X7XY\7]bx  cB  cB  CF  cG%;Ab%A"&&'=>%	J)
''--077

j8PQWWY9@!5!5d 6m_AnEUUu  wK  vL  LM  Nc  Md d" #*,  #0 '#*:: '*7'5&3 1.1AB"GGW  XD  !E %#+<<#=#+<<#=$L GGKK-GGNN$,# "		*> ?
 "A *D  	J)&&()?AxHII	Js   >EH 	I4IIr   )__name__
__module____qualname__r   rJ   __classcell__)r   s   @r   r   r      s    5Jr   r   )r   r   loggingr@   app.common.modelsr   r   app.repositories.base_repor   *app.twilio_service.services.twilio_servicer   &auth_service.services.settings_servicer	   r<   r1   	getLoggerrK   loggerr    r   r   <module>rW      s@    '   4 / ? > 	 
			8	$:J( :Jr   