
    Di                         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
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 d dlmZ  e j8                  e      Z G d d      Zy)    N)ListSet)settings)transaction)AppointmentNotificationContext)in_app_message_templatesms_templatebooking_email_context)MessageRepository)MessageCreateSerializer)
SmsService)NotificationService)EmailService)UserRepository)ProfileServicesc                       e Zd ZdZed        Zed        Zed        Zede	e
   f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	                  |      }t        j                  |j                        }| j                  |||      }|s| j                  ||dd|d           y t        j                         5  | j                  ||j                  |      }d d d        | j                  ||j                         | j                  |       | j                  ||t        j                   |      d	       t         j#                  d
|j$                         |S # 1 sw Y   xY w)Nz7AppointmentNotificationService.process called with None)appointmentextracompany)r   	recipientservice_managerszNo Default Transfer StaffzNo Default Transfer Staff for r   )userzNew Appointment Bookingz6Appointment notifications processed for appointment %s)loggererrorr   build_get_recipientr    get_service_managers_for_companyr   _collect_emails_send_emailr   atomic_create_message_send_in_app_notification	_send_smsr   resolve_display_nameinfoid)clsr   r   ctxr   r   emailsmessages           d/var/www/html/dp2/backend_v2.1/server/apps/appointments/services/appointment_notification_service.pyprocessz&AppointmentNotificationService.process!   sA   LLRS,22#
 &&{3	)JJ''
 $$#- % 
 OO+0Y0@A	 !)))[5H5H#NG " 	%%i1D1DgNc00i@%		
 	DNN	

 # "!s   &EE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_recipientS   s>    ''>>	NN89O9O8PQ     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   r6   r7   subjectbody)dataT)raise_exceptionr   zMessage created (id=%s) )	r   r)   r   is_validr   create_messagevalidated_datar   r(   )r   r   r+   r9   payload
serializerr-   s          r.   r$   z.AppointmentNotificationService._create_message]   s    &s+ # 1"#45,
 -':
D1#22 

''

 	%JJ	

 r4   returnc                    t               }t        j                  r|j                  t        j                         t	        j
                  | j                        }|r|j                  d |D               |r'|j                  r|j                  |j                         |r|j                  d |D               |D cg c]&  }|r"d|v r|j                         j                         ( }}t        t        |            S c c}w )Nr   c              3   N   K   | ]  }|j                   s|j                     y wNemail).0r   s     r.   	<genexpr>zAAppointmentNotificationService._collect_emails.<locals>.<genexpr>   s      ':tdjj

':   %%c              3   N   K   | ]  }|j                   s|j                     y wrE   rF   )rH   managers     r.   rI   zAAppointmentNotificationService._collect_emails.<locals>.<genexpr>   s      -='-=rJ   @)setr   %ALL_DEALERSHIPS_BOOKING_NOTIFY_EMAILSupdater   #get_booking_email_users_for_companyr   rG   addstriplowerlist)r   r   r   r,   booking_email_usersrG   cleaneds          r.   r!   z.AppointmentNotificationService._collect_emailsx   s    599MM(HHI,PP''
 MM ':  JJy'MM -=   
 KKM! 	 
 CL!!
s   ;+C<c                     	 t        j                  | ||j                  |j                         y # t        $ r"}t
        j                  d|        Y d }~y d }~ww xY w)N)r   r   titler-   zIn-app notification failed )r   send_message_notificationr8   r9   	Exceptionr   	exception)r   r   r-   excs       r.   r%   z8AppointmentNotificationService._send_in_app_notification   sW    	B99oo	  	B:3%@AA	Bs   -0 	AAAc                     	 t        |       }t        j                  | d   |       y # t        $ r"}t        j                  d|        Y d }~y d }~ww xY w)N
bdc_number)	to_numberr-   zSMS enqueue failed )r	   r   send_smsr[   r   r\   )r+   sms_textr]   s      r.   r&   z(AppointmentNotificationService._send_sms   sU    	:#C(Hl+   	:23%899	:s   %( 	AAAc           
          	 t        j                  |d|t        j                  t	        | |             y # t
        $ r"}t        j                  d|        Y d }~y d }~ww xY w)Nzappointment/booking_1.html)r8   html_contentrecipient_listbcckeyzEmail enqueue failed )r   
send_emailr   	EMAIL_BCCr
   r[   r   r\   )r+   r,   	full_namer8   r]   s        r.   r"   z*AppointmentNotificationService._send_email   s\    		<##9%&&)#y9  	<4SE:;;	<s   25 	A AA N)__name__
__module____qualname____doc__classmethodr/   staticmethodr   r$   r   strr!   r%   r&   r"   r<   r4   r.   r   r      s     / /b    4 "T#Y " "> 
B 
B 
: 
: < <r4   r   ) loggingtypingr   r   django.confr   	django.dbr   /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.core.repositoriesr   apps.userprofile.servicesr   	getLoggerrk   r   r   r<   r4   r.   <module>r      sS        ! Z 
 6 : = H : 1 5			8	$f< f<r4   