1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/app/interactors/log_user_session.rb

63 lines
1.2 KiB
Ruby
Raw Normal View History

2019-09-03 09:52:49 -04:00
# frozen_string_literal: true
class LogUserSession
include Interactor
def call
create_session
2019-09-06 15:26:19 -04:00
send_email_alerts
send_telegram_alerts
end
private
def contacts
2019-09-06 15:26:19 -04:00
@contacts ||=
context
.user
.account
.contact_list
.contacts
.where(send_security_notifications: true)
end
def email_contacts
@email_contacts ||=
contacts
.includes(:contact_network)
.where(contact_networks: { codename: :email })
end
def telegram_contacts
@telegram_contacts ||=
contacts
.includes(:contact_network)
.where(contact_networks: { codename: :telegram_id })
end
def create_session
@session = Session.create!(
2019-09-03 09:52:49 -04:00
account: context.user.account,
2019-09-03 10:01:00 -04:00
logged_at: context.user.current_sign_in_at,
2019-09-03 09:52:49 -04:00
ip_address: context.user.current_sign_in_ip,
)
end
2019-09-06 15:26:19 -04:00
def send_email_alerts
email_contacts.each do |contact|
NotificationMailer.signed_in(contact.value, @session).deliver_now
rescue
nil
end
end
2019-09-06 15:26:19 -04:00
def send_telegram_alerts
telegram_contacts.each do |contact|
SendTelegramMessage.call(
chat_id: contact.value,
text: I18n.translate(:new_sign_in),
)
end
end
2019-09-03 09:52:49 -04:00
end