2020-06-02 13:37:38 +03:00
|
|
|
defmodule Plausible.Workers.SendTrialNotifications do
|
|
|
|
use Plausible.Repo
|
2020-07-21 09:58:00 +03:00
|
|
|
|
2020-06-20 20:33:12 +03:00
|
|
|
use Oban.Worker,
|
|
|
|
queue: :trial_notification_emails,
|
|
|
|
max_attempts: 1
|
2020-07-21 09:58:00 +03:00
|
|
|
|
2020-06-02 13:37:38 +03:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
@impl Oban.Worker
|
2021-04-26 11:32:18 +03:00
|
|
|
def perform(_job) do
|
2020-06-08 10:35:13 +03:00
|
|
|
users =
|
|
|
|
Repo.all(
|
|
|
|
from u in Plausible.Auth.User,
|
|
|
|
left_join: s in Plausible.Billing.Subscription,
|
|
|
|
on: s.user_id == u.id,
|
|
|
|
where: is_nil(s.id),
|
|
|
|
order_by: u.inserted_at
|
|
|
|
)
|
2020-06-02 13:37:38 +03:00
|
|
|
|
|
|
|
for user <- users do
|
|
|
|
case Timex.diff(user.trial_expiry_date, Timex.today(), :days) do
|
2020-06-08 10:35:13 +03:00
|
|
|
7 ->
|
2021-06-16 15:00:07 +03:00
|
|
|
if Plausible.Auth.has_active_sites?(user, [:owner]) do
|
2020-06-02 13:37:38 +03:00
|
|
|
send_one_week_reminder(user)
|
|
|
|
end
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
1 ->
|
2021-06-16 15:00:07 +03:00
|
|
|
if Plausible.Auth.has_active_sites?(user, [:owner]) do
|
2020-06-02 13:37:38 +03:00
|
|
|
send_tomorrow_reminder(user)
|
|
|
|
end
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
0 ->
|
2021-06-16 15:00:07 +03:00
|
|
|
if Plausible.Auth.has_active_sites?(user, [:owner]) do
|
2020-06-02 13:37:38 +03:00
|
|
|
send_today_reminder(user)
|
|
|
|
end
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
-1 ->
|
2021-06-16 15:00:07 +03:00
|
|
|
if Plausible.Auth.has_active_sites?(user, [:owner]) do
|
2020-06-02 13:37:38 +03:00
|
|
|
send_over_reminder(user)
|
|
|
|
end
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2020-06-02 13:37:38 +03:00
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
defp send_one_week_reminder(user) do
|
|
|
|
PlausibleWeb.Email.trial_one_week_reminder(user)
|
2021-12-17 12:16:18 +03:00
|
|
|
|> Plausible.Mailer.send_email_safe()
|
2020-06-02 13:37:38 +03:00
|
|
|
end
|
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
defp send_tomorrow_reminder(user) do
|
2021-01-15 17:28:57 +03:00
|
|
|
usage = Plausible.Billing.usage_breakdown(user)
|
2020-06-02 13:37:38 +03:00
|
|
|
|
|
|
|
PlausibleWeb.Email.trial_upgrade_email(user, "tomorrow", usage)
|
2021-12-17 12:16:18 +03:00
|
|
|
|> Plausible.Mailer.send_email_safe()
|
2020-06-02 13:37:38 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
defp send_today_reminder(user) do
|
2021-01-15 17:28:57 +03:00
|
|
|
usage = Plausible.Billing.usage_breakdown(user)
|
2020-06-02 13:37:38 +03:00
|
|
|
|
|
|
|
PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2021-12-17 12:16:18 +03:00
|
|
|
|> Plausible.Mailer.send_email_safe()
|
2020-06-02 13:37:38 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
defp send_over_reminder(user) do
|
|
|
|
PlausibleWeb.Email.trial_over_email(user)
|
2021-12-17 12:16:18 +03:00
|
|
|
|> Plausible.Mailer.send_email_safe()
|
2020-06-02 13:37:38 +03:00
|
|
|
end
|
|
|
|
end
|