iachat/enterprise/app/models/captain/unit.rb
Rodrigo Borba 44908f32d1 feat: sistema de notificações de reserva com templates configuráveis
- Adiciona check_in_at/duration_hours ao schema do tool CreateReservationIntent
  para que a IA capture o horário EXATO de chegada informado pelo cliente
- Cria captain_notification_templates: label, content, timing_minutes,
  timing_direction (before/after), active, position
- Implementa SendNotificationService com interpolação de variáveis
  (guest_name, check_in_time, check_out_time, suite_name, unit_name)
- Implementa NotificationScannerJob (Sidekiq-cron a cada 5min) com
  janela de tolerância de ±5min e idempotência via metadata JSONB
- API REST: /captain/units/:unit_id/notification_templates (CRUD)
- Store Vuex captainNotificationTemplates + API client
- UI: página de gestão de templates com editor inline e botão '+'
- Configura rota captain_settings_notifications
- i18n PT/EN para todas as strings novas
- Rubocop e ESLint: zero offenses
2026-03-01 21:53:11 -03:00

114 lines
4.3 KiB
Ruby

# == Schema Information
#
# Table name: captain_units
#
# id :bigint not null, primary key
# inter_account_number :string
# inter_cert_content :text
# inter_cert_path :string
# inter_client_secret :string
# inter_key_content :text
# inter_key_path :string
# inter_pix_key :string
# last_synced_at :datetime
# leader_whatsapp :string
# name :string not null
# payment_receipt_review_enabled :boolean default(FALSE), not null
# plug_play_token :string
# proactive_pix_polling_enabled :boolean default(FALSE), not null
# reservation_source_tag :string
# reservations_sync_enabled :boolean
# status :string
# suite_category_images :jsonb not null
# visible_suite_categories :jsonb not null
# webhook_configured_at :datetime
# webhook_url :string
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# captain_brand_id :bigint not null
# inbox_id :bigint
# inter_client_id :string
# plug_play_id :string
#
# Indexes
#
# index_captain_units_on_account_id (account_id)
# index_captain_units_on_captain_brand_id (captain_brand_id)
# index_captain_units_on_inbox_id (inbox_id)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id)
# fk_rails_... (captain_brand_id => captain_brands.id)
# fk_rails_... (inbox_id => inboxes.id)
#
class Captain::Unit < ApplicationRecord
self.table_name = 'captain_units'
belongs_to :account
belongs_to :brand, class_name: 'Captain::Brand', foreign_key: 'captain_brand_id', inverse_of: :units
has_many :unit_inboxes, class_name: 'Captain::UnitInbox', foreign_key: :captain_unit_id,
inverse_of: :captain_unit, dependent: :destroy
has_many :inboxes, through: :unit_inboxes
has_many :pix_charges, class_name: 'Captain::PixCharge', dependent: :restrict_with_error
has_many :gallery_items, class_name: 'Captain::GalleryItem', foreign_key: :captain_unit_id, inverse_of: :captain_unit,
dependent: :destroy
has_many :notification_templates, class_name: 'Captain::NotificationTemplate', foreign_key: :captain_unit_id,
inverse_of: :unit, dependent: :destroy
encrypts :inter_client_secret
encrypts :inter_account_number
encrypts :inter_cert_content
encrypts :inter_key_content
enum status: { active: 'active', inactive: 'inactive' }, _default: 'active'
validates :name, presence: true
validate :proactive_pix_polling_requires_inter_credentials
def inter_credentials_present?
inter_client_id.present? &&
inter_client_secret.present? &&
inter_pix_key.present? &&
(inter_cert_content.present? || resolved_inter_cert_path.present?) &&
(inter_key_content.present? || resolved_inter_key_path.present?)
end
def resolved_inter_cert_path
resolve_certificate_path(inter_cert_path)
end
def resolved_inter_key_path
resolve_certificate_path(inter_key_path)
end
private
def proactive_pix_polling_requires_inter_credentials
return unless proactive_pix_polling_enabled?
return if inter_credentials_present?
errors.add(
:proactive_pix_polling_enabled,
'só pode ser habilitado quando a integração Inter estiver completa (client id/secret, chave pix, cert e key)'
)
end
# Resolve o path do certificado — suporta caminho absoluto, relativo ao Rails.root
# ou nome de arquivo simples dentro de storage/certs/.
def resolve_certificate_path(path)
return nil if path.blank?
return path if File.exist?(path)
rails_root_path = Rails.root.join(path).to_s
return rails_root_path if File.exist?(rails_root_path)
filename = File.basename(path)
fallback_path = Rails.root.join('storage/certs', filename).to_s
return fallback_path if File.exist?(fallback_path)
path # Retorna original se nenhum caminho for encontrado
end
end