
    ūi                        d dl Z d dlmZmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlm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 d dlmZ d dlmZ d dlmZmZm Z   e jB                  e"      Z# G d dejH                        Z%y)    N)viewsetsstatus)ValidationError)SearchFilterOrderingFilter)Response)action)IsAuthenticatedAllowAny)DjangoFilterBackend)BotType)AppointmentFilter)AppointmentServiceWebhookEventService) AppointmentLimitOffsetPagination)AppointmentRepository)AppointmentSerializerBookAppointmentInputSerializerWebhookSuccessSerializerc                       e Zd Z ej                         ZeZegZ	e
ZeeegZeZg dZg dZdgZd Z eddgdegg       d	        Z eddgd
egg       d        Z eddgdegg       d        Zy)AppointmentViewSet)nametwilio_call_sidcall__from_number)scheduled_dater   
created_atr   z-created_atc                    | j                   j                  }|j                  r| j                  j	                  d      S t        |dd       rO	 | j                  j                  |j                  t        j                  j                        j	                  d      S | j                  j                         S # t        $ r9}t        j                  |       | j                  j                         cY d }~S d }~ww xY w)Nz-scheduled_dateactive_company)companybot_type)requestuseris_superuserquerysetorder_bygetattrfilterr   r   	SALES_BOTvalue	Exceptionloggerinfonone)selfr"   excs      M/var/www/html/dp2/backend_v2.1/server/apps/appointments/views/appointments.pyget_querysetzAppointmentViewSet.get_queryset)   s    ||  ==))*;<<4)40,MMV $ 3 3!(!2!2!8!8   X/0 }}!!##	  ,C }}))++,s   AB4 4	C6=.C1+C61C6Fpostzbook-appointment)detailmethodsurl_pathpermission_classesauthentication_classesc                    t        j                  |dd      }t        |j                        }|j	                  d       	 t        j                  |j                         |r|j                          t        t!        ddi      j                  t        j"                  	      S # t        $ rP}t        j                  d|       |r|j                  |       t        ddit        j                  	      cY d
}~S d
}~ww xY w)z1
        Endpoint for sales bot webhook.
        retellzBook Sales AppointmentdataTraise_exceptionz$Failed booking sales appointment: %smessageFailed to book appointmentr   NAppointment booked successfully)r   	log_eventr   r;   is_validr   book_sales_callbackvalidated_datamark_successr*   r+   	exceptionmark_failedr   r   HTTP_500_INTERNAL_SERVER_ERRORr   HTTP_200_OKr.   r!   event
serializerr/   s        r0   book_appointmentz#AppointmentViewSet.book_appointment@   s     $--$
 4F
D1	22:3L3LM""$ $=>d6--
 	
  	CSI!!#&89<< 		s   1B" "	C;+AC60C;6C;zbook-service-appointmentc                 &   t        j                  |dd      }t        |j                        }|j	                  d       t
        j                  d       t
        j                  |j                         	 t        j                  |j                         |r|j                          t        t+        ddi      j                  t         j,                  	      S # t        $ rZ}t
        j                  d|       |r|j                  |       t        d|j                  it         j"                  	      cY d
}~S d
}~wt$        $ rP}t
        j'                  d|       |r|j                  |       t        ddit         j(                  	      cY d
}~S d
}~ww xY w)z3
        Endpoint for service bot webhook.
        r9   zBook Service Appointmentr:   Tr<   z Payload for appointment booking:z(Validation error booking appointment: %sr3   r@   Nz&Failed booking service appointment: %sr>   r?   rA   )r   rB   r   r;   rC   r+   r,   r   book_service_appointmentrE   rF   r   warningrH   r   r3   r   HTTP_400_BAD_REQUESTr*   rG   rI   r   rJ   rK   s        r0   rP   z+AppointmentViewSet.book_service_appointmentg   sU    $--&
 4F
D167GLL!	77
8Q8QR""$, $=>d6--
 	
)  	NNEsK!!#&3::&22   
	8 !!#&89<< 
	s2   51C 	FAD4.F4F AFFFzweek-dayc                 
   t        j                  |dd      }	 |st        ddit        j                        S |j
                  j                  di       j                  dd      }t        j                  |      }|j                          t        d|it        j                        S # t        $ rS}t        j                  d	|       |r|j                  |       t        dd
| it        j                        cY d}~S d}~ww xY w)z0
        Endpoint for week day webhook.
        r9   zGet Week Dayr>   z(Unable to process request as empty eventr@   argsbooking_datetimeNzFailed to get week day: %szFailed to get week day week_day)r   rB   r   r   rI   raw_payloadgetr   get_week_dayrF   r*   r+   rG   rH   rJ   )r.   r!   rL   rU   rV   r/   s         r0   rV   zAppointmentViewSet.week_day   s    $--
	 JK!@@ 
  %0044VR@DDEWY]^)667GHH  "%%
 	
  	93?!!#&5cU;<<< 		s$   B& AB& &	D/AC=7D=DN)__name__
__module____qualname__r   r1   r$   r   serializer_classr
   r6   r   pagination_classr   r   r   filter_backendsr   filterset_classsearch_fieldsordering_fieldsorderingr	   r   rN   rP   rV        r0   r   r      s    1$113H,)*7*L.IO'ODMO H$. #$:!

@ +$:!.
.
` $:! 
 
re   r   )&loggingrest_frameworkr   r   rest_framework.exceptionsr   rest_framework.filtersr   r   rest_framework.responser   rest_framework.decoratorsr	   rest_framework.permissionsr
   r   django_filters.rest_frameworkr   apps.calls.constantsr   apps.appointments.filtersr   apps.appointments.servicesr   r   apps.appointments.paginationr   apps.appointments.repositoriesr   apps.appointments.serializersr   r   r   	getLoggerrZ   r+   ModelViewSetr   rd   re   r0   <module>rv      s^     + 5 ? , , @ = ( 7 N I @  
		8	$l
.. l
re   