o
    Q?h.%                     @   s  d dl m Z  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g g dfd	ed
edededededede de fddZd?defddZd@ddZd g dddg fdefddZdd Zdd  Zd!d" Zd@d#d$Zd%d& Zd'd( Zd)d* Zd+d, Zd-ee fd.d/Zd@d0d1Zd2d3 Zd4d5 Zd6d7 Zd8d9 Z dAd:d;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, d d<l-m#Z. d=d> Z/dS )B    )datetime)SERVER_TIMESTAMPdb)	API_Error)getUserShops)
removehtml)ShopNTuid
platformIdshopIdenterpriseIdplatformProductIdnamedescription	createdAt	updatedAtc                 K   sf   t |}td|rt|||nd | |||t |t |t||||	r$t|	n|	t|
t|d|}|S )N)idr	   r
   r   r   r   r   r   r   r   pricetagsimages )strdictgetProductRefr   floatlist)r	   r
   r   r   r   r   r   r   r   r   r   r   
idRequiredkwargsproductr   r   G/var/www/html/riverr-enterprise-integrations-main/functions/Products.pyconvertProduct
   s&   r    Furlc                 C   s    t | |rt||dS ||dS )Nr!   r   primaryr   r   r"   r   r   r   convertImage+   s    r%   c                 C   s&   t t| t||rt|n|d}|S )N)r   valuer   r$   )r   r&   r   pr   r   r   convertProperty.   s   r(   r   c           	   
   K   s   |dv rd }dt | v rt | dd} tt | t||r!t|n|t||r+t |n||r2t |n|t|d}|r?|| |S )N)NoneNullnullnone/-)r   
propertiesr   deletedimageIdskur   r   )r   replacer   r   r   boolr   update)	r   r   r/   r0   r1   r2   r   r   variantr   r   r   convertVariant6   s   r7   c                 C   s   t d| d}|j| dd |d}dd | D }dd |D }t||}|D ]}||tdtd	 q.|D ]}| d|d
< ||dj|dd q?|j	S )Nproductsr   T)mergevariantsc                 S   s   g | ]}|j qS r   r   .0r6   r   r   r   
<listcomp>Y   s    zsaveProduct.<locals>.<listcomp>c                 S   s   g | ]}| d qS r;   )getr<   r   r   r   r>   Z   s    )r0   r   	productId)
r   
collectiondocumentr?   setfindDifferenceInTwoListsr5   r   r   r   )r   r:   refvariantsRefexistingVariantIdscurrentVariantIdsdeletedVariantsr6   r   r   r   saveProductU   s   

