fix: make cycle regeneration atomic on type change
Make cycle regeneration synchronous in the same transaction as the member update to ensure atomicity.
This commit is contained in:
parent
e9c53cc520
commit
1e5f84fd88
1 changed files with 10 additions and 20 deletions
|
|
@ -191,14 +191,13 @@ defmodule Mv.Membership.Member do
|
||||||
|
|
||||||
# Trigger cycle regeneration when membership_fee_type_id changes
|
# Trigger cycle regeneration when membership_fee_type_id changes
|
||||||
# This deletes future unpaid cycles and regenerates them with the new type/amount
|
# This deletes future unpaid cycles and regenerates them with the new type/amount
|
||||||
# Note: Cycle regeneration runs asynchronously to not block the action,
|
# Note: Cycle regeneration runs synchronously in the same transaction to ensure atomicity
|
||||||
# but in test environment it runs synchronously for DB sandbox compatibility
|
# CycleGenerator uses advisory locks and transactions internally to prevent race conditions
|
||||||
change after_action(fn changeset, member, _context ->
|
change after_action(fn changeset, member, _context ->
|
||||||
fee_type_changed =
|
fee_type_changed =
|
||||||
Ash.Changeset.changing_attribute?(changeset, :membership_fee_type_id)
|
Ash.Changeset.changing_attribute?(changeset, :membership_fee_type_id)
|
||||||
|
|
||||||
if fee_type_changed && member.membership_fee_type_id && member.join_date do
|
if fee_type_changed && member.membership_fee_type_id && member.join_date do
|
||||||
regenerate_fn = fn ->
|
|
||||||
case regenerate_cycles_on_type_change(member) do
|
case regenerate_cycles_on_type_change(member) do
|
||||||
:ok ->
|
:ok ->
|
||||||
:ok
|
:ok
|
||||||
|
|
@ -212,15 +211,6 @@ defmodule Mv.Membership.Member do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if Application.get_env(:mv, :sql_sandbox, false) do
|
|
||||||
# Run synchronously in test environment for DB sandbox compatibility
|
|
||||||
regenerate_fn.()
|
|
||||||
else
|
|
||||||
# Run asynchronously in other environments
|
|
||||||
Task.start(regenerate_fn)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
{:ok, member}
|
{:ok, member}
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue