Skip to content

Commit

Permalink
Merge branch 'master' into add-auth-get-coupon
Browse files Browse the repository at this point in the history
  • Loading branch information
tspenov authored Jan 21, 2025
2 parents eea0058 + 829d89a commit db45a54
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 133 deletions.
21 changes: 0 additions & 21 deletions lib/sanbase/accounts/email_jobs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,6 @@ defmodule Sanbase.Accounts.EmailJobs do
add_email_job(user.id, end_of_trial_template(), vars, scheduled_at: days_after(11))
end

def schedule_annual_discount_emails(subscription) do
user = Sanbase.Accounts.User.by_id!(subscription.user_id)
name = Sanbase.Accounts.User.get_name(user)

common_vars = %{
name: name,
username: name
}

vars_50 = Map.put(common_vars, :end_subscription_date, days_after(14) |> format_date())
vars_35 = Map.put(common_vars, :date, days_after(30) |> format_date())

add_email_job(subscription.user_id, during_trial_annual_discount_template(), vars_50,
scheduled_at: days_after(12)
)

add_email_job(subscription.user_id, after_trial_annual_discount_template(), vars_35,
scheduled_at: days_after(24)
)
end

def send_pro_started_email(subscription) do
user = Sanbase.Accounts.User.by_id!(subscription.user_id)
name = Sanbase.Accounts.User.get_name(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,16 @@ defmodule Sanbase.EventBus.BillingEventSubscriber do
when event_type == :create_subscription do
subscription = Sanbase.Billing.Subscription.by_id(event.data.subscription_id)

Sanbase.Accounts.get_user(subscription.user_id)
|> case do
{:ok, user} ->
{:ok, _settings} =
Sanbase.Accounts.UserSettings.update_settings(user, %{
is_subscribed_metric_updates: true
})

_ ->
:ok
end
toggle_settings(subscription.user_id, %{
is_subscribed_metric_updates: true
})

cond do
Subscription.trialing_sanbase_pro?(subscription) ->
EmailJobs.send_trial_started_email(subscription)
EmailJobs.schedule_trial_will_end_email(subscription)

case subscription.plan.interval do
"month" ->
# comment temporarily until we decide to bring back the annual discounts
# Sanbase.Accounts.EmailJobs.schedule_annual_discount_emails(subscription)
:ok

_ ->
:ok
end
:ok

Subscription.active_sanbase_pro?(subscription) ->
Sanbase.Accounts.EmailJobs.send_pro_started_email(subscription)
Expand All @@ -114,17 +98,9 @@ defmodule Sanbase.EventBus.BillingEventSubscriber do
when event_type == :cancel_subscription_at_period_end do
subscription = Sanbase.Billing.Subscription.by_id(event.data.subscription_id)

Sanbase.Accounts.get_user(subscription.user_id)
|> case do
{:ok, user} ->
{:ok, _settings} =
Sanbase.Accounts.UserSettings.update_settings(user, %{
is_subscribed_metric_updates: false
})

_ ->
:ok
end
toggle_settings(subscription.user_id, %{
is_subscribed_metric_updates: false
})
end

defp do_handle(:send_discord_notification, event_type, event)
Expand All @@ -150,4 +126,15 @@ defmodule Sanbase.EventBus.BillingEventSubscriber do
defp do_handle(_type, _event_type, _event) do
:ok
end

defp toggle_settings(user_id, settings) do
Sanbase.Accounts.get_user(user_id)
|> case do
{:ok, user} ->
Sanbase.Accounts.UserSettings.update_settings(user, settings)

_ ->
:ok
end
end
end
14 changes: 0 additions & 14 deletions lib/sanbase/email/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ defmodule Sanbase.Mailer do
alias Sanbase.Billing.{Subscription, Product}

@edu_templates ~w(first-edu-email second-edu-email)
@during_trial_annual_discount_template during_trial_annual_discount_template()
@after_trial_annual_discount_template after_trial_annual_discount_template()
@end_of_trial_template end_of_trial_template()
@trial_started_template trial_started_template()

Expand Down Expand Up @@ -75,18 +73,6 @@ defmodule Sanbase.Mailer do
not is_excluded_email?(user.email)
end

defp can_send?(user, template, _params)
when template == @during_trial_annual_discount_template do
res = Sanbase.Billing.Subscription.annual_discount_eligibility(user.id)
res.is_eligible and res.discount.percent_off == 50
end

defp can_send?(user, template, _params)
when template == @after_trial_annual_discount_template do
res = Sanbase.Billing.Subscription.annual_discount_eligibility(user.id)
res.is_eligible and res.discount.percent_off == 35
end

defp can_send?(_user, _template, _params), do: true

defp has_card?(user) do
Expand Down
8 changes: 0 additions & 8 deletions lib/sanbase/email/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,6 @@ defmodule Sanbase.Email.Template do
# Send 3 days before trial ends
@end_of_trial_template "trial-end-mail"

# Send 2 days before trial ends
@during_trial_annual_discount_template "50-percent-discount-offer"

# Send once - 1 week before monthly Sanbase Pro ends
@after_trial_annual_discount_template "35-percent-discount-offer"

@automatic_renewal_template "automatic_renewal"

# Send after 2 weeks of inactivity.
Expand All @@ -190,8 +184,6 @@ defmodule Sanbase.Email.Template do
def post_cancellation_template2, do: @post_cancellation_template2
def end_of_trial_template, do: @end_of_trial_template
def trial_started_template, do: @trial_started_template
def during_trial_annual_discount_template, do: @during_trial_annual_discount_template
def after_trial_annual_discount_template, do: @after_trial_annual_discount_template
def automatic_renewal_template, do: @automatic_renewal_template

def comment_notification_template, do: @comment_notification_template
Expand Down
11 changes: 2 additions & 9 deletions lib/sanbase/social_data/social_volume.ex
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,8 @@ defmodule Sanbase.SocialData.SocialVolume do
}
|> Jason.encode_to_iodata!()

