File size: 1,770 Bytes
6793beb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Send-reply node — sends the final response to the customer via email.
"""

from loguru import logger

from src.schemas.email import OutgoingEmail
from src.schemas.state import SupportState, TicketStatus
from src.services.email_service import send_reply


async def send_email_reply(state: SupportState) -> dict:
    """
    Send the drafted response to the customer.

    Builds an OutgoingEmail from the current state and dispatches
    it through the email service. The draft becomes the final response.
    """
    logger.info("Attempting to send reply for ticket")

    # The draft is promoted to the final response
    final_response = state.draft_response

    # Build the outgoing email
    outgoing = OutgoingEmail(
        to=state.email.sender,
        subject=f"Re: {state.email.subject}",
        body=final_response,
        in_reply_to=state.email.message_id,
    )

    # Attempt to send
    try:
        sent = await send_reply(outgoing)
        if sent:
            logger.info("Reply sent successfully")
            return {
                "final_response": final_response,
                "status": TicketStatus.SENT,
            }
        else:
            # Credentials not configured — mark as approved but unsent
            logger.info("Email sending skipped (credentials not configured)")
            return {
                "final_response": final_response,
                "status": TicketStatus.APPROVED,
            }
    except Exception as e:
        logger.error("Failed to send reply: {}", e)
        return {
            "final_response": final_response,
            "status": TicketStatus.SEND_FAILED,
            "metadata": {
                **state.metadata,
                "send_error": str(e),
            },
        }