add action changes for email sync

This commit is contained in:
Moritz 2025-10-16 17:51:31 +02:00
parent 91c5e17994
commit 5a0a261cd6
Signed by: moritz
GPG key ID: 1020A035E5DD0824
7 changed files with 331 additions and 0 deletions

View file

@ -89,6 +89,9 @@ defmodule Mv.Accounts.User do
# cannot be executed atomically. These validations need to query the database and perform
# complex checks that are not supported in atomic operations.
require_atomic? false
# Sync email changes to linked member
change Mv.Accounts.User.Changes.SyncEmailToMember
end
create :create_user do
@ -111,6 +114,9 @@ defmodule Mv.Accounts.User do
# If no member provided, that's fine (optional relationship)
on_missing: :ignore
)
# Override member email with user email when linking
change Mv.Accounts.User.Changes.OverrideMemberEmailOnLink
end
update :update_user do
@ -137,6 +143,11 @@ defmodule Mv.Accounts.User do
# If no member provided, remove existing relationship (allows member removal)
on_missing: :unrelate
)
# Sync email changes to linked member
change Mv.Accounts.User.Changes.SyncEmailToMember
# Override member email with user email when linking
change Mv.Accounts.User.Changes.OverrideMemberEmailOnLink
end
# Admin action for direct password changes in admin panel
@ -185,6 +196,9 @@ defmodule Mv.Accounts.User do
|> Ash.Changeset.change_attribute(:email, user_info["preferred_username"])
|> Ash.Changeset.change_attribute(:oidc_id, user_info["sub"] || user_info["id"])
end
# Override member email with user email when linking (if member relationship exists)
change Mv.Accounts.User.Changes.OverrideMemberEmailOnLink
end
end
@ -255,6 +269,14 @@ defmodule Mv.Accounts.User do
attributes do
uuid_primary_key :id
# IMPORTANT: Email Synchronization
# When user and member are linked, emails are automatically synced bidirectionally.
# User.email is the source of truth - when a link is established, member.email
# is overridden to match user.email. Subsequent changes to either email will
# sync to the other resource.
# See: Mv.Accounts.User.Changes.SyncEmailToMember
# Mv.Accounts.User.Changes.OverrideMemberEmailOnLink
# Mv.Membership.Member.Changes.SyncEmailToUser
attribute :email, :ci_string do
allow_nil? false
public? true