dawarich/app/services/visits/suggest.rb

47 lines
1.3 KiB
Ruby
Raw Normal View History

2024-08-05 15:23:08 -04:00
# frozen_string_literal: true
class Visits::Suggest
2024-08-12 16:18:11 -04:00
attr_reader :points, :user, :start_at, :end_at
def initialize(user, start_at:, end_at:)
@start_at = start_at.to_i
@end_at = end_at.to_i
@points = user.points.not_visited.order(timestamp: :asc).where(timestamp: start_at..end_at)
2024-08-12 16:18:11 -04:00
@user = user
2024-08-05 15:23:08 -04:00
end
def call
2025-03-02 15:24:57 -05:00
visits = Visits::SmartDetect.new(user, start_at:, end_at:).call
2025-07-14 15:15:45 -04:00
create_visits_notification(user) if visits.any?
2024-08-05 15:23:08 -04:00
return nil unless DawarichSettings.reverse_geocoding_enabled?
2024-08-05 15:23:08 -04:00
2025-03-02 15:24:57 -05:00
visits.each(&:async_reverse_geocode)
visits
rescue StandardError => e
# create a notification with stacktrace and what arguments were used
user.notifications.create!(
kind: :error,
title: 'Error suggesting visits',
content: "Error suggesting visits: #{e.message}\n#{e.backtrace.join("\n")}"
)
2025-05-03 14:36:09 -04:00
ExceptionReporter.call(e)
2024-08-05 15:23:08 -04:00
end
private
2024-08-12 16:18:11 -04:00
def create_visits_notification(user)
content = <<~CONTENT
2025-07-14 15:15:45 -04:00
New visits have been suggested based on your location data from #{Time.zone.at(start_at)} to #{Time.zone.at(end_at)}. You can review them on the <a href="/visits" class="link">Visits</a> page.
2024-08-12 16:18:11 -04:00
CONTENT
user.notifications.create!(
kind: :info,
title: 'New visits suggested',
content:
)
end
2024-08-05 15:23:08 -04:00
end