2020-06-02 13:37:38 +03:00
|
|
|
defmodule Plausible.Workers.SendCheckStatsEmails do
|
2020-03-23 12:34:25 +03:00
|
|
|
use Plausible.Repo
|
2020-06-02 13:37:38 +03:00
|
|
|
use Oban.Worker, queue: :check_stats_emails
|
2020-03-23 12:34:25 +03:00
|
|
|
|
2020-06-02 13:37:38 +03:00
|
|
|
@impl Oban.Worker
|
|
|
|
def perform(_args, _job) do
|
2020-03-23 12:34:25 +03:00
|
|
|
q =
|
|
|
|
from(u in Plausible.Auth.User,
|
2020-06-08 10:35:13 +03:00
|
|
|
left_join: ce in "check_stats_emails",
|
|
|
|
on: ce.user_id == u.id,
|
2020-03-23 12:34:25 +03:00
|
|
|
where: is_nil(ce.id),
|
|
|
|
where:
|
2020-06-08 10:35:13 +03:00
|
|
|
u.inserted_at > fragment("(now() at time zone 'utc') - '14 days'::interval") and
|
|
|
|
u.inserted_at < fragment("(now() at time zone 'utc') - '7 days'::interval") and
|
|
|
|
u.last_seen < fragment("(now() at time zone 'utc') - '7 days'::interval"),
|
2020-03-23 12:45:04 +03:00
|
|
|
preload: [sites: :weekly_report]
|
2020-03-23 12:34:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
for user <- Repo.all(q) do
|
2020-03-23 12:45:04 +03:00
|
|
|
enabled_report = Enum.any?(user.sites, fn site -> site.weekly_report end)
|
|
|
|
|
|
|
|
if Plausible.Auth.user_completed_setup?(user) && !enabled_report do
|
2020-06-02 13:37:38 +03:00
|
|
|
send_check_stats_email(user)
|
2020-03-23 12:34:25 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-02 13:37:38 +03:00
|
|
|
:ok
|
2020-03-23 12:34:25 +03:00
|
|
|
end
|
|
|
|
|
2020-06-02 13:37:38 +03:00
|
|
|
defp send_check_stats_email(user) do
|
2020-03-23 12:34:25 +03:00
|
|
|
PlausibleWeb.Email.check_stats_email(user)
|
2020-05-26 16:09:34 +03:00
|
|
|
|> Plausible.Mailer.send_email()
|
2020-03-23 12:34:25 +03:00
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
Repo.insert_all("check_stats_emails", [
|
|
|
|
%{
|
|
|
|
user_id: user.id,
|
|
|
|
timestamp: NaiveDateTime.utc_now()
|
|
|
|
}
|
|
|
|
])
|
2020-03-23 12:34:25 +03:00
|
|
|
end
|
|
|
|
end
|