Skip to content

Commit

Permalink
Merge branch 'master' into add-audit-log
Browse files Browse the repository at this point in the history
  • Loading branch information
tspenov authored Nov 6, 2024
2 parents 67b9e6a + 9089783 commit 93aadb3
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 48 deletions.
2 changes: 2 additions & 0 deletions lib/sanbase/accounts/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule Sanbase.Accounts.Settings do
field(:is_subscribed_marketing_emails, :boolean, default: false)
field(:is_subscribed_comments_emails, :boolean, default: true)
field(:is_subscribed_likes_emails, :boolean, default: true)
field(:is_subscribed_metric_updates, :boolean, default: false)

# 2. Email campaigns/lists through Mailchimp
field(:is_subscribed_monthly_newsletter, :boolean, default: true)
Expand Down Expand Up @@ -68,6 +69,7 @@ defmodule Sanbase.Accounts.Settings do
:is_subscribed_edu_emails,
:is_subscribed_monthly_newsletter,
:is_subscribed_biweekly_report,
:is_subscribed_metric_updates,
:is_subscribed_marketing_emails,
:is_subscribed_comments_emails,
:is_subscribed_likes_emails,
Expand Down
7 changes: 6 additions & 1 deletion lib/sanbase/accounts/user_settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ defmodule Sanbase.Accounts.UserSettings do

defp maybe_emit_event_on_changes(user_id, %{settings: settings}) do
settings_changes = settings.changes
email_lists_keys = [:is_subscribed_biweekly_report, :is_subscribed_monthly_newsletter]

email_lists_keys = [
:is_subscribed_biweekly_report,
:is_subscribed_monthly_newsletter,
:is_subscribed_metric_updates
]

for key <- Map.keys(settings_changes) do
if key in email_lists_keys do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ defmodule Sanbase.EventBus.BillingEventSubscriber do
when event_type == :create_subscription do
subscription = Sanbase.Billing.Subscription.by_id(event.data.subscription_id)

user = Sanbase.Accounts.get_user!(subscription.user_id)

{:ok, _settings} =
Sanbase.Accounts.UserSettings.update_settings(user, %{
is_subscribed_metric_updates: true
})

cond do
Subscription.trialing_sanbase_pro?(subscription) ->
EmailJobs.send_trial_started_email(subscription)
Expand All @@ -98,6 +105,17 @@ defmodule Sanbase.EventBus.BillingEventSubscriber do
end
end

defp do_handle(:cancel_subscription_at_period_end, event_type, event)
when event_type == :cancel_subscription_at_period_end do
subscription = Sanbase.Billing.Subscription.by_id(event.data.subscription_id)
user = Sanbase.Accounts.get_user!(subscription.user_id)

{:ok, _settings} =
Sanbase.Accounts.UserSettings.update_settings(user, %{
is_subscribed_metric_updates: false
})
end

defp do_handle(:send_discord_notification, event_type, event)
when event_type in @subscription_events or event_type in @payment_events do
Sanbase.Billing.DiscordNotification.handle_event(event_type, event)
Expand Down
4 changes: 3 additions & 1 deletion lib/sanbase/email/mailjet_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ defmodule Sanbase.Email.MailjetApiImpl do
@monthly_newsletter_list_id 61_085
@mailjet_sanr_list_id 10_321_582
@alpha_naratives_list_id 10_321_590
@metric_updates_list_id 10_326_520

@mailjet_lists %{
bi_weekly: @bi_weekly_list_id,
monthly_newsletter: @monthly_newsletter_list_id,
sanr_network_emails: @mailjet_sanr_list_id,
alpha_naratives_emails: @alpha_naratives_list_id
alpha_naratives_emails: @alpha_naratives_list_id,
metric_updates: @metric_updates_list_id
}

def subscribe(list_atom, email_or_emails) do
Expand Down
14 changes: 14 additions & 0 deletions lib/sanbase/email/mailjet_event_emitter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ defmodule Sanbase.Email.MailjetEventEmitter do
|> notify()
end

def handle_event({:ok, user_id}, :is_subscribed_metric_updates, data) do
event_type =
case data[:is_subscribed_metric_updates] do
true -> :subscribe_metric_updates
false -> :unsubscribe_metric_updates
end

%{
event_type: event_type,
user_id: user_id
}
|> notify()
end

defp notify(data) do
Sanbase.EventBus.notify(%{topic: @topic, data: data})
:ok
Expand Down
4 changes: 3 additions & 1 deletion lib/sanbase/event_bus/event_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ defmodule Sanbase.EventBus.EventValidation do
:subscribe_biweekly_report,
:unsubscribe_biweekly_report,
:subscribe_monthly_newsletter,
:unsubscribe_monthly_newsletter
:unsubscribe_monthly_newsletter,
:subscribe_metric_updates,
:unsubscribe_metric_updates
] do
valid_integer_id?(user_id)
end
Expand Down
23 changes: 23 additions & 0 deletions lib/sanbase/event_bus/user_events_subscriber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ defmodule Sanbase.EventBus.UserEventsSubscriber do
state
end

defp handle_event(
%{data: %{event_type: :subscribe_metric_updates, user_id: user_id}},
event_shadow,
state
) do
email = Sanbase.Accounts.get_user!(user_id).email

Sanbase.Email.MailjetApi.subscribe(:metric_updates, email)
EventBus.mark_as_completed({__MODULE__, event_shadow})
state
end

defp handle_event(
%{data: %{event_type: :unsubscribe_metric_updates, user_id: user_id}},
event_shadow,
state
) do
email = Sanbase.Accounts.get_user!(user_id).email
Sanbase.Email.MailjetApi.unsubscribe(:metric_updates, email)
EventBus.mark_as_completed({__MODULE__, event_shadow})
state
end

defp handle_event(_event, event_shadow, state) do
# The unhandled events are marked as completed
EventBus.mark_as_completed({__MODULE__, event_shadow})
Expand Down
2 changes: 2 additions & 0 deletions lib/sanbase_web/graphql/schema/types/user_settings_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule SanbaseWeb.Graphql.UserSettingsTypes do
field(:is_subscribed_marketing_emails, :boolean)
field(:is_subscribed_comments_emails, :boolean)
field(:is_subscribed_likes_emails, :boolean)
field(:is_subscribed_metric_updates, :boolean)
# Fixme: backwards compatibility, should be removed when removed from frontend.
# option is not used
field(:newsletter_subscription, :string, default_value: "OFF")
Expand Down Expand Up @@ -66,6 +67,7 @@ defmodule SanbaseWeb.Graphql.UserSettingsTypes do
field(:is_subscribed_marketing_emails, :boolean)
field(:is_subscribed_comments_emails, :boolean)
field(:is_subscribed_likes_emails, :boolean)
field(:is_subscribed_metric_updates, :boolean)
field(:sanbase_version, :string)

# Deprecated fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule SanbaseWeb.ManualNotificationLive do

@channel_discord :discord
@channel_email :email
@valid_channels [@channel_discord, @channel_email]

@impl true
def mount(_params, _session, socket) do
Expand Down
Loading

0 comments on commit 93aadb3

Please sign in to comment.