
    NhO                        d dl 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 d dlZd d	lmZ d d
lmZ ddlT d dlmZ  ej        e          Z ej        d          d             Z ej        d          d             Z ej        d          d             Z ej        d          dd            Z ej        d          dd            ZdS )    N)now)Responsestatus)render_to_string)EmailMessage)settings)app)	messaging)firebase_app   )*)	OrderItemsend_push_notification_task)namec           	      v   	 t          j        t          j        ||          |           }t          j        |          }t                              d|  d|            d| S # t          $ rI}t                              d|  dt          |                      dt          |           cY d }~S d }~ww xY w)N)titlebody)notificationtokenzPush notification sent to : z	Success: zPush notification failed for zError: )	r   MessageNotificationsendloggerinfo	Exceptionerrorstr)	fcm_tokenr   messagemsgresponsees         ,/var/www/html/e360mart/notification/tasks.pyr   r      s    
""/e'JJJ
 
 
 >#&&HHHhHHIII%8%%% " " "JYJJ#a&&JJKKK!Q!!!!!!!!"s   A"A% %
B8/>B3-B83B8!send_admin_notification_to_buyersc           	         	 t           j                            |           }n7# t           j        $ r% d|  d}t                              |           |cY S w xY wt          j                            d          }t                              d|j	         d|
                                 d           g }|j	        }|j        }|D ]U}t                              |j        ||	           |                    t!          ||t#                      
                     Vt           j                            |           t                              dt'          |           d|  d           dt'          |           dS )NidzNotification ID z not found.Tr   zSending notification 'z' to z active buyer devices.r    r   r!   )r   devicesent_atz!Notification history created for z devices (Notification ID: )zQueued push notifications for z buyer devices.)r   objectsgetDoesNotExistr   r   Devicefilterr   r   countdescriptionr   delay	device_idappendNotificationHistoryr   bulk_createlen)notification_idr   	error_msgactive_deviceshistory_entriesr   r!   r+   s           r%   r&   r&   "   s   #+//?/CC$   CCCC	Y
 ^**$*77N
KKp);pp.BVBVBXBXpppqqqOE&G   #))& 	* 	
 	
 	
 	2% EE 
  
  
 	 	 	 	 ++O<<<
KKwC4H4HwwetwwwxxxQC,@,@QQQQs    # 1AA create_vendor_order_notificationc           	         	 t           j                            dd                              |           }d|j        j         d|j        j         }d}t          j        	                    |j
        ||dt                                 d	|  S # t          $ r<}t                              d
|  dt          |                      d|  cY d }~S d }~ww xY w)Nvendororder_purchaser(   zNew order received for z
 - Order #zNew Order ReceivedF)rA   r   r!   is_readr,   z.Vendor notification created for order_item ID z4Failed to create vendor notification for order_item r   z2Error creating vendor notification for order_item )r   r.   select_relatedr/   productr   rB   r)   r8   createrA   r   r   r   r   r   )order_item_id
order_itemr!   r   r$   s        r%   r?   r?   J   s   T&55h@PQQUUYfUgg
nJ,>,DnnPZPiPlnn$#**$EE 	+ 	
 	
 	
 POOO T T Td=dd\_`a\b\bddeeeSMSSSSSSSSTs   BB 
C1C	CCcreate_user_notificationc                    	 t                               d|  d| d|            | dk    rt          j                            |d                                          }|st                               d|            dS t          j                            |||t                      d	
           	 t                              |j        ||           t                               d|j                    n# t          $ r7}t                               d| dt          |                      Y d }~nd }~ww xY w| dk    rt           j                            |d                                          }|st                               d|            dS t          j                            |||t                      d	           nt                               d|             dS t                               |                                  d|            |                                  dS # t          $ rB}	t                               d|  d| dt          |	                      d|  d| cY d }	~	S d }	~	ww xY w)Nu   📨 Sending notification to z ID z with title: buyerT)r6   r   z1Device not found or inactive for buyer device_id=zBuyer device not found.F)r+   r   r!   r,   rC   r*   z'Push notification sent to buyer device z+Failed to enqueue push for buyer device_id=r   rA   )r)   	is_activez&Vendor not found or inactive: user_id=zVendor not found.)rA   r   r!   r,   rC   zInvalid user_type passed: zInvalid user_type.z notification created for ID z notification created.z Error creating notification for z ID=z"Failed to create notification for )r   r   r1   r.   r2   firstwarningr8   rF   r   r   r5   r6   r   	exceptionr   Userr   
capitalize)
	user_typeuser_idr   r!   order_idrG   r+   
push_errorrA   r$   s
             r%   rI   rI   a   s   3M`I``7``Y^``aaa^**WT*JJPPRRF 1\SZ\\]]]00'.. /   m+11$.# 2   
 XfFVXXYYYY m m m  !kw!k!kZ]^hZiZi!k!kllllllllm (""\((Gt(DDJJLLF +QQQRRR**'.. /     LLAiAABBB''y++--UUGUUVVV&&((@@@@ M M M^I^^7^^VYZ[V\V\^^___LILL7LLLLLLLLMs^   A<H,  0H, 1AC6 5H, 6
D7 -D2-H, 2D77AH, AH, $AH, ,
I867I3-I83I8notify_vendor_payout_statusTc           	      R   	 |rdnd}|rd| d}nd| d}|r|d| z  }t                               d| ||           t                              d	|  d
| d|            dS # t          $ r8}t                              d|  dt          |                      Y d}~dS d}~ww xY w)zL
    Sends a notification to the vendor regarding their payout request.
    zPayout ApprovedzPayout RejectedzYour payout request of TZS z! has been approved and processed.z was rejected.z	 Reason: rA   )rR   rS   r   r!   z [PAYOUT NOTIFICATION] Vendor ID z - z - TZS z0Failed to send payout notification to vendor ID r   N)rI   r5   r   r   r   rO   r   )rA   payout_amountapprovedreasonr   r!   r$   s          r%   rV   rV      s!   
`%-D!!3D 	0dMdddGGQMQQQG 0/v/// 	!&&	 	' 	
 	
 	
 	_v__%__P]__````` ` ` `^F^^VYZ[V\V\^^_________`s   A A$ $
B&.-B!!B&)NN)TN) timedjango.utils.timezoner   rest_framework.responser   rest_frameworkr   django.template.loaderr   django.core.mailr   django.confr	   vendor_listing.celeryr
   loggingfirebase_adminr   notification.firebaser   modelsorder.modelsr   	getLogger__name__r   taskr   r&   r?   rI   rV        r%   <module>rm      s    % % % % % % , , , , , , ! ! ! ! ! ! 3 3 3 3 3 3 ) ) ) ) ) )             % % % % % %  $ $ $ $ $ $ . . . . . .     " " " " " "		8	$	$ 
,---" " .-" 
2333$R $R 43$RN 
1222T T 32T, 
)***4M 4M 4M +*4Mn 
,---` ` ` .-` ` `rl   