
    ghB=                        d dl mZmZmZ d dlmZmZmZ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mZ d dlZd dlZd dlmZ  ej.                  d	d
dg      ed               Z ej.                  ddg      ed               Z ej.                  ddg      ed               Z ej.                  ddg      ed               Z ej.                  ddg      d        Zy)    )jsonifyrequestcurrent_app)
login_userlogout_userlogin_requiredcurrent_user)db)auth_bp)UserSubscriptionPaymentHistory)datetime	timedeltaN)token_requiredz/profilePUTOPTIONS)methodsc                  "   t         j                  dk(  ryt        t         dt              } | rt	        | d      st        ddd      dfS t        d	t        t         j                               t        d
t         j                         t        dt         j                         t        dt         j                         	 t        j                         }t        d|       |st        d       t        ddd      dfS t        d| j                   d| j                          t        d| j                    d| j"                   d| j$                   d| j&                   d	       t        d|        t        d       t        d| j                          t        d| j                    d       t        d| j"                   d       t        d| j$                   d       t        d| j&                   d       d |v rc|d    j)                  d!d"      }|r|d#   nd$| _        t+        |      d"kD  r|d"   nd$| _        t        d%| j                    d| j"                   d       d&|v r#|d&   | _        t        d'| j$                   d       d(|v r#|d(   | _        t        d)| j&                   d       t        d*       t        d| j                          t        d| j                    d       t        d| j"                   d       t        d| j$                   d       t        d| j&                   d       d+|v r<d,|v r8| j-                  |d+         st        dd-d      dfS | j/                  |d,          	 d#d.lm} d#d/lm} t        d0       t8        j:                  j=                  |        d#d1lm }  || d2        || d3        || d&        || d(       t        d4       t8        j:                  jC                          t        d5       t        d6       t8        jD                  }|jG                         5 }t        d7| jH                           |d8      }	|jK                  |	| j                   | j"                  | j$                  | j&                  | jH                  | j                  d9      }
t        d:|
jL                   d;       ddd       t        d<       t8        j:                  jO                          t        d=| j                          tP        jR                  jU                  | j                        }|rAt        d>|j                    d|j"                   d|j$                   d|j&                   d	       nt        d?       |} t        dCdD| j                  | j                   | j"                  | j                  | j                  | j$                  | j&                  | jX                  ddddEdF	dG      }|S # t        $ r$}t        dt        |              d}Y d}~;d}~ww xY w# 1 sw Y   cxY w# t        $ r[}t        d@t        |              t8        j:                  jW                          t        ddAt        |       d      dBfcY d}~S d}~ww xY w)HzUpdate user profiler   )    r	   idFzAuthentication requiredsuccessmessagei  zRequest headers:zRequest method:zRequest content type:zRequest data:zParsed JSON data:zError parsing JSON: Nz No data provided or invalid JSON  zUpdating profile for user ID: z	, email: zCurrent user data: first_name='z', last_name='z', company='z', job_title=''zUpdate data received: z&Before update - User data in database:z  ID: z  First name: 'z  Last name: 'z  Company: 'z  Job title: 'name    r   r   zUpdated name to: first_name='companyzUpdated company to: '	job_titlezUpdated job_title to: 'z%After update - User data to be saved:currentPasswordnewPasswordzCurrent password is incorrect)r   textzMarking user object as modified)flag_modified
first_name	last_namezCommitting changes to databasezDatabase commit successfulz)Executing direct SQL update as a fallbackzCurrent auth_token: zUPDATE user SET first_name = :first_name, last_name = :last_name, company = :company, job_title = :job_title, auth_token = :auth_token WHERE id = :user_id)r(   r)   r!   r"   
auth_tokenuser_idzDirect SQL update affected z rowszExpiring all objects in sessionzReloading user with ID: z'After commit (fresh user): first_name='z+ERROR: Could not reload user from database!z&Error committing changes to database: zDatabase error: i  TzProfile updated successfullyplan_idstatus	is_annual)	r   r(   r)   emailusernamer!   r"   is_adminsubscription)r   r   user)-r   methodgetattrr	   hasattrr   printdictheaderscontent_typedataget_json	Exceptionstrr   r0   r(   r)   r!   r"   splitlencheck_passwordset_passwordflaskr   
sqlalchemyr&   r
   sessionaddsqlalchemy.orm.attributesr'   commitenginebeginr*   executerowcount
