99import dev .matheuscruz .domain .UserRepository ;
1010import dev .matheuscruz .infra .ai .TextAiService ;
1111import dev .matheuscruz .infra .ai .data .AiOperations ;
12- import dev .matheuscruz .infra .ai .data .ContextMessage ;
13- import dev .matheuscruz .infra .ai .data .RecordInfo ;
12+ import dev .matheuscruz .infra .ai .data .AllRecognizedOperations ;
13+ import dev .matheuscruz .infra .ai .data .RecognizedOperation ;
14+ import dev .matheuscruz .infra .ai .data .RecognizedTransaction ;
1415import dev .matheuscruz .infra .ai .data .SimpleMessage ;
1516import io .quarkus .narayana .jta .QuarkusTransaction ;
1617import io .quarkus .scheduler .Scheduled ;
@@ -36,7 +37,7 @@ public class SQS {
3637
3738 private static final ObjectReader INCOMING_MESSAGE_READER = new ObjectMapper ().readerFor (IncomingMessage .class );
3839
39- private static final ObjectReader AI_RESPONSE_READER = new ObjectMapper ().readerFor (ContextMessage .class );
40+ private static final ObjectReader AI_RESPONSE_READER = new ObjectMapper ().readerFor (RecognizedOperation .class );
4041
4142 public SQS (SqsClient sqs , @ ConfigProperty (name = "whatsapp.incoming-messages.queue-url" ) String incomingMessagesUrl ,
4243 @ ConfigProperty (name = "whatsapp.messages-processed.queue-url" ) String messagesProcessedUrl ,
@@ -76,31 +77,35 @@ private void processMessage(String body, String receiptHandle) {
7677
7778 private void handleUserMessage (User user , IncomingMessage message , String receiptHandle ) {
7879 try {
79- ContextMessage contextMessage = parseAiResponse (aiService .handleMessage (message .messageBody ()));
80-
81- switch (contextMessage .operation ()) {
82- case AiOperations .ADD_TRANSACTION ->
83- processTransactionMessage (user , message , receiptHandle , contextMessage );
84- case AiOperations .GET_BALANCE -> processSimpleMessage (user , message , receiptHandle , contextMessage );
85- default -> logger .warnf ("Unknown operation type: %s" , contextMessage .operation ());
80+ AllRecognizedOperations allRecognizedOperations = aiService .handleMessage (message .messageBody ());
81+
82+ for (RecognizedOperation recognizedOperation : allRecognizedOperations .all ()) {
83+ switch (recognizedOperation .operation ()) {
84+ case AiOperations .ADD_TRANSACTION ->
85+ processAddTransactionMessage (user , message , receiptHandle , recognizedOperation );
86+ case AiOperations .GET_BALANCE -> {
87+ logger .info ("Processing GET_BALANCE operation" + recognizedOperation .recognizedTransaction ());
88+ processSimpleMessage (user , message , receiptHandle , recognizedOperation );
89+ }
90+ default -> logger .warnf ("Unknown operation type: %s" , recognizedOperation .operation ());
91+ }
8692 }
8793
8894 } catch (Exception e ) {
8995 logger .error ("Failed to process message: " + message .messageId (), e );
9096 }
9197 }
9298
93- private void processTransactionMessage (User user , IncomingMessage message , String receiptHandle ,
94- ContextMessage contextMessage ) throws IOException {
95- RecordInfo transaction = objectMapper .readValue (contextMessage .content (), RecordInfo .class );
96-
97- sendProcessedMessage (
98- new TransactionMessageProcessed (AiOperations .ADD_TRANSACTION .commandName (), message .messageId (),
99- MessageStatus .PROCESSED , user .getPhoneNumber (), transaction .withError (), transaction ));
99+ private void processAddTransactionMessage (User user , IncomingMessage message , String receiptHandle ,
100+ RecognizedOperation recognizedOperation ) throws IOException {
101+ RecognizedTransaction recognizedTransaction = recognizedOperation .recognizedTransaction ();
102+ sendProcessedMessage (new TransactionMessageProcessed (AiOperations .ADD_TRANSACTION .commandName (),
103+ message .messageId (), MessageStatus .PROCESSED , user .getPhoneNumber (), recognizedTransaction .withError (),
104+ recognizedTransaction ));
100105
101- Record record = new Record .Builder ().userId (user .getId ()).amount (transaction .amount ())
102- .description (transaction .description ()).transaction (transaction .type ()). category ( transaction . category ())
103- .build ();
106+ Record record = new Record .Builder ().userId (user .getId ()).amount (recognizedTransaction .amount ())
107+ .description (recognizedTransaction .description ()).transaction (recognizedTransaction .type ())
108+ .category ( recognizedTransaction . category ()). build ();
104109
105110 QuarkusTransaction .requiringNew ().run (() -> recordRepository .persist (record ));
106111
@@ -110,8 +115,9 @@ private void processTransactionMessage(User user, IncomingMessage message, Strin
110115 }
111116
112117 private void processSimpleMessage (User user , IncomingMessage message , String receiptHandle ,
113- ContextMessage contextMessage ) throws IOException {
114- SimpleMessage response = new SimpleMessage (contextMessage .content ());
118+ RecognizedOperation recognizedOperation ) throws IOException {
119+ logger .infof ("Processing simple message for user %s" , recognizedOperation .recognizedTransaction ());
120+ SimpleMessage response = new SimpleMessage (recognizedOperation .recognizedTransaction ().description ());
115121 sendProcessedMessage (new SimpleMessageProcessed (AiOperations .GET_BALANCE .commandName (), message .messageId (),
116122 MessageStatus .PROCESSED , user .getPhoneNumber (), response ));
117123 deleteMessageUsing (receiptHandle );
@@ -124,14 +130,6 @@ private void sendProcessedMessage(Object processedMessage) throws JsonProcessing
124130 .messageDeduplicationId (UUID .randomUUID ().toString ()).queueUrl (processedMessagesUrl ));
125131 }
126132
127- private ContextMessage parseAiResponse (String response ) {
128- try {
129- return AI_RESPONSE_READER .readValue (response );
130- } catch (IOException e ) {
131- throw new RuntimeException ("Failed to parse AI response" , e );
132- }
133- }
134-
135133 private IncomingMessage parseIncomingMessage (String messageBody ) {
136134 try {
137135 return INCOMING_MESSAGE_READER .readValue (messageBody );
@@ -145,7 +143,7 @@ private void deleteMessageUsing(String receiptHandle) {
145143 }
146144
147145 public record TransactionMessageProcessed (String kind , String messageId , MessageStatus status , String user ,
148- Boolean withError , RecordInfo content ) {
146+ Boolean withError , RecognizedTransaction content ) {
149147 }
150148
151149 public record SimpleMessageProcessed (String kind , String messageId , MessageStatus status , String user ,
0 commit comments