Skip to content

Commit

Permalink
Fix setting user_id based on subscriber email
Browse files Browse the repository at this point in the history
  • Loading branch information
skanderm committed Aug 22, 2024
1 parent bddf079 commit 00d2bdb
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions server/lib/orcasite/notifications/resources/subscriber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ defmodule Orcasite.Notifications.Subscriber do
has_many :subscriptions, Subscription
end

code_interface do
define :by_email, args: [:email]
end

authentication do
domain Orcasite.Notifications

Expand Down Expand Up @@ -76,12 +72,6 @@ defmodule Orcasite.Notifications.Subscriber do
end
end

resource do
description """
A subscriber object. Can relate to an individual, an organization, a newsletter, or an admin.
"""
end

actions do
defaults [:create, :read, :update, :destroy]

Expand Down Expand Up @@ -122,7 +112,9 @@ defmodule Orcasite.Notifications.Subscriber do

change fn changeset, _context ->
meta =
case Orcasite.Accounts.User.by_email(Ash.Changeset.get_argument(changeset, :email)) do
case Orcasite.Accounts.User.by_email(Ash.Changeset.get_argument(changeset, :email),
authorize?: false
) do
{:ok, %{id: user_id}} -> %{user_id: user_id}
_ -> %{}
end
Expand Down Expand Up @@ -150,6 +142,26 @@ defmodule Orcasite.Notifications.Subscriber do
end
end

code_interface do
define :by_email, args: [:email]
end

resource do
description """
A subscriber object. Can relate to an individual, an organization, a newsletter, or an admin.
"""
end

admin do
table_columns [:id, :name, :meta, :inserted_at]
read_actions [:read, :by_email]
format_fields meta: {Jason, :encode!, []}

form do
field :event_type, type: :default
end
end

validations do
validate fn changeset, _context ->
# Check if email subscriber already exists
Expand All @@ -167,16 +179,6 @@ defmodule Orcasite.Notifications.Subscriber do
end
end

admin do
table_columns [:id, :name, :meta, :inserted_at]
read_actions [:read, :by_email]
format_fields meta: {Jason, :encode!, []}

form do
field :event_type, type: :default
end
end

json_api do
type "subscriber"

Expand Down

0 comments on commit 00d2bdb

Please sign in to comment.