
    ,ehU                         d dl Z d dlmZ d dlmZ d dlmZ d dlmZ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  G d
 de      Zy)    N)
send_email)get_twillio_client)BaseRepo)AdvisorSystemFailureLogs)datetime	timedeltatimezone)send_sms)get_app_url)SeverityLevelc            	            e Zd Z fdZddedededefdZddedededefdZddededefdZd	 Z	dededefd
Z
dededefdZdededefdZd Zd Zd Z xZS )SystemFailureRepoc                 $    t         |   |       y N)super__init__)selfdb	__class__s     F/var/www/html/DP/alpha_backend/app/repositories/system_failure_repo.pyr   zSystemFailureRepo.__init__   s        	componentseveritymessagetwilio_call_idc                 ,   | j                   j                  t              j                  t        j                  |k(  t        j
                  |k(  t        j
                  j                  d             j                         }|r|S | j                  ||||      S r   )	r   queryr   filterr   r   isnotfirst
create_logr   r   r   r   r   logs         r   create_unique_logz#SystemFailureRepo.create_unique_log   s{    ggmm-.55''94,,>,,2248
 %'	 	
 J??9hPPr   c                 d    | j                  t        ||||d      }| j                  |||       |S )N)r   r   r   r   )data)creater   check_for_alertr#   s         r   r"   zSystemFailureRepo.create_log   sB    kk+	W_ls  HV  3Wk  XXy':
r   c                    t        j                  t        j                        t	        d      z
  }| j
                  j                  t              j                  t        j                  |k\  t        j                  |k(  t        j                  |k(        j                         }t        d|        |dk\  r9|dk(  r | j                          | j                          | j!                  |||       y y )N
   )minuteszRecent logs:    transfer_call)r   nowr
   utcr	   r   r   r   r   
created_atr   r   countprintswitch_to_ultravoxsend_ultravox_switch_alertsend_system_failure_alert)r   r   r   r   ten_minutes_agorecent_logss         r   r)   z!SystemFailureRepo.check_for_alert!   s    ",,x||4y7LLggmm$56==((O;&&(2''94
 %'	 	 	k]+,!O+'')//1**9hH r   c                    t               }t        j                  d      }t        j                  d      }|j                  j	                  |      d   }|j
                  }t                d}|j                  |      j                  |dt                dd      }|j                  j                  j                  |      j                  |      j                          y )	NTWILIO_NUMBERTRUNK_ID)phone_numberr   z'/advisor/api/v1/twilio/incoming-webhookPOSTz+/advisor/api/v1/twilio/call-status-callback)	voice_urlvoice_methodstatus_callbackstatus_callback_method)r   osgetenvincoming_phone_numberslistsidr   updatetrunkingv1trunksphone_numbersdelete)r   clienttwilio_numbertrunkIdnumber
number_sidr>   r<   s           r   r4   z$SystemFailureRepo.switch_to_ultravox2   s    #%		/2))J'..	=	)!- ZZ
"}o%LM	44Z@GG*}o-XY#)	 H 
 	!!'*88DKKMr   c                 P    | j                  |||       | j                  |||       y r   )send_system_failure_emailsend_system_failure_sms)r   r   r   r   s       r   r6   z+SystemFailureRepo.send_system_failure_alertF   s&    &&y(GD$$Y'Br   c                 6   g }|j                  d       |j                  d       |j                  d       dj                  |      }d}d| dt        |      j                   d| d	}d
| dt        |      j                   d||nd d}t	        ||||       y)z=
        Send email notification for system failures
        shahzad@fluten.aismalik@fluten.aimimj1800@gmail.com, DealerPulse System Failure AlertzlDear Team,

A system failure has been detected in DealerPulse that needs your urgent attention:

Component: z
Severity: z

Message: zz

Please investigate this issue as soon as possible and take necessary actions to resolve it.

Regards,
DealerPulse Systemz<html>
<body>
<p>Dear Team,</p>
<p>A system failure has been detected in DealerPulse that needs your urgent attention:</p>
<p><strong>Component:</strong> z <br>
<strong>Severity:</strong> z<br>
<strong>Message:</strong> NzN/Az</p>
<p>Please investigate this issue as soon as possible and take necessary actions to resolve it.</p>
<p>Regards,<br>
DealerPulse System</p>
</body>
</html>	recipientsubjectbody	html_body)appendjoinr   namer   )	r   r   r   r   manager_emailsrecipient_emailsr]   
email_bodyr_   s	            r   rS   z+SystemFailureRepo.send_system_failure_emailJ   s    
 12012388N34 ; "''
( )

 
  !*{ +)(3889 :&-&97uE F	 	&		
r   c           
          ddg}|D ]?  }t        |d| dt        |      j                   d| t        j                  d             A y )N+923017357180+923334046969zSystem failure alert:  TWILIO_FROM_NUMBER	to_numberr   from_number)r   r   rb   rB   rC   )r   r   r   r   
to_numbersrP   s         r   rT   z)SystemFailureRepo.send_system_failure_smsx   sS    %7
 F 01]8=T=Y=Y<ZZ[\c[deII&:; !r   c                 D    | j                          | j                          y r   )send_ultravox_switch_emailsend_ultravox_switch_sms)r   s    r   r5   z,SystemFailureRepo.send_ultravox_switch_alert   s    '')%%'r   c                     g }|j                  d       |j                  d       |j                  d       dj                  |      }d}d}d}t        ||||       y	)
z=
        Send email notification for ultravox switch
        rV   rW   rX   rY   rZ   z}Dear Team,

Due to multiple system failures, system has been automatically switched to ultravox.

Regards,
DealerPulse Systemz<html>
<body>
<p>Dear Team,</p>
<p>Due to multiple system failures, system has been automatically switched to ultravox.</p>
<p>Regards,<br>
DealerPulse System</p>
</body>
</html>r[   N)r`   ra   r   )r   rc   rd   r]   re   r_   s         r   rp   z,SystemFailureRepo.send_ultravox_switch_email   sn    
 12012388N34
	 	&		
r   c                 \    ddg}|D ]#  }t        |dt        j                  d             % y )Nrg   rh   zSSystem has been automatically switched to ultravox due to multiple system failures.rj   rk   )r   rB   rC   )r   rn   rP   s      r   rq   z*SystemFailureRepo.send_ultravox_switch_sms   s1    %7
 F mII&:; !r   )NNr   )__name__
__module____qualname__r   strintr%   r"   r)   r4   r6   rS   rT   r5   rp   rq   __classcell__)r   s   @r   r   r      s    	Q3 	Q# 	Q 	Qdg 	QC 3  ]` 
I I Ic I"N(C# C Cs C,
3 ,
# ,
PS ,
\  c ("
Hr   r   )rB   !app.common.services.email_servicer   app.common.twillio_helperr   app.repositories.base_repor   app.common.modelsr   r   r   r	   r
   *app.twilio_service.services.twilio_servicer   app.utils.helpers.commonr   app.common.constantsr   r    r   r   <module>r      s1    	 8 8 / 8 2 2 ? 0 .d dr   