o
    ¿Ôi÷  ã                   @   sz   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eƒZdd„ Zdd
d„Zdd„ Zdd„ Zd	S )é    )ÚrequestÚjsonify)Úconnect_to_database)Úclosing)ÚBcrypt)ÚError)Úappc               
   C   s  züzËt  ¡ } g d¢}tƒ }|D ]"}|| vs| | s1td|› ddœƒdf  W W | ¡  | ¡  S qt| ¡ ƒ‹}|  d¡}| 	d|f¡ | 
¡ }|retdddœƒdfW  d   ƒ W W | ¡  | ¡  S g }g }|  ¡ D ]\}	}
| |	¡ |	d	kr‚t |
¡ d
¡}
| |
¡ qmd |¡}d dgt|ƒ ¡}d|› d|› d}| 	||¡ | ¡  tddiƒdfW  d   ƒ W W | ¡  | ¡  S 1 sÆw   Y  W n% tyñ } ztdt|ƒiƒdfW  Y d }~W | ¡  | ¡  S d }~ww W | ¡  | ¡  d S | ¡  | ¡  w )N)ÚemailÚpasswordÚ	firstnameÚlastnameÚ	doctor_idzMissing required field: Útrue©ÚmessageÚerroré  r	   z$SELECT * FROM users WHERE email = %szUser already existsr
   úutf-8ú, z%szINSERT INTO  users (z,role, status) VALUES (z,3,0)r   zCompounder added successfullyéÉ   r   )r   Úget_jsonr   r   ÚcommitÚcloser   ÚcursorÚgetÚexecuteÚfetchoneÚitemsÚappendÚbcryptÚgenerate_password_hashÚdecodeÚjoinÚlenr   Ústr)ÚdataÚrequired_fieldsÚ
connectionÚfieldr   r	   ÚuserÚfieldsÚvaluesÚkeyÚvalueÚ
fields_strÚplaceholdersÚqueryÚe© r2   úM/Users/harjaapdhillon/Desktop/newidea/medvista_backend/Services/Compounder.pyÚadd_compounder   sf   
á
û
ë
ÿÿ
ê
å 
€üéÿ
r4   Nc              
   C   s8  z’zlt j d¡}tƒ }|jdd}| r3| d| f¡ | ¡ }|s2tddiƒdfW W | ¡  | ¡  S n+|rU| d|f¡ | 	¡ }|sTtddiƒdfW W | ¡  | ¡  S n	| d	¡ | 	¡ }t|ƒd
fW W | ¡  | ¡  S  t
y’ } ztdt|ƒiƒdfW  Y d }~W | ¡  | ¡  S d }~ww | ¡  | ¡  w )Nr   T)Ú
dictionaryz.SELECT * FROM users WHERE id = %s AND role = 3r   úCompounder not foundé”  z5SELECT * FROM users WHERE doctor_id = %s AND role = 3z"SELECT * FROM users WHERE role = 3éÈ   r   r   )r   Úargsr   r   r   r   r   r   r   Úfetchallr   r$   )Úidr   Úconnr   Z
compounderr1   r2   r2   r3   Úget_compounder5   sJ   ÿ
íÿ

ô

ü 
€ü
r=   c              
   C   sp  z¨zzt  ¡ }tƒ }| ¡ }g }g }| ¡ D ]!\}}|dkr6|dkr)t |¡ d¡}| |› d¡ | |¡ q|sHt	dddœƒdfW W | 
¡  S | | ¡ d	d
 |¡› d}| ||¡ | ¡  | d| f¡ | ¡ }	dd„ |jD ƒ}
tt|
|	ƒƒ}W n' ty¢ } z| ¡  t	d|› ddœƒdfW  Y d }~W | 
¡  S d }~ww W | 
¡  n| 
¡  w t	dd|dœƒdfS )Nr;   r
   r   z = %szNo fields to updater   r   r   zUPDATE users SET r   z WHERE id = %sz!SELECT * FROM users WHERE id = %sc                 S   s   g | ]}|d  ‘qS )r   r2   )Ú.0Údescr2   r2   r3   Ú
<listcomp>r   s    z%update_compounder.<locals>.<listcomp>zInternal server error iô  zcompounder updated successfullyÚfalse)r   r   r%   r8   )r   r   r   r   r   r   r    r!   r   r   r   r"   r   r   r   ÚdescriptionÚdictÚzipÚ	ExceptionÚrollback)r;   r%   r'   r   r*   r+   r,   r-   r0   Úupdated_usersÚcolumn_namesÚupdated_user_datar1   r2   r2   r3   Úupdate_compounderV   sJ   ÿÿ
€

ñ$
€üþrJ   c              
   C   sÖ   zaz;t ƒ }| ¡ }| d| f¡ | ¡  |jdkr+tddiƒdfW W | ¡  | ¡  S tddiƒdfW W | ¡  | ¡  S  tya } ztdt|ƒiƒd	fW  Y d }~W | ¡  | ¡  S d }~ww | ¡  | ¡  w )
Nz,DELETE FROM users WHERE id = %s AND role = 3r   r   r6   r7   zCompounder deleted successfullyr8   r   r   )	r   r   r   r   Úrowcountr   r   r   r$   )r;   r<   r   r1   r2   r2   r3   Údelete_compounder~   s(   

û
ü 
€ü
rL   )N)Úflaskr   r   Úconfr   Ú
contextlibr   Úflask_bcryptr   Zmysql.connectorr   r   r   r4   r=   rJ   rL   r2   r2   r2   r3   Ú<module>   s    
*!(