Ë
    ÿ�j©  ã                   óŒ   — d Z ddlZddlZddlmZ ddlmZ  ej                  e«      Z	dZ
defd„Z	 dded	ed
ededededefd„Zy)a°  A tenant's connected Mailchimp account, and sending through it.

The connection is written by the dashboard's Mailchimp connect flow into the
tenant's own schema -- the same galaxiq_tenants database and per-tenant
`integrations` table that strategist_settings lives beside (see
app/services/infra/database.py's get_setting). This is deliberately NOT
app/services/integrations/platform.py: that module reads/writes a
differently-shaped `integrations` table in the separate galaxiq_master
database, used today only for this backend's own Shopify OAuth writes. A
tenant connecting Mailchimp, Facebook, Pinterest, etc. through the dashboard
never touches that table -- it lands here instead.
é    N)Úsql)Úget_db_connectionz2https://mandrillapp.com/api/1.0/messages/send.jsonÚ	tenant_idc                 ó  — 	 t        «       }	 |j	                  «       5 }|j                  t        j                  d«      j                  t        j                  | «      «      «       |j                  «       }ddd«       |j                  «        r|d   dk7  ry|d   xs i }|j                  d«      xs i j                  d	«      }|sy||j                  d
«      xs d|j                  d«      xs ddœS # t        $ r%}t        j                  d| › d|› �«       Y d}~yd}~ww xY w# 1 sw Y   Œ±xY w# t        $ r5}t        j                  d| › d|› �«       Y d}~|j                  «        yd}~ww xY w# |j                  «        w xY w)aZ  This tenant's active Mailchimp connection, or None.

    Returns {"transactional_api_key", "from_email", "from_name"} -- everything
    send_via_mandrill needs. None covers every reason it cannot be used: no
    connection, a disconnected/revoked one, or a connection made before SMS
    Mode existed that never captured a transactional key.
    z,Could not read the Mailchimp connection for ú: NzISELECT metadata, status FROM {}.integrations WHERE provider = 'mailchimp'é   ÚACTIVEr   ÚsmsÚtransactionalApiKeyÚemailÚ ÚaccountName)Útransactional_api_keyÚ
from_emailÚ	from_name)r   Ú	ExceptionÚloggerÚerrorÚcursorÚexecuter   ÚSQLÚformatÚ
IdentifierÚfetchoneÚcloseÚget)r   ÚconnÚexÚcurÚrowÚmetadataÚapi_keys          úB/var/www/html/strategist-ai/app/services/integrations/mailchimp.pyÚget_mailchimp_connectionr$      sj  € ðÜ Ó"ˆð
Ø�[‰[‹]ð 	!˜cØ�K‰KÜ—‘ð 7ó 8ç‘œŸ™ yÓ1Ó2ô4ð —,‘,“.ˆC÷	!ð 	�
‰
Œá�#�a‘&˜HÒ$Øà�1‰vŠ|˜€Hð �|‰|˜EÓ"Ò( b×-Ñ-Ð.CÓD€GÙØð ")ð
 —l‘l 7Ó+Ò1¨rØ—\‘\ -Ó0Ò6°Bñð øô9 ò Ü�‰ÐCÀIÀ;ÈbÐQSÐPTÐUÔVÜûðú÷
	!ð 	!ûô ò Ü�‰ÐCÀIÀ;ÈbÐQSÐPTÐUÔVÛà�
‰
�ûð	ûð 	�
‰
�úsX   ‚
C3 �D0 �AD$Á4D0 Ã3	D!Ã<DÄD!Ä$D-Ä)D0 Ä0	E.Ä9E)ÅE1 Å)E.Å.E1 Å1FÚ
connectionÚ
to_addressÚsubjectÚtextÚhtmlÚreply_toÚreturnc                 óF  — | d   | d   xs d|ddœg|||dœ}|rd|i|d<   | d	   |d
œ}	 t        j                  t        |d¬«      }|j                  «        |j	                  «       }	|	r|	d   ni }|j                  d«      dvrH|j                  d«      xs |j                  d«      xs d}t        j                  d|› d|› �«       dddœS t        j                  d|› d|›�«       d||dœS # t
        $ r,}
t        j                  d|› d|
› �d¬«       dddœcY d}
~
S d}
~
ww xY w)a¯  Send one email through Mailchimp Transactional (Mandrill).

    Never raises for a delivery failure -- mirrors send_email's own contract,
    since this is one of its branches. Error details are deliberately generic
    (no vendor name, no Mandrill reject reason): the chat model relays a tool
    error's detail straight into what it tells the customer, and neither is
    something a customer waiting on an email should see.
    r   r   NÚto)r   Útype)r   r   r-   r'   r(   r)   zReply-ToÚheadersr   )ÚkeyÚmessageé   )ÚjsonÚtimeoutzMandrill send to z	 failed: T)Úexc_infor   zCould not send the email.)ÚstatusÚdetailr   r6   )ÚsentÚqueuedÚ	scheduledÚreject_reasonÚrejectedzMandrill rejected email to r   zMAILCHIMP EMAIL SENT | to=z | subject=r8   )r6   r-   r'   )
ÚhttpxÚpostÚMANDRILL_SEND_URLÚraise_for_statusr3   r   r   r   r   Úinfo)r%   r&   r'   r(   r)   r*   r1   ÚpayloadÚresponseÚresultsr   ÚoutcomeÚreasons                r#   Úsend_via_mandrillrG   J   sU  € ð ! Ñ.Ø Ñ,Ò4°Ø#¨TÑ2Ð3ØØØñ€Gñ Ø(¨(Ð3ˆ�	Ñà Ð!8Ñ9ÀgÑN€GðJÜ—:‘:Ô/°gÀrÔJˆØ×!Ñ!Ô#Ø—-‘-“/ˆñ $ˆg�aŠj¨€GØ‡{�{�8ÓÐ$CÑCØ—‘˜_Ó-ÒT°·±¸XÓ1FÒTÈ*ˆÜ�‰Ð2°:°,¸bÀÀÐIÔJØ!Ð-HÑIÐIä
‡K�KÐ,¨Z¨L¸ÀGÀ;ÐOÔPØ J¸7ÑCÐCøô ò JÜ�‰Ð(¨¨°I¸b¸TÐBÈTˆÔRØ!Ð-HÑIÕIûðJús   ª<C+ Ã+	D Ã4!DÄD ÄD )N)Ú__doc__Úloggingr=   Úpsycopg2r   Úapp.services.infra.databaser   Ú	getLoggerÚ__name__r   r?   Ústrr$   ÚdictrG   © ó    r#   ú<module>rR      s…   ðñó ã Ý å 9à	ˆ×	Ñ	˜8Ó	$€àHÐ ð.¨ó .ðd =Añ'D $ð 'D°Cð 'DÀ#ð 'DØð'DØ'*ð'DØ69ð'DØEIô'DrQ   