expire_allr   querygetrollbackr2   )r4   r<   e
name_partsr   r&   r'   rJ   
connectionsqlresult
fresh_userresponses                 /var/www/html/app/auth/routes.pyupdate_profilerZ      sr   
 ~~" 7NL9D wtT*0
   	 

d7??34	
W^^,	
!7#7#78	/7<<(!!4(
 019
   	 
*477)9TZZL
IJ	+DOO+<N4>>JZZfgkgsgsft  uC  DH  DR  DR  CS  ST  U  V	"4&
)* 

23	F477)
	ODOO,A
./	N4>>*!
,-	La
()	N4>>*!
,-~&\''Q/
+5*Q-2*-j/A*=A2-doo->nT^^L\\]^_DI%dll^156dk*''7q9:	
12	F477)
	ODOO,A
./	N4>>*!
,-	La
()	N4>>*!
,- D ]d%:""4(9#:; :    	$}-.H%# 	/0


t 	<dL)dK(dI&dK(./


*+ 	9: \\^ 	Hz((9:; &C  ''"&//!%#||!%"&//#ww
F //@FG3	H8 	/0


 	(	23ZZ^^DGG,
;J<Q<Q;RR`akauau`v  wC  DN  DV  DV  CW  We  fp  fz  fz  e{  {|  }  ~?@  1''//ZZ

||!
 H* Ow  $SVH-.h	H 	HR  6s1vh?@


)#a&2
   	sR   : W- .B?X* -BX<CX* -	X6XXX'"X* *	Z3AZ	Z	Zz4/api/subscription/checkout/<plan_id>/<billing_cycle>GETc           	         t         j                  d   }| |vrt        ddd      dfS |dvrt        ddd      dfS ||    }|dk(  r|d	   n|d
   }t         j                  d   t        _        t        d|| ||t         j                  d   d      S )z2API endpoint for subscription checkout informationSUBSCRIPTION_PLANSFInvalid subscription planr   r   monthlyyearlyInvalid billing cyclera   price_yearlyprice_monthlySTRIPE_SECRET_KEYTSTRIPE_PUBLISHABLE_KEY)r   planr-   billing_cyclepricestripe_publishable_key)r   configr   stripeapi_key)r-   rh   plansrg   ri   s        rY   subscription_checkout_apiro      s    
 34Ee2
   	
 11.
   	
 >D$1X$=D 4CXE !''(;<FN &"-"4"45M"N      z/api/subscription/processPOSTc                     t        j                         } | j                  d      }| j                  d      }t        j                  d   }||vrt        ddd      S |dvrt        ddd      S ||   }|d	k(  r|d
   n|d   }|d	k(  }t        j                         }|t        |rdnd      z   }t        j                  j                  t        j                        j                         }	|	r&||	_        d|	_        ||	_        ||	_        ||	_        |	}
nZt        t        j                  |d|||t)        t+        j,                                     }
t.        j0                  j3                  |
       t5        t        j                  |
j                  ||ddt)        t+        j,                                     }t.        j0                  j3                  |       t.        j0                  j7                          t        dd|
j                  |
j                  |
j                   |
j"                  j9                         |
j$                  j9                         |
j&                  dd      S )z,API endpoint to process subscription paymentr-   rh   r]   Fr^   r   r_   rb   ra   rc   rd   im     )daysr+   active)r+   r-   r.   
start_dateend_dater/   subscription_idcredit_cardr   )r+   ry   amountpayment_datepayment_methodr.   transaction_idTz#Subscription processed successfully)r   r-   r.   rw   rx   r/   )r   r   r3   )r   r=   rP   r   rk   r   r   utcnowr   r   rO   	filter_byr	   r   firstr-   r.   rw   rx   r/   r?   uuiduuid4r
   rF   rG   r   rI   	isoformat)r<   r-   rh   rn   rg   ri   r/   rw   rx   existing_subscriptionr3   payments               rY   process_subscription_apir      s    Dhhy!GHH_-M 34Ee55PQRR1155LMNN >D$1X$=D 4CXE)I "JI)3DDH )..888QWWY(/%'/$+5()1&*3', $ OO!

