
    iz                        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
mZ d dlmZmZ d dlmZ d dlZ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 ddlmZ  ej@                  e!      Z" G d de
jF                        Z$y)    )render)action)IsAuthenticatedAllowAny)Response)viewsetsstatus)SearchFilterOrderingFilter)DjangoFilterBackendN)CompanyCompanyBotSettings)BotType   )Appointment)AppointmentSerializer)AppointmentFilter) AppointmentLimitOffsetPaginationc                       e Zd Zej                  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y)AppointmentViewSet)nametwilio_call_sidcall__from_number)scheduled_dateappointment_phone
created_atr   z-created_atc                    | j                   j                  }|j                  r| j                  j	                  d      S |j
                  rO	 | j                  j                  |j
                  t        j                  j                        j	                  d      S | j                  j                         S # t        j                  $ r | j                  j                         cY S w xY w)Nz-scheduled_date)companybot_type)requestuseris_superuserquerysetorder_byactive_companyfilterr   	SALES_BOTvaluer   DoesNotExistnone)selfr!   s     @/var/www/html/dp2/backend_v2.1/server/apps/appointments/views.pyget_querysetzAppointmentViewSet.get_queryset   s    ||  ==))*;<<,}}++D4G4GRYRcRcRiRi+jss  uF  G  G }}!!## '' ,}}))++,s   AB3 3-C#"C#Fpostzbook-appointment)detailmethodsurl_pathpermission_classesauthentication_classesc           	      P   |j                   }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      j                  d      j                  d      }|j                  d      j                  d      }t        j                  j	                  |	      j                         }	t        j                  j                  |t        j                  j                  ||||	j                  d
       t        j                  d       t        j                  d|        t        j                  d|        t        j                  d|        t        j                  d|        t        ddit        j                         S )!Handle incoming webhook requests.argsr   scheduleoffTimecalltelephony_identifierr   	to_numberphone_numberN)r   r   r   offtimer   r   r9   4=-=-=-=-=-=-=-=Webhook data received:=-=-=-=-=-=-=-=Name: Scheduled Date: 
Off Time: Twilio Call SID: messageAppointment booked successfullyr	   )datagetr   objectsr&   firstr   creater   r'   r(   r   loggerinfor   r	   HTTP_200_OK)
r+   r    webhook_datar6   r   r   r>   r   r;   companyBotSettingss
             r,   book_appointmentz#AppointmentViewSet.book_appointment/   sd    ||'xx*-((9%&**62667MNRRSde $$V,00=	/77>>I>V\\^""&&,,)+&.. 	# 	
 	JKfTFO$&~&678j	*+''89:$EFvOaOabb    zbook-service-appointmentc           	      P   |j                   }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      j                  d      j                  d      }|j                  d      j                  d      }t        j                  j	                  |	      j                         }	t        j                  j                  ||t        j                  j                  |||	j                  d
       t        j                  d       t        j                  d|        t        j                  d|        t        j                  d|        t        j                  d|        t        ddit        j                         S )r5   r6   r   appointment_datetimer=   r9   r:   r   r;   r<   N)r   r   r   r   r   r   r9   r?   r@   rA   rB   rC   rD   rE   rF   )rG   rH   r   rI   r&   rJ   r   rK   r   SERVICE_BOTr(   r   rL   rM   r   r	   rN   )
r+   r    rO   r6   r   r   r   r   r;   rP   s
             r,   book_service_appointmentz+AppointmentViewSet.book_service_appointmentS   sh    ||'xx"89 HH^4&**62667MNRRSde $$V,00=	/77>>I>V\\^""/((..)+&.. 	# 	
 	JKfTFO$&~&678j!2 345''89:$EFvOaOabbrR   N)__name__
__module____qualname__r   rI   allr#   r   serializer_classr   r2   r   pagination_classr   r
   r   filter_backendsr   filterset_classsearch_fieldsordering_fieldsorderingr-   r   r   rQ   rV    rR   r,   r   r      s    ""&&(H,)*7*L.IO'ODMSOH$  5H' (z#%	cc< 5H/ (z#%	ccrR   r   )%django.shortcutsr   rest_framework.decoratorsr   rest_framework.permissionsr   r   rest_framework.responser   rest_frameworkr   r	   rest_framework.filtersr
   r   django_filters.rest_frameworkr   jsonloggingapps.companies.modelsr   r   apps.calls.constantsr   modelsr   serializersr   filtersr   
paginationr   	getLoggerrW   rL   ModelViewSetr   rb   rR   r,   <module>rt      sZ    # , @ , + ? =   = (  . & 8			8	$ac.. acrR   