o
    AhI                     @   s>  d dl mZmZmZmZmZmZmZ d dlZd dl	m	Z	 d dl
mZmZmZmZ d dlZd dlmZmZ d dlmZ 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m Z  ddl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z. d dl/m0Z0 e  dZ1dZ2dZ3dZ4dZ5e Z6e6j7dee( deej8fdej9fddZ:e6j7dee;eee# e<f f dd ddddeej8fde<de<dee; dee; d ee; dej9fd!d"Z=e6j7d#e"deej8fd$e;dej9fd%d&Z>e6j7d'e"deej8fd$e;dej9fd(d)Z?e6j@de"ded*eej8fde;dej9fd+d,ZAe6jBd#e"deej8fd$e;d-edej9fd.d/ZCd$e;d0e;fd1d2ZDe6j@d3e&deej8fd4e&dej9fd5d6ZEe67d7eej8fd8e;dej9fd9d:ZFe6j7d;e&deej8fd8e;dej9fd<d=ZGe6j7d>ee;eee+ e<f f dd ddddeej8fde<de<dee; d$ee; d ee; dej9fd?d@ZHe6jBdAe&deej8fd8e;dBe'dej9fdCdDZIe6JdEeej8fd e;d8e;dej9fdFdGZKe6j@dHe)deej8fdIe*dej9fdJdKZLe6j7dHee) ddddeej8fd$ee; dee; de<dej9fdLdMZMdS )N    )	APIRouterDependsHTTPExceptionFile
UploadFileRequestFormN)datetime)ListOptionalDictUnion)ValidationError	SecretStr)Image)BytesIO)Fernet)load_dotenv)database)str_to_objectidobject_to_str_array   )
AppAppListAppBaseAppCategoryAppIntegrationAppIntegrationUpdateCategoryAppLog
AppLogBaseIntegrations)encrypt_datadecrypt_data)load_api_schema_as_jsonappapptypes	app_usersapp_logsflowsz/categories)response_modeldbc                 C   s$   | t  i ddd}dd |D S )N   )_idcategoryc                 S   s"   g | ]}t |d  |d dqS )r-   r.   )idnamestr).0cat r5   7/var/www/html/moveengine/app/v1/routers/saas/appflow.py
<listcomp>!   s   " z#list_categories.<locals>.<listcomp>)APP_CATEGORY_COLLECTIONfind)r+   
categoriesr5   r5   r6   list_categories   s   r;   /
   skiplimitq
project_id
account_idc                 C   s   i }|r||d< |r||d< |r2d|ddig|d< |t  d|i}|r2|d dt|d	 i |t |}t|t |d
d	| 
|}	t|	}	|	D ]
}
t|
d	 |
d< qR|	|dS )NrA   rB   r0   iz$regexz$optionsz$orr.   category_idr-   created_dater,   app_id)appstotal_count)r8   find_oneappendr2   APP_COLLECTIONcount_documentslistr9   sortr>   r?   r   )r>   r?   r@   rA   rB   r+   queryr.   
apps_countrH   r%   r5   r5   r6   	list_apps#   s    	&
rR   z	/{app_id}rG   c                    s   |t  dt| i}|d u rtdddt|I d H }|s%tddd|d }t||d< d	|v ra|d	 rad
|d	 v rJt|d	 d
 |d	 d
< d|d	 v radd |d	 d  D |d	 d< t	|d |d< |S )Nr-     App not foundstatus_codedetail  Failed to load App
components
api_schemaauth_configapi_keyoauthc                 S      i | ]	\}}|t |qS r5   )r#   r3   keyvaluer5   r5   r6   
<dictcomp>X       z retrieve_app.<locals>.<dictcomp>)
rL   rJ   r   r   r$   jsondumpsr#   itemsr2   )rG   r+   r%   r[   rZ   r5   r5   r6   retrieve_appF   s    "rh   z/a/{app_id}c                    s@   |t  dt| i}|d u rtdddt|d |d< |S )Nr-   rS   rT   rU   )rL   rJ   r   r   r2   )rG   r+   r%   r5   r5   r6   retrieve_admin_app_   s   ri   .c           
   
      s4  zt | }W n t jy   tdddw z	tdi |}W n ty4 } ztdt|dd }~ww | }t	 |d< |
dro|d 
drWt|d d |d d< |d 
drodd	 |d d  D |d d< |t |}i |d
t|ji}t|j}tjddd |j}	t||	I d H  |S )N  Invalid app JSON formatrU   rF   r\   r]   r^   c                 S   r_   r5   )r"   r`   r5   r5   r6   rc      rd   zadd_app.<locals>.<dictcomp>r-   z./public/appiconsT)exist_okr5   )re   loadsJSONDecodeErrorr   r   r   r2   dictr	   utcnowgetr"   rg   rL   
insert_oneinserted_idosmakedirsiconcreate_app_icon)
r%   r+   raw_app_dataapp_validatedefinal_app_datanew_appcreated_app
new_app_idicon_urlr5   r5   r6   add_appi   s4   
"
r   requestc              
      s@  |  I d H }|d}zt|}W n tjy"   tdddw z	tdi |}W n ty@ } ztdt|dd }~ww |t	 
dt| i}|d u rVtddd|jdd	}	|t	 dt| id
|	i}
|
jdkrutddd|t	 
dt| i}|rt|d |d< |d |d kr|d }t| |I d H  |S )Nr%   rj   rk   rU   r-   rS   rT   Texclude_unset$setr   rv   r5   )formrq   re   rm   rn   r   r   r   r2   rL   rJ   r   ro   
update_onematched_countrw   )rG   r   r+   	form_dataapp_data_jsonapp_data_dictapp_datarz   existing_appupdated_dataresultupdated_appr   r5   r5   r6   
update_app   s8   

