From c76c8d5c50582f10597c5cb2b28682f3533b4c8e Mon Sep 17 00:00:00 2001 From: Rodrigo Borba Date: Sun, 25 Jan 2026 08:20:21 -0300 Subject: [PATCH] =?UTF-8?q?refactor:=20Atualiza=20chamadas=20de=20log=20pa?= =?UTF-8?q?ra=20`Rails.logger`,=20refatora=20interpola=C3=A7=C3=A3o=20de?= =?UTF-8?q?=20strings=20e=20ajusta=20l=C3=B3gica=20de=20status=20de=20rese?= =?UTF-8?q?rva.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../captain/reservations/sync_service.rb | 14 ++++------ .../00_fix_tool_check_availability.rb | 10 +++---- debug_auth.rb | 26 +++++++++---------- debug_jasmine_tool.rb | 14 +++++----- .../captain/tools/check_availability_tool.rb | 22 ++++++---------- force_reset.rb | 6 ++--- 6 files changed, 40 insertions(+), 52 deletions(-) diff --git a/app/services/captain/reservations/sync_service.rb b/app/services/captain/reservations/sync_service.rb index 5552797..3c92195 100644 --- a/app/services/captain/reservations/sync_service.rb +++ b/app/services/captain/reservations/sync_service.rb @@ -116,7 +116,7 @@ module Captain contact = nil # Try finding by phone - contact = @account.contacts.find_by_phone_number(phone) if phone.present? + contact = @account.contacts.find_by(phone_number: phone) if phone.present? # Try finding by email contact = @account.contacts.find_by(email: email) if contact.nil? && email.present? @@ -162,16 +162,12 @@ module Captain return :scheduled unless check_in && check_out - if check_in.to_date == now.to_date - :scheduled # Or 'awaiting_checkin' if we want to be more specific, but MVP 'scheduled' is usually 'Entrada' + if now >= check_out + :completed elsif now >= check_in && now < check_out - :active # 'Hospedada' - elsif now >= check_out - :completed # 'Saída' / checkout done - elsif now < check_in - :scheduled + :active else - :scheduled # Default + :scheduled end end end diff --git a/config/initializers/00_fix_tool_check_availability.rb b/config/initializers/00_fix_tool_check_availability.rb index 10699b6..0417834 100644 --- a/config/initializers/00_fix_tool_check_availability.rb +++ b/config/initializers/00_fix_tool_check_availability.rb @@ -2,7 +2,7 @@ # This ensures the tool is enabled even if the console environment is broken. Rails.application.config.after_initialize do - puts '--- [FIX] Verifying CheckAvailabilityTool Config ---' + Rails.logger.info '--- [FIX] Verifying CheckAvailabilityTool Config ---' begin assistant = Captain::Assistant.first @@ -13,14 +13,14 @@ Rails.application.config.after_initialize do if config.new_record? || !config.is_enabled config.is_enabled = true config.save! - puts "--- [FIX] SUCCESS: check_availability ENABLED for #{assistant.name} ---" + Rails.logger.info "--- [FIX] SUCCESS: check_availability ENABLED for #{assistant.name} ---" else - puts "--- [FIX] SKIPPED: Already enabled for #{assistant.name} ---" + Rails.logger.info "--- [FIX] SKIPPED: Already enabled for #{assistant.name} ---" end else - puts '--- [FIX] WARNING: No Assistant found to fix. ---' + Rails.logger.warn '--- [FIX] WARNING: No Assistant found to fix. ---' end rescue StandardError => e - puts "--- [FIX] ERROR: #{e.message} ---" + Rails.logger.error "--- [FIX] ERROR: #{e.message} ---" end end diff --git a/debug_auth.rb b/debug_auth.rb index 6fc7772..3459338 100644 --- a/debug_auth.rb +++ b/debug_auth.rb @@ -3,31 +3,29 @@ password = 'Password123!' puts '=== DEBUGGING AUTH ===' user = User.find_by(email: email) -puts 'User found via find_by: ' + (user ? 'YES' : 'NO') +puts "User found via find_by: #{user ? 'YES' : 'NO'}" if user - puts 'User ID: ' + user.id.to_s - puts 'User Email: ' + user.email + puts "User ID: #{user.id}" + puts "User Email: #{user.email}" puts 'Checking User.from_email...' begin user_from_method = User.from_email(email) - puts 'User found via User.from_email: ' + (user_from_method ? 'YES' : 'NO') - rescue => e - puts 'Error in User.from_email: ' + e.message + puts "User found via User.from_email: #{user_from_method ? 'YES' : 'NO'}" + rescue StandardError => e + puts "Error in User.from_email: #{e.message}" end - + puts 'Checking valid_password?...' is_valid = user.valid_password?(password) - puts 'Password valid: ' + is_valid.to_s - + puts "Password valid: #{is_valid}" + puts 'Checking active_for_authentication?...' is_active = user.active_for_authentication? - puts 'Active for auth: ' + is_active.to_s - - if !is_active - puts 'User inactive message: ' + user.inactive_message.to_s - end + puts "Active for auth: #{is_active}" + + puts "User inactive message: #{user.inactive_message}" unless is_active else puts 'User not found!' end diff --git a/debug_jasmine_tool.rb b/debug_jasmine_tool.rb index 1bf8085..c93d176 100644 --- a/debug_jasmine_tool.rb +++ b/debug_jasmine_tool.rb @@ -3,13 +3,13 @@ begin inbox = Inbox.first account = inbox.account puts "Testing with Account: #{account.id}, Inbox: #{inbox.id}" - + key = 'status_suites' - + # Create a temporary config or ensure it exists config = Jasmine::ToolConfig.find_or_initialize_by( - account: account, - inbox: inbox, + account: account, + inbox: inbox, tool_key: key ) config.is_enabled = true @@ -22,11 +22,11 @@ begin puts "Config saved. ID: #{config.id}, Token present: #{config.plug_play_token.present?}" runner = Jasmine::ToolRunner.new(inbox, key) - puts "Runner initialized. Running..." + puts 'Runner initialized. Running...' result = runner.run puts "Result: #{result.inspect}" - -rescue => e + +rescue StandardError => e puts "CRITICAL ERROR: #{e.class} - #{e.message}" puts e.backtrace.join("\n") end diff --git a/enterprise/app/services/captain/tools/check_availability_tool.rb b/enterprise/app/services/captain/tools/check_availability_tool.rb index 1107ba9..5cc28bb 100644 --- a/enterprise/app/services/captain/tools/check_availability_tool.rb +++ b/enterprise/app/services/captain/tools/check_availability_tool.rb @@ -38,11 +38,9 @@ module Captain actual_params = resolve_params(args, params) account_id = @conversation&.account_id || @assistant&.account_id - File.open(Rails.root.join('log/tool_debug.log'), 'a') do |f| - f.puts "[#{Time.now}] STARTING CheckAvailabilityTool with params: #{actual_params}" - f.puts "[#{Time.now}] PRICING COUNT: #{Captain::Pricing.where(account_id: account_id).count}" - f.puts "[#{Time.now}] FIRST PRICING: #{Captain::Pricing.where(account_id: account_id).first.inspect}" - end + Rails.logger.info "[CheckAvailabilityTool] STARTING with params: #{actual_params}" + Rails.logger.debug { "[CheckAvailabilityTool] PRICING COUNT: #{Captain::Pricing.where(account_id: account_id).count}" } + Rails.logger.debug { "[CheckAvailabilityTool] FIRST PRICING: #{Captain::Pricing.where(account_id: account_id).first.inspect}" } suite_category = actual_params[:suite] requested_duration = actual_params[:duration].presence # Don't default yet @@ -57,7 +55,7 @@ module Captain # [DATE RESOLUTION] target_date = resolve_target_date(actual_params) - File.open(Rails.root.join('log/tool_debug.log'), 'a') { |f| f.puts "[#{Time.now}] RESOLVED DATE: #{target_date} | SUITE: #{suite_category}" } + Rails.logger.info "[CheckAvailabilityTool] RESOLVED DATE: #{target_date} | SUITE: #{suite_category}" # [DEBUG] Log the context current_inbox_id = @conversation&.inbox_id @@ -96,9 +94,7 @@ module Captain # Use the matched category if found, otherwise stick to the original input (fallback) final_suite_category = matched_category || suite_category - File.open(Rails.root.join('log/tool_debug.log'), 'a') do |f| - f.puts "[#{Time.now}] KEYWORD MATCH: Input='#{suite_category}' -> Resolved='#{final_suite_category}'" - end + Rails.logger.info "[CheckAvailabilityTool] KEYWORD MATCH: Input='#{suite_category}' -> Resolved='#{final_suite_category}'" pricing_scope = Captain::Pricing.where(account_id: account_id) .where('suite_category ILIKE ?', final_suite_category) @@ -130,7 +126,7 @@ module Captain if available_options.present? msg = "Disponível! Para a suíte #{final_suite_category} em #{target_date&.strftime('%d/%m')}, tenho estas opções: #{available_options}. Pergunte qual duração o cliente prefere." - File.open(Rails.root.join('log/tool_debug.log'), 'a') { |f| f.puts "[#{Time.now}] MENU MODE: #{msg}" } + Rails.logger.info "[CheckAvailabilityTool] MENU MODE: #{msg}" return msg else msg = "Não encontrei tarifas para a suíte #{final_suite_category} nesta data. Confirme o nome da suíte." @@ -146,7 +142,7 @@ module Captain final_price, unit: 'R$ ', separator: ',', delimiter: '.' )} (#{pricing.day_range})." persist_last_availability(final_suite_category, requested_duration, pricing, target_date) - File.open(Rails.root.join('log/tool_debug.log'), 'a') { |f| f.puts "[#{Time.now}] SUCCESS: #{msg}" } + Rails.logger.info "[CheckAvailabilityTool] SUCCESS: #{msg}" return msg else available_options = pricing_scope.map do |p| @@ -163,9 +159,7 @@ module Captain return msg end rescue StandardError => e - File.open(Rails.root.join('log/tool_debug.log'), 'a') do |f| - f.puts "[#{Time.now}] CRITICAL ERROR in CheckAvailabilityTool: #{e.message}\n#{e.backtrace.first(5).join("\n")}" - end + Rails.logger.error "[CheckAvailabilityTool] CRITICAL ERROR: #{e.message}\n#{e.backtrace.first(5).join("\n")}" raise e end diff --git a/force_reset.rb b/force_reset.rb index 107305a..7461db5 100644 --- a/force_reset.rb +++ b/force_reset.rb @@ -9,12 +9,12 @@ if u if u.save puts 'User saved successfully.' else - puts 'Error saving user: ' + u.errors.full_messages.join(', ') + puts "Error saving user: #{u.errors.full_messages.join(', ')}" end - + # Reload and verify u.reload - puts 'Password check immediately after save: ' + u.valid_password?(password).to_s + puts "Password check immediately after save: #{u.valid_password?(password)}" else puts 'User NOT FOUND to reset password.' end