options = [
recv_timeout: @recv_timeout
]

# metricshub reads data from request.query_string and request.form
# Put this header so the data is read from the body properly
headers = [
{"Content-Type", "application/x-www-form-urlencoded"}
]
options = [recv_timeout: @recv_timeout]
headers = [{"Content-Type", "application/json"}]

http_client().post(url, body, headers, options)
end
Expand Down
2 changes: 0 additions & 2 deletions lib/sanbase/transfers/erc20_transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ defmodule Sanbase.Transfers.Erc20Transfers do
alias Sanbase.ClickhouseRepo
alias Sanbase.Project

alias Sanbase.Utils.Config

defguard is_non_neg_integer(int) when is_integer(int) and int > 0

@spec top_wallet_transfers(
Expand Down
8 changes: 4 additions & 4 deletions lib/sanbase_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule SanbaseWeb do
def channel do
quote do
use Phoenix.Channel
import SanbaseWeb.Gettext
use Gettext, backend: SanbaseWeb.Gettext

unquote(verified_routes())
end
Expand All @@ -46,7 +46,7 @@ defmodule SanbaseWeb do
layouts: [html: SanbaseWeb.Layouts]

import Plug.Conn
import SanbaseWeb.Gettext
use Gettext, backend: SanbaseWeb.Gettext
unquote(verified_routes())
end
end
Expand Down Expand Up @@ -111,7 +111,7 @@ defmodule SanbaseWeb do
import Phoenix.View

import SanbaseWeb.ErrorHelpers
import SanbaseWeb.Gettext
use Gettext, backend: SanbaseWeb.Gettext
alias SanbaseWeb.Router.Helpers, as: Routes

unquote(verified_routes())
Expand All @@ -124,7 +124,7 @@ defmodule SanbaseWeb do
import Phoenix.HTML
# Core UI components and translation
import SanbaseWeb.CoreComponents
import SanbaseWeb.Gettext
use Gettext, backend: SanbaseWeb.Gettext

# Shortcut for generating JS commands
alias Phoenix.LiveView.JS
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule SanbaseWeb.CoreComponents do
use Phoenix.Component

alias Phoenix.LiveView.JS
import SanbaseWeb.Gettext
use Gettext, backend: SanbaseWeb.Gettext

@doc """
Renders a modal.
Expand Down
4 changes: 2 additions & 2 deletions lib/sanbase_web/gettext.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule SanbaseWeb.Gettext do
By using [Gettext](https://hexdocs.pm/gettext),
your module gains a set of macros for translations, for example:
import SanbaseWeb.Gettext
use Gettext, backend: SanbaseWeb.Gettext
# Simple translation
gettext "Here is the string to translate"
Expand All @@ -20,5 +20,5 @@ defmodule SanbaseWeb.Gettext do
See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
"""
use Gettext, otp_app: :sanbase
use Gettext.Backend, otp_app: :sanbase
end
2 changes: 1 addition & 1 deletion lib/sanbase_web/graphql/resolvers/user/user_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ defmodule SanbaseWeb.Graphql.Resolvers.UserResolver do
Sanbase.Queries.Authorization.query_executions_limit(subscription_product, plan_name)

result = %{
credits_availalbe_month: credits_limit,
credits_available_month: credits_limit,
credits_spent_month: details.monthly_credits_spent,
credits_remaining_month: credits_limit - details.monthly_credits_spent,
queries_executed_month: details.queries_executed_month,
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase_web/graphql/schema/types/user_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ defmodule SanbaseWeb.Graphql.UserTypes do
end

object :queries_executions_info do
field(:credits_availalbe_month, non_null(:integer))
field(:credits_available_month, non_null(:integer))
field(:credits_spent_month, non_null(:integer))
field(:credits_remaining_month, non_null(:integer))
field(:queries_executed_month, non_null(:integer))
Expand Down
34 changes: 34 additions & 0 deletions livebooks/gather_alerts_info.livemd
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Untitled notebook

## Section

```elixir
type = "metric_signal"
alerts = Sanbase.Alert.UserTrigger.get_all_triggers_by_type(type)

IO.puts("Got #{length(alerts)} alerts")
```

```elixir
data =
alerts
|> Enum.take(2)
|> Enum.map(fn alert ->
%{
alert_id: alert.id,
user_id: alert.user_id,
inserted_at: alert.inserted_at,
updated_at: alert.updated_at,
metric: alert.trigger.settings.metric,
target: alert.trigger.settings.target,
channels: alert.trigger.settings.channel |> List.wrap()
}
end)
```

```elixir
json = Jason.encode!(data)

Path.expand("~/alerts_data.json")
|> File.write!(json)
```
36 changes: 0 additions & 36 deletions test/sanbase/email/emails_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -257,42 +257,6 @@ defmodule Sanbase.EmailsTest do

refute called(Sanbase.TemplateMailer.send(context.user.email, :_, :_))
end

test "do not send discount 50% email", context do
assert :ok =
perform_job(Sanbase.Mailer, %{
"user_id" => context.user.id,
"template" => during_trial_annual_discount_template()
})

refute called(Sanbase.TemplateMailer.send(context.user.email, :_, :_))
end

test "do not send discount 35% email", context do
assert :ok =
perform_job(Sanbase.Mailer, %{
"user_id" => context.user.id,
"template" => after_trial_annual_discount_template()
})

refute called(Sanbase.TemplateMailer.send(context.user.email, :_, :_))
end

test "do not send discount 35% email if more than 30 days passed", context do
insert(:subscription_pro_sanbase,
user: context.user,
status: "trialing",
trial_end: days_after(-20)
)

assert :ok =
perform_job(Sanbase.Mailer, %{
"user_id" => context.user.id,
"template" => after_trial_annual_discount_template()
})

refute called(Sanbase.TemplateMailer.send(context.user.email, :_, :_))
end
end

defp email_login_verify_mutation(user) do
Expand Down
6 changes: 3 additions & 3 deletions test/sanbase_web/graphql/queries/queries_api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ defmodule SanbaseWeb.Graphql.QueriesApiTest do
|> get_in(["data", "currentUser", "queriesExecutionsInfo"])

assert stats == %{
"creditsAvailalbeMonth" => 500,
"creditsAvailableMonth" => 500,
"creditsRemainingMonth" => 499,
"creditsSpentMonth" => 1,
"queriesExecutedDay" => 1,
Expand Down Expand Up @@ -334,7 +334,7 @@ defmodule SanbaseWeb.Graphql.QueriesApiTest do
assert Process.get(:queries_dynamic_repo) == Sanbase.ClickhouseRepo.BusinessMaxUser

assert stats == %{
"creditsAvailalbeMonth" => 500_000,
"creditsAvailableMonth" => 500_000,
"creditsRemainingMonth" => 499_999,
"creditsSpentMonth" => 1,
"queriesExecutedDay" => 1,
Expand All @@ -357,7 +357,7 @@ defmodule SanbaseWeb.Graphql.QueriesApiTest do
currentUser {
queriesExecutionsInfo {
# credits info
creditsAvailalbeMonth
creditsAvailableMonth
creditsSpentMonth
creditsRemainingMonth
# queries executed
Expand Down

0 comments on commit db45a54

Please sign in to comment.