2020-06-02 13:37:38 +03:00
|
|
|
defmodule Plausible.Workers.SendSiteSetupEmails do
|
2020-03-23 12:34:25 +03:00
|
|
|
use Plausible.Repo
|
2020-06-02 13:37:38 +03:00
|
|
|
use Oban.Worker, queue: :site_setup_emails
|
2020-03-23 12:34:25 +03:00
|
|
|
require Logger
|
|
|
|
|
2020-06-02 13:37:38 +03:00
|
|
|
@impl Oban.Worker
|
2021-04-26 11:32:18 +03:00
|
|
|
def perform(_job) do
|
2020-06-02 13:37:38 +03:00
|
|
|
send_create_site_emails()
|
|
|
|
send_setup_help_emails()
|
|
|
|
send_setup_success_emails()
|
2020-03-23 12:34:25 +03:00
|
|
|
|
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_create_site_emails() do
|
2020-03-23 12:34:25 +03:00
|
|
|
q =
|
|
|
|
from(s in Plausible.Auth.User,
|
2020-06-08 10:35:13 +03:00
|
|
|
left_join: se in "create_site_emails",
|
|
|
|
on: se.user_id == s.id,
|
2020-03-23 12:34:25 +03:00
|
|
|
where: is_nil(se.id),
|
2020-06-08 10:35:13 +03:00
|
|
|
where:
|
|
|
|
s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval") and
|
|
|
|
s.inserted_at < fragment("(now() at time zone 'utc') - '48 hours'::interval"),
|
2020-03-23 12:34:25 +03:00
|
|
|
preload: :sites
|
|
|
|
)
|
|
|
|
|
|
|
|
for user <- Repo.all(q) do
|
2021-01-07 16:16:04 +03:00
|
|
|
if Enum.empty?(user.sites) do
|
2020-06-02 13:37:38 +03:00
|
|
|
send_create_site_email(user)
|
2020-03-23 12:34:25 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-02 13:37:38 +03:00
|
|
|
defp send_setup_help_emails() do
|
2020-03-23 12:34:25 +03:00
|
|
|
q =
|
|
|
|
from(s in Plausible.Site,
|
2020-06-08 10:35:13 +03:00
|
|
|
left_join: se in "setup_help_emails",
|
|
|
|
on: se.site_id == s.id,
|
2020-03-23 12:34:25 +03:00
|
|
|
where: is_nil(se.id),
|
2021-06-16 15:00:07 +03:00
|
|
|
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval")
|
2020-03-23 12:34:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
for site <- Repo.all(q) do
|
2021-06-16 15:00:07 +03:00
|
|
|
owner =
|
|
|
|
Plausible.Sites.owner_for(site)
|
|
|
|
|> Repo.preload(:subscription)
|
2020-03-23 12:34:25 +03:00
|
|
|
|
2021-09-06 13:54:51 +03:00
|
|
|
setup_completed = Plausible.Sites.has_stats?(site)
|
2020-03-23 12:34:25 +03:00
|
|
|
hours_passed = Timex.diff(Timex.now(), site.inserted_at, :hours)
|
|
|
|
|
|
|
|
if !setup_completed && hours_passed > 47 do
|
2020-06-02 13:37:38 +03:00
|
|
|
send_setup_help_email(owner, site)
|
2020-03-23 12:34:25 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-02 13:37:38 +03:00
|
|
|
defp send_setup_success_emails() do
|
2020-03-23 12:34:25 +03:00
|
|
|
q =
|
|
|
|
from(s in Plausible.Site,
|
2020-06-08 10:35:13 +03:00
|
|
|
left_join: se in "setup_success_emails",
|
|
|
|
on: se.site_id == s.id,
|
2020-03-23 12:34:25 +03:00
|
|
|
where: is_nil(se.id),
|
2021-06-16 15:00:07 +03:00
|
|
|
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval")
|
2020-03-23 12:34:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
for site <- Repo.all(q) do
|
2021-06-16 15:00:07 +03:00
|
|
|
owner =
|
|
|
|
Plausible.Sites.owner_for(site)
|
|
|
|
|> Repo.preload(:subscription)
|
2020-03-23 12:34:25 +03:00
|
|
|
|
2021-09-06 13:54:51 +03:00
|
|
|
if Plausible.Sites.has_stats?(site) do
|
2020-06-02 13:37:38 +03:00
|
|
|
send_setup_success_email(owner, site)
|
2020-03-23 12:34:25 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-02 13:37:38 +03:00
|
|
|
defp send_create_site_email(user) do
|
2020-03-23 12:34:25 +03:00
|
|
|
PlausibleWeb.Email.create_site_email(user)
|
2021-12-17 12:16:18 +03:00
|
|
|
|> Plausible.Mailer.send_email_safe()
|
2020-03-23 12:34:25 +03:00
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
Repo.insert_all("create_site_emails", [
|
|
|
|
%{
|
|
|
|
user_id: user.id,
|
|
|
|
timestamp: NaiveDateTime.utc_now()
|
|
|
|
}
|
|
|
|
])
|
2020-03-23 12:34:25 +03:00
|
|
|
end
|
|
|
|
|
2020-06-02 13:37:38 +03:00
|
|
|
defp send_setup_success_email(user, site) do
|
2020-03-23 12:34:25 +03:00
|
|
|
PlausibleWeb.Email.site_setup_success(user, site)
|
2021-12-17 12:16:18 +03:00
|
|
|
|> Plausible.Mailer.send_email_safe()
|
2020-03-23 12:34:25 +03:00
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
Repo.insert_all("setup_success_emails", [
|
|
|
|
%{
|
|
|
|
site_id: site.id,
|
|
|
|
timestamp: NaiveDateTime.utc_now()
|
|
|
|
}
|
|
|
|
])
|
2020-03-23 12:34:25 +03:00
|
|
|
end
|
|
|
|
|
2020-06-02 13:37:38 +03:00
|
|
|
defp send_setup_help_email(user, site) do
|
2020-03-23 12:34:25 +03:00
|
|
|
PlausibleWeb.Email.site_setup_help(user, site)
|
2021-12-17 12:16:18 +03:00
|
|
|
|> Plausible.Mailer.send_email_safe()
|
2020-03-23 12:34:25 +03:00
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
Repo.insert_all("setup_help_emails", [
|
|
|
|
%{
|
|
|
|
site_id: site.id,
|
|
|
|
timestamp: NaiveDateTime.utc_now()
|
|
|
|
}
|
|
|
|
])
|
2020-03-23 12:34:25 +03:00
|
|
|
end
|
|
|
|
end
|