r   r   c              	      s$  d|  d}d|  d}t |}|jdkrtd d S t|j}t|a}|jdks<|jdks<|jdkred	|j	v rez|
 d
 }W n tyR   |d}Y nw td|jd}|j||d |}n|d}|d}	|	|d |d}
|
|d W d    d S 1 sw   Y  d S )Nz./public/appicons/z.jpgz
_small.jpg   zImage URL is not validRGBALAPtransparencyr   RGB)   r   r   )mask)   r   JPEG)0   r   )requestsrq   rV   printr   contentr   openmodeinfosplit
IndexErrorconvertnewsizepasteresizesave)rG   r   	icon_pathsmall_icon_pathresponse
image_dataimalpha
backgroundim_largeim_smallr5   r5   r6   rw      s0   


(


"rw   z
/subscribeapp_integrationc           	   
      s   |   }|di di  D ]\}}t|tr(t| }||d d |< qz'|t |}i |dt	|j
i}|t dt|d idddii |W S  tyg } ztd	d
t	| dd }~ww )Nr\   credentialsintegration_idr-   rG   z$incintegrationsr,   rX   zDatabase insertion error: rU   )ro   rq   rg   
isinstancer   r"   get_secret_valueAPP_INTEGRATION_COLLECTIONrr   r2   rs   rL   r   r   	Exceptionr   )	r   r+   new_integration_datara   rb   encrypted_valuenew_integrationcreated_integrationrz   r5   r5   r6   subscribe_to_app   s&    

r   z/endpoints/{integration_id}r   c                    sf   |t  dt| i}|t dt|d i}t|I d H }|s(tdddt||d< |d S )Nr-   rG   rX   rY   rU   r[   )r   rJ   r   rL   r$   r   re   rf   )r   r+   integrationr%   r[   r5   r5   r6   retrieve_endpoints   s   r   z/integrations/{integration_id}c                 C   s>   |t  dt| i}|d u rtdddt|d |d< |S )Nr-   rS   Integration not foundrU   )r   rJ   r   r   r2   )r   r+   r   r5   r5   r6   retrieve_integration  s
   r   z /integrations/list/{account_id}/c                 C   s   i }|r||d< |r||d< |r|dd|d< |t  |}|t  || |}t|}	|	D ]
}
t|
d |
d< q1tt|	}||dS )	NrG   rB   rC   rD   r0   r-   r   )r   rI   )r   rM   r9   r>   r?   rN   r2   r   )r>   r?   r@   rG   rB   r+   rP   integrations_countintegrations_cursorintegrations_listr   r   r5   r5   r6   list_app_integrations  s   

r   z/subscribe/{integration_id}integration_datac                 C   sr  |t  dt| i}|d u rtddd| }|jdd D ]a\}}|dkrud|v rg|d rgi }|d  D ]\}}	t|	trMt	|	
 }
|
||< q9|	||< q9|ds]di i|d< |d d | q!d|v rp|d spq!||d< q!|d	kr~||d	< q!|||< q!|t  dt| id
|i}|jdkrtddd|t  dt| i}|rt|d |d< |S tddd)Nr-   rS   r   rU   Tr   r\   r   configurationsr   r   z"Integration not found after update)r   rJ   r   r   copyro   rg   r   r   r"   r   rq   updater   r   r2   )r   r   r+   existing_integrationr   ra   rb   updated_auth_configkvr   r   updated_integrationr5   r5   r6   update_integration2  s<   







r   z2/integrations/remove/{account_id}/{integration_id}c           	         s   |t  t|| d}|d u rtddd|t  dt|i}|jdkr-tddd|t d|idd	i}d
d |D }td| |t 	dddd |D iiddddi}|d 	dd|iidddii}tddd|j
d|j
 d|j
|j
dS )N)r-   rB   rS   z+Integration not found for the given accountrU   r-   r   r   app_integration_idr,   c                 S   s   g | ]}t |d  qS )r-   r1   )r3   flowr5   r5   r6   r7     s    z&delete_integration.<locals>.<listcomp>z	flow_ids z$inc                 S   s   g | ]}t |qS r5   )r   )r3   r/   r5   r5   r6   r7     s    r   F )statusr   project_flowsflow_idr   messagez Integration deleted successfullyflows_updatedproject_flows_updated)r   r   r   )r   rJ   r   r   
delete_onedeleted_countFLOWS_COLLECTIONr9   r   update_manymodified_count)	rB   r   r+   r   r   flow_idsflow_ids_listflows_update_resultproject_flows_update_resultr5   r5   r6   delete_integrationu  s:   



r   z/logslog_datac                 C   s0   |   }|t |}i |dt|ji}|S )Nr-   )ro   APP_LOG_COLLECTIONrr   r2   rs   )r   r+   new_log_datanew_logcreated_logr5   r5   r6   add_app_log  s   r   c                 C   sR   i }| r| |d< |r||d< t |t ||}|D ]
}t|d |d< q|S )NrG   rA   r-   )rN   r   r9   r?   r2   )rG   rA   r?   r+   rP   logslogr5   r5   r6   list_app_logs  s   r   )Nfastapir   r   r   r   r   r   r   re   r	   typingr
   r   r   r   rt   pydanticr   r   PILr   r   ior   cryptography.fernetr   dotenvr   app.dbr   app.v1.libraries.objectr   r   models.saas.appmodelr   r   r   r   r   r   r   r   r    r!   libraries.cryptor"   r#   app.v1.libraries.jsonloaderr$   rL   r8   r   r   r   routerrq   get_mongo_dbMongoDBr;   r2   intrR   rh   ri   postr   putr   rw   r   r   r   r   r   deleter   r   r   r5   r5   r5   r6   <module>   s   $ 0 "  	&($&&    !$B$- :