42 lines
1.4 KiB
Ruby
42 lines
1.4 KiB
Ruby
# app/models/notificacao_envio.rb
|
|
#
|
|
# Registro de UMA mensagem disparada. Existe para responder "o motorista
|
|
# recebeu?" — pergunta que hoje só tem resposta no log do Rails, porque o
|
|
# NotificacaoService engole a exceção de propósito (um SMTP fora do ar não pode
|
|
# travar o fechamento de um pagamento).
|
|
#
|
|
# Com o WhatsApp por sessão QR, que cai sozinha e exige repareamento, essa
|
|
# pergunta vira rotina — daí o log ser tabela, não linha de arquivo.
|
|
class NotificacaoEnvio < ApplicationRecord
|
|
STATUS = %w[pendente enviado falhou].freeze
|
|
CANAIS = %w[whatsapp email].freeze
|
|
|
|
belongs_to :evento_notificacao, optional: true
|
|
belongs_to :contato, optional: true
|
|
belongs_to :user, optional: true
|
|
|
|
validates :canal, inclusion: { in: CANAIS }
|
|
validates :status, inclusion: { in: STATUS }
|
|
validates :destino, presence: true
|
|
|
|
scope :recentes, -> { order(created_at: :desc) }
|
|
scope :falhados, -> { where(status: 'falhou') }
|
|
scope :enviados, -> { where(status: 'enviado') }
|
|
scope :no_periodo, ->(inicio, fim) {
|
|
where(created_at: inicio.to_date.beginning_of_day..fim.to_date.end_of_day)
|
|
}
|
|
|
|
def marcar_enviado!
|
|
update!(status: 'enviado', enviado_em: Time.current, erro: nil,
|
|
tentativas: tentativas + 1)
|
|
end
|
|
|
|
def marcar_falha!(erro)
|
|
update!(status: 'falhou', erro: erro.to_s.truncate(2000),
|
|
tentativas: tentativas + 1)
|
|
end
|
|
|
|
def enviado? = status == 'enviado'
|
|
def falhou? = status == 'falhou'
|
|
end
|