-
 	

|$ $$4::<(G JJNN7JJ8//#++"))&11;;=$--779%//
  rp   z/api/subscription/cancelc                      t         j                  j                  t        j                        j                         } | st        ddd      S d| _        t        j                  j                          t        ddd      S )zCancel subscriptionru   FzNo active subscription foundr   canceledTz"Subscription canceled successfully)r   rO   r   r	   r   r   r   r.   r
   rF   rI   )r3   s    rY   cancel_subscription_apir   8  sn      %%///HNNPL55STUU$LJJ7  rp   z/statusc                     t         j                  j                  d      } t        d|        | r| j	                  d      rq| j                  d      d   }t        d|       t        j                  j                         }|D ]4  }t        d|j                   d|j                   d	|j                          6 t        j                  j                  |
      j                         }|st        d       ddlm} t         j"                  j%                   |d      d|i      }|j'                         }|rFt        j                  j                  |      }t        d|j                   d|j                   d       nt        d       t        d|r|j                  nd       |rt        d|j                   d|j                   d       t(        j                  j                  |j                        j                         }t+        d|j                  |j,                  |j                  |j.                  xs d|j0                  xs d|j2                  xs d|j4                  xs d|j6                  |r|j8                  nd|r|j:                  nd|r|j<                  nddd	d      S t+        ddi      S t>        j@                  rt(        j                  j                  t>        j                        j                         }t+        dt>        j                  t>        j,                  t>        j                  t>        j.                  xs dt>        j0                  xs dt>        j2                  xs dt>        j4                  xs dt>        j6                  |r|j8                  nd|r|j:                  nd|r|j<                  nddd	d      S t+        ddi      S ) zCheck authentication statusAuthorizationzAuth header:zBearer r   r    zToken from request:zUser z (z) - auth_token: )r*   z8No exact match found, trying case-insensitive comparisonr   r%   z;SELECT id FROM user WHERE LOWER(auth_token) = LOWER(:token)tokenz&Found user by case-insensitive token: z (ID: )z3No user found even with case-insensitive comparisonzUser found by token:NonezFound user by token: ru   Tr   Nr,   )	r   r0   r1   r(   r)   r!   r"   r2   r3   )authenticatedr4   r   F)!r   r:   rP   r8   
startswithr@   r   rO   allr   r1   r*   r   r   rE   r&   r
   rF   rL   scalarr   r   r0   r(   r)   r!   r"   r2   r-   r.   r/   r	   is_authenticated)	auth_headerr   	all_usersur4   r&   rV   r+   r3   s	            rY   auth_statusr   I  s3    //%%o6K	.+&{--i8!!#&q)#U+ JJNN$	 	NAE!$$r!**-=all^LM	N zz##u#5;;= LM'ZZ''-j(knuw|m}~FmmoGzz~~g.>t}}oVTXT[T[S\\]^_KL$tdmmH)$--twwiqIJ'--777HNNPL!%''!ZZ $"&//"7R!%!52#||1r!%!52 $;G<#7#7T9E,"5"54?K\%;%;QU%  (    
	&	&#))33LOO3LRRT!"oo%++(11*55;)339r'//52)339r(117C|335Al11t;G!7!7T!
  	( U
  	rp   )rD   r   r   r   flask_loginr   r   r   r	   app.extensionsr
   app.authr   app.auth.modelsr   r   r   r   r   rl   r   app.utils.authr   routerZ   ro   r   r   r    rp   rY   <module>r      s   / / M M   > > (  
 * zE9#56t  7tl EPUwW  XB *VH=I  >IV )F8<  = y5'*W +Wrp   