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,
|
2022-04-18 11:05:08 +03:00
|
|
|
where: not is_nil(u.trial_expiry_date),
|
2020-06-08 10:35:13 +03:00
|
|
|
where: is_nil(s.id),
|
|
|
|
order_by: u.inserted_at
|
|
|
|
)
|
2020-06-02 13:37:38 +03:00
|
|
|
|
|
|
|
for user <- users do
|
2024-07-23 10:02:14 +03:00
|
|
|
case Timex.diff(user.trial_expiry_date, Date.utc_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)
|
2022-10-24 13:13:23 +03:00
|
|
|
|> Plausible.Mailer.send()
|
2020-06-02 13:37:38 +03:00
|
|
|
end
|
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
defp send_tomorrow_reminder(user) do
|
2024-06-17 09:25:46 +03:00
|
|
|
usage = Plausible.Billing.Quota.Usage.usage_cycle(user, :last_30_days)
|
2020-06-02 13:37:38 +03:00
|
|
|
|
|
|
|
PlausibleWeb.Email.trial_upgrade_email(user, "tomorrow", usage)
|
2022-10-24 13:13:23 +03:00
|
|
|
|> Plausible.Mailer.send()
|
2020-06-02 13:37:38 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
defp send_today_reminder(user) do
|
2024-06-17 09:25:46 +03:00
|
|
|
usage = Plausible.Billing.Quota.Usage.usage_cycle(user, :last_30_days)
|
2020-06-02 13:37:38 +03:00
|
|
|
|
|
|
|
PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2022-10-24 13:13:23 +03:00
|
|
|
|> Plausible.Mailer.send()
|
2020-06-02 13:37:38 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
defp send_over_reminder(user) do
|
|
|
|
PlausibleWeb.Email.trial_over_email(user)
|
2022-10-24 13:13:23 +03:00
|
|
|
|> Plausible.Mailer.send()
|
2020-06-02 13:37:38 +03:00
|
|
|
end
|
|
|
|
end
|