
    g$                     0   d dl mZ d dlmZ d dlmZ d dlmZ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 d dlmZ d d	lZd
ededefdZdedefdZdededefdZdedefdZdedefdZdededefdZdedededefdZdede	dedefdZdededefdZy	)    )HTTPException)Session)services)StoreCreateStoreResponseBranchCreateBranchResponseStoreUpdate)User)get_current_user)StoreBranch)FeedbackNstoredbcurrent_userc                 >   	 t        j                  ||      }|rt        ddddd      t        j                  || j                        rt        ddddd      t        j
                  || |      }ddd	|d
   |d   |d   |d   |d   |d   |d   |d   D cg c]%  }|d   |d
   |d   |d   |d   |d   |d   |d   d' c}ddS c c}w # t        $ r}|d }~wt        $ r6}t        j                          t        ddddt        |       d      d }~ww xY w)N  FzUser already owns a store.statuscodemessagestatus_codedetailzStore name must be unique.T   zStore created successfullystore_idnamestoretype_idbranch_limituser_id
created_at
updated_atbranches	branch_idbranch_nameaddresscontact_info)r%   r   r&   r'   r(   r!   r"   r#   r   r   r   r    r!   r"   r#   r$   r   r   r   data  zInternal server error: )
r   get_store_by_userr   get_store_by_namer   create_store	Exception	traceback	print_excstr)r   r   r   existing_store	new_storebranchhttp_exces           9/var/www/html/hubwallet-dev/src/apps/stores/controller.pycreate_store_controllerr:      s   7
!33BE"'A]^  %%b%**5"'A]^  ))"e\B	 3%j1!&) ). 9 ). 9$Y/'5'5 #,J"7  &,K%8$*:$6'-m'<#))#4(.~(>#))#4&,\&:&,\&:		
 	
    
#S?VWZ[\W]V^=_`
 	

s6   BC *C
C 
C 	DCD&1DDc                 *   	 t        j                  | |      }|st        ddddd      ddd|j                  |j                  |j
                  |j                  |j                  |j                  |j                  |j                  D cg c]]  }|j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  d	_ c}d
dS c c}w # t        $ r t        ddddd      w xY w)N  Fz/Store not found or does not belong to the user.r   r   T   z"Store details fetched successfullyr%   r   r&   r'   r(   r"   r#   r!   r)   r*   Store not found)r   r-   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   )r   r   
store_datar6   s       r9   get_store_controllerrA   J   s#   (
//LA
"'Ars  ;&//" * 7 7 * 7 7%--(33(33 %/$7$7 ! &,%5%5$*OO'-'9'9#)>>(.(;(;&,&7&7&,&7&7#)>>		
 	
  
#S=NO
 	

s   BC7 	A"C2+C7 2C7 7Dstore_updatec           
      l   t        j                  ||      }|st        ddddd      d}| j                  r,|j                  | j                  k7  r| j                  |_        d}| j
                  r,|j
                  | j
                  k7  r| j
                  |_        d}|st        ddddd      |j                          |j                  |       dd	d
|j                  |j                  |j
                  |j                  |j                  j                         |j                  j                         ddS )Nr<   Fr?   r   r   Tr   zNo changes were made.r=   zStore updated successfully)r   r   r    r!   r"   r#   r*   )r   r-   r   new_namer   r    commitrefreshr   r!   r"   	isoformatr#   )rB   r   r   r   updateds        r9   update_store_controllerrI   v   s+   &&r<8E#S=NO
 	
 G |/D/D!D!**
   U%7%7<;T;T%T)66 #S=TU
 	
 IIKJJu /JJ!..}}**446**446
	     c                     t        j                  | |      }|st        ddddd      t        j                  | |      S )Nr<   FzStore not found or unauthorizedr   r   )r   r-   r   delete_store)r   r   r   s      r9   delete_store_controllerrM      sG    &&r<8Euc]~4  A  	A   \22rJ   c                 0    t        j                  || |      S N)r   create_branch)r6   r   r   s      r9   create_branch_controllerrQ      s    !!"fl;;rJ   r   c                    	 t        j                  || |      }|st        ddddd      ddd|D cg c]]  }|j                  |j                  |j
                  |j                  |j                  |j                  |j                  |j                  d	_ c}d
S c c}w # t        $ r)}t        dddt        |j                        d      d }~ww xY w)Nr<   Fz No branches found for this storer   r   Tr=   zBranches fetched successfullyr>   r*   r,   )r   get_branches_by_store_idr   r%   r   r&   r'   r(   r"   r#   r!   r3   r   )r   r   r   r$   r6   r8   s         r9   #get_branches_by_store_id_controllerrT      s     44R<P"'Acd  6 !)  "(!1!1 &#)#5#5%~~$*$7$7"("3"3"("3"3%~~		
 	
  "'QXXO 	s)   1B A"B
B B 	C($CCr%   c                    	 t        j                  || ||      }|st        ddddd      ddd|j                  |j                  |j
                  |j                  |j                  |j                  |j                  |j                  d	d
S # t        $ r)}t        dddt        |j                        d      d }~ww xY w)Nr<   Fz0Branch not found or does not belong to the user.r   r   Tr=   z#Branch details fetched successfullyr>   r*   r,   )r   get_branch_by_idr   r%   r   r&   r'   r(   r"   r#   r!   r3   r   )r   r%   r   r   r6   r8   s         r9   get_branch_by_id_controllerrW      s    **2xLQ"'@rs  <#--"OO%11!>> & 3 3$//$//!>>		
 	
    "'AHHN 	s   B
B 	B?$B::B?r6   c                 2    t        j                  || ||      S rO   )r   update_branch)r%   r6   r   r   s       r9   update_branch_controllerrZ      s    !!"iFFrJ   c                 0    t        j                  || |      S rO   )r   delete_branch)r%   r   r   s      r9   delete_branch_controllerr]     s    !!"i>>rJ   ) fastapir   sqlalchemy.ormr   src.apps.storesr   src.apps.stores.schemasr   r   r   r	   r
   src.apps.users.modelsr   src.apps.auth.controllerr   src.apps.stores.modelsr   r   src.apps.feedback.modelsr   r1   r:   rA   rI   rM   rQ   r3   rT   rW   rZ   r]    rJ   r9   <module>rg      s   ! " $ h h & 5 / - 
8
; 8
G 8
4 8
v)
W )
D )
X-+ -7 -RV -^3 3t 3< < <
!# !7 !RV !N# # 7 Z^ DG G\ Gw G^b G? ? ? ?rJ   