rJ   c                    s    fdd| D S )Nc                    s   g | ]}| vr|qS r   r   )r=   itemlist2r   r   r>   d   s    z,findDifferenceInTwoLists.<locals>.<listcomp>r   )list1rM   r   rL   r   rD   c   s   rD   c                 C   s   | | | S Nr   )r
   r   r   r   r   r   r   f   s   r   c                 C   s   |r+t ddd|dd| dd|dd| }t|dkr)|d  S i S t ddd|dd| dd| }t|dkrM|d  S i S )Nr8   r   ==r	   r
   r   r   )r   rA   wherer?   lento_dict)r	   r
   r   r   rE   r   r   r   findProducti   s   6,rT   c                 C   s:   t d|  }t|jj |jr| S td i S )Nr8   zProduct not found)	r   rA   rB   r?   print	referencepathexistsrS   r@   rE   r   r   r   getProductByIds   s   rZ   c                 C   s   t ddd| dd| }t|dkr<|d }|jd| }|jd| }| | | fS td	| d
)NmanualProductsr	   rP   skusarray_containsr   r:   variantsMappingzProduct not found for sku i  )	r   rA   rQ   r?   rR   rV   rB   rS   r   )r	   r2   rE   thisrefr6   r^   r   r   r   getProductBySku{   s   "r`   c                 C   sD   t | t |} }td| d| }|jr | S i S )Nr8   r:   r   r   rA   rB   r?   rX   rS   r@   	variantIdrE   r   r   r   getProductVariant       rd   c                 C   sR   t ddd|  }|r'|d }t d|jd| }| S d S )Nr8   r   rP   r   r:   )r   rA   rQ   r?   rB   r   rS   )r   platformVariantIdr   r6   r   r   r   getProductVariantByPlatformId   s   "rg   returnc                 C   s(   t d| d }dd |D S )Nr8   r:   c                 S      g | ]}|  qS r   rS   )r=   rr   r   r   r>          z&getProductVariants.<locals>.<listcomp>)r   rA   rB   r?   rY   r   r   r   getProductVariants   s   rm   c                 C   sD   t d|  }|jr| }|r|t| |fS |i fS i i fS )NproductsMapping)r   rA   rB   r?   rX   rS   getVariantMapping)r@   rc   rE   mappingr   r   r   getProductsMapping   s   rq   c                 C   sD   t d|  }|jr | }|dd |jd D fS d S )Nrn   c                 S   ri   r   rj   )r=   vr   r   r   r>      rl   z-getProductVariantsMapping.<locals>.<listcomp>r^   )r   rA   rB   r?   rX   rS   rV   )r@   rE   rp   r   r   r   getProductVariantsMapping   s
   rs   c                 C   sZ   t d| d| }|jr| S t d|  d|  }|jr+| S i S )Nrn   r^   manualProducts/z/variantsMapping/r   rA   rB   r?   rX   rS   rb   r   r   r   ro      s
    ro   c                 C   s&   t d|  }|jr| S d S )Nr[   ru   rY   r   r   r   getManualProductById   s   rv   c                 C   sD   t | t |} }td| d| }|jr | S d S )Nr[   r:   ra   rb   r   r   r   getManualProductVariant   re   rw   c                 C   s   |s|sg S t d| d|  }d }|jr'| d}t|dd}t d|  d|  }|jsDt d|  d|  }|jrO| d	g S |rxt d|  d
|  }|jrx| di |i }|rxdd | D S g S )NzblankProducts/z/blankVariants/colorr-   r.   rt   z/colorImages/zproductsMapping/r   z/placements/previewImagesc                 S   s*   g | ]\}}t |d |d|dqS )r   r!   )r   r!   	placement)r   r?   )r=   keyr&   r   r   r   r>      s    z"getColorImages.<locals>.<listcomp>)r   rB   r?   rX   rS   r   r3   items)r@   blankProductIdblankVariantIdblankVariantrx   rE   ry   r   r   r   getColorImages   s"    
r   )updateShopProductsc                 C   sl   |  d d}t|d}dd |D dd |D dd |D dd |D d	d |D d
d |D d}|S )NcurrentUserr	   Tc                 S   (   g | ]}| d dkrtt|qS )r
   1)r?   updateUserEtsyProductsr   	from_dictr=   shopr   r   r   r>         ( z&updateUserProducts.<locals>.<listcomp>c                 S   "   g | ]}| d dkrt|qS )r
   2)r?   updateUserShopifyProductsr   r   r   r   r>         " c                 S   r   )r
   3)r?   updateUserWoocommerceProductsr   r   r   r   r>      r   c                 S   r   )r
   4)r?   updateUserSquareProductsr   r   r   r   r>      r   c                 S   r   )r
   10)r?   updateUserSquarespaceProductsr   r   r   r   r   r   r>      r   c                 S   r   )r
   8)r?   updateUserBigcartelProductsr   r   r   r   r   r   r>      r   )EtsyShopifyWooCommerceSquareSquarespace	Bigcartel)r?   r   )paramsr	   shopsresponser   r   r   updateUserProducts   s   
r   )NFrO   )NN)0r   configs.firebaser   r   functions.Responser   functions.Shopsr   !functions.Suppliers.BlankProductsr   V2.functions.Shops.mainr   r   r    r%   r(   r7   rJ   rD   r   rT   rZ   r`   rd   rg   r   r   rm   rq   rs   ro   rv   rw   r   functions.Shopify.Productsr   r   functions.Square.Productsr   functions.WooCommerce.Productsr   V2.functions.Bigcartel.Productsr   V2.functions.Etsy.Productsr   !V2.functions.Squarespace.Productsr   r   r   r   r   r   <module>   s|    	
!



	

