
    |i                         d dl Z 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 d dlmZ d dlmZ d d	lmZ d d
lmZ  e j*                  e      Z G d d      Zy)    N)settings)AppointmentNotificationContext)in_app_message_templatesms_templatebooking_email_context)MessageRepository)MessageCreateSerializer)
SmsService)NotificationService)EmailService)ProfileServicesc                   p    e Zd ZdZed        Zed        Zed        Zed        Z	ed        Z
ed        Zy)	AppointmentNotificationServicez
    Orchestrates all communications triggered by appointment creation.
    - In-app message
    - Notification
    - SMS (async)
    - Email (async)
    c                    |st         j                  d       y t        j                  |      }| j	                  |      }|s8t
        j                  }d}d|d    }| j                  |t        |      ||       y | j                  ||j                  |      }| j                  ||j                  |       | j                  |       t        j                  |      }d}t
        j                  }|r'|j                  r|j!                  |j                         | j                  |t        |      ||       t         j#                  d|j$                         |S )Nz7AppointmentNotificationService.process called with NonezNo Default Transfer StaffzNo Default Transfer Staff for company)userzNew Appointment Bookingz6Appointment notifications processed for appointment %s)loggererrorr   build_get_recipientr   EMPTY_RECIPIENT_NOTIFY_EMAILS_send_emaillist_create_messager   _send_in_app_notification	_send_smsr   resolve_display_nameemailappendinfoid)clsappointmentctx	recipientemails	full_namesubjectmessages           d/var/www/html/dp2/backend_v2.1/server/apps/appointments/services/appointment_notification_service.pyprocessz&AppointmentNotificationService.process   s   LLRS,22;?&&{3	;;F3I6s9~6FGGOOCfy'B%%i1D1DcJ%%i1D1DgNc#88iH	+77MM)//*T&\9g?DNN	

     c                 |    | j                   j                  }|s#t        j                  d| j                          y |S )Nz&No default_transfer_staff for company )r   default_transfer_staffr   warning
company_id)r#   r%   s     r*   r   z-AppointmentNotificationService._get_recipientD   s>    ''>>	NN89O9O8PQ r,   c                 
   t        |      }| j                  |d   |d   d|d}t        |      }|j                  d       t	        j
                  d
d|i|j                  }t        j                  d	|j                         |S )Ncustomer_namecustomer_numberzAppointment Message)r%   r2   r3   r(   body)dataT)raise_exceptionr   zMessage created (id=%s) )	r   r!   r	   is_validr   create_messagevalidated_datar   r    )r%   r   r$   r4   payload
serializerr)   s          r*   r   z.AppointmentNotificationService._create_messageN   s    &s+ # 1"#45,
 -':
D1#22 

''

 	%JJ	

 r,   c                 ^    t        j                  | ||j                  |j                         y )N)r   r   titler)   )r   send_message_notificationr(   r4   )r%   r   r)   s      r*   r   z8AppointmentNotificationService._send_in_app_notificationi   s$    55//LL		
r,   c                 N    t        |       }t        j                  | d   |       y )N
bdc_number)	to_numberr)   )r   r
   send_sms)r$   sms_texts     r*   r   z(AppointmentNotificationService._send_smsr   s%    $,'	
r,   c           	      P    |sy t        j                  |d|t        | |             y )Nz$appointment/appointment_booking.html)r(   html_contentrecipient_listkey)r   
send_emailr   )r$   r&   r'   r(   s       r*   r   z*AppointmentNotificationService._send_email{   s)    ?!%c95		
r,   N)__name__
__module____qualname____doc__classmethodr+   staticmethodr   r   r   r   r   r7   r,   r*   r   r      s     # #J    4 
 
 
 
 	
 	
r,   r   )loggingdjango.confr   /apps.appointments.services.notification_contextr   2apps.appointments.templates.notification_templatesr   r   r   apps.calls.repositoriesr   apps.calls.serializersr	   &apps.calls.services.twilio_sms_servicer
   (apps.calls.services.notification_servicer   !apps.calls.services.email_servicer   apps.userprofile.servicesr   	getLoggerrJ   r   r   r7   r,   r*   <module>r[      sL       Z 
 6 : = H : 5 
		8	$p
 p
r,   