63 lines
2.9 KiB
Ruby
63 lines
2.9 KiB
Ruby
# Configuração de SMTP e WhatsApp (Twilio) gravada no BANCO, editável pelo ADM
|
|
# em /admin/configuracao_notificacao — antes disso tudo vinha só do .env e mudar
|
|
# um servidor de e-mail exigia editar o arquivo no servidor e reiniciar.
|
|
#
|
|
# Tabela SINGLETON: uma linha só. `singleton_guard` com índice único é o que
|
|
# impede dois workers Puma criarem linhas concorrentes no primeiro acesso.
|
|
#
|
|
# As credenciais NÃO são importadas do ENV aqui de propósito: os campos nascem
|
|
# vazios e `smtp_ativo`/`whatsapp_ativo` respeitam as flags antigas, então
|
|
# `smtp_pronto?`/`whatsapp_pronto?` dão false e o app segue usando o .env
|
|
# exatamente como hoje até o admin preencher a tela.
|
|
class CreateConfiguracaoNotificacoes < ActiveRecord::Migration[7.1]
|
|
def up
|
|
create_table :configuracao_notificacoes do |t|
|
|
t.integer :singleton_guard, null: false, default: 0
|
|
|
|
# ── E-mail / SMTP ───────────────────────────────────────
|
|
t.boolean :smtp_ativo, null: false, default: false
|
|
t.string :smtp_address
|
|
t.integer :smtp_port, null: false, default: 587
|
|
t.string :smtp_username
|
|
t.text :smtp_password_cifrado # AES-256-GCM (concern AtributoCifrado)
|
|
t.string :smtp_domain
|
|
t.string :smtp_autenticacao, null: false, default: 'plain'
|
|
t.string :remetente_email
|
|
t.string :remetente_nome
|
|
|
|
# ── Destinatários administrativos ───────────────────────
|
|
t.string :email_admin
|
|
t.string :whatsapp_admin
|
|
|
|
# ── Notificação aos motoristas ──────────────────────────
|
|
t.boolean :email_notificacoes_ativo, null: false, default: false
|
|
|
|
# ── WhatsApp / Twilio ───────────────────────────────────
|
|
t.boolean :whatsapp_ativo, null: false, default: false
|
|
t.string :twilio_account_sid
|
|
t.text :twilio_auth_token_cifrado # AES-256-GCM
|
|
t.string :twilio_from
|
|
|
|
t.timestamps
|
|
end
|
|
|
|
add_index :configuracao_notificacoes, :singleton_guard, unique: true
|
|
|
|
# Herda as flags que hoje moram em `configuracoes` (chave/valor) e que nunca
|
|
# tiveram UI. SQL cru de propósito: migration não deve depender de model.
|
|
whats = select_value("SELECT valor FROM configuracoes WHERE chave = 'notificacao_whatsapp'")
|
|
email = select_value("SELECT valor FROM configuracoes WHERE chave = 'notificacao_email'")
|
|
|
|
execute(<<~SQL.squish)
|
|
INSERT INTO configuracao_notificacoes
|
|
(singleton_guard, whatsapp_ativo, email_notificacoes_ativo, smtp_ativo,
|
|
smtp_port, smtp_autenticacao, created_at, updated_at)
|
|
VALUES (0, #{whats == 'true'}, #{email == 'true'}, false, 587, 'plain', NOW(), NOW())
|
|
SQL
|
|
end
|
|
|
|
def down
|
|
drop_table :configuracao_notificacoes
|
|
end
|
|
end
|