
    ;i>                         d dl 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	d dl
mZ  ej                  e      Z G d d	      Zy)
    )MessageRepository)MessageCreateSerializer   )
SmsService)NotificationServiceN)timezonec                   N    e Zd Zedd       Zed        Zed        Zed        Zy)MessageServiceNc                     |t        j                         }| j                  r|j                  d      S t        j                  |       j                  d      S )Nz-created_at)r   base_querysetis_superuserorder_bylist_for_user)userqss     L/var/www/html/dp2/backend_v2.1/server/apps/calls/services/message_service.pylist_messageszMessageService.list_messages   sI    :"002B;;}-- ..t4==mLL    c                 .    t        j                  ||       S N)r   	get_by_id)r   msg_ids     r   get_messagezMessageService.get_message   s     **6488r   c                 ^    t         j                  | |      }|y t        j                  |      S r   )r
   r   r   mark_as_read)r   r   msgs      r   r   zMessageService.mark_as_read   s-    ((v6; --c22r   c           	         | t         j                  d       y| j                  j                  }|!t         j                  d| j                         y	 t        j                  d      }t        j                  | j                  |      }|j                  d      }|j                  d      }d	| d
| d| j                   }|j                  | j                  | j                  d|d}t        |      }	|	j!                  d       t#        j$                  di |	j&                  }
t         j)                  d|
j                  | j                         t+        j,                  ||	j&                  d   |	j&                  d          t         j)                  d|j                  |
j                         d| j                   d| j                   d| d
| d	}t/        j0                  | j                  j2                  |       t         j)                  d||       |
S # t        $ rC}t         j                  d| j                  |       t        | j                        }d}Y d}~d}~ww xY w)zO
        Creates a message for the appointment and sends notification.
        Nu-   Cannot create message — appointment is Nonez+Recipient user not found for appointment %szAmerica/Los_Angelesz%Y-%m-%dz%I:%M %pz-Failed converting time for appointment %s: %s z'New appointment created by Maya.
Date: z
Time: z Pacific Time
Name: zAppointment Message)	recipientcustomer_namecustomer_numbersubjectbody)dataT)raise_exceptionz/Message created (id=%s) for appointment (id=%s)r"   r#   )r   titlemessagez5Notification sent to user (id=%s) for message (id=%s)z!New Appointment from Maya:
Name: z
Phone: z
Date: z Pacific Time)	to_numberr'   z&date and time of appointment are %s %s )loggererrorcompanydefault_transfer_staffidpytzr   	localtimescheduled_datestrftime	Exception	exceptionstrnameappointment_phoner   is_validr   create_messagevalidated_datainfor   send_message_notificationr   send_sms
bdc_number)appointmentr   pacificscheduled_dtdate_strtime_strexcr#   payload
serializerr'   sms_texts               r   handle_appointment_messagez)MessageService.handle_appointment_message%   sG    LLHI''>>	LLFW
	mm$9:G#--k.H.H'RL#,,Z8H#,,Z8HJ J  %%&( 	 #(--*<<,
 -':
D1#22OZ5N5NO=JJNN	

 	55++I6--f5	

 	CLLJJ	
 %%& '!334 5J Jm	- 	 	!))44	

 	<hQq  	Lknn^ab;556HH	s   AH 	I8IIr   )__name__
__module____qualname__staticmethodr   r   r   rH   r)   r   r   r
   r
      sT    M M 9 9 3 3 O Or   r
   )apps.calls.repositoriesr   apps.calls.serializersr   twilio_sms_servicer   notification_servicer   loggingr/   django.utilsr   	getLoggerrI   r*   r
   r)   r   r   <module>rT      s:    5 : * 5   ! 
		8	$i ir   