Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

2.8 KB
Raw Blame
defmodule Plausible.Workers.NotifyAnnualRenewal do
  @moduledoc """
  Worker sending emails about upcoming renewal of annual subscriptions.
  """

  use Plausible.Repo
  use Oban.Worker, queue: :notify_annual_renewal

  require Plausible.Billing.Subscription.Status

  alias Money.Subscription
  alias Plausible.Billing.EnterprisePlan
  alias Plausible.Billing.Subscription
  alias Plausible.Teams

  @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
    sent_notification =
      from(
        s in "sent_renewal_notifications",
        where: s.timestamp > fragment("now() - INTERVAL '1 month'")
      )

    yearly_enterprise_plan =
      from(
        ep in EnterprisePlan,
        where: ep.team_id == parent_as(:team).id,
        where: ep.paddle_plan_id == parent_as(:subscription).paddle_plan_id,
        where: ep.paddle_plan_id != ^Subscription.manual_plan_id(),
        where: ep.billing_interval == :yearly,
        select: 1
      )

    teams =
      Repo.all(
        from t in Teams.Team,
          as: :team,
          inner_join: o in assoc(t, :owners),
          left_join: bm in assoc(t, :billing_members),
          inner_lateral_join: s in subquery(Teams.last_subscription_join_query()),
          as: :subscription,
          on: true,
          left_join: sent in ^sent_notification,
          on: o.id == sent.user_id,
          where: is_nil(sent.id),
          where: s.paddle_plan_id in @yearly_plans or exists(yearly_enterprise_plan),
          where:
            s.next_bill_date > fragment("now()::date") and
              s.next_bill_date <= fragment("now()::date + INTERVAL '7 days'"),
          preload: [owners: o, subscription: s, billing_members: bm]
      )

    for team <- teams do
      recipients = team.owners ++ team.billing_members

      case team.subscription.status do
        Subscription.Status.active() ->
          for recipient <- recipients do
            template = PlausibleWeb.Email.yearly_renewal_notification(team, recipient)
            Plausible.Mailer.send(template)
          end

        Subscription.Status.deleted() ->
          for recipient <- recipients do
            template = PlausibleWeb.Email.yearly_expiration_notification(team, recipient)
            Plausible.Mailer.send(template)
          end

        _ ->
          Sentry.capture_message("Invalid subscription for renewal",
            team: team
          )
      end

      for recipient <- recipients do
        Repo.insert_all("sent_renewal_notifications", [
          %{
            user_id: recipient.id,
            timestamp: NaiveDateTime.utc_now()
          }
        ])
      end
    end

    :ok
  end
end