
    ,eh                         d dl m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mZmZ d dlmZmZ d dlmZ d dlZd d	lmZmZ d dlZd d
lmZ d dlmZmZ d dl
mZ d dlmZmZ d dlZd Zd Z d Z!d Z"y)    )process_booking_intent)get_speech_to_text)BookingIntentCronJobStatusTwilioCallStatus)get_db)CallsLogAdvisorLoginLog)get_call_recording_public_urlget_call_status)SessionN)datetimedate)send_sms)logout_advisorset_user_offline)AppointmentScheduling)r   	timedeltac                  >    t        j                  t                      y)z>Wrapper function to run the async process_booking_intent_asyncN)asynciorunprocess_booking_intent_async     9/var/www/html/DP/alpha_backend/app/scheduler/cron_jobs.py$process_booking_intent_async_wrapperr      s    KK,./r   c                     K   t        t                     } 	 | j                  t              j	                  t        j
                  t        j                  k(        j                  d      j                         }|D ]@  }	 t        |j                        }|t        j                  k(  r-t        ||        d {    B 	 | j3                          y 7 # t        $ r}| j                  t              j	                  t        j                   |j                   k(        j#                         }t        j$                  |_        | j'                          | j)                  |       t+        t-        j.                                t+        dt1        |              Y d }~d }~ww xY w# t        $ r?}t+        t-        j.                                t+        dt1        |              Y d }~$d }~ww xY w# | j3                          w xY ww)N2   z!Error processing booking intent: )nextr   queryr	   filterbooking_intent_statusr   PENDINGlimitallr   twilio_call_idr   IN_PROGRESSr   	ExceptionidfirstFAILEDcommitrefreshprint	traceback
format_excstrclose)dbcallscallcall_statusecall_logs         r   r   r      sv    	fhB"))**m.C.CC

%)CCE 	 D-d.A.AB"2">">>,T2666 & 	
 7 88H-44X[[DGG5KLRRT1>1E1E.		

8$i**,-9#a&BC  <i""$%1#a&:;;< 	
s   G>A%F =(C%F &C5C6C:F <G) =G>C	FB7FF FF 	G&'4G!G) !G&&G) )G;;G>c                     t        t                     } 	 t        dd       | j                  t              j                         }|D ]2  }t        |j                  |        t        d|j                   d       4 	 | j                          y # t        $ r#}t        dt        |       d       Y d }~8d }~ww xY w# | j                          w xY w)NzLogging out all advisorsTflushzLogged out advisor z Error logging out all advisors: )r    r   r/   r!   r
   r&   r   r*   r)   r2   r3   )r4   advisorsadvisorr8   s       r   logout_all_advisorsr?   3   s    	fhB	(588G$((*G7::r*'

|4DA   	
  G0Q9FFG 	
s0   A'B <B= 	B:B50B= 5B::B= =Cc                  ,   t        t                     } 	 t        d       t        j                         j                  dd      }|t        d      z   }| j                  t              j                  t        j                  |k(  t        j                  dk(        j                         }t        ||d|       |D ]  }t        |j                  d|       d	}t        d
|j                   d|j                   d       t        |j                  |t        j                   d             d|_        | j#                           	 | j)                          y # t$        $ r#}t        dt'        |       d       Y d }~8d }~ww xY w# | j)                          w xY w)Nzcron Jobr   )secondmicrosecond   )minutesFz==55555555zHi, this is Hazel from SurfCity Nissan. Just a quick reminder about your scheduled callback with our sales team in 30 minutes. We look forward to speaking with you!z[Reminder] Sending SMS to z for appointment at Tr;   TWILIO_FROM_NUMBER)	to_numbermessagefrom_numberz%Error sending appointment reminders: )r    r   r/   r   utcnowreplacer   r!   r   r"   schedulereminder_sentr&   phoner   osgetenvr-   r)   r2   r3   )r4   nowtarget_timeappointmentsappointmentrH   r8   s          r   send_appointment_remindersrU   A   sc   	fhB+joo''qa'@Ib11 xx 56==!**k9!//58
 #% 	" 	lS45'K+&&
S9e  .{/@/@.AAUVaVjVjUkltxy%++II&:;
 )-K%IIK (& 	
  L5c!fX>dKKL 	
s0   D+E  F 	E>E94F 9E>>F F)#,app.advisor_service.services.advisor_servicer   app.common.ai_helpersr   app.common.constantsr   r   r   app.common.databaser   app.common.modelsr	   r
   r   app.common.twillio_helperr   r   sqlalchemy.ormr   r   r   r   r0   *app.twilio_service.services.twilio_servicer   "auth_service.services.auth_servicer   r   r   r   rO   r   r   r?   rU   r   r   r   <module>r_      sL    O 4 O O & 9 9 T "  #  ? O 3 ( 	0:-r   