2021-02-12 11:17:53 +03:00
defmodule Plausible.Workers.CheckUsage do
use Plausible.Repo
use Oban.Worker , queue : :check_usage
defmacro yesterday ( ) do
quote do
fragment ( " now() - INTERVAL '1 day' " )
end
end
defmacro last_day_of_month ( day ) do
quote do
fragment (
" (date_trunc('month', ?::date) + interval '1 month' - interval '1 day')::date " ,
unquote ( day )
)
end
end
defmacro day_of_month ( date ) do
quote do
fragment ( " EXTRACT(day from ?::date) " , unquote ( date ) )
end
end
defmacro least ( left , right ) do
quote do
fragment ( " least(?, ?) " , unquote ( left ) , unquote ( right ) )
end
end
@impl Oban.Worker
2021-04-26 11:32:18 +03:00
def perform ( _job , billing_mod \\ Plausible.Billing , today \\ Timex . today ( ) ) do
2021-03-29 14:53:34 +03:00
yesterday = today |> Timex . shift ( days : - 1 )
2021-02-12 11:17:53 +03:00
active_subscribers =
Repo . all (
from u in Plausible.Auth.User ,
join : s in Plausible.Billing.Subscription ,
on : s . user_id == u . id ,
where : s . status == " active " ,
2021-03-01 17:51:57 +03:00
where : not is_nil ( s . last_bill_date ) ,
2021-02-12 11:17:53 +03:00
# Accounts for situations like last_bill_date==2021-01-31 AND today==2021-03-01. Since February never reaches the 31st day, the account is checked on 2021-03-01.
where :
least ( day_of_month ( s . last_bill_date ) , day_of_month ( last_day_of_month ( ^ yesterday ) ) ) ==
day_of_month ( ^ yesterday ) ,
preload : [ subscription : s ]
)
for subscriber <- active_subscribers do
allowance = Plausible.Billing.Plans . allowance ( subscriber . subscription )
{ last_last_month , last_month } = billing_mod . last_two_billing_months_usage ( subscriber )
if last_last_month > allowance && last_month > allowance do
2021-02-18 16:46:58 +03:00
{ _ , last_cycle } = billing_mod . last_two_billing_cycles ( subscriber )
2021-05-06 11:46:22 +03:00
suggested_plan = Plausible.Billing.Plans . suggested_plan ( subscriber , last_month )
2021-05-11 11:30:47 +03:00
2021-05-06 11:46:22 +03:00
template =
PlausibleWeb.Email . over_limit_email ( subscriber , last_month , last_cycle , suggested_plan )
2021-05-10 12:52:42 +03:00
try do
Plausible.Mailer . send_email ( template )
rescue
_ -> nil
end
2021-02-12 11:17:53 +03:00
end
end
:ok
end
end