@@ -67,36 +67,43 @@ async def process_message(self, payload: TriageInputDTO) -> TriageResponseDTO:
6767
6868 current_state = TriageState (step ) if step is not None else None
6969
70- if payload .answer_text :
70+ if payload .answer_text is not None :
7171 last_interaction ["answer_text" ] = payload .answer_text
72- if payload .answer_value :
72+ if payload .answer_value is not None :
7373 last_interaction ["answer_value" ] = payload .answer_value
7474
7575 user_message = payload .answer_value if payload .answer_value else (payload .answer_text or "" )
7676
7777 bot_response = ChatbotFSM .process_interaction (current_state , user_message )
7878
79- new_question : dict [str , Any ] = {
80- "step" : bot_response .new_state .value ,
81- "question" : bot_response .response_text ,
82- "answer_text" : None ,
83- "answer_value" : None ,
84- "type" : "free_text" if bot_response .is_free_text else "quick_replies"
85- }
86-
87- triage .append (new_question )
88- attendance ["triage" ] = triage
79+ if not bot_response .is_finished :
80+ new_question : dict [str , Any ] = {
81+ "step" : bot_response .new_state .value if bot_response .new_state else "UNKNOWN" ,
82+ "question" : bot_response .response_text ,
83+ "answer_text" : None ,
84+ "answer_value" : None ,
85+ "type" : "free_text" if bot_response .is_free_text else "quick_replies"
86+ }
87+ triage .append (new_question )
8988
90- await self . repository . save_attendance ( payload . triage_id , attendance )
89+ attendance [ "triage" ] = triage
9190
9291 ticket_id = None
9392 if bot_response .new_state == TriageState .TICKET_CREATED :
9493 free_text_context = payload .answer_text if payload .answer_text else "Solicitação criada via URA"
9594 ticket_id = await self ._generate_ticket_with_context (attendance , free_text_context , payload .triage_id )
9695
97- formatted_step_id = f"step_{ bot_response .new_state .value .lower ()} "
96+ # Resolve o format do step id atual (fallback para unknown se for nulo)
97+ formatted_step_id = f"step_{ bot_response .new_state .value .lower ()} " if bot_response .new_state else "step_unknown"
9898
9999 if bot_response .is_finished :
100+ attendance ["status" ] = "finished"
101+ attendance ["end_date" ] = datetime .now (timezone .utc )
102+ attendance ["result" ] = {
103+ "type" : "Ticket" if ticket_id else "Resolved" ,
104+ "closure_message" : bot_response .response_text
105+ }
106+
100107 data = TriageData (
101108 triage_id = payload .triage_id ,
102109 finished = True ,
@@ -115,6 +122,8 @@ async def process_message(self, payload: TriageInputDTO) -> TriageResponseDTO:
115122 input = input_def
116123 )
117124
125+ await self .repository .save_attendance (payload .triage_id , attendance )
126+
118127 meta = TriageResponseMeta (
119128 timestamp = datetime .now (timezone .utc ).strftime ("%Y-%m-%dT%H:%M:%SZ" ),
120129 success = True ,
0 commit comments