mirror of
https://github.com/plausible/analytics.git
synced 2024-11-27 09:16:25 +03:00
3d2f356ba7
* rename enterprise?/1 function * change link text to Upgrade when subscription deleted * extract paddle_button and paddle_script components * create a new upgrade-to-enterprise-plan page * extract upgrade_link component * rename function * link to enterprise plan upgrade page from settings ...if the user has an enterprise plan configured * fetch enterprise plan price on the new page * add change_enterprise_plan functionality on the new page * render existing change_enterprise_plan_contact_us.html ...when subscribed to latest configured enterprise plan * rename vars and extract resumable? fn * remove dead billing route * small test refactor: extract convenience fn * add tests for... ...restricting paused and past_due subscription access to the new enterprise plan page. 1. redirect to /settings from the controller action 2. hiding the change-plan link from the user settings * implement redirect to /settings * hide the enterprise upgrade/change-plan link * add tests for a deleted enterprise subscription * plug in the new controller action and delete dead code * optimize for dark mode * fix compile warning * credo fix * display N/A instead of crash when price nil * change subscription.status type to Ecto.Enum Also, create a new `Subscription.Status` module that exposes macros to return the used atom values (prevent typos at compiletime). * fix bug (@conn not available anymore) * use Routes.billing_path where applicable * add a status() type * silence credo * refactor suggestion from review Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Remove the __using__ macro from Subscription.Status ... instead be explicit about requires and aliases and also order the use, import, require, and alias clauses according to https://github.com/christopheradams/elixir_style_guide#module-attribute-ordering * drop the virtual Enteprise 'price_per_interval' field * apply review suggestion to make the code more DRY * use dot syntax to fetch current user in new controller actions * fix formatting --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
73 lines
2.1 KiB
Elixir
73 lines
2.1 KiB
Elixir
defmodule Plausible.Workers.NotifyAnnualRenewal do
|
|
use Plausible.Repo
|
|
use Oban.Worker, queue: :notify_annual_renewal
|
|
require Plausible.Billing.Subscription.Status
|
|
alias Money.Subscription
|
|
alias Plausible.Billing.Subscription
|
|
|
|
@yearly_plans Plausible.Billing.Plans.yearly_product_ids()
|
|
|
|
@impl Oban.Worker
|
|
@doc """
|
|
Sends a notification at most 7 days and at least 1 day before the renewal of an annual subscription
|
|
"""
|
|
def perform(_job) do
|
|
current_subscriptions =
|
|
from(
|
|
s in Plausible.Billing.Subscription,
|
|
group_by: s.user_id,
|
|
select: %{
|
|
user_id: s.user_id,
|
|
inserted_at: max(s.inserted_at)
|
|
}
|
|
)
|
|
|
|
sent_notification =
|
|
from(
|
|
s in "sent_renewal_notifications",
|
|
where: s.timestamp > fragment("now() - INTERVAL '1 month'")
|
|
)
|
|
|
|
users =
|
|
Repo.all(
|
|
from u in Plausible.Auth.User,
|
|
join: cs in subquery(current_subscriptions),
|
|
on: cs.user_id == u.id,
|
|
join: s in Plausible.Billing.Subscription,
|
|
on: s.inserted_at == cs.inserted_at,
|
|
left_join: sent in ^sent_notification,
|
|
on: s.user_id == sent.user_id,
|
|
where: is_nil(sent.id),
|
|
where: s.paddle_plan_id in @yearly_plans,
|
|
where:
|
|
s.next_bill_date > fragment("now()::date") and
|
|
s.next_bill_date <= fragment("now()::date + INTERVAL '7 days'"),
|
|
preload: [subscription: s]
|
|
)
|
|
|
|
for user <- users do
|
|
case user.subscription.status do
|
|
Subscription.Status.active() ->
|
|
template = PlausibleWeb.Email.yearly_renewal_notification(user)
|
|
Plausible.Mailer.send(template)
|
|
|
|
Subscription.Status.deleted() ->
|
|
template = PlausibleWeb.Email.yearly_expiration_notification(user)
|
|
Plausible.Mailer.send(template)
|
|
|
|
_ ->
|
|
Sentry.capture_message("Invalid subscription for renewal", user: user)
|
|
end
|
|
|
|
Repo.insert_all("sent_renewal_notifications", [
|
|
%{
|
|
user_id: user.id,
|
|
timestamp: NaiveDateTime.utc_now()
|
|
}
|
|
])
|
|
end
|
|
|
|
:ok
|
|
end
|
|
end
|