
    V
h%                     t   S SK r S SKrS SKrS SKJrJr  S SKJr  S SKJrJ	r	  S SK
Jr  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJr  S SKJr  S SKr\" 5         \" S/SS9r\" SS9rSrSrSrSr " S S\ R@                  5      r!S r"S\#S\#4S jr$S\#S\S\#4S jr%S\#4S jr&S\#S \#S\#4S! jr'S\#4S" jr(g)#    N)datetime	timedelta)CryptContext)HTTPExceptionstatus)create_access_token)str_to_objectid)get_role_service)FeatureBase)verify_google_token)send_welcome_email)ObjectId)load_dotenvbcryptauto)schemes
deprecatedi7  )minutesusersaccountssubscriptionsprojectsc                   (   ^  \ rS rSrU 4S jrSrU =r$ )CustomJSONEncoder   c                    > [        U[        5      (       a  UR                  5       $ [        U[        5      (       a  [	        U5      $ [
        TU ]  U5      $ N)
isinstancer   	isoformatr   strsuperdefault)selfobj	__class__s     HC:\Suresh\moveshuttle\MDcreated\moveengine\app\v1\services\saas\login.pyr"   CustomJSONEncoder.default    sA    c8$$==?"c8$$s8Ows##     )__name__
__module____qualname____firstlineno__r"   __static_attributes____classcell__)r%   s   @r&   r   r      s    $ $r(   r   c                    [        U [        5      (       a/  U R                  5        VVs0 s H  u  pU[        U5      _M     snn$ [        U [        5      (       a  U  Vs/ s H  n[        U5      PM     sn$ [        U [
        R
                  5      (       a  U R                  5       $ [        U [        5      (       a  [        U 5      $ U $ s  snnf s  snf r   )	r   dictitemsserialize_datalistr   r   r   r    )r$   kvitems       r&   r3   r3   '   s    #t14=>!$$==	C		145t$55	C**	+	+}}	C	"	"3x
 >5s   CC	user_datareturnc                   #    [        [        R                  " SS5      5      nU R                  SS5        U R                  SS5        U R                  SS5        U R                  SU5        U R                  S	S5        U R	                  S
5      (       Ga  U[
           nUR                  S[        U S
   5      05      nU(       a  UR	                  SS5      U S'   UR	                  SS5      U S'   UR	                  SS5      U S'   UR	                  S5      (       af  U[           nUR                  S[        UR	                  S5      5      05      nU(       a*  UR	                  SS5      U S	'   UR	                  SU5      U S'   O$U[           nUR                  U S
   SS.5      nXS	'   U R	                  S5      n	U	(       a'  [        X5      n
U
(       a  U
R	                  S0 5      U S'   US   n[        UR                  5       5      nXS'   U $ 7f)z
Enriches user data with common details:
  - Account information (subscription, active agents, etc.)
  - Role rights from roles service.
ALLOWED_FREE_AGENTS5account_typer   subscription_id subscription_statussubscription_agentsactive_agents
account_id_idactive_subscription_idactive_agents_countsubscription_agents_countACTIVE)rC   r   rolespermissionsrole_rightsfeaturesrole_features)intosgetenv
setdefaultgetACCOUNT_COLLECTION_NAMEfind_oner	   SUBSCRIPTION_COLLECTION_NAMEPROJECT_COLLECTION_NAMEcount_documentsr
   r4   find)r8   dbr;   accounts_collectionaccount_infosubscription_collectionsubscriptionproject_collectionactive_projects_countrole_idrolefeatures_collectionfeatures_lists                r&   enrich_user_datard   3   s     bii(=sCD+*B/.3.0CD!,}}\"" !89*33UOIVbLc<d4ef(4(8(8(KIn%+7+;+;<TVX+YI'(/;/?/?@UWY/ZI+, 899*,-I*J'6???<+;+;<T+UVA    1=1A1ABWYZ1[Io.7C7G7GHcex7yI34%'(?%@"(:(J(J"+L"9&L )% .C/* mmG$G,'+xxr'BIm$ Z.,1134M!.o s   HHtoken_expires_deltac           
         #    [        X5      I Sh  vN n [        SU R                  SU R                  SS5      5      0US9n[        U 5      n SXS.$  N@7f)	z
Common login processing:
  - Enrich user data with account/role details.
  - Generate a JWT token.
  - Serialize user data for the response.
Nsubemailmobiler?   )dataexpires_deltazLogin Successful)messageusertoken)rd   r   rR   r3   )r8   rY   re   rn   s       r&   process_user_loginro   i   s[      'y55IY]]7IMM(B,GHI)E y)I)9MM 6s   AAAAc                 $  #    U[            R                  SU R                  05      nU(       d
  [        SSS9e[        R                  U R                  UR                  S5      5      (       d
  [        SSS9e[        X![        5      I Sh  vN $  N7f)	z#
Handles the email/password login.
rh   i  zUser not foundstatus_codedetailhashed_passwordi  zInvalid credentialsN)
COLLECTION_NAMErT   rh   r   pwd_contextverifypasswordrR   ro   access_token_expires)
user_loginrY   
user_in_dbs      r&   
login_userr|   z   s}      O$--w
8H8H.IJJ4DEEj11:>>BS3TUU4IJJ#J4HIIIIs   BB	B
Bpayload	otp_storec           	        #    U R                  S5      nU R                  S5      nU(       a  Uc
  [        SSS9eX2;  a
  [        SSS9eX#   n[        R                  " 5       US   :  a  X#	 [        SS	S9eUS   [	        U5      :w  a
  [        SS
S9eX#	 U[
           nUR                  SU05      nU(       dQ  U[        R                  " 5       [        R                  " 5       SSSSSS.nUR                  U5      R                  n	XS'   O0UR                  SUS   0SS[        R                  " 5       005        Un[        X[        5      I Sh  vN $  N7f)z
Verifies the OTP for a phone login.
This function handles:
  - Validating the OTP (using an in-memory store for demo purposes)
  - Looking up (or creating) the user by phone
  - Processing the login (enriching user data and generating JWT token)
phoneotpNi  z"Phone number and OTP are required.rq   z#OTP not sent for this phone number.expireszOTP expired.zInvalid OTP.ri   Tr?      )ri   created_date
last_login	is_activeis_verifiedmobile_verifiedrC   r`   rD   $setr   )rR   r   r   utcnowrN   ru   rT   
insert_oneinserted_id
update_onero   ry   )
r}   rY   r~   r   otp_submittedstoredusers_collectionr{   r8   r   s
             r&   verify_phone_otp_servicer      sd     KK EKK&MM)4XYY4YZZF6),,NCCe}M**NCC 	 /*!**He+<=J $OO-"//+#	
	 '11)<HH&% 	##Ju%&lHOO$567	
 	 $I3GHHHHs   EE	E
Ec                 ~  #    [        U R                  5      nU(       d  [        [        R                  SS9eU[
           nUR                  SUS   05      nU(       d  US   US   UR                  SS5      US   [        R                  " 5       [        R                  " 5       [        R                  " 5       SSS	S
.
nUS   nUR                  SUS   05      nU(       a*  UR                  SS5      US'   UR                  SS5      US'   UR                  U5      R                  n	XS'   UR                  [        US   US   5        [        Xb[        5      I Sh  vN $ [        R                  " 5       US   S.n
UR!                  SUS   0SU
05        UR#                  U
5        [        XR[        5      I Sh  vN $  Na N7f)z
Handles Google Connect login.
zInvalid Google token.rq   rh   namepicturer?   refresh_tokenTF)
rh   r   picurlgoogle_connectr   r   date_of_birthr   r   r   emailsrC   ra   	   r`   rD   N)r   r   r   )r   rn   r   r   HTTP_401_UNAUTHORIZEDru   rT   rR   r   r   r   r   add_taskr   ro   ry   r   update)google_tokenbackground_tasksrY   	user_infor   r{   r8   emails_collection
invitationr   update_datas              r&   google_connect_userr      s     $L$6$67I(D(DMdee/*!**GYw5G+HIJ w'f%mmIr2'8$OO-"//+%__.$
	 xL&//)G:L0MN
&0nn\2&FIl##->>&!#<Ii &11)<HH&%!!"4i6H)TZJ[\'	7KLLL &.__%6)TcJde##UJu,=$>@UV+&'
8LMMM M Ns%   EF=F9AF=4F;5F=;F=))jsonrO   randomr   r   passlib.contextr   fastapir   r   app.v1.dependencies.authr   app.v1.libraries.objectr	   app.v1.services.saas.rolesr
   app.v1.models.crudmodelr   app.v1.libraries.googleconnectr    app.v1.libraries.email_templatesr   bsonr   dotenvr   rv   ry   ru   rS   rU   rV   JSONEncoderr   r3   r1   rd   ro   r|   r   r   r)   r(   r&   <module>r      s     	  ( ( ) 8 3 7 / > ?    H:&A /  $ . $ $(( $
4d 44 4lN Ny NUY N"
J 
J7ID 7I 7I$ 7It(NT (Nr(   