Supabase REST manda response gzip por default. Faraday default não tem middleware de descompressão, então JSON.parse(gzip_bytes) explodia em JSON::ParserError, capturado pelo rescue → array vazio silencioso. OfferService#fetch_unidade retornava [] mesmo com row presente, caindo em "Sem unidade vinculada — tenant não resolvido". Fix em offer_service, weekly_report_service, notify_revealed_job e notify_revealed_scheduler_job. (get_reserva_preco_tool já tinha o fix.) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
63 lines
1.8 KiB
Ruby
63 lines
1.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Cron de fallback (a cada 5 min): enfileira NotifyRevealedJob pra qualquer
|
|
# draw que foi revelado há >60s e ainda não foi notificado. Cobre cenários onde
|
|
# o front não conseguiu chamar o endpoint (browser fechado, rede caiu).
|
|
class Captain::Roleta::NotifyRevealedSchedulerJob < ApplicationJob
|
|
DEFAULT_SCHEMA = 'reserva_hotel'
|
|
|
|
queue_as :scheduled_jobs
|
|
|
|
def perform
|
|
pending = fetch_pending_tokens
|
|
return if pending.blank?
|
|
|
|
Rails.logger.info "[NotifyRevealedScheduler] enfileirando #{pending.size} drawn(s)"
|
|
pending.each { |row| Captain::Roleta::NotifyRevealedJob.perform_later(row['token']) }
|
|
end
|
|
|
|
private
|
|
|
|
def fetch_pending_tokens
|
|
body = {
|
|
:select => 'token',
|
|
:status => 'eq.revealed',
|
|
:notified_at => 'is.null',
|
|
'revealed_at' => "lt.#{1.minute.ago.iso8601}",
|
|
:limit => 100
|
|
}
|
|
supabase_get('roulette_draws', body)
|
|
rescue StandardError => e
|
|
Rails.logger.warn("[NotifyRevealedScheduler] falha: #{e.class} - #{e.message}")
|
|
[]
|
|
end
|
|
|
|
def supabase_get(table, query)
|
|
url = "#{supabase_url}/rest/v1/#{table}"
|
|
response = Faraday.get(url, query) do |req|
|
|
req.headers['apikey'] = supabase_key
|
|
req.headers['Authorization'] = "Bearer #{supabase_key}"
|
|
req.headers['Accept-Profile'] = supabase_schema
|
|
req.headers['Accept'] = 'application/json'
|
|
req.headers['Accept-Encoding'] = 'identity'
|
|
end
|
|
return [] unless response.success?
|
|
|
|
JSON.parse(response.body)
|
|
rescue JSON::ParserError
|
|
[]
|
|
end
|
|
|
|
def supabase_url
|
|
ENV.fetch('RESERVA_1001_SUPABASE_URL').chomp('/')
|
|
end
|
|
|
|
def supabase_key
|
|
ENV.fetch('RESERVA_1001_SUPABASE_ANON_KEY')
|
|
end
|
|
|
|
def supabase_schema
|
|
ENV.fetch('RESERVA_1001_SUPABASE_SCHEMA', DEFAULT_SCHEMA)
|
|
end
|
|
end
|